blob: a07b6d6d4a301db016d7671e903db40a70c94766 [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 Moolenaar87e25fd2005-07-27 21:13:01 +0000813static 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
833
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000834/* Character used as separated in autoload function/variable names. */
835#define AUTOLOAD_CHAR '#'
836
Bram Moolenaar33570922005-01-25 22:26:29 +0000837/*
838 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000839 */
840 void
841eval_init()
842{
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 int i;
844 struct vimvar *p;
845
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200846 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
847 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200848 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000849 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000850 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000851
852 for (i = 0; i < VV_LEN; ++i)
853 {
854 p = &vimvars[i];
855 STRCPY(p->vv_di.di_key, p->vv_name);
856 if (p->vv_flags & VV_RO)
857 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
858 else if (p->vv_flags & VV_RO_SBX)
859 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
860 else
861 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000862
863 /* add to v: scope dict, unless the value is not always available */
864 if (p->vv_type != VAR_UNKNOWN)
865 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000866 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000867 /* add to compat scope dict */
868 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000870 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200871 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200872
873#ifdef EBCDIC
874 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100875 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200876 */
877 sortFunctions();
878#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000879}
880
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000881#if defined(EXITFREE) || defined(PROTO)
882 void
883eval_clear()
884{
885 int i;
886 struct vimvar *p;
887
888 for (i = 0; i < VV_LEN; ++i)
889 {
890 p = &vimvars[i];
891 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000892 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000893 vim_free(p->vv_str);
894 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000895 }
896 else if (p->vv_di.di_tv.v_type == VAR_LIST)
897 {
898 list_unref(p->vv_list);
899 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000900 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901 }
902 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000903 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000904 hash_clear(&compat_hashtab);
905
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100907# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200908 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100909# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000910
911 /* global variables */
912 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000914 /* autoloaded script names */
915 ga_clear_strings(&ga_loaded);
916
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200917 /* script-local variables */
918 for (i = 1; i <= ga_scripts.ga_len; ++i)
919 {
920 vars_clear(&SCRIPT_VARS(i));
921 vim_free(SCRIPT_SV(i));
922 }
923 ga_clear(&ga_scripts);
924
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000925 /* unreferenced lists and dicts */
926 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000927
928 /* functions */
929 free_all_functions();
930 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000931}
932#endif
933
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 * Return the name of the executed function.
936 */
937 char_u *
938func_name(cookie)
939 void *cookie;
940{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000941 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943
944/*
945 * Return the address holding the next breakpoint line for a funccall cookie.
946 */
947 linenr_T *
948func_breakpoint(cookie)
949 void *cookie;
950{
Bram Moolenaar33570922005-01-25 22:26:29 +0000951 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952}
953
954/*
955 * Return the address holding the debug tick for a funccall cookie.
956 */
957 int *
958func_dbg_tick(cookie)
959 void *cookie;
960{
Bram Moolenaar33570922005-01-25 22:26:29 +0000961 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/*
965 * Return the nesting level for a funccall cookie.
966 */
967 int
968func_level(cookie)
969 void *cookie;
970{
Bram Moolenaar33570922005-01-25 22:26:29 +0000971 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972}
973
974/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000975funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000977/* pointer to list of previously used funccal, still around because some
978 * item in it is still being used. */
979funccall_T *previous_funccal = NULL;
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981/*
982 * Return TRUE when a function was ended by a ":return" command.
983 */
984 int
985current_func_returned()
986{
987 return current_funccal->returned;
988}
989
990
991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 * Set an internal variable to a string value. Creates the variable if it does
993 * not already exist.
994 */
995 void
996set_internal_string_var(name, value)
997 char_u *name;
998 char_u *value;
999{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001000 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001001 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
1003 val = vim_strsave(value);
1004 if (val != NULL)
1005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001007 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001009 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012 }
1013}
1014
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001015static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001016static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017static char_u *redir_endp = NULL;
1018static char_u *redir_varname = NULL;
1019
1020/*
1021 * Start recording command output to a variable
1022 * Returns OK if successfully completed the setup. FAIL otherwise.
1023 */
1024 int
1025var_redir_start(name, append)
1026 char_u *name;
1027 int append; /* append to an existing variable */
1028{
1029 int save_emsg;
1030 int err;
1031 typval_T tv;
1032
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001033 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001034 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001035 {
1036 EMSG(_(e_invarg));
1037 return FAIL;
1038 }
1039
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001040 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 redir_varname = vim_strsave(name);
1042 if (redir_varname == NULL)
1043 return FAIL;
1044
1045 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1046 if (redir_lval == NULL)
1047 {
1048 var_redir_stop();
1049 return FAIL;
1050 }
1051
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001052 /* The output is stored in growarray "redir_ga" until redirection ends. */
1053 ga_init2(&redir_ga, (int)sizeof(char), 500);
1054
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001056 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1057 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1059 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001060 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 if (redir_endp != NULL && *redir_endp != NUL)
1062 /* Trailing characters are present after the variable name */
1063 EMSG(_(e_trailing));
1064 else
1065 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001066 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 var_redir_stop();
1068 return FAIL;
1069 }
1070
1071 /* check if we can write to the variable: set it to or append an empty
1072 * string */
1073 save_emsg = did_emsg;
1074 did_emsg = FALSE;
1075 tv.v_type = VAR_STRING;
1076 tv.vval.v_string = (char_u *)"";
1077 if (append)
1078 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1079 else
1080 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001083 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 if (err)
1085 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001086 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 var_redir_stop();
1088 return FAIL;
1089 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090
1091 return OK;
1092}
1093
1094/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001095 * Append "value[value_len]" to the variable set by var_redir_start().
1096 * The actual appending is postponed until redirection ends, because the value
1097 * appended may in fact be the string we write to, changing it may cause freed
1098 * memory to be used:
1099 * :redir => foo
1100 * :let foo
1101 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 */
1103 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001104var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001108 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109
1110 if (redir_lval == NULL)
1111 return;
1112
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001113 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001119 {
1120 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 }
1123 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125}
1126
1127/*
1128 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001129 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 */
1131 void
1132var_redir_stop()
1133{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 typval_T tv;
1135
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 if (redir_lval != NULL)
1137 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 /* If there was no error: assign the text to the variable. */
1139 if (redir_endp != NULL)
1140 {
1141 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1142 tv.v_type = VAR_STRING;
1143 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001144 /* Call get_lval() again, if it's inside a Dict or List it may
1145 * have changed. */
1146 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1147 FALSE, FALSE, FALSE, FNE_CHECK_START);
1148 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1149 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1150 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001151 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001153 /* free the collected output */
1154 vim_free(redir_ga.ga_data);
1155 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 vim_free(redir_lval);
1158 redir_lval = NULL;
1159 }
1160 vim_free(redir_varname);
1161 redir_varname = NULL;
1162}
1163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164# if defined(FEAT_MBYTE) || defined(PROTO)
1165 int
1166eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1167 char_u *enc_from;
1168 char_u *enc_to;
1169 char_u *fname_from;
1170 char_u *fname_to;
1171{
1172 int err = FALSE;
1173
1174 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1175 set_vim_var_string(VV_CC_TO, enc_to, -1);
1176 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1177 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1178 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1179 err = TRUE;
1180 set_vim_var_string(VV_CC_FROM, NULL, -1);
1181 set_vim_var_string(VV_CC_TO, NULL, -1);
1182 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1183 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1184
1185 if (err)
1186 return FAIL;
1187 return OK;
1188}
1189# endif
1190
1191# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1192 int
1193eval_printexpr(fname, args)
1194 char_u *fname;
1195 char_u *args;
1196{
1197 int err = FALSE;
1198
1199 set_vim_var_string(VV_FNAME_IN, fname, -1);
1200 set_vim_var_string(VV_CMDARG, args, -1);
1201 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1202 err = TRUE;
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_CMDARG, NULL, -1);
1205
1206 if (err)
1207 {
1208 mch_remove(fname);
1209 return FAIL;
1210 }
1211 return OK;
1212}
1213# endif
1214
1215# if defined(FEAT_DIFF) || defined(PROTO)
1216 void
1217eval_diff(origfile, newfile, outfile)
1218 char_u *origfile;
1219 char_u *newfile;
1220 char_u *outfile;
1221{
1222 int err = FALSE;
1223
1224 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1225 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1226 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1227 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1228 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1229 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1230 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1231}
1232
1233 void
1234eval_patch(origfile, difffile, outfile)
1235 char_u *origfile;
1236 char_u *difffile;
1237 char_u *outfile;
1238{
1239 int err;
1240
1241 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1242 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1243 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1244 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1245 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1246 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1247 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1248}
1249# endif
1250
1251/*
1252 * Top level evaluation function, returning a boolean.
1253 * Sets "error" to TRUE if there was an error.
1254 * Return TRUE or FALSE.
1255 */
1256 int
1257eval_to_bool(arg, error, nextcmd, skip)
1258 char_u *arg;
1259 int *error;
1260 char_u **nextcmd;
1261 int skip; /* only parse, don't execute */
1262{
Bram Moolenaar33570922005-01-25 22:26:29 +00001263 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 int retval = FALSE;
1265
1266 if (skip)
1267 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 else
1271 {
1272 *error = FALSE;
1273 if (!skip)
1274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001275 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278 }
1279 if (skip)
1280 --emsg_skip;
1281
1282 return retval;
1283}
1284
1285/*
1286 * Top level evaluation function, returning a string. If "skip" is TRUE,
1287 * only parsing to "nextcmd" is done, without reporting errors. Return
1288 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1289 */
1290 char_u *
1291eval_to_string_skip(arg, nextcmd, skip)
1292 char_u *arg;
1293 char_u **nextcmd;
1294 int skip; /* only parse, don't execute */
1295{
Bram Moolenaar33570922005-01-25 22:26:29 +00001296 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 char_u *retval;
1298
1299 if (skip)
1300 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 retval = NULL;
1303 else
1304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 retval = vim_strsave(get_tv_string(&tv));
1306 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308 if (skip)
1309 --emsg_skip;
1310
1311 return retval;
1312}
1313
1314/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001315 * Skip over an expression at "*pp".
1316 * Return FAIL for an error, OK otherwise.
1317 */
1318 int
1319skip_expr(pp)
1320 char_u **pp;
1321{
Bram Moolenaar33570922005-01-25 22:26:29 +00001322 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323
1324 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001325 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001326}
1327
1328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001330 * When "convert" is TRUE convert a List into a sequence of lines and convert
1331 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 * Return pointer to allocated memory, or NULL for failure.
1333 */
1334 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 char_u *arg;
1337 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
Bram Moolenaar33570922005-01-25 22:26:29 +00001340 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001342 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001343#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001347 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 retval = NULL;
1349 else
1350 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001352 {
1353 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001354 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001355 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001356 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001357 if (tv.vval.v_list->lv_len > 0)
1358 ga_append(&ga, NL);
1359 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 ga_append(&ga, NUL);
1361 retval = (char_u *)ga.ga_data;
1362 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363#ifdef FEAT_FLOAT
1364 else if (convert && tv.v_type == VAR_FLOAT)
1365 {
1366 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1367 retval = vim_strsave(numbuf);
1368 }
1369#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001370 else
1371 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374
1375 return retval;
1376}
1377
1378/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379 * Call eval_to_string() without using current local variables and using
1380 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
1382 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 char_u *arg;
1385 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 char_u *retval;
1389 void *save_funccalp;
1390
1391 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 ++sandbox;
1394 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001395 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 if (use_sandbox)
1397 --sandbox;
1398 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 restore_funccal(save_funccalp);
1400 return retval;
1401}
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403/*
1404 * Top level evaluation function, returning a number.
1405 * Evaluates "expr" silently.
1406 * Returns -1 for an error.
1407 */
1408 int
1409eval_to_number(expr)
1410 char_u *expr;
1411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001414 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 ++emsg_off;
1417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = -1;
1420 else
1421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001422 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 --emsg_off;
1426
1427 return retval;
1428}
1429
Bram Moolenaara40058a2005-07-11 22:42:07 +00001430/*
1431 * Prepare v: variable "idx" to be used.
1432 * Save the current typeval in "save_tv".
1433 * When not used yet add the variable to the v: hashtable.
1434 */
1435 static void
1436prepare_vimvar(idx, save_tv)
1437 int idx;
1438 typval_T *save_tv;
1439{
1440 *save_tv = vimvars[idx].vv_tv;
1441 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1442 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1443}
1444
1445/*
1446 * Restore v: variable "idx" to typeval "save_tv".
1447 * When no longer defined, remove the variable from the v: hashtable.
1448 */
1449 static void
1450restore_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 hashitem_T *hi;
1455
Bram Moolenaara40058a2005-07-11 22:42:07 +00001456 vimvars[idx].vv_tv = *save_tv;
1457 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1458 {
1459 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1460 if (HASHITEM_EMPTY(hi))
1461 EMSG2(_(e_intern2), "restore_vimvar()");
1462 else
1463 hash_remove(&vimvarht, hi);
1464 }
1465}
1466
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001467#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468/*
1469 * Evaluate an expression to a list with suggestions.
1470 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001471 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472 */
1473 list_T *
1474eval_spell_expr(badword, expr)
1475 char_u *badword;
1476 char_u *expr;
1477{
1478 typval_T save_val;
1479 typval_T rettv;
1480 list_T *list = NULL;
1481 char_u *p = skipwhite(expr);
1482
1483 /* Set "v:val" to the bad word. */
1484 prepare_vimvar(VV_VAL, &save_val);
1485 vimvars[VV_VAL].vv_type = VAR_STRING;
1486 vimvars[VV_VAL].vv_str = badword;
1487 if (p_verbose == 0)
1488 ++emsg_off;
1489
1490 if (eval1(&p, &rettv, TRUE) == OK)
1491 {
1492 if (rettv.v_type != VAR_LIST)
1493 clear_tv(&rettv);
1494 else
1495 list = rettv.vval.v_list;
1496 }
1497
1498 if (p_verbose == 0)
1499 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001500 restore_vimvar(VV_VAL, &save_val);
1501
1502 return list;
1503}
1504
1505/*
1506 * "list" is supposed to contain two items: a word and a number. Return the
1507 * word in "pp" and the number as the return value.
1508 * Return -1 if anything isn't right.
1509 * Used to get the good word and score from the eval_spell_expr() result.
1510 */
1511 int
1512get_spellword(list, pp)
1513 list_T *list;
1514 char_u **pp;
1515{
1516 listitem_T *li;
1517
1518 li = list->lv_first;
1519 if (li == NULL)
1520 return -1;
1521 *pp = get_tv_string(&li->li_tv);
1522
1523 li = li->li_next;
1524 if (li == NULL)
1525 return -1;
1526 return get_tv_number(&li->li_tv);
1527}
1528#endif
1529
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001531 * Top level evaluation function.
1532 * Returns an allocated typval_T with the result.
1533 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 */
1535 typval_T *
1536eval_expr(arg, nextcmd)
1537 char_u *arg;
1538 char_u **nextcmd;
1539{
1540 typval_T *tv;
1541
1542 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 {
1545 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001546 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001547 }
1548
1549 return tv;
1550}
1551
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001554 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555 * Uses argv[argc] for the function arguments. Only Number and String
1556 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001557 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001559 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001560call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 char_u *func;
1562 int argc;
1563 char_u **argv;
1564 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001565 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 long n;
1570 int len;
1571 int i;
1572 int doesrange;
1573 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001576 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 for (i = 0; i < argc; i++)
1581 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001582 /* Pass a NULL or empty argument as an empty string */
1583 if (argv[i] == NULL || *argv[i] == NUL)
1584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001585 argvars[i].v_type = VAR_STRING;
1586 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001587 continue;
1588 }
1589
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001590 if (str_arg_only)
1591 len = 0;
1592 else
1593 /* Recognize a number argument, the others must be strings. */
1594 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (len != 0 && len == (int)STRLEN(argv[i]))
1596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001597 argvars[i].v_type = VAR_NUMBER;
1598 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 else
1601 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001602 argvars[i].v_type = VAR_STRING;
1603 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
1605 }
1606
1607 if (safe)
1608 {
1609 save_funccalp = save_funccal();
1610 ++sandbox;
1611 }
1612
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1614 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (safe)
1618 {
1619 --sandbox;
1620 restore_funccal(save_funccalp);
1621 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001622 vim_free(argvars);
1623
1624 if (ret == FAIL)
1625 clear_tv(rettv);
1626
1627 return ret;
1628}
1629
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001630/*
1631 * Call vimL function "func" and return the result as a number.
1632 * Returns -1 when calling the function fails.
1633 * Uses argv[argc] for the function arguments.
1634 */
1635 long
1636call_func_retnr(func, argc, argv, safe)
1637 char_u *func;
1638 int argc;
1639 char_u **argv;
1640 int safe; /* use the sandbox */
1641{
1642 typval_T rettv;
1643 long retval;
1644
1645 /* All arguments are passed as strings, no conversion to number. */
1646 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1647 return -1;
1648
1649 retval = get_tv_number_chk(&rettv, NULL);
1650 clear_tv(&rettv);
1651 return retval;
1652}
1653
1654#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1655 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1656
Bram Moolenaar4f688582007-07-24 12:34:30 +00001657# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001659 * Call vimL function "func" and return the result as a string.
1660 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 * Uses argv[argc] for the function arguments.
1662 */
1663 void *
1664call_func_retstr(func, argc, argv, safe)
1665 char_u *func;
1666 int argc;
1667 char_u **argv;
1668 int safe; /* use the sandbox */
1669{
1670 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001671 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001673 /* All arguments are passed as strings, no conversion to number. */
1674 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 return NULL;
1676
1677 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 return retval;
1680}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001681# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001683/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001684 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001686 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 */
1688 void *
1689call_func_retlist(func, argc, argv, safe)
1690 char_u *func;
1691 int argc;
1692 char_u **argv;
1693 int safe; /* use the sandbox */
1694{
1695 typval_T rettv;
1696
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001697 /* All arguments are passed as strings, no conversion to number. */
1698 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 return NULL;
1700
1701 if (rettv.v_type != VAR_LIST)
1702 {
1703 clear_tv(&rettv);
1704 return NULL;
1705 }
1706
1707 return rettv.vval.v_list;
1708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709#endif
1710
1711/*
1712 * Save the current function call pointer, and set it to NULL.
1713 * Used when executing autocommands and for ":source".
1714 */
1715 void *
1716save_funccal()
1717{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 current_funccal = NULL;
1721 return (void *)fc;
1722}
1723
1724 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001725restore_funccal(vfc)
1726 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728 funccall_T *fc = (funccall_T *)vfc;
1729
1730 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731}
1732
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733#if defined(FEAT_PROFILE) || defined(PROTO)
1734/*
1735 * Prepare profiling for entering a child or something else that is not
1736 * counted for the script/function itself.
1737 * Should always be called in pair with prof_child_exit().
1738 */
1739 void
1740prof_child_enter(tm)
1741 proftime_T *tm; /* place to store waittime */
1742{
1743 funccall_T *fc = current_funccal;
1744
1745 if (fc != NULL && fc->func->uf_profiling)
1746 profile_start(&fc->prof_child);
1747 script_prof_save(tm);
1748}
1749
1750/*
1751 * Take care of time spent in a child.
1752 * Should always be called after prof_child_enter().
1753 */
1754 void
1755prof_child_exit(tm)
1756 proftime_T *tm; /* where waittime was stored */
1757{
1758 funccall_T *fc = current_funccal;
1759
1760 if (fc != NULL && fc->func->uf_profiling)
1761 {
1762 profile_end(&fc->prof_child);
1763 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1764 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1765 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1766 }
1767 script_prof_restore(tm);
1768}
1769#endif
1770
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_FOLDING
1773/*
1774 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1775 * it in "*cp". Doesn't give error messages.
1776 */
1777 int
1778eval_foldexpr(arg, cp)
1779 char_u *arg;
1780 int *cp;
1781{
Bram Moolenaar33570922005-01-25 22:26:29 +00001782 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 int retval;
1784 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001785 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1786 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001789 if (use_sandbox)
1790 ++sandbox;
1791 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 retval = 0;
1795 else
1796 {
1797 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001798 if (tv.v_type == VAR_NUMBER)
1799 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001800 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 retval = 0;
1802 else
1803 {
1804 /* If the result is a string, check if there is a non-digit before
1805 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (!VIM_ISDIGIT(*s) && *s != '-')
1808 *cp = *s++;
1809 retval = atol((char *)s);
1810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001814 if (use_sandbox)
1815 --sandbox;
1816 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 return retval;
1819}
1820#endif
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 * ":let" list all variable values
1824 * ":let var1 var2" list variable values
1825 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 * ":let var += expr" assignment command.
1827 * ":let var -= expr" assignment command.
1828 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831 void
1832ex_let(eap)
1833 exarg_T *eap;
1834{
1835 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001836 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 int var_count = 0;
1840 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001843 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 argend = skip_var_list(arg, &var_count, &semicolon);
1846 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001848 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1849 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001853 /*
1854 * ":let" without "=": list variables
1855 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 if (*arg == '[')
1857 EMSG(_(e_invarg));
1858 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_glob_vars(&first);
1865 list_buf_vars(&first);
1866 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001867#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001869#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_script_vars(&first);
1871 list_func_vars(&first);
1872 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 eap->nextcmd = check_nextcmd(arg);
1875 }
1876 else
1877 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 op[0] = '=';
1879 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001880 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 {
1882 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1883 op[0] = expr[-1]; /* +=, -= or .= */
1884 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (eap->skip)
1888 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (eap->skip)
1891 {
1892 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 --emsg_skip;
1895 }
1896 else if (i != FAIL)
1897 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001898 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 }
1903}
1904
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905/*
1906 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1907 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 * When "nextchars" is not NULL it points to a string with characters that
1909 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1910 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 * Returns OK or FAIL;
1912 */
1913 static int
1914ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1915 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001916 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 int copy; /* copy values from "tv", don't move */
1918 int semicolon; /* from skip_var_list() */
1919 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921{
1922 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 listitem_T *item;
1926 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927
1928 if (*arg != '[')
1929 {
1930 /*
1931 * ":let var = expr" or ":for var in list"
1932 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 return FAIL;
1935 return OK;
1936 }
1937
1938 /*
1939 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1940 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001941 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 {
1943 EMSG(_(e_listreq));
1944 return FAIL;
1945 }
1946
1947 i = list_len(l);
1948 if (semicolon == 0 && var_count < i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953 if (var_count - semicolon > i)
1954 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001955 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 return FAIL;
1957 }
1958
1959 item = l->lv_first;
1960 while (*arg != ']')
1961 {
1962 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 item = item->li_next;
1965 if (arg == NULL)
1966 return FAIL;
1967
1968 arg = skipwhite(arg);
1969 if (*arg == ';')
1970 {
1971 /* Put the rest of the list (may be empty) in the var after ';'.
1972 * Create a new list for this. */
1973 l = list_alloc();
1974 if (l == NULL)
1975 return FAIL;
1976 while (item != NULL)
1977 {
1978 list_append_tv(l, &item->li_tv);
1979 item = item->li_next;
1980 }
1981
1982 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001983 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 ltv.vval.v_list = l;
1985 l->lv_refcount = 1;
1986
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1988 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 clear_tv(&ltv);
1990 if (arg == NULL)
1991 return FAIL;
1992 break;
1993 }
1994 else if (*arg != ',' && *arg != ']')
1995 {
1996 EMSG2(_(e_intern2), "ex_let_vars()");
1997 return FAIL;
1998 }
1999 }
2000
2001 return OK;
2002}
2003
2004/*
2005 * Skip over assignable variable "var" or list of variables "[var, var]".
2006 * Used for ":let varvar = expr" and ":for varvar in expr".
2007 * For "[var, var]" increment "*var_count" for each variable.
2008 * for "[var, var; var]" set "semicolon".
2009 * Return NULL for an error.
2010 */
2011 static char_u *
2012skip_var_list(arg, var_count, semicolon)
2013 char_u *arg;
2014 int *var_count;
2015 int *semicolon;
2016{
2017 char_u *p, *s;
2018
2019 if (*arg == '[')
2020 {
2021 /* "[var, var]": find the matching ']'. */
2022 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002023 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 {
2025 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2026 s = skip_var_one(p);
2027 if (s == p)
2028 {
2029 EMSG2(_(e_invarg2), p);
2030 return NULL;
2031 }
2032 ++*var_count;
2033
2034 p = skipwhite(s);
2035 if (*p == ']')
2036 break;
2037 else if (*p == ';')
2038 {
2039 if (*semicolon == 1)
2040 {
2041 EMSG(_("Double ; in list of variables"));
2042 return NULL;
2043 }
2044 *semicolon = 1;
2045 }
2046 else if (*p != ',')
2047 {
2048 EMSG2(_(e_invarg2), p);
2049 return NULL;
2050 }
2051 }
2052 return p + 1;
2053 }
2054 else
2055 return skip_var_one(arg);
2056}
2057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002059 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002060 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 static char_u *
2063skip_var_one(arg)
2064 char_u *arg;
2065{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002066 if (*arg == '@' && arg[1] != NUL)
2067 return arg + 2;
2068 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2069 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070}
2071
Bram Moolenaara7043832005-01-21 11:56:39 +00002072/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 * List variables for hashtab "ht" with prefix "prefix".
2074 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082{
Bram Moolenaar33570922005-01-25 22:26:29 +00002083 hashitem_T *hi;
2084 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 int todo;
2086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002087 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002088 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2089 {
2090 if (!HASHITEM_EMPTY(hi))
2091 {
2092 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 di = HI2DI(hi);
2094 if (empty || di->di_tv.v_type != VAR_STRING
2095 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 }
2098 }
2099}
2100
2101/*
2102 * List global variables.
2103 */
2104 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105list_glob_vars(first)
2106 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002107{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002109}
2110
2111/*
2112 * List buffer variables.
2113 */
2114 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115list_buf_vars(first)
2116 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002117{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 char_u numbuf[NUMBUFLEN];
2119
Bram Moolenaar429fa852013-04-15 12:27:36 +02002120 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122
2123 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2125 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List window variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_win_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002135 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137}
2138
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002139#ifdef FEAT_WINDOWS
2140/*
2141 * List tab page variables.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_tab_vars(first)
2145 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002146{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002147 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002149}
2150#endif
2151
Bram Moolenaara7043832005-01-21 11:56:39 +00002152/*
2153 * List Vim variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_vim_vars(first)
2157 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160}
2161
2162/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163 * List script-local variables, if there is a script.
2164 */
2165 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166list_script_vars(first)
2167 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168{
2169 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2171 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172}
2173
2174/*
2175 * List function variables, if there is a function.
2176 */
2177 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178list_func_vars(first)
2179 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180{
2181 if (current_funccal != NULL)
2182 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002184}
2185
2186/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 * List variables in "arg".
2188 */
2189 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 exarg_T *eap;
2192 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194{
2195 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 char_u *name_start;
2199 char_u *arg_subsc;
2200 char_u *tofree;
2201 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202
2203 while (!ends_excmd(*arg) && !got_int)
2204 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002205 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002207 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002208 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2209 {
2210 emsg_severe = TRUE;
2211 EMSG(_(e_trailing));
2212 break;
2213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* get_name_len() takes care of expanding curly braces */
2218 name_start = name = arg;
2219 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2220 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 /* This is mainly to keep test 49 working: when expanding
2223 * curly braces fails overrule the exception error message. */
2224 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 emsg_severe = TRUE;
2227 EMSG2(_(e_invarg2), arg);
2228 break;
2229 }
2230 error = TRUE;
2231 }
2232 else
2233 {
2234 if (tofree != NULL)
2235 name = tofree;
2236 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 else
2239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 /* handle d.key, l[idx], f(expr) */
2241 arg_subsc = arg;
2242 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 'g': list_glob_vars(first); break;
2251 case 'b': list_buf_vars(first); break;
2252 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002253#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 'v': list_vim_vars(first); break;
2257 case 's': list_script_vars(first); break;
2258 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 default:
2260 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
2263 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 {
2265 char_u numbuf[NUMBUFLEN];
2266 char_u *tf;
2267 int c;
2268 char_u *s;
2269
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002270 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 c = *arg;
2272 *arg = NUL;
2273 list_one_var_a((char_u *)"",
2274 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002275 tv.v_type,
2276 s == NULL ? (char_u *)"" : s,
2277 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 *arg = c;
2279 vim_free(tf);
2280 }
2281 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285
2286 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288
2289 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 }
2291
2292 return arg;
2293}
2294
2295/*
2296 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2297 * Returns a pointer to the char just after the var name.
2298 * Returns NULL if there is an error.
2299 */
2300 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002303 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002305 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307{
2308 int c1;
2309 char_u *name;
2310 char_u *p;
2311 char_u *arg_end = NULL;
2312 int len;
2313 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315
2316 /*
2317 * ":let $VAR = expr": Set environment variable.
2318 */
2319 if (*arg == '$')
2320 {
2321 /* Find the end of the name. */
2322 ++arg;
2323 name = arg;
2324 len = get_env_len(&arg);
2325 if (len == 0)
2326 EMSG2(_(e_invarg2), name - 1);
2327 else
2328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 if (op != NULL && (*op == '+' || *op == '-'))
2330 EMSG2(_(e_letwrong), op);
2331 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002332 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002334 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 {
2336 c1 = name[len];
2337 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 p = get_tv_string_chk(tv);
2339 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340 {
2341 int mustfree = FALSE;
2342 char_u *s = vim_getenv(name, &mustfree);
2343
2344 if (s != NULL)
2345 {
2346 p = tofree = concat_str(s, p);
2347 if (mustfree)
2348 vim_free(s);
2349 }
2350 }
2351 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 if (STRICMP(name, "HOME") == 0)
2355 init_homedir();
2356 else if (didset_vim && STRICMP(name, "VIM") == 0)
2357 didset_vim = FALSE;
2358 else if (didset_vimruntime
2359 && STRICMP(name, "VIMRUNTIME") == 0)
2360 didset_vimruntime = FALSE;
2361 arg_end = arg;
2362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 }
2366 }
2367 }
2368
2369 /*
2370 * ":let &option = expr": Set option value.
2371 * ":let &l:option = expr": Set local option value.
2372 * ":let &g:option = expr": Set global option value.
2373 */
2374 else if (*arg == '&')
2375 {
2376 /* Find the end of the name. */
2377 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 if (p == NULL || (endchars != NULL
2379 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 EMSG(_(e_letunexp));
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 long n;
2384 int opt_type;
2385 long numval;
2386 char_u *stringval = NULL;
2387 char_u *s;
2388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 c1 = *p;
2390 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391
2392 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 s = get_tv_string_chk(tv); /* != NULL if number or string */
2394 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 {
2396 opt_type = get_option_value(arg, &numval,
2397 &stringval, opt_flags);
2398 if ((opt_type == 1 && *op == '.')
2399 || (opt_type == 0 && *op != '.'))
2400 EMSG2(_(e_letwrong), op);
2401 else
2402 {
2403 if (opt_type == 1) /* number */
2404 {
2405 if (*op == '+')
2406 n = numval + n;
2407 else
2408 n = numval - n;
2409 }
2410 else if (opt_type == 0 && stringval != NULL) /* string */
2411 {
2412 s = concat_str(stringval, s);
2413 vim_free(stringval);
2414 stringval = s;
2415 }
2416 }
2417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 if (s != NULL)
2419 {
2420 set_option_value(arg, n, s, opt_flags);
2421 arg_end = p;
2422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 }
2426 }
2427
2428 /*
2429 * ":let @r = expr": Set register contents.
2430 */
2431 else if (*arg == '@')
2432 {
2433 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 if (op != NULL && (*op == '+' || *op == '-'))
2435 EMSG2(_(e_letwrong), op);
2436 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 EMSG(_(e_letunexp));
2439 else
2440 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002441 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 char_u *s;
2443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 p = get_tv_string_chk(tv);
2445 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002447 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (s != NULL)
2449 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002450 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(s);
2452 }
2453 }
2454 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 arg_end = arg + 1;
2458 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002459 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 }
2461 }
2462
2463 /*
2464 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002465 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002469 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002471 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2475 EMSG(_(e_letunexp));
2476 else
2477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 arg_end = p;
2480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484
2485 else
2486 EMSG2(_(e_invarg2), arg);
2487
2488 return arg_end;
2489}
2490
2491/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2493 */
2494 static int
2495check_changedtick(arg)
2496 char_u *arg;
2497{
2498 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2499 {
2500 EMSG2(_(e_readonlyvar), arg);
2501 return TRUE;
2502 }
2503 return FALSE;
2504}
2505
2506/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 * Get an lval: variable, Dict item or List item that can be assigned a value
2508 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2509 * "name.key", "name.key[expr]" etc.
2510 * Indexing only works if "name" is an existing List or Dictionary.
2511 * "name" points to the start of the name.
2512 * If "rettv" is not NULL it points to the value to be assigned.
2513 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2514 * wrong; must end in space or cmd separator.
2515 *
2516 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002517 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 */
2520 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002521get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 typval_T *rettv;
2524 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 int unlet;
2526 int skip;
2527 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002528 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 char_u *expr_start, *expr_end;
2532 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 dictitem_T *v;
2534 typval_T var1;
2535 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544
2545 if (skip)
2546 {
2547 /* When skipping just find the end of the name. */
2548 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 }
2551
2552 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (expr_start != NULL)
2555 {
2556 /* Don't expand the name when we already know there is an error. */
2557 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2558 && *p != '[' && *p != '.')
2559 {
2560 EMSG(_(e_trailing));
2561 return NULL;
2562 }
2563
2564 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2565 if (lp->ll_exp_name == NULL)
2566 {
2567 /* Report an invalid expression in braces, unless the
2568 * expression evaluation has been cancelled due to an
2569 * aborting error, an interrupt, or an exception. */
2570 if (!aborting() && !quiet)
2571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002572 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 EMSG2(_(e_invarg2), name);
2574 return NULL;
2575 }
2576 }
2577 lp->ll_name = lp->ll_exp_name;
2578 }
2579 else
2580 lp->ll_name = name;
2581
2582 /* Without [idx] or .key we are done. */
2583 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2584 return p;
2585
2586 cc = *p;
2587 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002588 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 if (v == NULL && !quiet)
2590 EMSG2(_(e_undefvar), lp->ll_name);
2591 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 if (v == NULL)
2593 return NULL;
2594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /*
2596 * Loop until no more [idx] or .key is following.
2597 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2602 && !(lp->ll_tv->v_type == VAR_DICT
2603 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_("E689: Can only index a List or Dictionary"));
2607 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!quiet)
2612 EMSG(_("E708: [:] must come last"));
2613 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 len = -1;
2617 if (*p == '.')
2618 {
2619 key = p + 1;
2620 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2621 ;
2622 if (len == 0)
2623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_(e_emptykey));
2626 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 }
2628 p = key + len;
2629 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 else
2631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002633 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 if (*p == ':')
2635 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 else
2637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 empty1 = FALSE;
2639 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002641 if (get_tv_string_chk(&var1) == NULL)
2642 {
2643 /* not a number or string */
2644 clear_tv(&var1);
2645 return NULL;
2646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 }
2648
2649 /* Optionally get the second index [ :expr]. */
2650 if (*p == ':')
2651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002655 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656 if (!empty1)
2657 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (rettv != NULL && (rettv->v_type != VAR_LIST
2661 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 if (!empty1)
2666 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 }
2669 p = skipwhite(p + 1);
2670 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 else
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (!empty1)
2678 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002681 if (get_tv_string_chk(&var2) == NULL)
2682 {
2683 /* not a number or string */
2684 if (!empty1)
2685 clear_tv(&var1);
2686 clear_tv(&var2);
2687 return NULL;
2688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (!quiet)
2698 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705
2706 /* Skip to past ']'. */
2707 ++p;
2708 }
2709
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
2712 if (len == -1)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002715 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (*key == NUL)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002724 lp->ll_list = NULL;
2725 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002726 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002727
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002728 /* When assigning to a scope dictionary check that a function and
2729 * variable name is valid (only variable name unless it is l: or
2730 * g: dictionary). Disallow overwriting a builtin function. */
2731 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002732 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002733 int prevval;
2734 int wrong;
2735
2736 if (len != -1)
2737 {
2738 prevval = key[len];
2739 key[len] = NUL;
2740 }
2741 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2742 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002743 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002744 || !valid_varname(key);
2745 if (len != -1)
2746 key[len] = prevval;
2747 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002748 return NULL;
2749 }
2750
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002753 /* Can't add "v:" variable. */
2754 if (lp->ll_dict == &vimvardict)
2755 {
2756 EMSG2(_(e_illvar), name);
2757 return NULL;
2758 }
2759
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002760 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002764 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (len == -1)
2766 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 }
2769 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 if (len == -1)
2774 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 p = NULL;
2777 break;
2778 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 /* existing variable, need to check if it can be changed */
2780 else if (var_check_ro(lp->ll_di->di_flags, name))
2781 return NULL;
2782
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 if (len == -1)
2784 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 }
2787 else
2788 {
2789 /*
2790 * Get the number and item for the only or first index of the List.
2791 */
2792 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 else
2795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002796 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var1);
2798 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002799 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_list = lp->ll_tv->vval.v_list;
2801 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2802 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002804 if (lp->ll_n1 < 0)
2805 {
2806 lp->ll_n1 = 0;
2807 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2808 }
2809 }
2810 if (lp->ll_li == NULL)
2811 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002814 if (!quiet)
2815 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818
2819 /*
2820 * May need to find the item or absolute index for the second
2821 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 * When no index given: "lp->ll_empty2" is TRUE.
2823 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 {
2834 if (!quiet)
2835 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2842 if (lp->ll_n1 < 0)
2843 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2844 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 {
2846 if (!quiet)
2847 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002849 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002850 }
2851
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 return p;
2857}
2858
2859/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 */
2862 static void
2863clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865{
2866 vim_free(lp->ll_exp_name);
2867 vim_free(lp->ll_newkey);
2868}
2869
2870/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002871 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002873 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 */
2875 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882{
2883 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 listitem_T *ri;
2885 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886
2887 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 cc = *endp;
2892 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 if (op != NULL && *op != '=')
2894 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002897 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002898 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002899 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900 {
2901 if (tv_op(&tv, rettv, op) == OK)
2902 set_var(lp->ll_name, &tv, FALSE);
2903 clear_tv(&tv);
2904 }
2905 }
2906 else
2907 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002911 else if (tv_check_lock(lp->ll_newkey == NULL
2912 ? lp->ll_tv->v_lock
2913 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2914 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 else if (lp->ll_range)
2916 {
2917 /*
2918 * Assign the List values to the list items.
2919 */
2920 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002921 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 if (op != NULL && *op != '=')
2923 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2924 else
2925 {
2926 clear_tv(&lp->ll_li->li_tv);
2927 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 ri = ri->li_next;
2930 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2931 break;
2932 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002935 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 ri = NULL;
2938 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 lp->ll_li = lp->ll_li->li_next;
2942 ++lp->ll_n1;
2943 }
2944 if (ri != NULL)
2945 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 else if (lp->ll_empty2
2947 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 : lp->ll_n1 != lp->ll_n2)
2949 EMSG(_("E711: List value has not enough items"));
2950 }
2951 else
2952 {
2953 /*
2954 * Assign to a List or Dictionary item.
2955 */
2956 if (lp->ll_newkey != NULL)
2957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 if (op != NULL && *op != '=')
2959 {
2960 EMSG2(_(e_letwrong), op);
2961 return;
2962 }
2963
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002965 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 if (di == NULL)
2967 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2969 {
2970 vim_free(di);
2971 return;
2972 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002974 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002975 else if (op != NULL && *op != '=')
2976 {
2977 tv_op(lp->ll_tv, rettv, op);
2978 return;
2979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002982
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 /*
2984 * Assign the value to the variable or list item.
2985 */
2986 if (copy)
2987 copy_tv(rettv, lp->ll_tv);
2988 else
2989 {
2990 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002991 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002993 }
2994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995}
2996
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002997/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2999 * Returns OK or FAIL.
3000 */
3001 static int
3002tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 typval_T *tv1;
3004 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 char_u *op;
3006{
3007 long n;
3008 char_u numbuf[NUMBUFLEN];
3009 char_u *s;
3010
3011 /* Can't do anything with a Funcref or a Dict on the right. */
3012 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3013 {
3014 switch (tv1->v_type)
3015 {
3016 case VAR_DICT:
3017 case VAR_FUNC:
3018 break;
3019
3020 case VAR_LIST:
3021 if (*op != '+' || tv2->v_type != VAR_LIST)
3022 break;
3023 /* List += List */
3024 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3025 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3026 return OK;
3027
3028 case VAR_NUMBER:
3029 case VAR_STRING:
3030 if (tv2->v_type == VAR_LIST)
3031 break;
3032 if (*op == '+' || *op == '-')
3033 {
3034 /* nr += nr or nr -= nr*/
3035 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003036#ifdef FEAT_FLOAT
3037 if (tv2->v_type == VAR_FLOAT)
3038 {
3039 float_T f = n;
3040
3041 if (*op == '+')
3042 f += tv2->vval.v_float;
3043 else
3044 f -= tv2->vval.v_float;
3045 clear_tv(tv1);
3046 tv1->v_type = VAR_FLOAT;
3047 tv1->vval.v_float = f;
3048 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050#endif
3051 {
3052 if (*op == '+')
3053 n += get_tv_number(tv2);
3054 else
3055 n -= get_tv_number(tv2);
3056 clear_tv(tv1);
3057 tv1->v_type = VAR_NUMBER;
3058 tv1->vval.v_number = n;
3059 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 }
3061 else
3062 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063 if (tv2->v_type == VAR_FLOAT)
3064 break;
3065
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 /* str .= str */
3067 s = get_tv_string(tv1);
3068 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_STRING;
3071 tv1->vval.v_string = s;
3072 }
3073 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074
3075#ifdef FEAT_FLOAT
3076 case VAR_FLOAT:
3077 {
3078 float_T f;
3079
3080 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3081 && tv2->v_type != VAR_NUMBER
3082 && tv2->v_type != VAR_STRING))
3083 break;
3084 if (tv2->v_type == VAR_FLOAT)
3085 f = tv2->vval.v_float;
3086 else
3087 f = get_tv_number(tv2);
3088 if (*op == '+')
3089 tv1->vval.v_float += f;
3090 else
3091 tv1->vval.v_float -= f;
3092 }
3093 return OK;
3094#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 }
3096 }
3097
3098 EMSG2(_(e_letwrong), op);
3099 return FAIL;
3100}
3101
3102/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003103 * Add a watcher to a list.
3104 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003105 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003107 list_T *l;
3108 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109{
3110 lw->lw_next = l->lv_watch;
3111 l->lv_watch = lw;
3112}
3113
3114/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003115 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116 * No warning when it isn't found...
3117 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003118 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003120 list_T *l;
3121 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122{
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124
3125 lwp = &l->lv_watch;
3126 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3127 {
3128 if (lw == lwrem)
3129 {
3130 *lwp = lw->lw_next;
3131 break;
3132 }
3133 lwp = &lw->lw_next;
3134 }
3135}
3136
3137/*
3138 * Just before removing an item from a list: advance watchers to the next
3139 * item.
3140 */
3141 static void
3142list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003143 list_T *l;
3144 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145{
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147
3148 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3149 if (lw->lw_item == item)
3150 lw->lw_item = item->li_next;
3151}
3152
3153/*
3154 * Evaluate the expression used in a ":for var in expr" command.
3155 * "arg" points to "var".
3156 * Set "*errp" to TRUE for an error, FALSE otherwise;
3157 * Return a pointer that holds the info. Null when there is an error.
3158 */
3159 void *
3160eval_for_line(arg, errp, nextcmdp, skip)
3161 char_u *arg;
3162 int *errp;
3163 char_u **nextcmdp;
3164 int skip;
3165{
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 typval_T tv;
3169 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170
3171 *errp = TRUE; /* default: there is an error */
3172
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 if (fi == NULL)
3175 return NULL;
3176
3177 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3178 if (expr == NULL)
3179 return fi;
3180
3181 expr = skipwhite(expr);
3182 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3183 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003184 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 return fi;
3186 }
3187
3188 if (skip)
3189 ++emsg_skip;
3190 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3191 {
3192 *errp = FALSE;
3193 if (!skip)
3194 {
3195 l = tv.vval.v_list;
3196 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003197 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 clear_tv(&tv);
3200 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 else
3202 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003203 /* No need to increment the refcount, it's already set for the
3204 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 fi->fi_list = l;
3206 list_add_watch(l, &fi->fi_lw);
3207 fi->fi_lw.lw_item = l->lv_first;
3208 }
3209 }
3210 }
3211 if (skip)
3212 --emsg_skip;
3213
3214 return fi;
3215}
3216
3217/*
3218 * Use the first item in a ":for" list. Advance to the next.
3219 * Assign the values to the variable (list). "arg" points to the first one.
3220 * Return TRUE when a valid item was found, FALSE when at end of list or
3221 * something wrong.
3222 */
3223 int
3224next_for_item(fi_void, arg)
3225 void *fi_void;
3226 char_u *arg;
3227{
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231
3232 item = fi->fi_lw.lw_item;
3233 if (item == NULL)
3234 result = FALSE;
3235 else
3236 {
3237 fi->fi_lw.lw_item = item->li_next;
3238 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3239 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3240 }
3241 return result;
3242}
3243
3244/*
3245 * Free the structure used to store info used by ":for".
3246 */
3247 void
3248free_for_info(fi_void)
3249 void *fi_void;
3250{
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003253 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 list_unref(fi->fi_list);
3257 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 vim_free(fi);
3259}
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3262
3263 void
3264set_context_for_expression(xp, arg, cmdidx)
3265 expand_T *xp;
3266 char_u *arg;
3267 cmdidx_T cmdidx;
3268{
3269 int got_eq = FALSE;
3270 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 if (cmdidx == CMD_let)
3274 {
3275 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003276 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 {
3278 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003279 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 {
3281 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 if (vim_iswhite(*p))
3284 break;
3285 }
3286 return;
3287 }
3288 }
3289 else
3290 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3291 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 while ((xp->xp_pattern = vim_strpbrk(arg,
3293 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3294 {
3295 c = *xp->xp_pattern;
3296 if (c == '&')
3297 {
3298 c = xp->xp_pattern[1];
3299 if (c == '&')
3300 {
3301 ++xp->xp_pattern;
3302 xp->xp_context = cmdidx != CMD_let || got_eq
3303 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3304 }
3305 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003308 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3309 xp->xp_pattern += 2;
3310
3311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313 else if (c == '$')
3314 {
3315 /* environment variable */
3316 xp->xp_context = EXPAND_ENV_VARS;
3317 }
3318 else if (c == '=')
3319 {
3320 got_eq = TRUE;
3321 xp->xp_context = EXPAND_EXPRESSION;
3322 }
3323 else if (c == '<'
3324 && xp->xp_context == EXPAND_FUNCTIONS
3325 && vim_strchr(xp->xp_pattern, '(') == NULL)
3326 {
3327 /* Function name can start with "<SNR>" */
3328 break;
3329 }
3330 else if (cmdidx != CMD_let || got_eq)
3331 {
3332 if (c == '"') /* string */
3333 {
3334 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3335 if (c == '\\' && xp->xp_pattern[1] != NUL)
3336 ++xp->xp_pattern;
3337 xp->xp_context = EXPAND_NOTHING;
3338 }
3339 else if (c == '\'') /* literal string */
3340 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003341 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3343 /* skip */ ;
3344 xp->xp_context = EXPAND_NOTHING;
3345 }
3346 else if (c == '|')
3347 {
3348 if (xp->xp_pattern[1] == '|')
3349 {
3350 ++xp->xp_pattern;
3351 xp->xp_context = EXPAND_EXPRESSION;
3352 }
3353 else
3354 xp->xp_context = EXPAND_COMMANDS;
3355 }
3356 else
3357 xp->xp_context = EXPAND_EXPRESSION;
3358 }
3359 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360 /* Doesn't look like something valid, expand as an expression
3361 * anyway. */
3362 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 arg = xp->xp_pattern;
3364 if (*arg != NUL)
3365 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3366 /* skip */ ;
3367 }
3368 xp->xp_pattern = arg;
3369}
3370
3371#endif /* FEAT_CMDL_COMPL */
3372
3373/*
3374 * ":1,25call func(arg1, arg2)" function call.
3375 */
3376 void
3377ex_call(eap)
3378 exarg_T *eap;
3379{
3380 char_u *arg = eap->arg;
3381 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003383 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003385 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 linenr_T lnum;
3387 int doesrange;
3388 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003391 if (eap->skip)
3392 {
3393 /* trans_function_name() doesn't work well when skipping, use eval0()
3394 * instead to skip to any following command, e.g. for:
3395 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003396 ++emsg_skip;
3397 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3398 clear_tv(&rettv);
3399 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003400 return;
3401 }
3402
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003403 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003404 if (fudi.fd_newkey != NULL)
3405 {
3406 /* Still need to give an error message for missing key. */
3407 EMSG2(_(e_dictkey), fudi.fd_newkey);
3408 vim_free(fudi.fd_newkey);
3409 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003410 if (tofree == NULL)
3411 return;
3412
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003413 /* Increase refcount on dictionary, it could get deleted when evaluating
3414 * the arguments. */
3415 if (fudi.fd_dict != NULL)
3416 ++fudi.fd_dict->dv_refcount;
3417
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003418 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003419 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003420 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar532c7802005-01-27 14:44:31 +00003422 /* Skip white space to allow ":call func ()". Not good, but required for
3423 * backward compatibility. */
3424 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427 if (*startarg != '(')
3428 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003429 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 goto end;
3431 }
3432
3433 /*
3434 * When skipping, evaluate the function once, to find the end of the
3435 * arguments.
3436 * When the function takes a range, this is discovered after the first
3437 * call, and the loop is broken.
3438 */
3439 if (eap->skip)
3440 {
3441 ++emsg_skip;
3442 lnum = eap->line2; /* do it once, also with an invalid range */
3443 }
3444 else
3445 lnum = eap->line1;
3446 for ( ; lnum <= eap->line2; ++lnum)
3447 {
3448 if (!eap->skip && eap->addr_count > 0)
3449 {
3450 curwin->w_cursor.lnum = lnum;
3451 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003452#ifdef FEAT_VIRTUALEDIT
3453 curwin->w_cursor.coladd = 0;
3454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003458 eap->line1, eap->line2, &doesrange,
3459 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 failed = TRUE;
3462 break;
3463 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003464
3465 /* Handle a function returning a Funcref, Dictionary or List. */
3466 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3467 {
3468 failed = TRUE;
3469 break;
3470 }
3471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003472 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (doesrange || eap->skip)
3474 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003477 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003479 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (aborting())
3481 break;
3482 }
3483 if (eap->skip)
3484 --emsg_skip;
3485
3486 if (!failed)
3487 {
3488 /* Check for trailing illegal characters and a following command. */
3489 if (!ends_excmd(*arg))
3490 {
3491 emsg_severe = TRUE;
3492 EMSG(_(e_trailing));
3493 }
3494 else
3495 eap->nextcmd = check_nextcmd(arg);
3496 }
3497
3498end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003499 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003500 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501}
3502
3503/*
3504 * ":unlet[!] var1 ... " command.
3505 */
3506 void
3507ex_unlet(eap)
3508 exarg_T *eap;
3509{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003510 ex_unletlock(eap, eap->arg, 0);
3511}
3512
3513/*
3514 * ":lockvar" and ":unlockvar" commands
3515 */
3516 void
3517ex_lockvar(eap)
3518 exarg_T *eap;
3519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003521 int deep = 2;
3522
3523 if (eap->forceit)
3524 deep = -1;
3525 else if (vim_isdigit(*arg))
3526 {
3527 deep = getdigits(&arg);
3528 arg = skipwhite(arg);
3529 }
3530
3531 ex_unletlock(eap, arg, deep);
3532}
3533
3534/*
3535 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3536 */
3537 static void
3538ex_unletlock(eap, argstart, deep)
3539 exarg_T *eap;
3540 char_u *argstart;
3541 int deep;
3542{
3543 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003546 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 do
3549 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003551 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3552 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 if (lv.ll_name == NULL)
3554 error = TRUE; /* error but continue parsing */
3555 if (name_end == NULL || (!vim_iswhite(*name_end)
3556 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 if (name_end != NULL)
3559 {
3560 emsg_severe = TRUE;
3561 EMSG(_(e_trailing));
3562 }
3563 if (!(eap->skip || error))
3564 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 break;
3566 }
3567
3568 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 {
3570 if (eap->cmdidx == CMD_unlet)
3571 {
3572 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3573 error = TRUE;
3574 }
3575 else
3576 {
3577 if (do_lock_var(&lv, name_end, deep,
3578 eap->cmdidx == CMD_lockvar) == FAIL)
3579 error = TRUE;
3580 }
3581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003583 if (!eap->skip)
3584 clear_lval(&lv);
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 arg = skipwhite(name_end);
3587 } while (!ends_excmd(*arg));
3588
3589 eap->nextcmd = check_nextcmd(arg);
3590}
3591
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003594 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 int forceit;
3597{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 int ret = OK;
3599 int cc;
3600
3601 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 cc = *name_end;
3604 *name_end = NUL;
3605
3606 /* Normal name or expanded name. */
3607 if (check_changedtick(lp->ll_name))
3608 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3614 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 else if (lp->ll_range)
3616 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003617 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618
3619 /* Delete a range of List items. */
3620 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3621 {
3622 li = lp->ll_li->li_next;
3623 listitem_remove(lp->ll_list, lp->ll_li);
3624 lp->ll_li = li;
3625 ++lp->ll_n1;
3626 }
3627 }
3628 else
3629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 /* unlet a List item. */
3632 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003635 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 }
3637
3638 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639}
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641/*
3642 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 */
3645 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
Bram Moolenaar33570922005-01-25 22:26:29 +00003650 hashtab_T *ht;
3651 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003652 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003653 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
Bram Moolenaar33570922005-01-25 22:26:29 +00003655 ht = find_var_ht(name, &varname);
3656 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 hi = hash_find(ht, varname);
3659 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003660 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003661 di = HI2DI(hi);
3662 if (var_check_fixed(di->di_flags, name)
3663 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 return FAIL;
3665 delete_var(ht, hi);
3666 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003669 if (forceit)
3670 return OK;
3671 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return FAIL;
3673}
3674
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675/*
3676 * Lock or unlock variable indicated by "lp".
3677 * "deep" is the levels to go (-1 for unlimited);
3678 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3679 */
3680 static int
3681do_lock_var(lp, name_end, deep, lock)
3682 lval_T *lp;
3683 char_u *name_end;
3684 int deep;
3685 int lock;
3686{
3687 int ret = OK;
3688 int cc;
3689 dictitem_T *di;
3690
3691 if (deep == 0) /* nothing to do */
3692 return OK;
3693
3694 if (lp->ll_tv == NULL)
3695 {
3696 cc = *name_end;
3697 *name_end = NUL;
3698
3699 /* Normal name or expanded name. */
3700 if (check_changedtick(lp->ll_name))
3701 ret = FAIL;
3702 else
3703 {
3704 di = find_var(lp->ll_name, NULL);
3705 if (di == NULL)
3706 ret = FAIL;
3707 else
3708 {
3709 if (lock)
3710 di->di_flags |= DI_FLAGS_LOCK;
3711 else
3712 di->di_flags &= ~DI_FLAGS_LOCK;
3713 item_lock(&di->di_tv, deep, lock);
3714 }
3715 }
3716 *name_end = cc;
3717 }
3718 else if (lp->ll_range)
3719 {
3720 listitem_T *li = lp->ll_li;
3721
3722 /* (un)lock a range of List items. */
3723 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3724 {
3725 item_lock(&li->li_tv, deep, lock);
3726 li = li->li_next;
3727 ++lp->ll_n1;
3728 }
3729 }
3730 else if (lp->ll_list != NULL)
3731 /* (un)lock a List item. */
3732 item_lock(&lp->ll_li->li_tv, deep, lock);
3733 else
3734 /* un(lock) a Dictionary item. */
3735 item_lock(&lp->ll_di->di_tv, deep, lock);
3736
3737 return ret;
3738}
3739
3740/*
3741 * Lock or unlock an item. "deep" is nr of levels to go.
3742 */
3743 static void
3744item_lock(tv, deep, lock)
3745 typval_T *tv;
3746 int deep;
3747 int lock;
3748{
3749 static int recurse = 0;
3750 list_T *l;
3751 listitem_T *li;
3752 dict_T *d;
3753 hashitem_T *hi;
3754 int todo;
3755
3756 if (recurse >= DICT_MAXNEST)
3757 {
3758 EMSG(_("E743: variable nested too deep for (un)lock"));
3759 return;
3760 }
3761 if (deep == 0)
3762 return;
3763 ++recurse;
3764
3765 /* lock/unlock the item itself */
3766 if (lock)
3767 tv->v_lock |= VAR_LOCKED;
3768 else
3769 tv->v_lock &= ~VAR_LOCKED;
3770
3771 switch (tv->v_type)
3772 {
3773 case VAR_LIST:
3774 if ((l = tv->vval.v_list) != NULL)
3775 {
3776 if (lock)
3777 l->lv_lock |= VAR_LOCKED;
3778 else
3779 l->lv_lock &= ~VAR_LOCKED;
3780 if (deep < 0 || deep > 1)
3781 /* recursive: lock/unlock the items the List contains */
3782 for (li = l->lv_first; li != NULL; li = li->li_next)
3783 item_lock(&li->li_tv, deep - 1, lock);
3784 }
3785 break;
3786 case VAR_DICT:
3787 if ((d = tv->vval.v_dict) != NULL)
3788 {
3789 if (lock)
3790 d->dv_lock |= VAR_LOCKED;
3791 else
3792 d->dv_lock &= ~VAR_LOCKED;
3793 if (deep < 0 || deep > 1)
3794 {
3795 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003796 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3798 {
3799 if (!HASHITEM_EMPTY(hi))
3800 {
3801 --todo;
3802 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3803 }
3804 }
3805 }
3806 }
3807 }
3808 --recurse;
3809}
3810
Bram Moolenaara40058a2005-07-11 22:42:07 +00003811/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003812 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3813 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003814 */
3815 static int
3816tv_islocked(tv)
3817 typval_T *tv;
3818{
3819 return (tv->v_lock & VAR_LOCKED)
3820 || (tv->v_type == VAR_LIST
3821 && tv->vval.v_list != NULL
3822 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3823 || (tv->v_type == VAR_DICT
3824 && tv->vval.v_dict != NULL
3825 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3826}
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3829/*
3830 * Delete all "menutrans_" variables.
3831 */
3832 void
3833del_menutrans_vars()
3834{
Bram Moolenaar33570922005-01-25 22:26:29 +00003835 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003836 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003839 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003840 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003841 {
3842 if (!HASHITEM_EMPTY(hi))
3843 {
3844 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3846 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003847 }
3848 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850}
3851#endif
3852
3853#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3854
3855/*
3856 * Local string buffer for the next two functions to store a variable name
3857 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3858 * get_user_var_name().
3859 */
3860
3861static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3862
3863static char_u *varnamebuf = NULL;
3864static int varnamebuflen = 0;
3865
3866/*
3867 * Function to concatenate a prefix and a variable name.
3868 */
3869 static char_u *
3870cat_prefix_varname(prefix, name)
3871 int prefix;
3872 char_u *name;
3873{
3874 int len;
3875
3876 len = (int)STRLEN(name) + 3;
3877 if (len > varnamebuflen)
3878 {
3879 vim_free(varnamebuf);
3880 len += 10; /* some additional space */
3881 varnamebuf = alloc(len);
3882 if (varnamebuf == NULL)
3883 {
3884 varnamebuflen = 0;
3885 return NULL;
3886 }
3887 varnamebuflen = len;
3888 }
3889 *varnamebuf = prefix;
3890 varnamebuf[1] = ':';
3891 STRCPY(varnamebuf + 2, name);
3892 return varnamebuf;
3893}
3894
3895/*
3896 * Function given to ExpandGeneric() to obtain the list of user defined
3897 * (global/buffer/window/built-in) variable names.
3898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 char_u *
3900get_user_var_name(xp, idx)
3901 expand_T *xp;
3902 int idx;
3903{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003904 static long_u gdone;
3905 static long_u bdone;
3906 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003907#ifdef FEAT_WINDOWS
3908 static long_u tdone;
3909#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003910 static int vidx;
3911 static hashitem_T *hi;
3912 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003915 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003916 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003917#ifdef FEAT_WINDOWS
3918 tdone = 0;
3919#endif
3920 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003921
3922 /* Global variables */
3923 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003925 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003926 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003927 else
3928 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 while (HASHITEM_EMPTY(hi))
3930 ++hi;
3931 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3932 return cat_prefix_varname('g', hi->hi_key);
3933 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003935
3936 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003937 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003942 else
3943 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 while (HASHITEM_EMPTY(hi))
3945 ++hi;
3946 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 return (char_u *)"b:changedtick";
3952 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003953
3954 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003955 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003958 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003960 else
3961 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003962 while (HASHITEM_EMPTY(hi))
3963 ++hi;
3964 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003966
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003967#ifdef FEAT_WINDOWS
3968 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003969 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003970 if (tdone < ht->ht_used)
3971 {
3972 if (tdone++ == 0)
3973 hi = ht->ht_array;
3974 else
3975 ++hi;
3976 while (HASHITEM_EMPTY(hi))
3977 ++hi;
3978 return cat_prefix_varname('t', hi->hi_key);
3979 }
3980#endif
3981
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 /* v: variables */
3983 if (vidx < VV_LEN)
3984 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 vim_free(varnamebuf);
3987 varnamebuf = NULL;
3988 varnamebuflen = 0;
3989 return NULL;
3990}
3991
3992#endif /* FEAT_CMDL_COMPL */
3993
3994/*
3995 * types for expressions.
3996 */
3997typedef enum
3998{
3999 TYPE_UNKNOWN = 0
4000 , TYPE_EQUAL /* == */
4001 , TYPE_NEQUAL /* != */
4002 , TYPE_GREATER /* > */
4003 , TYPE_GEQUAL /* >= */
4004 , TYPE_SMALLER /* < */
4005 , TYPE_SEQUAL /* <= */
4006 , TYPE_MATCH /* =~ */
4007 , TYPE_NOMATCH /* !~ */
4008} exptype_T;
4009
4010/*
4011 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4014 */
4015
4016/*
4017 * Handle zero level expression.
4018 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004019 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004020 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * Return OK or FAIL.
4022 */
4023 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 char_u **nextcmd;
4028 int evaluate;
4029{
4030 int ret;
4031 char_u *p;
4032
4033 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (ret == FAIL || !ends_excmd(*p))
4036 {
4037 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 /*
4040 * Report the invalid expression unless the expression evaluation has
4041 * been cancelled due to an aborting error, an interrupt, or an
4042 * exception.
4043 */
4044 if (!aborting())
4045 EMSG2(_(e_invexpr2), arg);
4046 ret = FAIL;
4047 }
4048 if (nextcmd != NULL)
4049 *nextcmd = check_nextcmd(p);
4050
4051 return ret;
4052}
4053
4054/*
4055 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004056 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 *
4058 * "arg" must point to the first non-white of the expression.
4059 * "arg" is advanced to the next non-white after the recognized expression.
4060 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004061 * Note: "rettv.v_lock" is not set.
4062 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 * Return OK or FAIL.
4064 */
4065 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004066eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 int evaluate;
4070{
4071 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
4074 /*
4075 * Get the first variable.
4076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
4079
4080 if ((*arg)[0] == '?')
4081 {
4082 result = FALSE;
4083 if (evaluate)
4084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 int error = FALSE;
4086
4087 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090 if (error)
4091 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093
4094 /*
4095 * Get the second variable.
4096 */
4097 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100
4101 /*
4102 * Check for the ":".
4103 */
4104 if ((*arg)[0] != ':')
4105 {
4106 EMSG(_("E109: Missing ':' after '?'"));
4107 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 return FAIL;
4110 }
4111
4112 /*
4113 * Get the third variable.
4114 */
4115 *arg = skipwhite(*arg + 1);
4116 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4117 {
4118 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 return FAIL;
4121 }
4122 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125
4126 return OK;
4127}
4128
4129/*
4130 * Handle first level expression:
4131 * expr2 || expr2 || expr2 logical OR
4132 *
4133 * "arg" must point to the first non-white of the expression.
4134 * "arg" is advanced to the next non-white after the recognized expression.
4135 *
4136 * Return OK or FAIL.
4137 */
4138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int evaluate;
4143{
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 long result;
4146 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
4150 * Get the first variable.
4151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 return FAIL;
4154
4155 /*
4156 * Repeat until there is no following "||".
4157 */
4158 first = TRUE;
4159 result = FALSE;
4160 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4161 {
4162 if (evaluate && first)
4163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (error)
4168 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 first = FALSE;
4170 }
4171
4172 /*
4173 * Get the second variable.
4174 */
4175 *arg = skipwhite(*arg + 2);
4176 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4177 return FAIL;
4178
4179 /*
4180 * Compute the result.
4181 */
4182 if (evaluate && !result)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 if (evaluate)
4191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 rettv->v_type = VAR_NUMBER;
4193 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 }
4196
4197 return OK;
4198}
4199
4200/*
4201 * Handle second level expression:
4202 * expr3 && expr3 && expr3 logical AND
4203 *
4204 * "arg" must point to the first non-white of the expression.
4205 * "arg" is advanced to the next non-white after the recognized expression.
4206 *
4207 * Return OK or FAIL.
4208 */
4209 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 int evaluate;
4214{
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 long result;
4217 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220 /*
4221 * Get the first variable.
4222 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 return FAIL;
4225
4226 /*
4227 * Repeat until there is no following "&&".
4228 */
4229 first = TRUE;
4230 result = TRUE;
4231 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4232 {
4233 if (evaluate && first)
4234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004235 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (error)
4239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 first = FALSE;
4241 }
4242
4243 /*
4244 * Get the second variable.
4245 */
4246 *arg = skipwhite(*arg + 2);
4247 if (eval4(arg, &var2, evaluate && result) == FAIL)
4248 return FAIL;
4249
4250 /*
4251 * Compute the result.
4252 */
4253 if (evaluate && result)
4254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (error)
4259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 if (evaluate)
4262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 rettv->v_type = VAR_NUMBER;
4264 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 }
4267
4268 return OK;
4269}
4270
4271/*
4272 * Handle third level expression:
4273 * var1 == var2
4274 * var1 =~ var2
4275 * var1 != var2
4276 * var1 !~ var2
4277 * var1 > var2
4278 * var1 >= var2
4279 * var1 < var2
4280 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004281 * var1 is var2
4282 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 *
4284 * "arg" must point to the first non-white of the expression.
4285 * "arg" is advanced to the next non-white after the recognized expression.
4286 *
4287 * Return OK or FAIL.
4288 */
4289 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 int evaluate;
4294{
Bram Moolenaar33570922005-01-25 22:26:29 +00004295 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 char_u *p;
4297 int i;
4298 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004299 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 int len = 2;
4301 long n1, n2;
4302 char_u *s1, *s2;
4303 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4304 regmatch_T regmatch;
4305 int ic;
4306 char_u *save_cpo;
4307
4308 /*
4309 * Get the first variable.
4310 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313
4314 p = *arg;
4315 switch (p[0])
4316 {
4317 case '=': if (p[1] == '=')
4318 type = TYPE_EQUAL;
4319 else if (p[1] == '~')
4320 type = TYPE_MATCH;
4321 break;
4322 case '!': if (p[1] == '=')
4323 type = TYPE_NEQUAL;
4324 else if (p[1] == '~')
4325 type = TYPE_NOMATCH;
4326 break;
4327 case '>': if (p[1] != '=')
4328 {
4329 type = TYPE_GREATER;
4330 len = 1;
4331 }
4332 else
4333 type = TYPE_GEQUAL;
4334 break;
4335 case '<': if (p[1] != '=')
4336 {
4337 type = TYPE_SMALLER;
4338 len = 1;
4339 }
4340 else
4341 type = TYPE_SEQUAL;
4342 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004343 case 'i': if (p[1] == 's')
4344 {
4345 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4346 len = 5;
4347 if (!vim_isIDc(p[len]))
4348 {
4349 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4350 type_is = TRUE;
4351 }
4352 }
4353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355
4356 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004357 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 */
4359 if (type != TYPE_UNKNOWN)
4360 {
4361 /* extra question mark appended: ignore case */
4362 if (p[len] == '?')
4363 {
4364 ic = TRUE;
4365 ++len;
4366 }
4367 /* extra '#' appended: match case */
4368 else if (p[len] == '#')
4369 {
4370 ic = FALSE;
4371 ++len;
4372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004373 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 else
4375 ic = p_ic;
4376
4377 /*
4378 * Get the second variable.
4379 */
4380 *arg = skipwhite(p + len);
4381 if (eval5(arg, &var2, evaluate) == FAIL)
4382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004383 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 return FAIL;
4385 }
4386
4387 if (evaluate)
4388 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 if (type_is && rettv->v_type != var2.v_type)
4390 {
4391 /* For "is" a different type always means FALSE, for "notis"
4392 * it means TRUE. */
4393 n1 = (type == TYPE_NEQUAL);
4394 }
4395 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4396 {
4397 if (type_is)
4398 {
4399 n1 = (rettv->v_type == var2.v_type
4400 && rettv->vval.v_list == var2.vval.v_list);
4401 if (type == TYPE_NEQUAL)
4402 n1 = !n1;
4403 }
4404 else if (rettv->v_type != var2.v_type
4405 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4406 {
4407 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004408 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004410 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004411 clear_tv(rettv);
4412 clear_tv(&var2);
4413 return FAIL;
4414 }
4415 else
4416 {
4417 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004418 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4419 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004420 if (type == TYPE_NEQUAL)
4421 n1 = !n1;
4422 }
4423 }
4424
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004425 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4426 {
4427 if (type_is)
4428 {
4429 n1 = (rettv->v_type == var2.v_type
4430 && rettv->vval.v_dict == var2.vval.v_dict);
4431 if (type == TYPE_NEQUAL)
4432 n1 = !n1;
4433 }
4434 else if (rettv->v_type != var2.v_type
4435 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4436 {
4437 if (rettv->v_type != var2.v_type)
4438 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4439 else
4440 EMSG(_("E736: Invalid operation for Dictionary"));
4441 clear_tv(rettv);
4442 clear_tv(&var2);
4443 return FAIL;
4444 }
4445 else
4446 {
4447 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004448 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4449 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004450 if (type == TYPE_NEQUAL)
4451 n1 = !n1;
4452 }
4453 }
4454
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4456 {
4457 if (rettv->v_type != var2.v_type
4458 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4459 {
4460 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004461 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004463 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 clear_tv(rettv);
4465 clear_tv(&var2);
4466 return FAIL;
4467 }
4468 else
4469 {
4470 /* Compare two Funcrefs for being equal or unequal. */
4471 if (rettv->vval.v_string == NULL
4472 || var2.vval.v_string == NULL)
4473 n1 = FALSE;
4474 else
4475 n1 = STRCMP(rettv->vval.v_string,
4476 var2.vval.v_string) == 0;
4477 if (type == TYPE_NEQUAL)
4478 n1 = !n1;
4479 }
4480 }
4481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004482#ifdef FEAT_FLOAT
4483 /*
4484 * If one of the two variables is a float, compare as a float.
4485 * When using "=~" or "!~", always compare as string.
4486 */
4487 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4488 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4489 {
4490 float_T f1, f2;
4491
4492 if (rettv->v_type == VAR_FLOAT)
4493 f1 = rettv->vval.v_float;
4494 else
4495 f1 = get_tv_number(rettv);
4496 if (var2.v_type == VAR_FLOAT)
4497 f2 = var2.vval.v_float;
4498 else
4499 f2 = get_tv_number(&var2);
4500 n1 = FALSE;
4501 switch (type)
4502 {
4503 case TYPE_EQUAL: n1 = (f1 == f2); break;
4504 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4505 case TYPE_GREATER: n1 = (f1 > f2); break;
4506 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4507 case TYPE_SMALLER: n1 = (f1 < f2); break;
4508 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4509 case TYPE_UNKNOWN:
4510 case TYPE_MATCH:
4511 case TYPE_NOMATCH: break; /* avoid gcc warning */
4512 }
4513 }
4514#endif
4515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 /*
4517 * If one of the two variables is a number, compare as a number.
4518 * When using "=~" or "!~", always compare as string.
4519 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004520 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 n1 = get_tv_number(rettv);
4524 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (n1 == n2); break;
4528 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4529 case TYPE_GREATER: n1 = (n1 > n2); break;
4530 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4531 case TYPE_SMALLER: n1 = (n1 < n2); break;
4532 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4533 case TYPE_UNKNOWN:
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH: break; /* avoid gcc warning */
4536 }
4537 }
4538 else
4539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540 s1 = get_tv_string_buf(rettv, buf1);
4541 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4543 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4544 else
4545 i = 0;
4546 n1 = FALSE;
4547 switch (type)
4548 {
4549 case TYPE_EQUAL: n1 = (i == 0); break;
4550 case TYPE_NEQUAL: n1 = (i != 0); break;
4551 case TYPE_GREATER: n1 = (i > 0); break;
4552 case TYPE_GEQUAL: n1 = (i >= 0); break;
4553 case TYPE_SMALLER: n1 = (i < 0); break;
4554 case TYPE_SEQUAL: n1 = (i <= 0); break;
4555
4556 case TYPE_MATCH:
4557 case TYPE_NOMATCH:
4558 /* avoid 'l' flag in 'cpoptions' */
4559 save_cpo = p_cpo;
4560 p_cpo = (char_u *)"";
4561 regmatch.regprog = vim_regcomp(s2,
4562 RE_MAGIC + RE_STRING);
4563 regmatch.rm_ic = ic;
4564 if (regmatch.regprog != NULL)
4565 {
4566 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4567 vim_free(regmatch.regprog);
4568 if (type == TYPE_NOMATCH)
4569 n1 = !n1;
4570 }
4571 p_cpo = save_cpo;
4572 break;
4573
4574 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4575 }
4576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004577 clear_tv(rettv);
4578 clear_tv(&var2);
4579 rettv->v_type = VAR_NUMBER;
4580 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 }
4583
4584 return OK;
4585}
4586
4587/*
4588 * Handle fourth level expression:
4589 * + number addition
4590 * - number subtraction
4591 * . string concatenation
4592 *
4593 * "arg" must point to the first non-white of the expression.
4594 * "arg" is advanced to the next non-white after the recognized expression.
4595 *
4596 * Return OK or FAIL.
4597 */
4598 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 int evaluate;
4603{
Bram Moolenaar33570922005-01-25 22:26:29 +00004604 typval_T var2;
4605 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 int op;
4607 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004608#ifdef FEAT_FLOAT
4609 float_T f1 = 0, f2 = 0;
4610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 char_u *s1, *s2;
4612 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4613 char_u *p;
4614
4615 /*
4616 * Get the first variable.
4617 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004618 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 return FAIL;
4620
4621 /*
4622 * Repeat computing, until no '+', '-' or '.' is following.
4623 */
4624 for (;;)
4625 {
4626 op = **arg;
4627 if (op != '+' && op != '-' && op != '.')
4628 break;
4629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630 if ((op != '+' || rettv->v_type != VAR_LIST)
4631#ifdef FEAT_FLOAT
4632 && (op == '.' || rettv->v_type != VAR_FLOAT)
4633#endif
4634 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004635 {
4636 /* For "list + ...", an illegal use of the first operand as
4637 * a number cannot be determined before evaluating the 2nd
4638 * operand: if this is also a list, all is ok.
4639 * For "something . ...", "something - ..." or "non-list + ...",
4640 * we know that the first operand needs to be a string or number
4641 * without evaluating the 2nd operand. So check before to avoid
4642 * side effects after an error. */
4643 if (evaluate && get_tv_string_chk(rettv) == NULL)
4644 {
4645 clear_tv(rettv);
4646 return FAIL;
4647 }
4648 }
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 /*
4651 * Get the second variable.
4652 */
4653 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004654 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 return FAIL;
4658 }
4659
4660 if (evaluate)
4661 {
4662 /*
4663 * Compute the result.
4664 */
4665 if (op == '.')
4666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004667 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4668 s2 = get_tv_string_buf_chk(&var2, buf2);
4669 if (s2 == NULL) /* type error ? */
4670 {
4671 clear_tv(rettv);
4672 clear_tv(&var2);
4673 return FAIL;
4674 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004675 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 clear_tv(rettv);
4677 rettv->v_type = VAR_STRING;
4678 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004680 else if (op == '+' && rettv->v_type == VAR_LIST
4681 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004682 {
4683 /* concatenate Lists */
4684 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4685 &var3) == FAIL)
4686 {
4687 clear_tv(rettv);
4688 clear_tv(&var2);
4689 return FAIL;
4690 }
4691 clear_tv(rettv);
4692 *rettv = var3;
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 else
4695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 int error = FALSE;
4697
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004698#ifdef FEAT_FLOAT
4699 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004700 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701 f1 = rettv->vval.v_float;
4702 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 else
4705#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 n1 = get_tv_number_chk(rettv, &error);
4708 if (error)
4709 {
4710 /* This can only happen for "list + non-list". For
4711 * "non-list + ..." or "something - ...", we returned
4712 * before evaluating the 2nd operand. */
4713 clear_tv(rettv);
4714 return FAIL;
4715 }
4716#ifdef FEAT_FLOAT
4717 if (var2.v_type == VAR_FLOAT)
4718 f1 = n1;
4719#endif
4720 }
4721#ifdef FEAT_FLOAT
4722 if (var2.v_type == VAR_FLOAT)
4723 {
4724 f2 = var2.vval.v_float;
4725 n2 = 0;
4726 }
4727 else
4728#endif
4729 {
4730 n2 = get_tv_number_chk(&var2, &error);
4731 if (error)
4732 {
4733 clear_tv(rettv);
4734 clear_tv(&var2);
4735 return FAIL;
4736 }
4737#ifdef FEAT_FLOAT
4738 if (rettv->v_type == VAR_FLOAT)
4739 f2 = n2;
4740#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743
4744#ifdef FEAT_FLOAT
4745 /* If there is a float on either side the result is a float. */
4746 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4747 {
4748 if (op == '+')
4749 f1 = f1 + f2;
4750 else
4751 f1 = f1 - f2;
4752 rettv->v_type = VAR_FLOAT;
4753 rettv->vval.v_float = f1;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756#endif
4757 {
4758 if (op == '+')
4759 n1 = n1 + n2;
4760 else
4761 n1 = n1 - n2;
4762 rettv->v_type = VAR_NUMBER;
4763 rettv->vval.v_number = n1;
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 }
4769 return OK;
4770}
4771
4772/*
4773 * Handle fifth level expression:
4774 * * number multiplication
4775 * / number division
4776 * % number modulo
4777 *
4778 * "arg" must point to the first non-white of the expression.
4779 * "arg" is advanced to the next non-white after the recognized expression.
4780 *
4781 * Return OK or FAIL.
4782 */
4783 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004784eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004788 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
Bram Moolenaar33570922005-01-25 22:26:29 +00004790 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 int op;
4792 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793#ifdef FEAT_FLOAT
4794 int use_float = FALSE;
4795 float_T f1 = 0, f2;
4796#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 /*
4800 * Get the first variable.
4801 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004802 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 return FAIL;
4804
4805 /*
4806 * Repeat computing, until no '*', '/' or '%' is following.
4807 */
4808 for (;;)
4809 {
4810 op = **arg;
4811 if (op != '*' && op != '/' && op != '%')
4812 break;
4813
4814 if (evaluate)
4815 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004816#ifdef FEAT_FLOAT
4817 if (rettv->v_type == VAR_FLOAT)
4818 {
4819 f1 = rettv->vval.v_float;
4820 use_float = TRUE;
4821 n1 = 0;
4822 }
4823 else
4824#endif
4825 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004826 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 if (error)
4828 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 else
4831 n1 = 0;
4832
4833 /*
4834 * Get the second variable.
4835 */
4836 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004837 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 return FAIL;
4839
4840 if (evaluate)
4841 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842#ifdef FEAT_FLOAT
4843 if (var2.v_type == VAR_FLOAT)
4844 {
4845 if (!use_float)
4846 {
4847 f1 = n1;
4848 use_float = TRUE;
4849 }
4850 f2 = var2.vval.v_float;
4851 n2 = 0;
4852 }
4853 else
4854#endif
4855 {
4856 n2 = get_tv_number_chk(&var2, &error);
4857 clear_tv(&var2);
4858 if (error)
4859 return FAIL;
4860#ifdef FEAT_FLOAT
4861 if (use_float)
4862 f2 = n2;
4863#endif
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866 /*
4867 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870#ifdef FEAT_FLOAT
4871 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873 if (op == '*')
4874 f1 = f1 * f2;
4875 else if (op == '/')
4876 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004877# ifdef VMS
4878 /* VMS crashes on divide by zero, work around it */
4879 if (f2 == 0.0)
4880 {
4881 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004882 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004884 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004885 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004886 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887 }
4888 else
4889 f1 = f1 / f2;
4890# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891 /* We rely on the floating point library to handle divide
4892 * by zero to result in "inf" and not a crash. */
4893 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004894# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004898 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 return FAIL;
4900 }
4901 rettv->v_type = VAR_FLOAT;
4902 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 if (op == '*')
4908 n1 = n1 * n2;
4909 else if (op == '/')
4910 {
4911 if (n2 == 0) /* give an error message? */
4912 {
4913 if (n1 == 0)
4914 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4915 else if (n1 < 0)
4916 n1 = -0x7fffffffL;
4917 else
4918 n1 = 0x7fffffffL;
4919 }
4920 else
4921 n1 = n1 / n2;
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 {
4925 if (n2 == 0) /* give an error message? */
4926 n1 = 0;
4927 else
4928 n1 = n1 % n2;
4929 }
4930 rettv->v_type = VAR_NUMBER;
4931 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 }
4935
4936 return OK;
4937}
4938
4939/*
4940 * Handle sixth level expression:
4941 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004942 * "string" string constant
4943 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 * &option-name option value
4945 * @r register contents
4946 * identifier variable value
4947 * function() function call
4948 * $VAR environment variable
4949 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004950 * [expr, expr] List
4951 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 *
4953 * Also handle:
4954 * ! in front logical NOT
4955 * - in front unary minus
4956 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004957 * trailing [] subscript in String or List
4958 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 *
4960 * "arg" must point to the first non-white of the expression.
4961 * "arg" is advanced to the next non-white after the recognized expression.
4962 *
4963 * Return OK or FAIL.
4964 */
4965 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004966eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004970 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 long n;
4973 int len;
4974 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 char_u *start_leader, *end_leader;
4976 int ret = OK;
4977 char_u *alias;
4978
4979 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985 /*
4986 * Skip '!' and '-' characters. They are handled later.
4987 */
4988 start_leader = *arg;
4989 while (**arg == '!' || **arg == '-' || **arg == '+')
4990 *arg = skipwhite(*arg + 1);
4991 end_leader = *arg;
4992
4993 switch (**arg)
4994 {
4995 /*
4996 * Number constant.
4997 */
4998 case '0':
4999 case '1':
5000 case '2':
5001 case '3':
5002 case '4':
5003 case '5':
5004 case '6':
5005 case '7':
5006 case '8':
5007 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 {
5009#ifdef FEAT_FLOAT
5010 char_u *p = skipdigits(*arg + 1);
5011 int get_float = FALSE;
5012
5013 /* We accept a float when the format matches
5014 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005015 * strict to avoid backwards compatibility problems.
5016 * Don't look for a float after the "." operator, so that
5017 * ":let vers = 1.2.3" doesn't fail. */
5018 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 get_float = TRUE;
5021 p = skipdigits(p + 2);
5022 if (*p == 'e' || *p == 'E')
5023 {
5024 ++p;
5025 if (*p == '-' || *p == '+')
5026 ++p;
5027 if (!vim_isdigit(*p))
5028 get_float = FALSE;
5029 else
5030 p = skipdigits(p + 1);
5031 }
5032 if (ASCII_ISALPHA(*p) || *p == '.')
5033 get_float = FALSE;
5034 }
5035 if (get_float)
5036 {
5037 float_T f;
5038
5039 *arg += string2float(*arg, &f);
5040 if (evaluate)
5041 {
5042 rettv->v_type = VAR_FLOAT;
5043 rettv->vval.v_float = f;
5044 }
5045 }
5046 else
5047#endif
5048 {
5049 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5050 *arg += len;
5051 if (evaluate)
5052 {
5053 rettv->v_type = VAR_NUMBER;
5054 rettv->vval.v_number = n;
5055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 /*
5061 * String constant: "string".
5062 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 break;
5065
5066 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070 break;
5071
5072 /*
5073 * List: [expr, expr]
5074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077
5078 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 * Dictionary: {key: val, key: val}
5080 */
5081 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5082 break;
5083
5084 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005085 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005087 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089
5090 /*
5091 * Environment variable: $VAR.
5092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 break;
5095
5096 /*
5097 * Register contents: @r.
5098 */
5099 case '@': ++*arg;
5100 if (evaluate)
5101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005103 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 if (**arg != NUL)
5106 ++*arg;
5107 break;
5108
5109 /*
5110 * nested expression: (expression).
5111 */
5112 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005113 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (**arg == ')')
5115 ++*arg;
5116 else if (ret == OK)
5117 {
5118 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 ret = FAIL;
5121 }
5122 break;
5123
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 break;
5126 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127
5128 if (ret == NOTDONE)
5129 {
5130 /*
5131 * Must be a variable or function name.
5132 * Can also be a curly-braces kind of name: {expr}.
5133 */
5134 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005135 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 if (alias != NULL)
5137 s = alias;
5138
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005139 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 ret = FAIL;
5141 else
5142 {
5143 if (**arg == '(') /* recursive! */
5144 {
5145 /* If "s" is the name of a variable of type VAR_FUNC
5146 * use its contents. */
5147 s = deref_func_name(s, &len);
5148
5149 /* Invoke the function. */
5150 ret = get_func_tv(s, len, rettv, arg,
5151 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005152 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005153
5154 /* If evaluate is FALSE rettv->v_type was not set in
5155 * get_func_tv, but it's needed in handle_subscript() to parse
5156 * what follows. So set it here. */
5157 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5158 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005159 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005160 rettv->v_type = VAR_FUNC;
5161 }
5162
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 /* Stop the expression evaluation when immediately
5164 * aborting on error, or when an interrupt occurred or
5165 * an exception was thrown but not caught. */
5166 if (aborting())
5167 {
5168 if (ret == OK)
5169 clear_tv(rettv);
5170 ret = FAIL;
5171 }
5172 }
5173 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005174 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005175 else
5176 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005178 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 }
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 *arg = skipwhite(*arg);
5182
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005183 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5184 * expr(expr). */
5185 if (ret == OK)
5186 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188 /*
5189 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5190 */
5191 if (ret == OK && evaluate && end_leader > start_leader)
5192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005194 int val = 0;
5195#ifdef FEAT_FLOAT
5196 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 if (rettv->v_type == VAR_FLOAT)
5199 f = rettv->vval.v_float;
5200 else
5201#endif
5202 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 clear_tv(rettv);
5206 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 else
5209 {
5210 while (end_leader > start_leader)
5211 {
5212 --end_leader;
5213 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005214 {
5215#ifdef FEAT_FLOAT
5216 if (rettv->v_type == VAR_FLOAT)
5217 f = !f;
5218 else
5219#endif
5220 val = !val;
5221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 {
5224#ifdef FEAT_FLOAT
5225 if (rettv->v_type == VAR_FLOAT)
5226 f = -f;
5227 else
5228#endif
5229 val = -val;
5230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005232#ifdef FEAT_FLOAT
5233 if (rettv->v_type == VAR_FLOAT)
5234 {
5235 clear_tv(rettv);
5236 rettv->vval.v_float = f;
5237 }
5238 else
5239#endif
5240 {
5241 clear_tv(rettv);
5242 rettv->v_type = VAR_NUMBER;
5243 rettv->vval.v_number = val;
5244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247
5248 return ret;
5249}
5250
5251/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005252 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5253 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5255 */
5256 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262{
5263 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005264 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 long len = -1;
5267 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005271 if (rettv->v_type == VAR_FUNC
5272#ifdef FEAT_FLOAT
5273 || rettv->v_type == VAR_FLOAT
5274#endif
5275 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 if (verbose)
5278 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 return FAIL;
5280 }
5281
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 /*
5285 * dict.name
5286 */
5287 key = *arg + 1;
5288 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5289 ;
5290 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 }
5294 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 /*
5297 * something[idx]
5298 *
5299 * Get the (first) variable from inside the [].
5300 */
5301 *arg = skipwhite(*arg + 1);
5302 if (**arg == ':')
5303 empty1 = TRUE;
5304 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5305 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5307 {
5308 /* not a number or string */
5309 clear_tv(&var1);
5310 return FAIL;
5311 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312
5313 /*
5314 * Get the second variable from inside the [:].
5315 */
5316 if (**arg == ':')
5317 {
5318 range = TRUE;
5319 *arg = skipwhite(*arg + 1);
5320 if (**arg == ']')
5321 empty2 = TRUE;
5322 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 if (!empty1)
5325 clear_tv(&var1);
5326 return FAIL;
5327 }
5328 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5329 {
5330 /* not a number or string */
5331 if (!empty1)
5332 clear_tv(&var1);
5333 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005334 return FAIL;
5335 }
5336 }
5337
5338 /* Check for the ']'. */
5339 if (**arg != ']')
5340 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005341 if (verbose)
5342 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 clear_tv(&var1);
5344 if (range)
5345 clear_tv(&var2);
5346 return FAIL;
5347 }
5348 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 }
5350
5351 if (evaluate)
5352 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 n1 = 0;
5354 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 n1 = get_tv_number(&var1);
5357 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 }
5359 if (range)
5360 {
5361 if (empty2)
5362 n2 = -1;
5363 else
5364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365 n2 = get_tv_number(&var2);
5366 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 }
5368 }
5369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 {
5372 case VAR_NUMBER:
5373 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 len = (long)STRLEN(s);
5376 if (range)
5377 {
5378 /* The resulting variable is a substring. If the indexes
5379 * are out of range the result is empty. */
5380 if (n1 < 0)
5381 {
5382 n1 = len + n1;
5383 if (n1 < 0)
5384 n1 = 0;
5385 }
5386 if (n2 < 0)
5387 n2 = len + n2;
5388 else if (n2 >= len)
5389 n2 = len;
5390 if (n1 >= len || n2 < 0 || n1 > n2)
5391 s = NULL;
5392 else
5393 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5394 }
5395 else
5396 {
5397 /* The resulting variable is a string of a single
5398 * character. If the index is too big or negative the
5399 * result is empty. */
5400 if (n1 >= len || n1 < 0)
5401 s = NULL;
5402 else
5403 s = vim_strnsave(s + n1, 1);
5404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005405 clear_tv(rettv);
5406 rettv->v_type = VAR_STRING;
5407 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 break;
5409
5410 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 if (n1 < 0)
5413 n1 = len + n1;
5414 if (!empty1 && (n1 < 0 || n1 >= len))
5415 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005416 /* For a range we allow invalid values and return an empty
5417 * list. A list index out of range is an error. */
5418 if (!range)
5419 {
5420 if (verbose)
5421 EMSGN(_(e_listidx), n1);
5422 return FAIL;
5423 }
5424 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 }
5426 if (range)
5427 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 list_T *l;
5429 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430
5431 if (n2 < 0)
5432 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005433 else if (n2 >= len)
5434 n2 = len - 1;
5435 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005436 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 l = list_alloc();
5438 if (l == NULL)
5439 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 n1 <= n2; ++n1)
5442 {
5443 if (list_append_tv(l, &item->li_tv) == FAIL)
5444 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005445 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 return FAIL;
5447 }
5448 item = item->li_next;
5449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005450 clear_tv(rettv);
5451 rettv->v_type = VAR_LIST;
5452 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005453 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 else
5456 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005457 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 clear_tv(rettv);
5459 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462
5463 case VAR_DICT:
5464 if (range)
5465 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005466 if (verbose)
5467 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 if (len == -1)
5469 clear_tv(&var1);
5470 return FAIL;
5471 }
5472 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474
5475 if (len == -1)
5476 {
5477 key = get_tv_string(&var1);
5478 if (*key == NUL)
5479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005480 if (verbose)
5481 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 clear_tv(&var1);
5483 return FAIL;
5484 }
5485 }
5486
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005487 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005489 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005490 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 if (len == -1)
5492 clear_tv(&var1);
5493 if (item == NULL)
5494 return FAIL;
5495
5496 copy_tv(&item->di_tv, &var1);
5497 clear_tv(rettv);
5498 *rettv = var1;
5499 }
5500 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 }
5502 }
5503
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 return OK;
5505}
5506
5507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * Get an option value.
5509 * "arg" points to the '&' or '+' before the option name.
5510 * "arg" is advanced to character after the option name.
5511 * Return OK or FAIL.
5512 */
5513 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005516 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 int evaluate;
5518{
5519 char_u *option_end;
5520 long numval;
5521 char_u *stringval;
5522 int opt_type;
5523 int c;
5524 int working = (**arg == '+'); /* has("+option") */
5525 int ret = OK;
5526 int opt_flags;
5527
5528 /*
5529 * Isolate the option name and find its value.
5530 */
5531 option_end = find_option_end(arg, &opt_flags);
5532 if (option_end == NULL)
5533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 EMSG2(_("E112: Option name missing: %s"), *arg);
5536 return FAIL;
5537 }
5538
5539 if (!evaluate)
5540 {
5541 *arg = option_end;
5542 return OK;
5543 }
5544
5545 c = *option_end;
5546 *option_end = NUL;
5547 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 if (opt_type == -3) /* invalid name */
5551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 EMSG2(_("E113: Unknown option: %s"), *arg);
5554 ret = FAIL;
5555 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 if (opt_type == -2) /* hidden string option */
5559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 rettv->v_type = VAR_STRING;
5561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 else if (opt_type == -1) /* hidden number option */
5564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 rettv->v_type = VAR_NUMBER;
5566 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 else if (opt_type == 1) /* number option */
5569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 rettv->v_type = VAR_NUMBER;
5571 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573 else /* string option */
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 rettv->v_type = VAR_STRING;
5576 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578 }
5579 else if (working && (opt_type == -2 || opt_type == -1))
5580 ret = FAIL;
5581
5582 *option_end = c; /* put back for error messages */
5583 *arg = option_end;
5584
5585 return ret;
5586}
5587
5588/*
5589 * Allocate a variable for a string constant.
5590 * Return OK or FAIL.
5591 */
5592 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int evaluate;
5597{
5598 char_u *p;
5599 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 int extra = 0;
5601
5602 /*
5603 * Find the end of the string, skipping backslashed characters.
5604 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 if (*p == '\\' && p[1] != NUL)
5608 {
5609 ++p;
5610 /* A "\<x>" form occupies at least 4 characters, and produces up
5611 * to 6 characters: reserve space for 2 extra */
5612 if (*p == '<')
5613 extra += 2;
5614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
5616
5617 if (*p != '"')
5618 {
5619 EMSG2(_("E114: Missing quote: %s"), *arg);
5620 return FAIL;
5621 }
5622
5623 /* If only parsing, set *arg and return here */
5624 if (!evaluate)
5625 {
5626 *arg = p + 1;
5627 return OK;
5628 }
5629
5630 /*
5631 * Copy the string into allocated memory, handling backslashed
5632 * characters.
5633 */
5634 name = alloc((unsigned)(p - *arg + extra));
5635 if (name == NULL)
5636 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 rettv->v_type = VAR_STRING;
5638 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 if (*p == '\\')
5643 {
5644 switch (*++p)
5645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 case 'b': *name++ = BS; ++p; break;
5647 case 'e': *name++ = ESC; ++p; break;
5648 case 'f': *name++ = FF; ++p; break;
5649 case 'n': *name++ = NL; ++p; break;
5650 case 'r': *name++ = CAR; ++p; break;
5651 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
5653 case 'X': /* hex: "\x1", "\x12" */
5654 case 'x':
5655 case 'u': /* Unicode: "\u0023" */
5656 case 'U':
5657 if (vim_isxdigit(p[1]))
5658 {
5659 int n, nr;
5660 int c = toupper(*p);
5661
5662 if (c == 'X')
5663 n = 2;
5664 else
5665 n = 4;
5666 nr = 0;
5667 while (--n >= 0 && vim_isxdigit(p[1]))
5668 {
5669 ++p;
5670 nr = (nr << 4) + hex2nr(*p);
5671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673#ifdef FEAT_MBYTE
5674 /* For "\u" store the number according to
5675 * 'encoding'. */
5676 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
5679#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683
5684 /* octal: "\1", "\12", "\123" */
5685 case '0':
5686 case '1':
5687 case '2':
5688 case '3':
5689 case '4':
5690 case '5':
5691 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 case '7': *name = *p++ - '0';
5693 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 *name = (*name << 3) + *p++ - '0';
5696 if (*p >= '0' && *p <= '7')
5697 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 break;
5701
5702 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (extra != 0)
5705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708 }
5709 /* FALLTHROUGH */
5710
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 break;
5713 }
5714 }
5715 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 *arg = p + 1;
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 return OK;
5723}
5724
5725/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 * Return OK or FAIL.
5728 */
5729 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 int evaluate;
5734{
5735 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 int reduce = 0;
5738
5739 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 */
5742 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 break;
5748 ++reduce;
5749 ++p;
5750 }
5751 }
5752
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 return FAIL;
5757 }
5758
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 if (!evaluate)
5761 {
5762 *arg = p + 1;
5763 return OK;
5764 }
5765
5766 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 */
5769 str = alloc((unsigned)((p - *arg) - reduce));
5770 if (str == NULL)
5771 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 rettv->v_type = VAR_STRING;
5773 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 break;
5781 ++p;
5782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 *arg = p + 1;
5787
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 return OK;
5789}
5790
5791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792 * Allocate a variable for a List and fill it from "*arg".
5793 * Return OK or FAIL.
5794 */
5795 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005796get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005798 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 int evaluate;
5800{
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 list_T *l = NULL;
5802 typval_T tv;
5803 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804
5805 if (evaluate)
5806 {
5807 l = list_alloc();
5808 if (l == NULL)
5809 return FAIL;
5810 }
5811
5812 *arg = skipwhite(*arg + 1);
5813 while (**arg != ']' && **arg != NUL)
5814 {
5815 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5816 goto failret;
5817 if (evaluate)
5818 {
5819 item = listitem_alloc();
5820 if (item != NULL)
5821 {
5822 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005823 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 list_append(l, item);
5825 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005826 else
5827 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 }
5829
5830 if (**arg == ']')
5831 break;
5832 if (**arg != ',')
5833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 goto failret;
5836 }
5837 *arg = skipwhite(*arg + 1);
5838 }
5839
5840 if (**arg != ']')
5841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843failret:
5844 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005845 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 return FAIL;
5847 }
5848
5849 *arg = skipwhite(*arg + 1);
5850 if (evaluate)
5851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005852 rettv->v_type = VAR_LIST;
5853 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 ++l->lv_refcount;
5855 }
5856
5857 return OK;
5858}
5859
5860/*
5861 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005862 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005864 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865list_alloc()
5866{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005867 list_T *l;
5868
5869 l = (list_T *)alloc_clear(sizeof(list_T));
5870 if (l != NULL)
5871 {
5872 /* Prepend the list to the list of lists for garbage collection. */
5873 if (first_list != NULL)
5874 first_list->lv_used_prev = l;
5875 l->lv_used_prev = NULL;
5876 l->lv_used_next = first_list;
5877 first_list = l;
5878 }
5879 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880}
5881
5882/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005883 * Allocate an empty list for a return value.
5884 * Returns OK or FAIL.
5885 */
5886 static int
5887rettv_list_alloc(rettv)
5888 typval_T *rettv;
5889{
5890 list_T *l = list_alloc();
5891
5892 if (l == NULL)
5893 return FAIL;
5894
5895 rettv->vval.v_list = l;
5896 rettv->v_type = VAR_LIST;
5897 ++l->lv_refcount;
5898 return OK;
5899}
5900
5901/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 * Unreference a list: decrement the reference count and free it when it
5903 * becomes zero.
5904 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005905 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005909 if (l != NULL && --l->lv_refcount <= 0)
5910 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911}
5912
5913/*
5914 * Free a list, including all items it points to.
5915 * Ignores the reference count.
5916 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005917 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005918list_free(l, recurse)
5919 list_T *l;
5920 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921{
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005924 /* Remove the list from the list of lists for garbage collection. */
5925 if (l->lv_used_prev == NULL)
5926 first_list = l->lv_used_next;
5927 else
5928 l->lv_used_prev->lv_used_next = l->lv_used_next;
5929 if (l->lv_used_next != NULL)
5930 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5931
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934 /* Remove the item before deleting it. */
5935 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005936 if (recurse || (item->li_tv.v_type != VAR_LIST
5937 && item->li_tv.v_type != VAR_DICT))
5938 clear_tv(&item->li_tv);
5939 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
5941 vim_free(l);
5942}
5943
5944/*
5945 * Allocate a list item.
5946 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005947 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948listitem_alloc()
5949{
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951}
5952
5953/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005954 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005956 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 vim_free(item);
5962}
5963
5964/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005965 * Remove a list item from a List and free it. Also clears the value.
5966 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005967 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005968listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 list_T *l;
5970 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005971{
5972 list_remove(l, item, item);
5973 listitem_free(item);
5974}
5975
5976/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 * Get the number of items in a list.
5978 */
5979 static long
5980list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 if (l == NULL)
5984 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005985 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 * Return TRUE when two lists have exactly the same values.
5990 */
5991 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005992list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 list_T *l1;
5994 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005996 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997{
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006000 if (l1 == NULL || l2 == NULL)
6001 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006002 if (l1 == l2)
6003 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006004 if (list_len(l1) != list_len(l2))
6005 return FALSE;
6006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 for (item1 = l1->lv_first, item2 = l2->lv_first;
6008 item1 != NULL && item2 != NULL;
6009 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006010 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 return FALSE;
6012 return item1 == NULL && item2 == NULL;
6013}
6014
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006015#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6016 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006017/*
6018 * Return the dictitem that an entry in a hashtable points to.
6019 */
6020 dictitem_T *
6021dict_lookup(hi)
6022 hashitem_T *hi;
6023{
6024 return HI2DI(hi);
6025}
6026#endif
6027
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 * Return TRUE when two dictionaries have exactly the same key/values.
6030 */
6031 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 dict_T *d1;
6034 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006037{
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 hashitem_T *hi;
6039 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006040 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006042 if (d1 == NULL || d2 == NULL)
6043 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006044 if (d1 == d2)
6045 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006046 if (dict_len(d1) != dict_len(d2))
6047 return FALSE;
6048
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006049 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006052 if (!HASHITEM_EMPTY(hi))
6053 {
6054 item2 = dict_find(d2, hi->hi_key, -1);
6055 if (item2 == NULL)
6056 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006058 return FALSE;
6059 --todo;
6060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006061 }
6062 return TRUE;
6063}
6064
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065static int tv_equal_recurse_limit;
6066
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006067/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006068 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006069 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006070 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071 */
6072 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006073tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006074 typval_T *tv1;
6075 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 int ic; /* ignore case */
6077 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078{
6079 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006080 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006082 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006083
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006084 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006087 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 * recursiveness to a limit. We guess they are equal then.
6089 * A fixed limit has the problem of still taking an awful long time.
6090 * Reduce the limit every time running into it. That should work fine for
6091 * deeply linked structures that are not recursively linked and catch
6092 * recursiveness quickly. */
6093 if (!recursive)
6094 tv_equal_recurse_limit = 1000;
6095 if (recursive_cnt >= tv_equal_recurse_limit)
6096 {
6097 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006098 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006100
6101 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006103 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006104 ++recursive_cnt;
6105 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6106 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006107 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006108
6109 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 ++recursive_cnt;
6111 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6112 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006113 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114
6115 case VAR_FUNC:
6116 return (tv1->vval.v_string != NULL
6117 && tv2->vval.v_string != NULL
6118 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6119
6120 case VAR_NUMBER:
6121 return tv1->vval.v_number == tv2->vval.v_number;
6122
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006123#ifdef FEAT_FLOAT
6124 case VAR_FLOAT:
6125 return tv1->vval.v_float == tv2->vval.v_float;
6126#endif
6127
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 case VAR_STRING:
6129 s1 = get_tv_string_buf(tv1, buf1);
6130 s2 = get_tv_string_buf(tv2, buf2);
6131 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 return TRUE;
6136}
6137
6138/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 * Locate item with index "n" in list "l" and return it.
6140 * A negative index is counted from the end; -1 is the last item.
6141 * Returns NULL when "n" is out of range.
6142 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006143 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006145 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 long n;
6147{
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 long idx;
6150
6151 if (l == NULL)
6152 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006153
6154 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006156 n = l->lv_len + n;
6157
6158 /* Check for index out of range. */
6159 if (n < 0 || n >= l->lv_len)
6160 return NULL;
6161
6162 /* When there is a cached index may start search from there. */
6163 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006165 if (n < l->lv_idx / 2)
6166 {
6167 /* closest to the start of the list */
6168 item = l->lv_first;
6169 idx = 0;
6170 }
6171 else if (n > (l->lv_idx + l->lv_len) / 2)
6172 {
6173 /* closest to the end of the list */
6174 item = l->lv_last;
6175 idx = l->lv_len - 1;
6176 }
6177 else
6178 {
6179 /* closest to the cached index */
6180 item = l->lv_idx_item;
6181 idx = l->lv_idx;
6182 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 }
6184 else
6185 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006186 if (n < l->lv_len / 2)
6187 {
6188 /* closest to the start of the list */
6189 item = l->lv_first;
6190 idx = 0;
6191 }
6192 else
6193 {
6194 /* closest to the end of the list */
6195 item = l->lv_last;
6196 idx = l->lv_len - 1;
6197 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006199
6200 while (n > idx)
6201 {
6202 /* search forward */
6203 item = item->li_next;
6204 ++idx;
6205 }
6206 while (n < idx)
6207 {
6208 /* search backward */
6209 item = item->li_prev;
6210 --idx;
6211 }
6212
6213 /* cache the used index */
6214 l->lv_idx = idx;
6215 l->lv_idx_item = item;
6216
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 return item;
6218}
6219
6220/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006221 * Get list item "l[idx]" as a number.
6222 */
6223 static long
6224list_find_nr(l, idx, errorp)
6225 list_T *l;
6226 long idx;
6227 int *errorp; /* set to TRUE when something wrong */
6228{
6229 listitem_T *li;
6230
6231 li = list_find(l, idx);
6232 if (li == NULL)
6233 {
6234 if (errorp != NULL)
6235 *errorp = TRUE;
6236 return -1L;
6237 }
6238 return get_tv_number_chk(&li->li_tv, errorp);
6239}
6240
6241/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006242 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6243 */
6244 char_u *
6245list_find_str(l, idx)
6246 list_T *l;
6247 long idx;
6248{
6249 listitem_T *li;
6250
6251 li = list_find(l, idx - 1);
6252 if (li == NULL)
6253 {
6254 EMSGN(_(e_listidx), idx);
6255 return NULL;
6256 }
6257 return get_tv_string(&li->li_tv);
6258}
6259
6260/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006261 * Locate "item" list "l" and return its index.
6262 * Returns -1 when "item" is not in the list.
6263 */
6264 static long
6265list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 list_T *l;
6267 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268{
6269 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006271
6272 if (l == NULL)
6273 return -1;
6274 idx = 0;
6275 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6276 ++idx;
6277 if (li == NULL)
6278 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006279 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280}
6281
6282/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 * Append item "item" to the end of list "l".
6284 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006285 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 list_T *l;
6288 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289{
6290 if (l->lv_last == NULL)
6291 {
6292 /* empty list */
6293 l->lv_first = item;
6294 l->lv_last = item;
6295 item->li_prev = NULL;
6296 }
6297 else
6298 {
6299 l->lv_last->li_next = item;
6300 item->li_prev = l->lv_last;
6301 l->lv_last = item;
6302 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006304 item->li_next = NULL;
6305}
6306
6307/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006311 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 list_T *l;
6314 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006316 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317
Bram Moolenaar05159a02005-02-26 23:04:13 +00006318 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006320 copy_tv(tv, &li->li_tv);
6321 list_append(l, li);
6322 return OK;
6323}
6324
6325/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006326 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006327 * Return FAIL when out of memory.
6328 */
6329 int
6330list_append_dict(list, dict)
6331 list_T *list;
6332 dict_T *dict;
6333{
6334 listitem_T *li = listitem_alloc();
6335
6336 if (li == NULL)
6337 return FAIL;
6338 li->li_tv.v_type = VAR_DICT;
6339 li->li_tv.v_lock = 0;
6340 li->li_tv.vval.v_dict = dict;
6341 list_append(list, li);
6342 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343 return OK;
6344}
6345
6346/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006348 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006349 * Returns FAIL when out of memory.
6350 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006351 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006353 list_T *l;
6354 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356{
6357 listitem_T *li = listitem_alloc();
6358
6359 if (li == NULL)
6360 return FAIL;
6361 list_append(l, li);
6362 li->li_tv.v_type = VAR_STRING;
6363 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006364 if (str == NULL)
6365 li->li_tv.vval.v_string = NULL;
6366 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006367 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 return FAIL;
6369 return OK;
6370}
6371
6372/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006373 * Append "n" to list "l".
6374 * Returns FAIL when out of memory.
6375 */
6376 static int
6377list_append_number(l, n)
6378 list_T *l;
6379 varnumber_T n;
6380{
6381 listitem_T *li;
6382
6383 li = listitem_alloc();
6384 if (li == NULL)
6385 return FAIL;
6386 li->li_tv.v_type = VAR_NUMBER;
6387 li->li_tv.v_lock = 0;
6388 li->li_tv.vval.v_number = n;
6389 list_append(l, li);
6390 return OK;
6391}
6392
6393/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 * If "item" is NULL append at the end.
6396 * Return FAIL when out of memory.
6397 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006398 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 list_T *l;
6401 typval_T *tv;
6402 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403{
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405
6406 if (ni == NULL)
6407 return FAIL;
6408 copy_tv(tv, &ni->li_tv);
6409 if (item == NULL)
6410 /* Append new item at end of list. */
6411 list_append(l, ni);
6412 else
6413 {
6414 /* Insert new item before existing item. */
6415 ni->li_prev = item->li_prev;
6416 ni->li_next = item;
6417 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 ++l->lv_idx;
6421 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 l->lv_idx_item = NULL;
6426 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 }
6430 return OK;
6431}
6432
6433/*
6434 * Extend "l1" with "l2".
6435 * If "bef" is NULL append at the end, otherwise insert before this item.
6436 * Returns FAIL when out of memory.
6437 */
6438 static int
6439list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 list_T *l1;
6441 list_T *l2;
6442 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443{
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006445 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006447 /* We also quit the loop when we have inserted the original item count of
6448 * the list, avoid a hang when we extend a list with itself. */
6449 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6451 return FAIL;
6452 return OK;
6453}
6454
6455/*
6456 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6457 * Return FAIL when out of memory.
6458 */
6459 static int
6460list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 list_T *l1;
6462 list_T *l2;
6463 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464{
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006467 if (l1 == NULL || l2 == NULL)
6468 return FAIL;
6469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006471 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472 if (l == NULL)
6473 return FAIL;
6474 tv->v_type = VAR_LIST;
6475 tv->vval.v_list = l;
6476
6477 /* append all items from the second list */
6478 return list_extend(l, l2, NULL);
6479}
6480
6481/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 * Returns NULL when out of memory.
6486 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *copy;
6494 listitem_T *item;
6495 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496
6497 if (orig == NULL)
6498 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499
6500 copy = list_alloc();
6501 if (copy != NULL)
6502 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 if (copyID != 0)
6504 {
6505 /* Do this before adding the items, because one of the items may
6506 * refer back to this list. */
6507 orig->lv_copyID = copyID;
6508 orig->lv_copylist = copy;
6509 }
6510 for (item = orig->lv_first; item != NULL && !got_int;
6511 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 {
6513 ni = listitem_alloc();
6514 if (ni == NULL)
6515 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006517 {
6518 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6519 {
6520 vim_free(ni);
6521 break;
6522 }
6523 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 list_append(copy, ni);
6527 }
6528 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (item != NULL)
6530 {
6531 list_unref(copy);
6532 copy = NULL;
6533 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 }
6535
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 return copy;
6537}
6538
6539/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006541 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006543 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006544list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 list_T *l;
6546 listitem_T *item;
6547 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548{
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 /* notify watchers */
6552 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006553 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006554 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 list_fix_watch(l, ip);
6556 if (ip == item2)
6557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
6560 if (item2->li_next == NULL)
6561 l->lv_last = item->li_prev;
6562 else
6563 item2->li_next->li_prev = item->li_prev;
6564 if (item->li_prev == NULL)
6565 l->lv_first = item2->li_next;
6566 else
6567 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569}
6570
6571/*
6572 * Return an allocated string with the string representation of a list.
6573 * May return NULL.
6574 */
6575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579{
6580 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581
6582 if (tv->vval.v_list == NULL)
6583 return NULL;
6584 ga_init2(&ga, (int)sizeof(char), 80);
6585 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006586 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 {
6588 vim_free(ga.ga_data);
6589 return NULL;
6590 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 ga_append(&ga, ']');
6592 ga_append(&ga, NUL);
6593 return (char_u *)ga.ga_data;
6594}
6595
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006596typedef struct join_S {
6597 char_u *s;
6598 char_u *tofree;
6599} join_T;
6600
6601 static int
6602list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6603 garray_T *gap; /* to store the result in */
6604 list_T *l;
6605 char_u *sep;
6606 int echo_style;
6607 int copyID;
6608 garray_T *join_gap; /* to keep each list item string */
6609{
6610 int i;
6611 join_T *p;
6612 int len;
6613 int sumlen = 0;
6614 int first = TRUE;
6615 char_u *tofree;
6616 char_u numbuf[NUMBUFLEN];
6617 listitem_T *item;
6618 char_u *s;
6619
6620 /* Stringify each item in the list. */
6621 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6622 {
6623 if (echo_style)
6624 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6625 else
6626 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6627 if (s == NULL)
6628 return FAIL;
6629
6630 len = (int)STRLEN(s);
6631 sumlen += len;
6632
6633 ga_grow(join_gap, 1);
6634 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6635 if (tofree != NULL || s != numbuf)
6636 {
6637 p->s = s;
6638 p->tofree = tofree;
6639 }
6640 else
6641 {
6642 p->s = vim_strnsave(s, len);
6643 p->tofree = p->s;
6644 }
6645
6646 line_breakcheck();
6647 }
6648
6649 /* Allocate result buffer with its total size, avoid re-allocation and
6650 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6651 if (join_gap->ga_len >= 2)
6652 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6653 if (ga_grow(gap, sumlen + 2) == FAIL)
6654 return FAIL;
6655
6656 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6657 {
6658 if (first)
6659 first = FALSE;
6660 else
6661 ga_concat(gap, sep);
6662 p = ((join_T *)join_gap->ga_data) + i;
6663
6664 if (p->s != NULL)
6665 ga_concat(gap, p->s);
6666 line_breakcheck();
6667 }
6668
6669 return OK;
6670}
6671
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006673 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006674 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006676 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006678list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006680 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006682 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006683 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006685 garray_T join_ga;
6686 int retval;
6687 join_T *p;
6688 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006690 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6691 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6692
6693 /* Dispose each item in join_ga. */
6694 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696 p = (join_T *)join_ga.ga_data;
6697 for (i = 0; i < join_ga.ga_len; ++i)
6698 {
6699 vim_free(p->tofree);
6700 ++p;
6701 }
6702 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704
6705 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006706}
6707
6708/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006709 * Garbage collection for lists and dictionaries.
6710 *
6711 * We use reference counts to be able to free most items right away when they
6712 * are no longer used. But for composite items it's possible that it becomes
6713 * unused while the reference count is > 0: When there is a recursive
6714 * reference. Example:
6715 * :let l = [1, 2, 3]
6716 * :let d = {9: l}
6717 * :let l[1] = d
6718 *
6719 * Since this is quite unusual we handle this with garbage collection: every
6720 * once in a while find out which lists and dicts are not referenced from any
6721 * variable.
6722 *
6723 * Here is a good reference text about garbage collection (refers to Python
6724 * but it applies to all reference-counting mechanisms):
6725 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006726 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006727
6728/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006729 * Do garbage collection for lists and dicts.
6730 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 int
6733garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006735 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 buf_T *buf;
6737 win_T *wp;
6738 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006739 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006740 int did_free;
6741 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006742#ifdef FEAT_WINDOWS
6743 tabpage_T *tp;
6744#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006746 /* Only do this once. */
6747 want_garbage_collect = FALSE;
6748 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006749 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006750
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006751 /* We advance by two because we add one for items referenced through
6752 * previous_funccal. */
6753 current_copyID += COPYID_INC;
6754 copyID = current_copyID;
6755
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006756 /*
6757 * 1. Go through all accessible variables and mark all lists and dicts
6758 * with copyID.
6759 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006760
6761 /* Don't free variables in the previous_funccal list unless they are only
6762 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006763 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006764 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6765 {
6766 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6767 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6768 }
6769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 /* script-local variables */
6771 for (i = 1; i <= ga_scripts.ga_len; ++i)
6772 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6773
6774 /* buffer-local variables */
6775 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006776 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777
6778 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006780 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006781#ifdef FEAT_AUTOCMD
6782 if (aucmd_win != NULL)
6783 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6784#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786#ifdef FEAT_WINDOWS
6787 /* tabpage-local variables */
6788 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006789 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006790#endif
6791
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792 /* global variables */
6793 set_ref_in_ht(&globvarht, copyID);
6794
6795 /* function-local variables */
6796 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6797 {
6798 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6799 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6800 }
6801
Bram Moolenaard812df62008-11-09 12:46:09 +00006802 /* v: vars */
6803 set_ref_in_ht(&vimvarht, copyID);
6804
Bram Moolenaar1dced572012-04-05 16:54:08 +02006805#ifdef FEAT_LUA
6806 set_ref_in_lua(copyID);
6807#endif
6808
Bram Moolenaardb913952012-06-29 12:54:53 +02006809#ifdef FEAT_PYTHON
6810 set_ref_in_python(copyID);
6811#endif
6812
6813#ifdef FEAT_PYTHON3
6814 set_ref_in_python3(copyID);
6815#endif
6816
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006817 /*
6818 * 2. Free lists and dictionaries that are not referenced.
6819 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006820 did_free = free_unref_items(copyID);
6821
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006822 /*
6823 * 3. Check if any funccal can be freed now.
6824 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006825 for (pfc = &previous_funccal; *pfc != NULL; )
6826 {
6827 if (can_free_funccal(*pfc, copyID))
6828 {
6829 fc = *pfc;
6830 *pfc = fc->caller;
6831 free_funccal(fc, TRUE);
6832 did_free = TRUE;
6833 did_free_funccal = TRUE;
6834 }
6835 else
6836 pfc = &(*pfc)->caller;
6837 }
6838 if (did_free_funccal)
6839 /* When a funccal was freed some more items might be garbage
6840 * collected, so run again. */
6841 (void)garbage_collect();
6842
6843 return did_free;
6844}
6845
6846/*
6847 * Free lists and dictionaries that are no longer referenced.
6848 */
6849 static int
6850free_unref_items(copyID)
6851 int copyID;
6852{
6853 dict_T *dd;
6854 list_T *ll;
6855 int did_free = FALSE;
6856
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 */
6860 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006863 /* Free the Dictionary and ordinary items it contains, but don't
6864 * recurse into Lists and Dictionaries, they will be in the list
6865 * of dicts or list of lists. */
6866 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 did_free = TRUE;
6868
6869 /* restart, next dict may also have been freed */
6870 dd = first_dict;
6871 }
6872 else
6873 dd = dd->dv_used_next;
6874
6875 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006876 * Go through the list of lists and free items without the copyID.
6877 * But don't free a list that has a watcher (used in a for loop), these
6878 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 */
6880 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6882 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006884 /* Free the List and ordinary items it contains, but don't recurse
6885 * into Lists and Dictionaries, they will be in the list of dicts
6886 * or list of lists. */
6887 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888 did_free = TRUE;
6889
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006890 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 ll = first_list;
6892 }
6893 else
6894 ll = ll->lv_used_next;
6895
6896 return did_free;
6897}
6898
6899/*
6900 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6901 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006902 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903set_ref_in_ht(ht, copyID)
6904 hashtab_T *ht;
6905 int copyID;
6906{
6907 int todo;
6908 hashitem_T *hi;
6909
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006910 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 for (hi = ht->ht_array; todo > 0; ++hi)
6912 if (!HASHITEM_EMPTY(hi))
6913 {
6914 --todo;
6915 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6916 }
6917}
6918
6919/*
6920 * Mark all lists and dicts referenced through list "l" with "copyID".
6921 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006922 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923set_ref_in_list(l, copyID)
6924 list_T *l;
6925 int copyID;
6926{
6927 listitem_T *li;
6928
6929 for (li = l->lv_first; li != NULL; li = li->li_next)
6930 set_ref_in_item(&li->li_tv, copyID);
6931}
6932
6933/*
6934 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6935 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006936 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937set_ref_in_item(tv, copyID)
6938 typval_T *tv;
6939 int copyID;
6940{
6941 dict_T *dd;
6942 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006943
6944 switch (tv->v_type)
6945 {
6946 case VAR_DICT:
6947 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006948 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006949 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 /* Didn't see this dict yet. */
6951 dd->dv_copyID = copyID;
6952 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955
6956 case VAR_LIST:
6957 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006958 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 /* Didn't see this list yet. */
6961 ll->lv_copyID = copyID;
6962 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967}
6968
6969/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006970 * Allocate an empty header for a dictionary.
6971 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006972 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006973dict_alloc()
6974{
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006976
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006978 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006979 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006980 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 if (first_dict != NULL)
6982 first_dict->dv_used_prev = d;
6983 d->dv_used_next = first_dict;
6984 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006985 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006989 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006990 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006991 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006992 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006993 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006994}
6995
6996/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006997 * Allocate an empty dict for a return value.
6998 * Returns OK or FAIL.
6999 */
7000 static int
7001rettv_dict_alloc(rettv)
7002 typval_T *rettv;
7003{
7004 dict_T *d = dict_alloc();
7005
7006 if (d == NULL)
7007 return FAIL;
7008
7009 rettv->vval.v_dict = d;
7010 rettv->v_type = VAR_DICT;
7011 ++d->dv_refcount;
7012 return OK;
7013}
7014
7015
7016/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017 * Unreference a Dictionary: decrement the reference count and free it when it
7018 * becomes zero.
7019 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007020 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007023{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007024 if (d != NULL && --d->dv_refcount <= 0)
7025 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007026}
7027
7028/*
7029 * Free a Dictionary, including all items it contains.
7030 * Ignores the reference count.
7031 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007032 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007033dict_free(d, recurse)
7034 dict_T *d;
7035 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007037 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007039 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 /* Remove the dict from the list of dicts for garbage collection. */
7042 if (d->dv_used_prev == NULL)
7043 first_dict = d->dv_used_next;
7044 else
7045 d->dv_used_prev->dv_used_next = d->dv_used_next;
7046 if (d->dv_used_next != NULL)
7047 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7048
7049 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007050 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007051 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007052 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007053 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054 if (!HASHITEM_EMPTY(hi))
7055 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007056 /* Remove the item before deleting it, just in case there is
7057 * something recursive causing trouble. */
7058 di = HI2DI(hi);
7059 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007060 if (recurse || (di->di_tv.v_type != VAR_LIST
7061 && di->di_tv.v_type != VAR_DICT))
7062 clear_tv(&di->di_tv);
7063 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007064 --todo;
7065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007067 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068 vim_free(d);
7069}
7070
7071/*
7072 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007073 * The "key" is copied to the new item.
7074 * Note that the value of the item "di_tv" still needs to be initialized!
7075 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007077 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078dictitem_alloc(key)
7079 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080{
Bram Moolenaar33570922005-01-25 22:26:29 +00007081 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007083 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007084 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007085 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007087 di->di_flags = 0;
7088 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090}
7091
7092/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093 * Make a copy of a Dictionary item.
7094 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007098{
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007101 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7102 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 if (di != NULL)
7104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007106 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 copy_tv(&org->di_tv, &di->di_tv);
7108 }
7109 return di;
7110}
7111
7112/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 * Remove item "item" from Dictionary "dict" and free it.
7114 */
7115 static void
7116dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 dict_T *dict;
7118 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119{
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 if (HASHITEM_EMPTY(hi))
7124 EMSG2(_(e_intern2), "dictitem_remove()");
7125 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 dictitem_free(item);
7128}
7129
7130/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131 * Free a dict item. Also clears the value.
7132 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007133 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007137 clear_tv(&item->di_tv);
7138 vim_free(item);
7139}
7140
7141/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7143 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145 * Returns NULL when out of memory.
7146 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007148dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152{
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 dict_T *copy;
7154 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157
7158 if (orig == NULL)
7159 return NULL;
7160
7161 copy = dict_alloc();
7162 if (copy != NULL)
7163 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007164 if (copyID != 0)
7165 {
7166 orig->dv_copyID = copyID;
7167 orig->dv_copydict = copy;
7168 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007169 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007170 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 --todo;
7175
7176 di = dictitem_alloc(hi->hi_key);
7177 if (di == NULL)
7178 break;
7179 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 {
7181 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7182 copyID) == FAIL)
7183 {
7184 vim_free(di);
7185 break;
7186 }
7187 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 else
7189 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7190 if (dict_add(copy, di) == FAIL)
7191 {
7192 dictitem_free(di);
7193 break;
7194 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007199 if (todo > 0)
7200 {
7201 dict_unref(copy);
7202 copy = NULL;
7203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 }
7205
7206 return copy;
7207}
7208
7209/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007211 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007213 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007215 dict_T *d;
7216 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217{
Bram Moolenaar33570922005-01-25 22:26:29 +00007218 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219}
7220
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007222 * Add a number or string entry to dictionary "d".
7223 * When "str" is NULL use number "nr", otherwise use "str".
7224 * Returns FAIL when out of memory and when key already exists.
7225 */
7226 int
7227dict_add_nr_str(d, key, nr, str)
7228 dict_T *d;
7229 char *key;
7230 long nr;
7231 char_u *str;
7232{
7233 dictitem_T *item;
7234
7235 item = dictitem_alloc((char_u *)key);
7236 if (item == NULL)
7237 return FAIL;
7238 item->di_tv.v_lock = 0;
7239 if (str == NULL)
7240 {
7241 item->di_tv.v_type = VAR_NUMBER;
7242 item->di_tv.vval.v_number = nr;
7243 }
7244 else
7245 {
7246 item->di_tv.v_type = VAR_STRING;
7247 item->di_tv.vval.v_string = vim_strsave(str);
7248 }
7249 if (dict_add(d, item) == FAIL)
7250 {
7251 dictitem_free(item);
7252 return FAIL;
7253 }
7254 return OK;
7255}
7256
7257/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007258 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007259 * Returns FAIL when out of memory and when key already exists.
7260 */
7261 int
7262dict_add_list(d, key, list)
7263 dict_T *d;
7264 char *key;
7265 list_T *list;
7266{
7267 dictitem_T *item;
7268
7269 item = dictitem_alloc((char_u *)key);
7270 if (item == NULL)
7271 return FAIL;
7272 item->di_tv.v_lock = 0;
7273 item->di_tv.v_type = VAR_LIST;
7274 item->di_tv.vval.v_list = list;
7275 if (dict_add(d, item) == FAIL)
7276 {
7277 dictitem_free(item);
7278 return FAIL;
7279 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007280 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007281 return OK;
7282}
7283
7284/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007285 * Get the number of items in a Dictionary.
7286 */
7287 static long
7288dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 if (d == NULL)
7292 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007293 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294}
7295
7296/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297 * Find item "key[len]" in Dictionary "d".
7298 * If "len" is negative use strlen(key).
7299 * Returns NULL when not found.
7300 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007301 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 char_u *key;
7305 int len;
7306{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307#define AKEYLEN 200
7308 char_u buf[AKEYLEN];
7309 char_u *akey;
7310 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 if (len < 0)
7314 akey = key;
7315 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007316 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 tofree = akey = vim_strnsave(key, len);
7318 if (akey == NULL)
7319 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007320 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 else
7322 {
7323 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007324 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 akey = buf;
7326 }
7327
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 vim_free(tofree);
7330 if (HASHITEM_EMPTY(hi))
7331 return NULL;
7332 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333}
7334
7335/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007336 * Get a string item from a dictionary.
7337 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007338 * Returns NULL if the entry doesn't exist or out of memory.
7339 */
7340 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007341get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007342 dict_T *d;
7343 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007344 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007345{
7346 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007347 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007348
7349 di = dict_find(d, key, -1);
7350 if (di == NULL)
7351 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007352 s = get_tv_string(&di->di_tv);
7353 if (save && s != NULL)
7354 s = vim_strsave(s);
7355 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007356}
7357
7358/*
7359 * Get a number item from a dictionary.
7360 * Returns 0 if the entry doesn't exist or out of memory.
7361 */
7362 long
7363get_dict_number(d, key)
7364 dict_T *d;
7365 char_u *key;
7366{
7367 dictitem_T *di;
7368
7369 di = dict_find(d, key, -1);
7370 if (di == NULL)
7371 return 0;
7372 return get_tv_number(&di->di_tv);
7373}
7374
7375/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376 * Return an allocated string with the string representation of a Dictionary.
7377 * May return NULL.
7378 */
7379 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007380dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007382 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383{
7384 garray_T ga;
7385 int first = TRUE;
7386 char_u *tofree;
7387 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 return NULL;
7395 ga_init2(&ga, (int)sizeof(char), 80);
7396 ga_append(&ga, '{');
7397
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007398 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007399 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 --todo;
7404
7405 if (first)
7406 first = FALSE;
7407 else
7408 ga_concat(&ga, (char_u *)", ");
7409
7410 tofree = string_quote(hi->hi_key, FALSE);
7411 if (tofree != NULL)
7412 {
7413 ga_concat(&ga, tofree);
7414 vim_free(tofree);
7415 }
7416 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007417 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418 if (s != NULL)
7419 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007421 if (s == NULL)
7422 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 if (todo > 0)
7426 {
7427 vim_free(ga.ga_data);
7428 return NULL;
7429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430
7431 ga_append(&ga, '}');
7432 ga_append(&ga, NUL);
7433 return (char_u *)ga.ga_data;
7434}
7435
7436/*
7437 * Allocate a variable for a Dictionary and fill it from "*arg".
7438 * Return OK or FAIL. Returns NOTDONE for {expr}.
7439 */
7440 static int
7441get_dict_tv(arg, rettv, evaluate)
7442 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444 int evaluate;
7445{
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 dict_T *d = NULL;
7447 typval_T tvkey;
7448 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007449 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453
7454 /*
7455 * First check if it's not a curly-braces thing: {expr}.
7456 * Must do this without evaluating, otherwise a function may be called
7457 * twice. Unfortunately this means we need to call eval1() twice for the
7458 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007461 if (*start != '}')
7462 {
7463 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7464 return FAIL;
7465 if (*start == '}')
7466 return NOTDONE;
7467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468
7469 if (evaluate)
7470 {
7471 d = dict_alloc();
7472 if (d == NULL)
7473 return FAIL;
7474 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007475 tvkey.v_type = VAR_UNKNOWN;
7476 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477
7478 *arg = skipwhite(*arg + 1);
7479 while (**arg != '}' && **arg != NUL)
7480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007481 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 goto failret;
7483 if (**arg != ':')
7484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007485 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 goto failret;
7488 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007489 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007491 key = get_tv_string_buf_chk(&tvkey, buf);
7492 if (key == NULL || *key == NUL)
7493 {
7494 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7495 if (key != NULL)
7496 EMSG(_(e_emptykey));
7497 clear_tv(&tvkey);
7498 goto failret;
7499 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501
7502 *arg = skipwhite(*arg + 1);
7503 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7504 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007505 if (evaluate)
7506 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 goto failret;
7508 }
7509 if (evaluate)
7510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 if (item != NULL)
7513 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007514 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 clear_tv(&tv);
7517 goto failret;
7518 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 item = dictitem_alloc(key);
7520 clear_tv(&tvkey);
7521 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007524 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 if (dict_add(d, item) == FAIL)
7526 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 }
7528 }
7529
7530 if (**arg == '}')
7531 break;
7532 if (**arg != ',')
7533 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007534 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 goto failret;
7536 }
7537 *arg = skipwhite(*arg + 1);
7538 }
7539
7540 if (**arg != '}')
7541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007542 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543failret:
7544 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007545 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546 return FAIL;
7547 }
7548
7549 *arg = skipwhite(*arg + 1);
7550 if (evaluate)
7551 {
7552 rettv->v_type = VAR_DICT;
7553 rettv->vval.v_dict = d;
7554 ++d->dv_refcount;
7555 }
7556
7557 return OK;
7558}
7559
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007561 * Return a string with the string representation of a variable.
7562 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007563 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007564 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007565 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007566 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 */
7568 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007569echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007571 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007572 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 static int recurse = 0;
7576 char_u *r = NULL;
7577
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007580 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007581 *tofree = NULL;
7582 return NULL;
7583 }
7584 ++recurse;
7585
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007586 switch (tv->v_type)
7587 {
7588 case VAR_FUNC:
7589 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007590 r = tv->vval.v_string;
7591 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007593 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007594 if (tv->vval.v_list == NULL)
7595 {
7596 *tofree = NULL;
7597 r = NULL;
7598 }
7599 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7600 {
7601 *tofree = NULL;
7602 r = (char_u *)"[...]";
7603 }
7604 else
7605 {
7606 tv->vval.v_list->lv_copyID = copyID;
7607 *tofree = list2string(tv, copyID);
7608 r = *tofree;
7609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613 if (tv->vval.v_dict == NULL)
7614 {
7615 *tofree = NULL;
7616 r = NULL;
7617 }
7618 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7619 {
7620 *tofree = NULL;
7621 r = (char_u *)"{...}";
7622 }
7623 else
7624 {
7625 tv->vval.v_dict->dv_copyID = copyID;
7626 *tofree = dict2string(tv, copyID);
7627 r = *tofree;
7628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007631 case VAR_STRING:
7632 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 *tofree = NULL;
7634 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007635 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007637#ifdef FEAT_FLOAT
7638 case VAR_FLOAT:
7639 *tofree = NULL;
7640 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7641 r = numbuf;
7642 break;
7643#endif
7644
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007646 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649
7650 --recurse;
7651 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007652}
7653
7654/*
7655 * Return a string with the string representation of a variable.
7656 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7657 * "numbuf" is used for a number.
7658 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007659 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 */
7661 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007662tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007664 char_u **tofree;
7665 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007666 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667{
7668 switch (tv->v_type)
7669 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007670 case VAR_FUNC:
7671 *tofree = string_quote(tv->vval.v_string, TRUE);
7672 return *tofree;
7673 case VAR_STRING:
7674 *tofree = string_quote(tv->vval.v_string, FALSE);
7675 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007676#ifdef FEAT_FLOAT
7677 case VAR_FLOAT:
7678 *tofree = NULL;
7679 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7680 return numbuf;
7681#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007682 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007683 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007688 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007689 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690}
7691
7692/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 * Return string "str" in ' quotes, doubling ' characters.
7694 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 */
7697 static char_u *
7698string_quote(str, function)
7699 char_u *str;
7700 int function;
7701{
Bram Moolenaar33570922005-01-25 22:26:29 +00007702 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 char_u *p, *r, *s;
7704
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 len = (function ? 13 : 3);
7706 if (str != NULL)
7707 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007708 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 for (p = str; *p != NUL; mb_ptr_adv(p))
7710 if (*p == '\'')
7711 ++len;
7712 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 s = r = alloc(len);
7714 if (r != NULL)
7715 {
7716 if (function)
7717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 r += 10;
7720 }
7721 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 if (str != NULL)
7724 for (p = str; *p != NUL; )
7725 {
7726 if (*p == '\'')
7727 *r++ = '\'';
7728 MB_COPY_CHAR(p, r);
7729 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 if (function)
7732 *r++ = ')';
7733 *r++ = NUL;
7734 }
7735 return s;
7736}
7737
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007738#ifdef FEAT_FLOAT
7739/*
7740 * Convert the string "text" to a floating point number.
7741 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7742 * this always uses a decimal point.
7743 * Returns the length of the text that was consumed.
7744 */
7745 static int
7746string2float(text, value)
7747 char_u *text;
7748 float_T *value; /* result stored here */
7749{
7750 char *s = (char *)text;
7751 float_T f;
7752
7753 f = strtod(s, &s);
7754 *value = f;
7755 return (int)((char_u *)s - text);
7756}
7757#endif
7758
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 * Get the value of an environment variable.
7761 * "arg" is pointing to the '$'. It is advanced to after the name.
7762 * If the environment variable was not set, silently assume it is empty.
7763 * Always return OK.
7764 */
7765 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 int evaluate;
7770{
7771 char_u *string = NULL;
7772 int len;
7773 int cc;
7774 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007775 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
7777 ++*arg;
7778 name = *arg;
7779 len = get_env_len(arg);
7780 if (evaluate)
7781 {
7782 if (len != 0)
7783 {
7784 cc = name[len];
7785 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007786 /* first try vim_getenv(), fast for normal environment vars */
7787 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007789 {
7790 if (!mustfree)
7791 string = vim_strsave(string);
7792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 else
7794 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007795 if (mustfree)
7796 vim_free(string);
7797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 /* next try expanding things like $VIM and ${HOME} */
7799 string = expand_env_save(name - 1);
7800 if (string != NULL && *string == '$')
7801 {
7802 vim_free(string);
7803 string = NULL;
7804 }
7805 }
7806 name[len] = cc;
7807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 rettv->v_type = VAR_STRING;
7809 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 }
7811
7812 return OK;
7813}
7814
7815/*
7816 * Array with names and number of arguments of all internal functions
7817 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7818 */
7819static struct fst
7820{
7821 char *f_name; /* function name */
7822 char f_min_argc; /* minimal number of arguments */
7823 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007824 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007825 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826} functions[] =
7827{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007828#ifdef FEAT_FLOAT
7829 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007830 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007831#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007832 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007833 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"append", 2, 2, f_append},
7835 {"argc", 0, 0, f_argc},
7836 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007837 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007839 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007840 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007841 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007844 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {"bufexists", 1, 1, f_bufexists},
7846 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7847 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7848 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7849 {"buflisted", 1, 1, f_buflisted},
7850 {"bufloaded", 1, 1, f_bufloaded},
7851 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007852 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"bufwinnr", 1, 1, f_bufwinnr},
7854 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007855 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007857#ifdef FEAT_FLOAT
7858 {"ceil", 1, 1, f_ceil},
7859#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007860 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007861 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007863 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007865#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007866 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007867 {"complete_add", 1, 1, f_complete_add},
7868 {"complete_check", 0, 0, f_complete_check},
7869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
7873 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007874 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007875#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007876 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007878 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007879 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {"delete", 1, 1, f_delete},
7881 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007882 {"diff_filler", 1, 1, f_diff_filler},
7883 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007884 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"eventhandler", 0, 0, f_eventhandler},
7888 {"executable", 1, 1, f_executable},
7889 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007890#ifdef FEAT_FLOAT
7891 {"exp", 1, 1, f_exp},
7892#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007893 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007894 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007895 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7897 {"filereadable", 1, 1, f_filereadable},
7898 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007900 {"finddir", 1, 3, f_finddir},
7901 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#ifdef FEAT_FLOAT
7903 {"float2nr", 1, 1, f_float2nr},
7904 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007905 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007907 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"fnamemodify", 2, 2, f_fnamemodify},
7909 {"foldclosed", 1, 1, f_foldclosed},
7910 {"foldclosedend", 1, 1, f_foldclosedend},
7911 {"foldlevel", 1, 1, f_foldlevel},
7912 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007913 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007916 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007917 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007918 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007919 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"getchar", 0, 1, f_getchar},
7921 {"getcharmod", 0, 0, f_getcharmod},
7922 {"getcmdline", 0, 0, f_getcmdline},
7923 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007924 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007926 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007927 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {"getfsize", 1, 1, f_getfsize},
7929 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007930 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007931 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007932 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007933 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007934 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007935 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007936 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007937 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007939 {"gettabvar", 2, 3, f_gettabvar},
7940 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {"getwinposx", 0, 0, f_getwinposx},
7942 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007943 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007944 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007945 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007948 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007949 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7951 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7952 {"histadd", 2, 2, f_histadd},
7953 {"histdel", 1, 2, f_histdel},
7954 {"histget", 1, 2, f_histget},
7955 {"histnr", 1, 1, f_histnr},
7956 {"hlID", 1, 1, f_hlID},
7957 {"hlexists", 1, 1, f_hlexists},
7958 {"hostname", 0, 0, f_hostname},
7959 {"iconv", 3, 3, f_iconv},
7960 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007961 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007962 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007964 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"inputrestore", 0, 0, f_inputrestore},
7966 {"inputsave", 0, 0, f_inputsave},
7967 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007968 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007969 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007971 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007973 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"libcall", 3, 3, f_libcall},
7978 {"libcallnr", 3, 3, f_libcallnr},
7979 {"line", 1, 1, f_line},
7980 {"line2byte", 1, 1, f_line2byte},
7981 {"lispindent", 1, 1, f_lispindent},
7982 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007984 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985 {"log10", 1, 1, f_log10},
7986#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007987#ifdef FEAT_LUA
7988 {"luaeval", 1, 2, f_luaeval},
7989#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007991 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007992 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007994 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007995 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007996 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007997 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007998 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007999 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008000 {"max", 1, 1, f_max},
8001 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008002#ifdef vim_mkdir
8003 {"mkdir", 1, 3, f_mkdir},
8004#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008005 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008006#ifdef FEAT_MZSCHEME
8007 {"mzeval", 1, 1, f_mzeval},
8008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008010 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008011 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008012 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008013#ifdef FEAT_FLOAT
8014 {"pow", 2, 2, f_pow},
8015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008017 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008018 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008019#ifdef FEAT_PYTHON3
8020 {"py3eval", 1, 1, f_py3eval},
8021#endif
8022#ifdef FEAT_PYTHON
8023 {"pyeval", 1, 1, f_pyeval},
8024#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008025 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008026 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008027 {"reltime", 0, 2, f_reltime},
8028 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"remote_expr", 2, 3, f_remote_expr},
8030 {"remote_foreground", 1, 1, f_remote_foreground},
8031 {"remote_peek", 1, 2, f_remote_peek},
8032 {"remote_read", 1, 1, f_remote_read},
8033 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008034 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008036 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008038 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008039#ifdef FEAT_FLOAT
8040 {"round", 1, 1, f_round},
8041#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008042 {"screencol", 0, 0, f_screencol},
8043 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008044 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008045 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008046 {"searchpair", 3, 7, f_searchpair},
8047 {"searchpairpos", 3, 7, f_searchpairpos},
8048 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"server2client", 2, 2, f_server2client},
8050 {"serverlist", 0, 0, f_serverlist},
8051 {"setbufvar", 3, 3, f_setbufvar},
8052 {"setcmdpos", 1, 1, f_setcmdpos},
8053 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008054 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008055 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008056 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008057 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008059 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008060 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008062#ifdef FEAT_CRYPT
8063 {"sha256", 1, 1, f_sha256},
8064#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008065 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008066 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#ifdef FEAT_FLOAT
8069 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008070 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008072 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008073 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008074 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008075 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008076 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"sqrt", 1, 1, f_sqrt},
8079 {"str2float", 1, 1, f_str2float},
8080#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008081 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008082 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008083 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084#ifdef HAVE_STRFTIME
8085 {"strftime", 1, 2, f_strftime},
8086#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008087 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008088 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"strlen", 1, 1, f_strlen},
8090 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008091 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008093 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"submatch", 1, 1, f_submatch},
8095 {"substitute", 4, 4, f_substitute},
8096 {"synID", 3, 3, f_synID},
8097 {"synIDattr", 2, 3, f_synIDattr},
8098 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008099 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008100 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008101 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008102 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008103 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008104 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008105 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008106 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008107#ifdef FEAT_FLOAT
8108 {"tan", 1, 1, f_tan},
8109 {"tanh", 1, 1, f_tanh},
8110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008112 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"tolower", 1, 1, f_tolower},
8114 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008115 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117 {"trunc", 1, 1, f_trunc},
8118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008120 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008121 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008122 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"virtcol", 1, 1, f_virtcol},
8124 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008125 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"winbufnr", 1, 1, f_winbufnr},
8127 {"wincol", 0, 0, f_wincol},
8128 {"winheight", 1, 1, f_winheight},
8129 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008130 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008132 {"winrestview", 1, 1, f_winrestview},
8133 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008135 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008136 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137};
8138
8139#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8140
8141/*
8142 * Function given to ExpandGeneric() to obtain the list of internal
8143 * or user defined function names.
8144 */
8145 char_u *
8146get_function_name(xp, idx)
8147 expand_T *xp;
8148 int idx;
8149{
8150 static int intidx = -1;
8151 char_u *name;
8152
8153 if (idx == 0)
8154 intidx = -1;
8155 if (intidx < 0)
8156 {
8157 name = get_user_func_name(xp, idx);
8158 if (name != NULL)
8159 return name;
8160 }
8161 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8162 {
8163 STRCPY(IObuff, functions[intidx].f_name);
8164 STRCAT(IObuff, "(");
8165 if (functions[intidx].f_max_argc == 0)
8166 STRCAT(IObuff, ")");
8167 return IObuff;
8168 }
8169
8170 return NULL;
8171}
8172
8173/*
8174 * Function given to ExpandGeneric() to obtain the list of internal or
8175 * user defined variable or function names.
8176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 char_u *
8178get_expr_name(xp, idx)
8179 expand_T *xp;
8180 int idx;
8181{
8182 static int intidx = -1;
8183 char_u *name;
8184
8185 if (idx == 0)
8186 intidx = -1;
8187 if (intidx < 0)
8188 {
8189 name = get_function_name(xp, idx);
8190 if (name != NULL)
8191 return name;
8192 }
8193 return get_user_var_name(xp, ++intidx);
8194}
8195
8196#endif /* FEAT_CMDL_COMPL */
8197
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008198#if defined(EBCDIC) || defined(PROTO)
8199/*
8200 * Compare struct fst by function name.
8201 */
8202 static int
8203compare_func_name(s1, s2)
8204 const void *s1;
8205 const void *s2;
8206{
8207 struct fst *p1 = (struct fst *)s1;
8208 struct fst *p2 = (struct fst *)s2;
8209
8210 return STRCMP(p1->f_name, p2->f_name);
8211}
8212
8213/*
8214 * Sort the function table by function name.
8215 * The sorting of the table above is ASCII dependant.
8216 * On machines using EBCDIC we have to sort it.
8217 */
8218 static void
8219sortFunctions()
8220{
8221 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8222
8223 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8224}
8225#endif
8226
8227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228/*
8229 * Find internal function in table above.
8230 * Return index, or -1 if not found
8231 */
8232 static int
8233find_internal_func(name)
8234 char_u *name; /* name of the function */
8235{
8236 int first = 0;
8237 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8238 int cmp;
8239 int x;
8240
8241 /*
8242 * Find the function name in the table. Binary search.
8243 */
8244 while (first <= last)
8245 {
8246 x = first + ((unsigned)(last - first) >> 1);
8247 cmp = STRCMP(name, functions[x].f_name);
8248 if (cmp < 0)
8249 last = x - 1;
8250 else if (cmp > 0)
8251 first = x + 1;
8252 else
8253 return x;
8254 }
8255 return -1;
8256}
8257
8258/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008259 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8260 * name it contains, otherwise return "name".
8261 */
8262 static char_u *
8263deref_func_name(name, lenp)
8264 char_u *name;
8265 int *lenp;
8266{
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 int cc;
8269
8270 cc = name[*lenp];
8271 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008272 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008273 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 {
8278 *lenp = 0;
8279 return (char_u *)""; /* just in case */
8280 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008281 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008282 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008283 }
8284
8285 return name;
8286}
8287
8288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 * Allocate a variable for the result of a function.
8290 * Return OK or FAIL.
8291 */
8292 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008293get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8294 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 char_u *name; /* name of the function */
8296 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 char_u **arg; /* argument, pointing to the '(' */
8299 linenr_T firstline; /* first line of range */
8300 linenr_T lastline; /* last line of range */
8301 int *doesrange; /* return: function handled range */
8302 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008303 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304{
8305 char_u *argp;
8306 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008307 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 int argcount = 0; /* number of arguments found */
8309
8310 /*
8311 * Get the arguments.
8312 */
8313 argp = *arg;
8314 while (argcount < MAX_FUNC_ARGS)
8315 {
8316 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8317 if (*argp == ')' || *argp == ',' || *argp == NUL)
8318 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8320 {
8321 ret = FAIL;
8322 break;
8323 }
8324 ++argcount;
8325 if (*argp != ',')
8326 break;
8327 }
8328 if (*argp == ')')
8329 ++argp;
8330 else
8331 ret = FAIL;
8332
8333 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008334 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008335 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 {
8338 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008339 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008341 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343
8344 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008345 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
8347 *arg = skipwhite(argp);
8348 return ret;
8349}
8350
8351
8352/*
8353 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008354 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008355 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 */
8357 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008358call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008359 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008360 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008364 typval_T *argvars; /* vars for arguments, must have "argcount"
8365 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 linenr_T firstline; /* first line of range */
8367 linenr_T lastline; /* last line of range */
8368 int *doesrange; /* return: function handled range */
8369 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008370 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373#define ERROR_UNKNOWN 0
8374#define ERROR_TOOMANY 1
8375#define ERROR_TOOFEW 2
8376#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008377#define ERROR_DICT 4
8378#define ERROR_NONE 5
8379#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 int error = ERROR_NONE;
8381 int i;
8382 int llen;
8383 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384#define FLEN_FIXED 40
8385 char_u fname_buf[FLEN_FIXED + 1];
8386 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008387 char_u *name;
8388
8389 /* Make a copy of the name, if it comes from a funcref variable it could
8390 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008391 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008392 if (name == NULL)
8393 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395 /*
8396 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8397 * Change <SNR>123_name() to K_SNR 123_name().
8398 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 llen = eval_fname_script(name);
8401 if (llen > 0)
8402 {
8403 fname_buf[0] = K_SPECIAL;
8404 fname_buf[1] = KS_EXTRA;
8405 fname_buf[2] = (int)KE_SNR;
8406 i = 3;
8407 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8408 {
8409 if (current_SID <= 0)
8410 error = ERROR_SCRIPT;
8411 else
8412 {
8413 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8414 i = (int)STRLEN(fname_buf);
8415 }
8416 }
8417 if (i + STRLEN(name + llen) < FLEN_FIXED)
8418 {
8419 STRCPY(fname_buf + i, name + llen);
8420 fname = fname_buf;
8421 }
8422 else
8423 {
8424 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8425 if (fname == NULL)
8426 error = ERROR_OTHER;
8427 else
8428 {
8429 mch_memmove(fname, fname_buf, (size_t)i);
8430 STRCPY(fname + i, name + llen);
8431 }
8432 }
8433 }
8434 else
8435 fname = name;
8436
8437 *doesrange = FALSE;
8438
8439
8440 /* execute the function if no errors detected and executing */
8441 if (evaluate && error == ERROR_NONE)
8442 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008443 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8444 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 error = ERROR_UNKNOWN;
8446
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008447 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {
8449 /*
8450 * User defined function.
8451 */
8452 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008455 /* Trigger FuncUndefined event, may load the function. */
8456 if (fp == NULL
8457 && apply_autocmds(EVENT_FUNCUNDEFINED,
8458 fname, fname, TRUE, NULL)
8459 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008461 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 fp = find_func(fname);
8463 }
8464#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008466 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008467 {
8468 /* loaded a package, search for the function again */
8469 fp = find_func(fname);
8470 }
8471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 if (fp != NULL)
8473 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008474 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008478 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008480 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008481 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 else
8483 {
8484 /*
8485 * Call the user function.
8486 * Save and restore search patterns, script variables and
8487 * redo buffer.
8488 */
8489 save_search_patterns();
8490 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008491 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008492 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008493 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008494 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8495 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8496 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008497 /* Function was unreferenced while being used, free it
8498 * now. */
8499 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 restoreRedobuff();
8501 restore_search_patterns();
8502 error = ERROR_NONE;
8503 }
8504 }
8505 }
8506 else
8507 {
8508 /*
8509 * Find the function name in the table, call its implementation.
8510 */
8511 i = find_internal_func(fname);
8512 if (i >= 0)
8513 {
8514 if (argcount < functions[i].f_min_argc)
8515 error = ERROR_TOOFEW;
8516 else if (argcount > functions[i].f_max_argc)
8517 error = ERROR_TOOMANY;
8518 else
8519 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 error = ERROR_NONE;
8523 }
8524 }
8525 }
8526 /*
8527 * The function call (or "FuncUndefined" autocommand sequence) might
8528 * have been aborted by an error, an interrupt, or an explicitly thrown
8529 * exception that has not been caught so far. This situation can be
8530 * tested for by calling aborting(). For an error in an internal
8531 * function or for the "E132" error in call_user_func(), however, the
8532 * throw point at which the "force_abort" flag (temporarily reset by
8533 * emsg()) is normally updated has not been reached yet. We need to
8534 * update that flag first to make aborting() reliable.
8535 */
8536 update_force_abort();
8537 }
8538 if (error == ERROR_NONE)
8539 ret = OK;
8540
8541 /*
8542 * Report an error unless the argument evaluation or function call has been
8543 * cancelled due to an aborting error, an interrupt, or an exception.
8544 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008545 if (!aborting())
8546 {
8547 switch (error)
8548 {
8549 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008550 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008551 break;
8552 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008553 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008554 break;
8555 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008556 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008557 name);
8558 break;
8559 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008560 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008561 name);
8562 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008563 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008564 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565 name);
8566 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 }
8568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 if (fname != name && fname != fname_buf)
8571 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008572 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
8574 return ret;
8575}
8576
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008577/*
8578 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008579 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008580 */
8581 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008582emsg_funcname(ermsg, name)
8583 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008584 char_u *name;
8585{
8586 char_u *p;
8587
8588 if (*name == K_SPECIAL)
8589 p = concat_str((char_u *)"<SNR>", name + 3);
8590 else
8591 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008592 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 if (p != name)
8594 vim_free(p);
8595}
8596
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008597/*
8598 * Return TRUE for a non-zero Number and a non-empty String.
8599 */
8600 static int
8601non_zero_arg(argvars)
8602 typval_T *argvars;
8603{
8604 return ((argvars[0].v_type == VAR_NUMBER
8605 && argvars[0].vval.v_number != 0)
8606 || (argvars[0].v_type == VAR_STRING
8607 && argvars[0].vval.v_string != NULL
8608 && *argvars[0].vval.v_string != NUL));
8609}
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611/*********************************************
8612 * Implementation of the built-in functions
8613 */
8614
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008615#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008616static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8617
8618/*
8619 * Get the float value of "argvars[0]" into "f".
8620 * Returns FAIL when the argument is not a Number or Float.
8621 */
8622 static int
8623get_float_arg(argvars, f)
8624 typval_T *argvars;
8625 float_T *f;
8626{
8627 if (argvars[0].v_type == VAR_FLOAT)
8628 {
8629 *f = argvars[0].vval.v_float;
8630 return OK;
8631 }
8632 if (argvars[0].v_type == VAR_NUMBER)
8633 {
8634 *f = (float_T)argvars[0].vval.v_number;
8635 return OK;
8636 }
8637 EMSG(_("E808: Number or Float required"));
8638 return FAIL;
8639}
8640
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008641/*
8642 * "abs(expr)" function
8643 */
8644 static void
8645f_abs(argvars, rettv)
8646 typval_T *argvars;
8647 typval_T *rettv;
8648{
8649 if (argvars[0].v_type == VAR_FLOAT)
8650 {
8651 rettv->v_type = VAR_FLOAT;
8652 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8653 }
8654 else
8655 {
8656 varnumber_T n;
8657 int error = FALSE;
8658
8659 n = get_tv_number_chk(&argvars[0], &error);
8660 if (error)
8661 rettv->vval.v_number = -1;
8662 else if (n > 0)
8663 rettv->vval.v_number = n;
8664 else
8665 rettv->vval.v_number = -n;
8666 }
8667}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008668
8669/*
8670 * "acos()" function
8671 */
8672 static void
8673f_acos(argvars, rettv)
8674 typval_T *argvars;
8675 typval_T *rettv;
8676{
8677 float_T f;
8678
8679 rettv->v_type = VAR_FLOAT;
8680 if (get_float_arg(argvars, &f) == OK)
8681 rettv->vval.v_float = acos(f);
8682 else
8683 rettv->vval.v_float = 0.0;
8684}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008685#endif
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008688 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 */
8690 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008691f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
Bram Moolenaar33570922005-01-25 22:26:29 +00008695 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008698 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008700 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008701 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008702 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008704 }
8705 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008706 EMSG(_(e_listreq));
8707}
8708
8709/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008710 * "and(expr, expr)" function
8711 */
8712 static void
8713f_and(argvars, rettv)
8714 typval_T *argvars;
8715 typval_T *rettv;
8716{
8717 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8718 & get_tv_number_chk(&argvars[1], NULL);
8719}
8720
8721/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722 * "append(lnum, string/list)" function
8723 */
8724 static void
8725f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 typval_T *argvars;
8727 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728{
8729 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008730 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 list_T *l = NULL;
8732 listitem_T *li = NULL;
8733 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008734 long added = 0;
8735
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736 lnum = get_tv_lnum(argvars);
8737 if (lnum >= 0
8738 && lnum <= curbuf->b_ml.ml_line_count
8739 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008740 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008741 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008742 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 l = argvars[1].vval.v_list;
8744 if (l == NULL)
8745 return;
8746 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008748 for (;;)
8749 {
8750 if (l == NULL)
8751 tv = &argvars[1]; /* append a string */
8752 else if (li == NULL)
8753 break; /* end of list */
8754 else
8755 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008756 line = get_tv_string_chk(tv);
8757 if (line == NULL) /* type error */
8758 {
8759 rettv->vval.v_number = 1; /* Failed */
8760 break;
8761 }
8762 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008763 ++added;
8764 if (l == NULL)
8765 break;
8766 li = li->li_next;
8767 }
8768
8769 appended_lines_mark(lnum, added);
8770 if (curwin->w_cursor.lnum > lnum)
8771 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 else
8774 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776
8777/*
8778 * "argc()" function
8779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008782 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786}
8787
8788/*
8789 * "argidx()" function
8790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008793 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797}
8798
8799/*
8800 * "argv(nr)" function
8801 */
8802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 typval_T *argvars;
8805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806{
8807 int idx;
8808
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008809 if (argvars[0].v_type != VAR_UNKNOWN)
8810 {
8811 idx = get_tv_number_chk(&argvars[0], NULL);
8812 if (idx >= 0 && idx < ARGCOUNT)
8813 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8814 else
8815 rettv->vval.v_string = NULL;
8816 rettv->v_type = VAR_STRING;
8817 }
8818 else if (rettv_list_alloc(rettv) == OK)
8819 for (idx = 0; idx < ARGCOUNT; ++idx)
8820 list_append_string(rettv->vval.v_list,
8821 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822}
8823
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008824#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008825/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008826 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008827 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008828 static void
8829f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008830 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008831 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008832{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008833 float_T f;
8834
8835 rettv->v_type = VAR_FLOAT;
8836 if (get_float_arg(argvars, &f) == OK)
8837 rettv->vval.v_float = asin(f);
8838 else
8839 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008840}
8841
8842/*
8843 * "atan()" function
8844 */
8845 static void
8846f_atan(argvars, rettv)
8847 typval_T *argvars;
8848 typval_T *rettv;
8849{
8850 float_T f;
8851
8852 rettv->v_type = VAR_FLOAT;
8853 if (get_float_arg(argvars, &f) == OK)
8854 rettv->vval.v_float = atan(f);
8855 else
8856 rettv->vval.v_float = 0.0;
8857}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008858
8859/*
8860 * "atan2()" function
8861 */
8862 static void
8863f_atan2(argvars, rettv)
8864 typval_T *argvars;
8865 typval_T *rettv;
8866{
8867 float_T fx, fy;
8868
8869 rettv->v_type = VAR_FLOAT;
8870 if (get_float_arg(argvars, &fx) == OK
8871 && get_float_arg(&argvars[1], &fy) == OK)
8872 rettv->vval.v_float = atan2(fx, fy);
8873 else
8874 rettv->vval.v_float = 0.0;
8875}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008876#endif
8877
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878/*
8879 * "browse(save, title, initdir, default)" function
8880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008883 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
8886#ifdef FEAT_BROWSE
8887 int save;
8888 char_u *title;
8889 char_u *initdir;
8890 char_u *defname;
8891 char_u buf[NUMBUFLEN];
8892 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008895 save = get_tv_number_chk(&argvars[0], &error);
8896 title = get_tv_string_chk(&argvars[1]);
8897 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8898 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008900 if (error || title == NULL || initdir == NULL || defname == NULL)
8901 rettv->vval.v_string = NULL;
8902 else
8903 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008904 do_browse(save ? BROWSE_SAVE : 0,
8905 title, defname, NULL, initdir, NULL, curbuf);
8906#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008908#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008910}
8911
8912/*
8913 * "browsedir(title, initdir)" function
8914 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008918 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008919{
8920#ifdef FEAT_BROWSE
8921 char_u *title;
8922 char_u *initdir;
8923 char_u buf[NUMBUFLEN];
8924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008925 title = get_tv_string_chk(&argvars[0]);
8926 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008928 if (title == NULL || initdir == NULL)
8929 rettv->vval.v_string = NULL;
8930 else
8931 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008932 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937}
8938
Bram Moolenaar33570922005-01-25 22:26:29 +00008939static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008940
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941/*
8942 * Find a buffer by number or exact name.
8943 */
8944 static buf_T *
8945find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008950 if (avar->v_type == VAR_NUMBER)
8951 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008952 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008955 if (buf == NULL)
8956 {
8957 /* No full path name match, try a match with a URL or a "nofile"
8958 * buffer, these don't use the full path. */
8959 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8960 if (buf->b_fname != NULL
8961 && (path_with_url(buf->b_fname)
8962#ifdef FEAT_QUICKFIX
8963 || bt_nofile(buf)
8964#endif
8965 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008966 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008967 break;
8968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 }
8970 return buf;
8971}
8972
8973/*
8974 * "bufexists(expr)" function
8975 */
8976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 typval_T *argvars;
8979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
8983
8984/*
8985 * "buflisted(expr)" function
8986 */
8987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 buf_T *buf;
8993
8994 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996}
8997
8998/*
8999 * "bufloaded(expr)" function
9000 */
9001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006 buf_T *buf;
9007
9008 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010}
9011
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009012static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009013
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014/*
9015 * Get buffer by number or pattern.
9016 */
9017 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009018get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009020 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009022 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 int save_magic;
9024 char_u *save_cpo;
9025 buf_T *buf;
9026
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 if (tv->v_type == VAR_NUMBER)
9028 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009029 if (tv->v_type != VAR_STRING)
9030 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 if (name == NULL || *name == NUL)
9032 return curbuf;
9033 if (name[0] == '$' && name[1] == NUL)
9034 return lastbuf;
9035
9036 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9037 save_magic = p_magic;
9038 p_magic = TRUE;
9039 save_cpo = p_cpo;
9040 p_cpo = (char_u *)"";
9041
9042 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009043 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044
9045 p_magic = save_magic;
9046 p_cpo = save_cpo;
9047
9048 /* If not found, try expanding the name, like done for bufexists(). */
9049 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052 return buf;
9053}
9054
9055/*
9056 * "bufname(expr)" function
9057 */
9058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009060 typval_T *argvars;
9061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 buf_T *buf;
9064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009067 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 --emsg_off;
9074}
9075
9076/*
9077 * "bufnr(expr)" function
9078 */
9079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 typval_T *argvars;
9082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
9084 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009085 int error = FALSE;
9086 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009088 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009090 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009091 --emsg_off;
9092
9093 /* If the buffer isn't found and the second argument is not zero create a
9094 * new buffer. */
9095 if (buf == NULL
9096 && argvars[1].v_type != VAR_UNKNOWN
9097 && get_tv_number_chk(&argvars[1], &error) != 0
9098 && !error
9099 && (name = get_tv_string_chk(&argvars[0])) != NULL
9100 && !error)
9101 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107}
9108
9109/*
9110 * "bufwinnr(nr)" function
9111 */
9112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009114 typval_T *argvars;
9115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116{
9117#ifdef FEAT_WINDOWS
9118 win_T *wp;
9119 int winnr = 0;
9120#endif
9121 buf_T *buf;
9122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009125 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126#ifdef FEAT_WINDOWS
9127 for (wp = firstwin; wp; wp = wp->w_next)
9128 {
9129 ++winnr;
9130 if (wp->w_buffer == buf)
9131 break;
9132 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136#endif
9137 --emsg_off;
9138}
9139
9140/*
9141 * "byte2line(byte)" function
9142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009145 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147{
9148#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#else
9151 long boff = 0;
9152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009153 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 (linenr_T)0, &boff);
9159#endif
9160}
9161
9162/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009163 * "byteidx()" function
9164 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *argvars;
9168 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009169{
9170#ifdef FEAT_MBYTE
9171 char_u *t;
9172#endif
9173 char_u *str;
9174 long idx;
9175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 str = get_tv_string_chk(&argvars[0]);
9177 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009180 return;
9181
9182#ifdef FEAT_MBYTE
9183 t = str;
9184 for ( ; idx > 0; idx--)
9185 {
9186 if (*t == NUL) /* EOL reached */
9187 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009188 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009189 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009190 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009191#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009192 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009194#endif
9195}
9196
Bram Moolenaardb913952012-06-29 12:54:53 +02009197 int
9198func_call(name, args, selfdict, rettv)
9199 char_u *name;
9200 typval_T *args;
9201 dict_T *selfdict;
9202 typval_T *rettv;
9203{
9204 listitem_T *item;
9205 typval_T argv[MAX_FUNC_ARGS + 1];
9206 int argc = 0;
9207 int dummy;
9208 int r = 0;
9209
9210 for (item = args->vval.v_list->lv_first; item != NULL;
9211 item = item->li_next)
9212 {
9213 if (argc == MAX_FUNC_ARGS)
9214 {
9215 EMSG(_("E699: Too many arguments"));
9216 break;
9217 }
9218 /* Make a copy of each argument. This is needed to be able to set
9219 * v_lock to VAR_FIXED in the copy without changing the original list.
9220 */
9221 copy_tv(&item->li_tv, &argv[argc++]);
9222 }
9223
9224 if (item == NULL)
9225 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9226 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9227 &dummy, TRUE, selfdict);
9228
9229 /* Free the arguments. */
9230 while (argc > 0)
9231 clear_tv(&argv[--argc]);
9232
9233 return r;
9234}
9235
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009236/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009237 * "call(func, arglist)" function
9238 */
9239 static void
9240f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009241 typval_T *argvars;
9242 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009243{
9244 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009245 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009246
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009247 if (argvars[1].v_type != VAR_LIST)
9248 {
9249 EMSG(_(e_listreq));
9250 return;
9251 }
9252 if (argvars[1].vval.v_list == NULL)
9253 return;
9254
9255 if (argvars[0].v_type == VAR_FUNC)
9256 func = argvars[0].vval.v_string;
9257 else
9258 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009259 if (*func == NUL)
9260 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009261
Bram Moolenaare9a41262005-01-15 22:18:47 +00009262 if (argvars[2].v_type != VAR_UNKNOWN)
9263 {
9264 if (argvars[2].v_type != VAR_DICT)
9265 {
9266 EMSG(_(e_dictreq));
9267 return;
9268 }
9269 selfdict = argvars[2].vval.v_dict;
9270 }
9271
Bram Moolenaardb913952012-06-29 12:54:53 +02009272 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009273}
9274
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009275#ifdef FEAT_FLOAT
9276/*
9277 * "ceil({float})" function
9278 */
9279 static void
9280f_ceil(argvars, rettv)
9281 typval_T *argvars;
9282 typval_T *rettv;
9283{
9284 float_T f;
9285
9286 rettv->v_type = VAR_FLOAT;
9287 if (get_float_arg(argvars, &f) == OK)
9288 rettv->vval.v_float = ceil(f);
9289 else
9290 rettv->vval.v_float = 0.0;
9291}
9292#endif
9293
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009294/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009295 * "changenr()" function
9296 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009297 static void
9298f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009299 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009300 typval_T *rettv;
9301{
9302 rettv->vval.v_number = curbuf->b_u_seq_cur;
9303}
9304
9305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 * "char2nr(string)" function
9307 */
9308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009310 typval_T *argvars;
9311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313#ifdef FEAT_MBYTE
9314 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009315 {
9316 int utf8 = 0;
9317
9318 if (argvars[1].v_type != VAR_UNKNOWN)
9319 utf8 = get_tv_number_chk(&argvars[1], NULL);
9320
9321 if (utf8)
9322 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9323 else
9324 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 else
9327#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329}
9330
9331/*
9332 * "cindent(lnum)" function
9333 */
9334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009336 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338{
9339#ifdef FEAT_CINDENT
9340 pos_T pos;
9341 linenr_T lnum;
9342
9343 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9346 {
9347 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 curwin->w_cursor = pos;
9350 }
9351 else
9352#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354}
9355
9356/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009357 * "clearmatches()" function
9358 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009359 static void
9360f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009361 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009362 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009363{
9364#ifdef FEAT_SEARCH_EXTRA
9365 clear_matches(curwin);
9366#endif
9367}
9368
9369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 * "col(string)" function
9371 */
9372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009374 typval_T *argvars;
9375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
9377 colnr_T col = 0;
9378 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009379 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009381 fp = var2fpos(&argvars[0], FALSE, &fnum);
9382 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 {
9384 if (fp->col == MAXCOL)
9385 {
9386 /* '> can be MAXCOL, get the length of the line then */
9387 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009388 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 else
9390 col = MAXCOL;
9391 }
9392 else
9393 {
9394 col = fp->col + 1;
9395#ifdef FEAT_VIRTUALEDIT
9396 /* col(".") when the cursor is on the NUL at the end of the line
9397 * because of "coladd" can be seen as an extra column. */
9398 if (virtual_active() && fp == &curwin->w_cursor)
9399 {
9400 char_u *p = ml_get_cursor();
9401
9402 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9403 curwin->w_virtcol - curwin->w_cursor.coladd))
9404 {
9405# ifdef FEAT_MBYTE
9406 int l;
9407
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009408 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 col += l;
9410# else
9411 if (*p != NUL && p[1] == NUL)
9412 ++col;
9413# endif
9414 }
9415 }
9416#endif
9417 }
9418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420}
9421
Bram Moolenaar572cb562005-08-05 21:35:02 +00009422#if defined(FEAT_INS_EXPAND)
9423/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009424 * "complete()" function
9425 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009426 static void
9427f_complete(argvars, rettv)
9428 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009429 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009430{
9431 int startcol;
9432
9433 if ((State & INSERT) == 0)
9434 {
9435 EMSG(_("E785: complete() can only be used in Insert mode"));
9436 return;
9437 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009438
9439 /* Check for undo allowed here, because if something was already inserted
9440 * the line was already saved for undo and this check isn't done. */
9441 if (!undo_allowed())
9442 return;
9443
Bram Moolenaarade00832006-03-10 21:46:58 +00009444 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9445 {
9446 EMSG(_(e_invarg));
9447 return;
9448 }
9449
9450 startcol = get_tv_number_chk(&argvars[0], NULL);
9451 if (startcol <= 0)
9452 return;
9453
9454 set_completion(startcol - 1, argvars[1].vval.v_list);
9455}
9456
9457/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009458 * "complete_add()" function
9459 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009460 static void
9461f_complete_add(argvars, rettv)
9462 typval_T *argvars;
9463 typval_T *rettv;
9464{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009465 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009466}
9467
9468/*
9469 * "complete_check()" function
9470 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009471 static void
9472f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009473 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009474 typval_T *rettv;
9475{
9476 int saved = RedrawingDisabled;
9477
9478 RedrawingDisabled = 0;
9479 ins_compl_check_keys(0);
9480 rettv->vval.v_number = compl_interrupted;
9481 RedrawingDisabled = saved;
9482}
9483#endif
9484
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485/*
9486 * "confirm(message, buttons[, default [, type]])" function
9487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009490 typval_T *argvars UNUSED;
9491 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
9493#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9494 char_u *message;
9495 char_u *buttons = NULL;
9496 char_u buf[NUMBUFLEN];
9497 char_u buf2[NUMBUFLEN];
9498 int def = 1;
9499 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009500 char_u *typestr;
9501 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503 message = get_tv_string_chk(&argvars[0]);
9504 if (message == NULL)
9505 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009506 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009508 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9509 if (buttons == NULL)
9510 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009511 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009514 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009516 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9517 if (typestr == NULL)
9518 error = TRUE;
9519 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009521 switch (TOUPPER_ASC(*typestr))
9522 {
9523 case 'E': type = VIM_ERROR; break;
9524 case 'Q': type = VIM_QUESTION; break;
9525 case 'I': type = VIM_INFO; break;
9526 case 'W': type = VIM_WARNING; break;
9527 case 'G': type = VIM_GENERIC; break;
9528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 }
9530 }
9531 }
9532 }
9533
9534 if (buttons == NULL || *buttons == NUL)
9535 buttons = (char_u *)_("&Ok");
9536
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009537 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009539 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540#endif
9541}
9542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009543/*
9544 * "copy()" function
9545 */
9546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 typval_T *argvars;
9549 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009550{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009551 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009554#ifdef FEAT_FLOAT
9555/*
9556 * "cos()" function
9557 */
9558 static void
9559f_cos(argvars, rettv)
9560 typval_T *argvars;
9561 typval_T *rettv;
9562{
9563 float_T f;
9564
9565 rettv->v_type = VAR_FLOAT;
9566 if (get_float_arg(argvars, &f) == OK)
9567 rettv->vval.v_float = cos(f);
9568 else
9569 rettv->vval.v_float = 0.0;
9570}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009571
9572/*
9573 * "cosh()" function
9574 */
9575 static void
9576f_cosh(argvars, rettv)
9577 typval_T *argvars;
9578 typval_T *rettv;
9579{
9580 float_T f;
9581
9582 rettv->v_type = VAR_FLOAT;
9583 if (get_float_arg(argvars, &f) == OK)
9584 rettv->vval.v_float = cosh(f);
9585 else
9586 rettv->vval.v_float = 0.0;
9587}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009588#endif
9589
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009591 * "count()" function
9592 */
9593 static void
9594f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009595 typval_T *argvars;
9596 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009597{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009598 long n = 0;
9599 int ic = FALSE;
9600
Bram Moolenaare9a41262005-01-15 22:18:47 +00009601 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009602 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009603 listitem_T *li;
9604 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009606
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607 if ((l = argvars[0].vval.v_list) != NULL)
9608 {
9609 li = l->lv_first;
9610 if (argvars[2].v_type != VAR_UNKNOWN)
9611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612 int error = FALSE;
9613
9614 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 if (argvars[3].v_type != VAR_UNKNOWN)
9616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009617 idx = get_tv_number_chk(&argvars[3], &error);
9618 if (!error)
9619 {
9620 li = list_find(l, idx);
9621 if (li == NULL)
9622 EMSGN(_(e_listidx), idx);
9623 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009625 if (error)
9626 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009627 }
9628
9629 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009630 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009631 ++n;
9632 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009633 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009634 else if (argvars[0].v_type == VAR_DICT)
9635 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009636 int todo;
9637 dict_T *d;
9638 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009639
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009640 if ((d = argvars[0].vval.v_dict) != NULL)
9641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 int error = FALSE;
9643
Bram Moolenaare9a41262005-01-15 22:18:47 +00009644 if (argvars[2].v_type != VAR_UNKNOWN)
9645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009647 if (argvars[3].v_type != VAR_UNKNOWN)
9648 EMSG(_(e_invarg));
9649 }
9650
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009651 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009652 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009653 {
9654 if (!HASHITEM_EMPTY(hi))
9655 {
9656 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009657 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009658 ++n;
9659 }
9660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 }
9662 }
9663 else
9664 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009665 rettv->vval.v_number = n;
9666}
9667
9668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9670 *
9671 * Checks the existence of a cscope connection.
9672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009675 typval_T *argvars UNUSED;
9676 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
9678#ifdef FEAT_CSCOPE
9679 int num = 0;
9680 char_u *dbpath = NULL;
9681 char_u *prepend = NULL;
9682 char_u buf[NUMBUFLEN];
9683
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009684 if (argvars[0].v_type != VAR_UNKNOWN
9685 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009687 num = (int)get_tv_number(&argvars[0]);
9688 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 }
9692
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694#endif
9695}
9696
9697/*
9698 * "cursor(lnum, col)" function
9699 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009700 * Moves the cursor to the specified line and column.
9701 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 typval_T *argvars;
9706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009709#ifdef FEAT_VIRTUALEDIT
9710 long coladd = 0;
9711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009713 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009714 if (argvars[1].v_type == VAR_UNKNOWN)
9715 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009716 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009717
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009718 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009719 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 line = pos.lnum;
9721 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009722#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009723 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009724#endif
9725 }
9726 else
9727 {
9728 line = get_tv_lnum(argvars);
9729 col = get_tv_number_chk(&argvars[1], NULL);
9730#ifdef FEAT_VIRTUALEDIT
9731 if (argvars[2].v_type != VAR_UNKNOWN)
9732 coladd = get_tv_number_chk(&argvars[2], NULL);
9733#endif
9734 }
9735 if (line < 0 || col < 0
9736#ifdef FEAT_VIRTUALEDIT
9737 || coladd < 0
9738#endif
9739 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009740 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (line > 0)
9742 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 if (col > 0)
9744 curwin->w_cursor.col = col - 1;
9745#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009746 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747#endif
9748
9749 /* Make sure the cursor is in a valid position. */
9750 check_cursor();
9751#ifdef FEAT_MBYTE
9752 /* Correct cursor for multi-byte character. */
9753 if (has_mbyte)
9754 mb_adjust_cursor();
9755#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009756
9757 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009758 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759}
9760
9761/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009762 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 */
9764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009766 typval_T *argvars;
9767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009769 int noref = 0;
9770
9771 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009773 if (noref < 0 || noref > 1)
9774 EMSG(_(e_invarg));
9775 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009776 {
9777 current_copyID += COPYID_INC;
9778 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780}
9781
9782/*
9783 * "delete()" function
9784 */
9785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009787 typval_T *argvars;
9788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
9790 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794}
9795
9796/*
9797 * "did_filetype()" function
9798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009801 typval_T *argvars UNUSED;
9802 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803{
9804#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806#endif
9807}
9808
9809/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009810 * "diff_filler()" function
9811 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009814 typval_T *argvars UNUSED;
9815 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009816{
9817#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009819#endif
9820}
9821
9822/*
9823 * "diff_hlID()" function
9824 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009827 typval_T *argvars UNUSED;
9828 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009829{
9830#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009832 static linenr_T prev_lnum = 0;
9833 static int changedtick = 0;
9834 static int fnum = 0;
9835 static int change_start = 0;
9836 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009837 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009838 int filler_lines;
9839 int col;
9840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009841 if (lnum < 0) /* ignore type error in {lnum} arg */
9842 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009843 if (lnum != prev_lnum
9844 || changedtick != curbuf->b_changedtick
9845 || fnum != curbuf->b_fnum)
9846 {
9847 /* New line, buffer, change: need to get the values. */
9848 filler_lines = diff_check(curwin, lnum);
9849 if (filler_lines < 0)
9850 {
9851 if (filler_lines == -1)
9852 {
9853 change_start = MAXCOL;
9854 change_end = -1;
9855 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9856 hlID = HLF_ADD; /* added line */
9857 else
9858 hlID = HLF_CHD; /* changed line */
9859 }
9860 else
9861 hlID = HLF_ADD; /* added line */
9862 }
9863 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009864 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009865 prev_lnum = lnum;
9866 changedtick = curbuf->b_changedtick;
9867 fnum = curbuf->b_fnum;
9868 }
9869
9870 if (hlID == HLF_CHD || hlID == HLF_TXD)
9871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009872 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009873 if (col >= change_start && col <= change_end)
9874 hlID = HLF_TXD; /* changed text */
9875 else
9876 hlID = HLF_CHD; /* changed line */
9877 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009878 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009879#endif
9880}
9881
9882/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009883 * "empty({expr})" function
9884 */
9885 static void
9886f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009887 typval_T *argvars;
9888 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009889{
9890 int n;
9891
9892 switch (argvars[0].v_type)
9893 {
9894 case VAR_STRING:
9895 case VAR_FUNC:
9896 n = argvars[0].vval.v_string == NULL
9897 || *argvars[0].vval.v_string == NUL;
9898 break;
9899 case VAR_NUMBER:
9900 n = argvars[0].vval.v_number == 0;
9901 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009902#ifdef FEAT_FLOAT
9903 case VAR_FLOAT:
9904 n = argvars[0].vval.v_float == 0.0;
9905 break;
9906#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009907 case VAR_LIST:
9908 n = argvars[0].vval.v_list == NULL
9909 || argvars[0].vval.v_list->lv_first == NULL;
9910 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 case VAR_DICT:
9912 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009915 default:
9916 EMSG2(_(e_intern2), "f_empty()");
9917 n = 0;
9918 }
9919
9920 rettv->vval.v_number = n;
9921}
9922
9923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 * "escape({string}, {chars})" function
9925 */
9926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009928 typval_T *argvars;
9929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930{
9931 char_u buf[NUMBUFLEN];
9932
Bram Moolenaar758711c2005-02-02 23:11:38 +00009933 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9934 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936}
9937
9938/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009939 * "eval()" function
9940 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009941 static void
9942f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009943 typval_T *argvars;
9944 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009945{
9946 char_u *s;
9947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 s = get_tv_string_chk(&argvars[0]);
9949 if (s != NULL)
9950 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9953 {
9954 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009955 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009956 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009957 else if (*s != NUL)
9958 EMSG(_(e_trailing));
9959}
9960
9961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 * "eventhandler()" function
9963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009966 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970}
9971
9972/*
9973 * "executable()" function
9974 */
9975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009977 typval_T *argvars;
9978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981}
9982
9983/*
9984 * "exists()" function
9985 */
9986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009988 typval_T *argvars;
9989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990{
9991 char_u *p;
9992 char_u *name;
9993 int n = FALSE;
9994 int len = 0;
9995
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009996 no_autoload = TRUE;
9997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 if (*p == '$') /* environment variable */
10000 {
10001 /* first try "normal" environment variables (fast) */
10002 if (mch_getenv(p + 1) != NULL)
10003 n = TRUE;
10004 else
10005 {
10006 /* try expanding things like $VIM and ${HOME} */
10007 p = expand_env_save(p);
10008 if (p != NULL && *p != '$')
10009 n = TRUE;
10010 vim_free(p);
10011 }
10012 }
10013 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010016 if (*skipwhite(p) != NUL)
10017 n = FALSE; /* trailing garbage */
10018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 else if (*p == '*') /* internal or user defined function */
10020 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010021 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 }
10023 else if (*p == ':')
10024 {
10025 n = cmd_exists(p + 1);
10026 }
10027 else if (*p == '#')
10028 {
10029#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010030 if (p[1] == '#')
10031 n = autocmd_supported(p + 2);
10032 else
10033 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034#endif
10035 }
10036 else /* internal variable */
10037 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010038 char_u *tofree;
10039 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010041 /* get_name_len() takes care of expanding curly braces */
10042 name = p;
10043 len = get_name_len(&p, &tofree, TRUE, FALSE);
10044 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010046 if (tofree != NULL)
10047 name = tofree;
10048 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10049 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010051 /* handle d.key, l[idx], f(expr) */
10052 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10053 if (n)
10054 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 }
10056 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010057 if (*p != NUL)
10058 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010060 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 }
10062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010064
10065 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066}
10067
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010068#ifdef FEAT_FLOAT
10069/*
10070 * "exp()" function
10071 */
10072 static void
10073f_exp(argvars, rettv)
10074 typval_T *argvars;
10075 typval_T *rettv;
10076{
10077 float_T f;
10078
10079 rettv->v_type = VAR_FLOAT;
10080 if (get_float_arg(argvars, &f) == OK)
10081 rettv->vval.v_float = exp(f);
10082 else
10083 rettv->vval.v_float = 0.0;
10084}
10085#endif
10086
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087/*
10088 * "expand()" function
10089 */
10090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *argvars;
10093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094{
10095 char_u *s;
10096 int len;
10097 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010098 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010101 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010104 if (argvars[1].v_type != VAR_UNKNOWN
10105 && argvars[2].v_type != VAR_UNKNOWN
10106 && get_tv_number_chk(&argvars[2], &error)
10107 && !error)
10108 {
10109 rettv->v_type = VAR_LIST;
10110 rettv->vval.v_list = NULL;
10111 }
10112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 if (*s == '%' || *s == '#' || *s == '<')
10115 {
10116 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010117 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010119 if (rettv->v_type == VAR_LIST)
10120 {
10121 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10122 list_append_string(rettv->vval.v_list, result, -1);
10123 else
10124 vim_free(result);
10125 }
10126 else
10127 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 }
10129 else
10130 {
10131 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010132 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 if (argvars[1].v_type != VAR_UNKNOWN
10134 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010135 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010136 if (!error)
10137 {
10138 ExpandInit(&xpc);
10139 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010140 if (p_wic)
10141 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010142 if (rettv->v_type == VAR_STRING)
10143 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10144 options, WILD_ALL);
10145 else if (rettv_list_alloc(rettv) != FAIL)
10146 {
10147 int i;
10148
10149 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10150 for (i = 0; i < xpc.xp_numfiles; i++)
10151 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10152 ExpandCleanup(&xpc);
10153 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154 }
10155 else
10156 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 }
10158}
10159
10160/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010161 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010163 */
10164 static void
10165f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010166 typval_T *argvars;
10167 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010168{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010169 char *arg_errmsg = N_("extend() argument");
10170
Bram Moolenaare9a41262005-01-15 22:18:47 +000010171 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010172 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010173 list_T *l1, *l2;
10174 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010175 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010177
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 l1 = argvars[0].vval.v_list;
10179 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010180 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010181 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010182 {
10183 if (argvars[2].v_type != VAR_UNKNOWN)
10184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 before = get_tv_number_chk(&argvars[2], &error);
10186 if (error)
10187 return; /* type error; errmsg already given */
10188
Bram Moolenaar758711c2005-02-02 23:11:38 +000010189 if (before == l1->lv_len)
10190 item = NULL;
10191 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010192 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010193 item = list_find(l1, before);
10194 if (item == NULL)
10195 {
10196 EMSGN(_(e_listidx), before);
10197 return;
10198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 }
10200 }
10201 else
10202 item = NULL;
10203 list_extend(l1, l2, item);
10204
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 copy_tv(&argvars[0], rettv);
10206 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010208 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10209 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010210 dict_T *d1, *d2;
10211 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 char_u *action;
10213 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010214 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010215 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216
10217 d1 = argvars[0].vval.v_dict;
10218 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010219 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010220 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010221 {
10222 /* Check the third argument. */
10223 if (argvars[2].v_type != VAR_UNKNOWN)
10224 {
10225 static char *(av[]) = {"keep", "force", "error"};
10226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 action = get_tv_string_chk(&argvars[2]);
10228 if (action == NULL)
10229 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 for (i = 0; i < 3; ++i)
10231 if (STRCMP(action, av[i]) == 0)
10232 break;
10233 if (i == 3)
10234 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010235 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 return;
10237 }
10238 }
10239 else
10240 action = (char_u *)"force";
10241
10242 /* Go over all entries in the second dict and add them to the
10243 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010244 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010245 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010246 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010247 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010249 --todo;
10250 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010251 if (d1->dv_scope != 0)
10252 {
10253 /* Disallow replacing a builtin function in l: and g:.
10254 * Check the key to be valid when adding to any
10255 * scope. */
10256 if (d1->dv_scope == VAR_DEF_SCOPE
10257 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10258 && var_check_func_name(hi2->hi_key,
10259 di1 == NULL))
10260 break;
10261 if (!valid_varname(hi2->hi_key))
10262 break;
10263 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010264 if (di1 == NULL)
10265 {
10266 di1 = dictitem_copy(HI2DI(hi2));
10267 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10268 dictitem_free(di1);
10269 }
10270 else if (*action == 'e')
10271 {
10272 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10273 break;
10274 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010275 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010276 {
10277 clear_tv(&di1->di_tv);
10278 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10279 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 }
10281 }
10282
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 copy_tv(&argvars[0], rettv);
10284 }
10285 }
10286 else
10287 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010288}
10289
10290/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010291 * "feedkeys()" function
10292 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010293 static void
10294f_feedkeys(argvars, rettv)
10295 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010296 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010297{
10298 int remap = TRUE;
10299 char_u *keys, *flags;
10300 char_u nbuf[NUMBUFLEN];
10301 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010302 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010303
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010304 /* This is not allowed in the sandbox. If the commands would still be
10305 * executed in the sandbox it would be OK, but it probably happens later,
10306 * when "sandbox" is no longer set. */
10307 if (check_secure())
10308 return;
10309
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010310 keys = get_tv_string(&argvars[0]);
10311 if (*keys != NUL)
10312 {
10313 if (argvars[1].v_type != VAR_UNKNOWN)
10314 {
10315 flags = get_tv_string_buf(&argvars[1], nbuf);
10316 for ( ; *flags != NUL; ++flags)
10317 {
10318 switch (*flags)
10319 {
10320 case 'n': remap = FALSE; break;
10321 case 'm': remap = TRUE; break;
10322 case 't': typed = TRUE; break;
10323 }
10324 }
10325 }
10326
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010327 /* Need to escape K_SPECIAL and CSI before putting the string in the
10328 * typeahead buffer. */
10329 keys_esc = vim_strsave_escape_csi(keys);
10330 if (keys_esc != NULL)
10331 {
10332 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010333 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010334 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010335 if (vgetc_busy)
10336 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010337 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010338 }
10339}
10340
10341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 * "filereadable()" function
10343 */
10344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010345f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010346 typval_T *argvars;
10347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010349 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 char_u *p;
10351 int n;
10352
Bram Moolenaarc236c162008-07-13 17:41:49 +000010353#ifndef O_NONBLOCK
10354# define O_NONBLOCK 0
10355#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010356 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010357 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10358 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 {
10360 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010361 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 }
10363 else
10364 n = FALSE;
10365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367}
10368
10369/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010370 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 * rights to write into.
10372 */
10373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010374f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010375 typval_T *argvars;
10376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010378 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379}
10380
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010381static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010382
10383 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010384findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010385 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010386 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010387 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010388{
10389#ifdef FEAT_SEARCHPATH
10390 char_u *fname;
10391 char_u *fresult = NULL;
10392 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10393 char_u *p;
10394 char_u pathbuf[NUMBUFLEN];
10395 int count = 1;
10396 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010397 int error = FALSE;
10398#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010399
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010400 rettv->vval.v_string = NULL;
10401 rettv->v_type = VAR_STRING;
10402
10403#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010404 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010405
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010408 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10409 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010410 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 else
10412 {
10413 if (*p != NUL)
10414 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010417 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010419 }
10420
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010421 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10422 error = TRUE;
10423
10424 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 do
10427 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010428 if (rettv->v_type == VAR_STRING)
10429 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 fresult = find_file_in_path_option(first ? fname : NULL,
10431 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010432 0, first, path,
10433 find_what,
10434 curbuf->b_ffname,
10435 find_what == FINDFILE_DIR
10436 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010437 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010438
10439 if (fresult != NULL && rettv->v_type == VAR_LIST)
10440 list_append_string(rettv->vval.v_list, fresult, -1);
10441
10442 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010444
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010445 if (rettv->v_type == VAR_STRING)
10446 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010447#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010448}
10449
Bram Moolenaar33570922005-01-25 22:26:29 +000010450static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10451static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010452
10453/*
10454 * Implementation of map() and filter().
10455 */
10456 static void
10457filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010458 typval_T *argvars;
10459 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010460 int map;
10461{
10462 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010463 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010464 listitem_T *li, *nli;
10465 list_T *l = NULL;
10466 dictitem_T *di;
10467 hashtab_T *ht;
10468 hashitem_T *hi;
10469 dict_T *d = NULL;
10470 typval_T save_val;
10471 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010472 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010473 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010474 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10475 char *arg_errmsg = (map ? N_("map() argument")
10476 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010477 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010478 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010479
Bram Moolenaare9a41262005-01-15 22:18:47 +000010480 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010481 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010482 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010483 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 return;
10485 }
10486 else if (argvars[0].v_type == VAR_DICT)
10487 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010488 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010489 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010490 return;
10491 }
10492 else
10493 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010494 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010495 return;
10496 }
10497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010498 expr = get_tv_string_buf_chk(&argvars[1], buf);
10499 /* On type errors, the preceding call has already displayed an error
10500 * message. Avoid a misleading error message for an empty string that
10501 * was not passed as argument. */
10502 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010504 prepare_vimvar(VV_VAL, &save_val);
10505 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010506
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010507 /* We reset "did_emsg" to be able to detect whether an error
10508 * occurred during evaluation of the expression. */
10509 save_did_emsg = did_emsg;
10510 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010511
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010512 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 vimvars[VV_KEY].vv_type = VAR_STRING;
10516
10517 ht = &d->dv_hashtab;
10518 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010519 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010520 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 if (!HASHITEM_EMPTY(hi))
10523 {
10524 --todo;
10525 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010526 if (tv_check_lock(di->di_tv.v_lock,
10527 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010528 break;
10529 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010530 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010531 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 break;
10533 if (!map && rem)
10534 dictitem_remove(d, di);
10535 clear_tv(&vimvars[VV_KEY].vv_tv);
10536 }
10537 }
10538 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 }
10540 else
10541 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010542 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 for (li = l->lv_first; li != NULL; li = nli)
10545 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010546 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010547 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010548 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010549 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010550 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010551 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010552 break;
10553 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010555 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010556 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010557 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010558
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010559 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010561
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010562 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010563 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564
10565 copy_tv(&argvars[0], rettv);
10566}
10567
10568 static int
10569filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010570 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010571 char_u *expr;
10572 int map;
10573 int *remp;
10574{
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010577 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578
Bram Moolenaar33570922005-01-25 22:26:29 +000010579 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 s = expr;
10581 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010582 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 if (*s != NUL) /* check for trailing chars after expr */
10584 {
10585 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010586 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 }
10588 if (map)
10589 {
10590 /* map(): replace the list item value */
10591 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010592 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 *tv = rettv;
10594 }
10595 else
10596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 int error = FALSE;
10598
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010602 /* On type error, nothing has been removed; return FAIL to stop the
10603 * loop. The error message was given by get_tv_number_chk(). */
10604 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010605 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010607 retval = OK;
10608theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010609 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010610 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010611}
10612
10613/*
10614 * "filter()" function
10615 */
10616 static void
10617f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010618 typval_T *argvars;
10619 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010620{
10621 filter_map(argvars, rettv, FALSE);
10622}
10623
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010625 * "finddir({fname}[, {path}[, {count}]])" function
10626 */
10627 static void
10628f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010629 typval_T *argvars;
10630 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010631{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010632 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010633}
10634
10635/*
10636 * "findfile({fname}[, {path}[, {count}]])" function
10637 */
10638 static void
10639f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010640 typval_T *argvars;
10641 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010642{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010643 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010644}
10645
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010646#ifdef FEAT_FLOAT
10647/*
10648 * "float2nr({float})" function
10649 */
10650 static void
10651f_float2nr(argvars, rettv)
10652 typval_T *argvars;
10653 typval_T *rettv;
10654{
10655 float_T f;
10656
10657 if (get_float_arg(argvars, &f) == OK)
10658 {
10659 if (f < -0x7fffffff)
10660 rettv->vval.v_number = -0x7fffffff;
10661 else if (f > 0x7fffffff)
10662 rettv->vval.v_number = 0x7fffffff;
10663 else
10664 rettv->vval.v_number = (varnumber_T)f;
10665 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010666}
10667
10668/*
10669 * "floor({float})" function
10670 */
10671 static void
10672f_floor(argvars, rettv)
10673 typval_T *argvars;
10674 typval_T *rettv;
10675{
10676 float_T f;
10677
10678 rettv->v_type = VAR_FLOAT;
10679 if (get_float_arg(argvars, &f) == OK)
10680 rettv->vval.v_float = floor(f);
10681 else
10682 rettv->vval.v_float = 0.0;
10683}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010684
10685/*
10686 * "fmod()" function
10687 */
10688 static void
10689f_fmod(argvars, rettv)
10690 typval_T *argvars;
10691 typval_T *rettv;
10692{
10693 float_T fx, fy;
10694
10695 rettv->v_type = VAR_FLOAT;
10696 if (get_float_arg(argvars, &fx) == OK
10697 && get_float_arg(&argvars[1], &fy) == OK)
10698 rettv->vval.v_float = fmod(fx, fy);
10699 else
10700 rettv->vval.v_float = 0.0;
10701}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010702#endif
10703
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010705 * "fnameescape({string})" function
10706 */
10707 static void
10708f_fnameescape(argvars, rettv)
10709 typval_T *argvars;
10710 typval_T *rettv;
10711{
10712 rettv->vval.v_string = vim_strsave_fnameescape(
10713 get_tv_string(&argvars[0]), FALSE);
10714 rettv->v_type = VAR_STRING;
10715}
10716
10717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 * "fnamemodify({fname}, {mods})" function
10719 */
10720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010722 typval_T *argvars;
10723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724{
10725 char_u *fname;
10726 char_u *mods;
10727 int usedlen = 0;
10728 int len;
10729 char_u *fbuf = NULL;
10730 char_u buf[NUMBUFLEN];
10731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 fname = get_tv_string_chk(&argvars[0]);
10733 mods = get_tv_string_buf_chk(&argvars[1], buf);
10734 if (fname == NULL || mods == NULL)
10735 fname = NULL;
10736 else
10737 {
10738 len = (int)STRLEN(fname);
10739 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010742 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 vim_free(fbuf);
10748}
10749
Bram Moolenaar33570922005-01-25 22:26:29 +000010750static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
10752/*
10753 * "foldclosed()" function
10754 */
10755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010757 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010758 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010759 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760{
10761#ifdef FEAT_FOLDING
10762 linenr_T lnum;
10763 linenr_T first, last;
10764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10767 {
10768 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10769 {
10770 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010771 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 return;
10775 }
10776 }
10777#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779}
10780
10781/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010782 * "foldclosed()" function
10783 */
10784 static void
10785f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010786 typval_T *argvars;
10787 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010788{
10789 foldclosed_both(argvars, rettv, FALSE);
10790}
10791
10792/*
10793 * "foldclosedend()" function
10794 */
10795 static void
10796f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010797 typval_T *argvars;
10798 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010799{
10800 foldclosed_both(argvars, rettv, TRUE);
10801}
10802
10803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 * "foldlevel()" function
10805 */
10806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010808 typval_T *argvars UNUSED;
10809 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810{
10811#ifdef FEAT_FOLDING
10812 linenr_T lnum;
10813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818}
10819
10820/*
10821 * "foldtext()" function
10822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827{
10828#ifdef FEAT_FOLDING
10829 linenr_T lnum;
10830 char_u *s;
10831 char_u *r;
10832 int len;
10833 char *txt;
10834#endif
10835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->v_type = VAR_STRING;
10837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010839 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10840 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10841 <= curbuf->b_ml.ml_line_count
10842 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 {
10844 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10846 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 {
10848 if (!linewhite(lnum))
10849 break;
10850 ++lnum;
10851 }
10852
10853 /* Find interesting text in this line. */
10854 s = skipwhite(ml_get(lnum));
10855 /* skip C comment-start */
10856 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010859 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010860 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010861 {
10862 s = skipwhite(ml_get(lnum + 1));
10863 if (*s == '*')
10864 s = skipwhite(s + 1);
10865 }
10866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 txt = _("+-%s%3ld lines: ");
10868 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 + 20 /* for %3ld */
10871 + STRLEN(s))); /* concatenated */
10872 if (r != NULL)
10873 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10875 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10876 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 len = (int)STRLEN(r);
10878 STRCAT(r, s);
10879 /* remove 'foldmarker' and 'commentstring' */
10880 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 }
10883 }
10884#endif
10885}
10886
10887/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010888 * "foldtextresult(lnum)" function
10889 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010892 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010894{
10895#ifdef FEAT_FOLDING
10896 linenr_T lnum;
10897 char_u *text;
10898 char_u buf[51];
10899 foldinfo_T foldinfo;
10900 int fold_count;
10901#endif
10902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903 rettv->v_type = VAR_STRING;
10904 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010905#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 /* treat illegal types and illegal string values for {lnum} the same */
10908 if (lnum < 0)
10909 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010910 fold_count = foldedCount(curwin, lnum, &foldinfo);
10911 if (fold_count > 0)
10912 {
10913 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10914 &foldinfo, buf);
10915 if (text == buf)
10916 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010918 }
10919#endif
10920}
10921
10922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 * "foreground()" function
10924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010927 typval_T *argvars UNUSED;
10928 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930#ifdef FEAT_GUI
10931 if (gui.in_use)
10932 gui_mch_set_foreground();
10933#else
10934# ifdef WIN32
10935 win32_set_foreground();
10936# endif
10937#endif
10938}
10939
10940/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010941 * "function()" function
10942 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010945 typval_T *argvars;
10946 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010947{
10948 char_u *s;
10949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010950 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010951 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010952 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010953 /* Don't check an autoload name for existence here. */
10954 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010955 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010956 else
10957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958 rettv->vval.v_string = vim_strsave(s);
10959 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010960 }
10961}
10962
10963/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010964 * "garbagecollect()" function
10965 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010966 static void
10967f_garbagecollect(argvars, rettv)
10968 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010969 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010970{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010971 /* This is postponed until we are back at the toplevel, because we may be
10972 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10973 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010974
10975 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10976 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010977}
10978
10979/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010980 * "get()" function
10981 */
10982 static void
10983f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010984 typval_T *argvars;
10985 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010986{
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 listitem_T *li;
10988 list_T *l;
10989 dictitem_T *di;
10990 dict_T *d;
10991 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010992
Bram Moolenaare9a41262005-01-15 22:18:47 +000010993 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010997 int error = FALSE;
10998
10999 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11000 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011002 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011003 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011004 else if (argvars[0].v_type == VAR_DICT)
11005 {
11006 if ((d = argvars[0].vval.v_dict) != NULL)
11007 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011008 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 if (di != NULL)
11010 tv = &di->di_tv;
11011 }
11012 }
11013 else
11014 EMSG2(_(e_listdictarg), "get()");
11015
11016 if (tv == NULL)
11017 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011018 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019 copy_tv(&argvars[2], rettv);
11020 }
11021 else
11022 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011023}
11024
Bram Moolenaar342337a2005-07-21 21:11:17 +000011025static 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 +000011026
11027/*
11028 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011029 * Return a range (from start to end) of lines in rettv from the specified
11030 * buffer.
11031 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011032 */
11033 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011034get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011035 buf_T *buf;
11036 linenr_T start;
11037 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011038 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011039 typval_T *rettv;
11040{
11041 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011042
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011043 if (retlist && rettv_list_alloc(rettv) == FAIL)
11044 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011045
11046 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11047 return;
11048
11049 if (!retlist)
11050 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011051 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11052 p = ml_get_buf(buf, start, FALSE);
11053 else
11054 p = (char_u *)"";
11055
11056 rettv->v_type = VAR_STRING;
11057 rettv->vval.v_string = vim_strsave(p);
11058 }
11059 else
11060 {
11061 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011062 return;
11063
11064 if (start < 1)
11065 start = 1;
11066 if (end > buf->b_ml.ml_line_count)
11067 end = buf->b_ml.ml_line_count;
11068 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011069 if (list_append_string(rettv->vval.v_list,
11070 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011071 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011072 }
11073}
11074
11075/*
11076 * "getbufline()" function
11077 */
11078 static void
11079f_getbufline(argvars, rettv)
11080 typval_T *argvars;
11081 typval_T *rettv;
11082{
11083 linenr_T lnum;
11084 linenr_T end;
11085 buf_T *buf;
11086
11087 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11088 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011089 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011090 --emsg_off;
11091
Bram Moolenaar661b1822005-07-28 22:36:45 +000011092 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011093 if (argvars[2].v_type == VAR_UNKNOWN)
11094 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011095 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011096 end = get_tv_lnum_buf(&argvars[2], buf);
11097
Bram Moolenaar342337a2005-07-21 21:11:17 +000011098 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011099}
11100
Bram Moolenaar0d660222005-01-07 21:51:51 +000011101/*
11102 * "getbufvar()" function
11103 */
11104 static void
11105f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011106 typval_T *argvars;
11107 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011108{
11109 buf_T *buf;
11110 buf_T *save_curbuf;
11111 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011112 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011113 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11116 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011117 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011118 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011120 rettv->v_type = VAR_STRING;
11121 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122
11123 if (buf != NULL && varname != NULL)
11124 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011125 /* set curbuf to be our buf, temporarily */
11126 save_curbuf = curbuf;
11127 curbuf = buf;
11128
Bram Moolenaar0d660222005-01-07 21:51:51 +000011129 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011130 {
11131 if (get_option_tv(&varname, rettv, TRUE) == OK)
11132 done = TRUE;
11133 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011134 else if (STRCMP(varname, "changedtick") == 0)
11135 {
11136 rettv->v_type = VAR_NUMBER;
11137 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011138 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011139 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011140 else
11141 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011142 /* Look up the variable. */
11143 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11144 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11145 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011146 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011147 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011148 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011149 done = TRUE;
11150 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011152
11153 /* restore previous notion of curbuf */
11154 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155 }
11156
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011157 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11158 /* use the default value */
11159 copy_tv(&argvars[2], rettv);
11160
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161 --emsg_off;
11162}
11163
11164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 * "getchar()" function
11166 */
11167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011169 typval_T *argvars;
11170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
11172 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011173 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011175 /* Position the cursor. Needed after a message that ends in a space. */
11176 windgoto(msg_row, msg_col);
11177
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 ++no_mapping;
11179 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011180 for (;;)
11181 {
11182 if (argvars[0].v_type == VAR_UNKNOWN)
11183 /* getchar(): blocking wait. */
11184 n = safe_vgetc();
11185 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11186 /* getchar(1): only check if char avail */
11187 n = vpeekc();
11188 else if (error || vpeekc() == NUL)
11189 /* illegal argument or getchar(0) and no char avail: return zero */
11190 n = 0;
11191 else
11192 /* getchar(0) and char avail: return char */
11193 n = safe_vgetc();
11194 if (n == K_IGNORE)
11195 continue;
11196 break;
11197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 --no_mapping;
11199 --allow_keys;
11200
Bram Moolenaar219b8702006-11-01 14:32:36 +000011201 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11202 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11203 vimvars[VV_MOUSE_COL].vv_nr = 0;
11204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 if (IS_SPECIAL(n) || mod_mask != 0)
11207 {
11208 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11209 int i = 0;
11210
11211 /* Turn a special key into three bytes, plus modifier. */
11212 if (mod_mask != 0)
11213 {
11214 temp[i++] = K_SPECIAL;
11215 temp[i++] = KS_MODIFIER;
11216 temp[i++] = mod_mask;
11217 }
11218 if (IS_SPECIAL(n))
11219 {
11220 temp[i++] = K_SPECIAL;
11221 temp[i++] = K_SECOND(n);
11222 temp[i++] = K_THIRD(n);
11223 }
11224#ifdef FEAT_MBYTE
11225 else if (has_mbyte)
11226 i += (*mb_char2bytes)(n, temp + i);
11227#endif
11228 else
11229 temp[i++] = n;
11230 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 rettv->v_type = VAR_STRING;
11232 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011233
11234#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011235 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011236 {
11237 int row = mouse_row;
11238 int col = mouse_col;
11239 win_T *win;
11240 linenr_T lnum;
11241# ifdef FEAT_WINDOWS
11242 win_T *wp;
11243# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011244 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011245
11246 if (row >= 0 && col >= 0)
11247 {
11248 /* Find the window at the mouse coordinates and compute the
11249 * text position. */
11250 win = mouse_find_win(&row, &col);
11251 (void)mouse_comp_pos(win, &row, &col, &lnum);
11252# ifdef FEAT_WINDOWS
11253 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011254 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011255# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011256 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011257 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11258 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11259 }
11260 }
11261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 }
11263}
11264
11265/*
11266 * "getcharmod()" function
11267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011270 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274}
11275
11276/*
11277 * "getcmdline()" function
11278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011281 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->v_type = VAR_STRING;
11285 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286}
11287
11288/*
11289 * "getcmdpos()" function
11290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011293 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297}
11298
11299/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011300 * "getcmdtype()" function
11301 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011302 static void
11303f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011304 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011305 typval_T *rettv;
11306{
11307 rettv->v_type = VAR_STRING;
11308 rettv->vval.v_string = alloc(2);
11309 if (rettv->vval.v_string != NULL)
11310 {
11311 rettv->vval.v_string[0] = get_cmdline_type();
11312 rettv->vval.v_string[1] = NUL;
11313 }
11314}
11315
11316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 * "getcwd()" function
11318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011321 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011324 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011327 rettv->vval.v_string = NULL;
11328 cwd = alloc(MAXPATHL);
11329 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011331 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11332 {
11333 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011335 if (rettv->vval.v_string != NULL)
11336 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011338 }
11339 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 }
11341}
11342
11343/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011344 * "getfontname()" function
11345 */
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011349 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011350{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->v_type = VAR_STRING;
11352 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011353#ifdef FEAT_GUI
11354 if (gui.in_use)
11355 {
11356 GuiFont font;
11357 char_u *name = NULL;
11358
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011359 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011360 {
11361 /* Get the "Normal" font. Either the name saved by
11362 * hl_set_font_name() or from the font ID. */
11363 font = gui.norm_font;
11364 name = hl_get_font_name();
11365 }
11366 else
11367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011369 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11370 return;
11371 font = gui_mch_get_font(name, FALSE);
11372 if (font == NOFONT)
11373 return; /* Invalid font name, return empty string. */
11374 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011376 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011377 gui_mch_free_font(font);
11378 }
11379#endif
11380}
11381
11382/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011383 * "getfperm({fname})" function
11384 */
11385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011387 typval_T *argvars;
11388 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011389{
11390 char_u *fname;
11391 struct stat st;
11392 char_u *perm = NULL;
11393 char_u flags[] = "rwx";
11394 int i;
11395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011399 if (mch_stat((char *)fname, &st) >= 0)
11400 {
11401 perm = vim_strsave((char_u *)"---------");
11402 if (perm != NULL)
11403 {
11404 for (i = 0; i < 9; i++)
11405 {
11406 if (st.st_mode & (1 << (8 - i)))
11407 perm[i] = flags[i % 3];
11408 }
11409 }
11410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011412}
11413
11414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 * "getfsize({fname})" function
11416 */
11417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011419 typval_T *argvars;
11420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
11422 char_u *fname;
11423 struct stat st;
11424
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428
11429 if (mch_stat((char *)fname, &st) >= 0)
11430 {
11431 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011432 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011436
11437 /* non-perfect check for overflow */
11438 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11439 rettv->vval.v_number = -2;
11440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 }
11442 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444}
11445
11446/*
11447 * "getftime({fname})" function
11448 */
11449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011451 typval_T *argvars;
11452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453{
11454 char_u *fname;
11455 struct stat st;
11456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458
11459 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463}
11464
11465/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011466 * "getftype({fname})" function
11467 */
11468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011472{
11473 char_u *fname;
11474 struct stat st;
11475 char_u *type = NULL;
11476 char *t;
11477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481 if (mch_lstat((char *)fname, &st) >= 0)
11482 {
11483#ifdef S_ISREG
11484 if (S_ISREG(st.st_mode))
11485 t = "file";
11486 else if (S_ISDIR(st.st_mode))
11487 t = "dir";
11488# ifdef S_ISLNK
11489 else if (S_ISLNK(st.st_mode))
11490 t = "link";
11491# endif
11492# ifdef S_ISBLK
11493 else if (S_ISBLK(st.st_mode))
11494 t = "bdev";
11495# endif
11496# ifdef S_ISCHR
11497 else if (S_ISCHR(st.st_mode))
11498 t = "cdev";
11499# endif
11500# ifdef S_ISFIFO
11501 else if (S_ISFIFO(st.st_mode))
11502 t = "fifo";
11503# endif
11504# ifdef S_ISSOCK
11505 else if (S_ISSOCK(st.st_mode))
11506 t = "fifo";
11507# endif
11508 else
11509 t = "other";
11510#else
11511# ifdef S_IFMT
11512 switch (st.st_mode & S_IFMT)
11513 {
11514 case S_IFREG: t = "file"; break;
11515 case S_IFDIR: t = "dir"; break;
11516# ifdef S_IFLNK
11517 case S_IFLNK: t = "link"; break;
11518# endif
11519# ifdef S_IFBLK
11520 case S_IFBLK: t = "bdev"; break;
11521# endif
11522# ifdef S_IFCHR
11523 case S_IFCHR: t = "cdev"; break;
11524# endif
11525# ifdef S_IFIFO
11526 case S_IFIFO: t = "fifo"; break;
11527# endif
11528# ifdef S_IFSOCK
11529 case S_IFSOCK: t = "socket"; break;
11530# endif
11531 default: t = "other";
11532 }
11533# else
11534 if (mch_isdir(fname))
11535 t = "dir";
11536 else
11537 t = "file";
11538# endif
11539#endif
11540 type = vim_strsave((char_u *)t);
11541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011543}
11544
11545/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011546 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011547 */
11548 static void
11549f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 typval_T *argvars;
11551 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011552{
11553 linenr_T lnum;
11554 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011555 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011556
11557 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011558 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011559 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011560 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011561 retlist = FALSE;
11562 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011563 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011564 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011565 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011566 retlist = TRUE;
11567 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011568
Bram Moolenaar342337a2005-07-21 21:11:17 +000011569 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011570}
11571
11572/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011573 * "getmatches()" function
11574 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011575 static void
11576f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011577 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011578 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011579{
11580#ifdef FEAT_SEARCH_EXTRA
11581 dict_T *dict;
11582 matchitem_T *cur = curwin->w_match_head;
11583
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011584 if (rettv_list_alloc(rettv) == OK)
11585 {
11586 while (cur != NULL)
11587 {
11588 dict = dict_alloc();
11589 if (dict == NULL)
11590 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011591 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11592 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11593 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11594 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11595 list_append_dict(rettv->vval.v_list, dict);
11596 cur = cur->next;
11597 }
11598 }
11599#endif
11600}
11601
11602/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011603 * "getpid()" function
11604 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011605 static void
11606f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011607 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011608 typval_T *rettv;
11609{
11610 rettv->vval.v_number = mch_get_pid();
11611}
11612
11613/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011614 * "getpos(string)" function
11615 */
11616 static void
11617f_getpos(argvars, rettv)
11618 typval_T *argvars;
11619 typval_T *rettv;
11620{
11621 pos_T *fp;
11622 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011623 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011624
11625 if (rettv_list_alloc(rettv) == OK)
11626 {
11627 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011628 fp = var2fpos(&argvars[0], TRUE, &fnum);
11629 if (fnum != -1)
11630 list_append_number(l, (varnumber_T)fnum);
11631 else
11632 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011633 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11634 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011635 list_append_number(l, (fp != NULL)
11636 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011637 : (varnumber_T)0);
11638 list_append_number(l,
11639#ifdef FEAT_VIRTUALEDIT
11640 (fp != NULL) ? (varnumber_T)fp->coladd :
11641#endif
11642 (varnumber_T)0);
11643 }
11644 else
11645 rettv->vval.v_number = FALSE;
11646}
11647
11648/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011649 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011650 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011651 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011652f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011653 typval_T *argvars UNUSED;
11654 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011655{
11656#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011657 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011658#endif
11659
Bram Moolenaar2641f772005-03-25 21:58:17 +000011660#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011661 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011662 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011663 wp = NULL;
11664 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11665 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011666 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011667 if (wp == NULL)
11668 return;
11669 }
11670
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011671 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011672 }
11673#endif
11674}
11675
11676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 * "getreg()" function
11678 */
11679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 typval_T *argvars;
11682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683{
11684 char_u *strregname;
11685 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011686 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011689 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 strregname = get_tv_string_chk(&argvars[0]);
11692 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011693 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011694 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011697 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 regname = (strregname == NULL ? '"' : *strregname);
11699 if (regname == 0)
11700 regname = '"';
11701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703 rettv->vval.v_string = error ? NULL :
11704 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705}
11706
11707/*
11708 * "getregtype()" function
11709 */
11710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011711f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011712 typval_T *argvars;
11713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714{
11715 char_u *strregname;
11716 int regname;
11717 char_u buf[NUMBUFLEN + 2];
11718 long reglen = 0;
11719
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011720 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 {
11722 strregname = get_tv_string_chk(&argvars[0]);
11723 if (strregname == NULL) /* type error; errmsg already given */
11724 {
11725 rettv->v_type = VAR_STRING;
11726 rettv->vval.v_string = NULL;
11727 return;
11728 }
11729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 else
11731 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733
11734 regname = (strregname == NULL ? '"' : *strregname);
11735 if (regname == 0)
11736 regname = '"';
11737
11738 buf[0] = NUL;
11739 buf[1] = NUL;
11740 switch (get_reg_type(regname, &reglen))
11741 {
11742 case MLINE: buf[0] = 'V'; break;
11743 case MCHAR: buf[0] = 'v'; break;
11744#ifdef FEAT_VISUAL
11745 case MBLOCK:
11746 buf[0] = Ctrl_V;
11747 sprintf((char *)buf + 1, "%ld", reglen + 1);
11748 break;
11749#endif
11750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751 rettv->v_type = VAR_STRING;
11752 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753}
11754
11755/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011756 * "gettabvar()" function
11757 */
11758 static void
11759f_gettabvar(argvars, rettv)
11760 typval_T *argvars;
11761 typval_T *rettv;
11762{
11763 tabpage_T *tp;
11764 dictitem_T *v;
11765 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011766 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011767
11768 rettv->v_type = VAR_STRING;
11769 rettv->vval.v_string = NULL;
11770
11771 varname = get_tv_string_chk(&argvars[1]);
11772 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11773 if (tp != NULL && varname != NULL)
11774 {
11775 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011776 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011777 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011778 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011779 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011780 done = TRUE;
11781 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011782 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011783
11784 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011785 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011786}
11787
11788/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011789 * "gettabwinvar()" function
11790 */
11791 static void
11792f_gettabwinvar(argvars, rettv)
11793 typval_T *argvars;
11794 typval_T *rettv;
11795{
11796 getwinvar(argvars, rettv, 1);
11797}
11798
11799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 * "getwinposx()" function
11801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011804 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808#ifdef FEAT_GUI
11809 if (gui.in_use)
11810 {
11811 int x, y;
11812
11813 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 }
11816#endif
11817}
11818
11819/*
11820 * "getwinposy()" function
11821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011823f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011824 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828#ifdef FEAT_GUI
11829 if (gui.in_use)
11830 {
11831 int x, y;
11832
11833 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835 }
11836#endif
11837}
11838
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011839/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011840 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011841 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011842 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011843find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011844 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011845 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011846{
11847#ifdef FEAT_WINDOWS
11848 win_T *wp;
11849#endif
11850 int nr;
11851
11852 nr = get_tv_number_chk(vp, NULL);
11853
11854#ifdef FEAT_WINDOWS
11855 if (nr < 0)
11856 return NULL;
11857 if (nr == 0)
11858 return curwin;
11859
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011860 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11861 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011862 if (--nr <= 0)
11863 break;
11864 return wp;
11865#else
11866 if (nr == 0 || nr == 1)
11867 return curwin;
11868 return NULL;
11869#endif
11870}
11871
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872/*
11873 * "getwinvar()" function
11874 */
11875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011877 typval_T *argvars;
11878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011880 getwinvar(argvars, rettv, 0);
11881}
11882
11883/*
11884 * getwinvar() and gettabwinvar()
11885 */
11886 static void
11887getwinvar(argvars, rettv, off)
11888 typval_T *argvars;
11889 typval_T *rettv;
11890 int off; /* 1 for gettabwinvar() */
11891{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 win_T *win, *oldcurwin;
11893 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011894 dictitem_T *v;
Bram Moolenaar105bc352013-05-17 16:03:57 +020011895 tabpage_T *tp, *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011896 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011898#ifdef FEAT_WINDOWS
11899 if (off == 1)
11900 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11901 else
11902 tp = curtab;
11903#endif
11904 win = find_win_by_nr(&argvars[off], tp);
11905 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011906 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011908 rettv->v_type = VAR_STRING;
11909 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910
11911 if (win != NULL && varname != NULL)
11912 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011913 /* Set curwin to be our win, temporarily. Also set the tabpage,
11914 * otherwise the window is not valid. */
11915 switch_win(&oldcurwin, &oldtabpage, win, tp);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011916
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011918 {
11919 if (get_option_tv(&varname, rettv, 1) == OK)
11920 done = TRUE;
11921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 else
11923 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011924 /* Look up the variable. */
11925 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11926 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011928 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011929 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011930 done = TRUE;
11931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011933
11934 /* restore previous notion of curwin */
Bram Moolenaar105bc352013-05-17 16:03:57 +020011935 restore_win(oldcurwin, oldtabpage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 }
11937
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011938 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11939 /* use the default return value */
11940 copy_tv(&argvars[off + 2], rettv);
11941
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 --emsg_off;
11943}
11944
11945/*
11946 * "glob()" function
11947 */
11948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 typval_T *argvars;
11951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011953 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011957 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011958 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011959 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011960 if (argvars[1].v_type != VAR_UNKNOWN)
11961 {
11962 if (get_tv_number_chk(&argvars[1], &error))
11963 options |= WILD_KEEP_ALL;
11964 if (argvars[2].v_type != VAR_UNKNOWN
11965 && get_tv_number_chk(&argvars[2], &error))
11966 {
11967 rettv->v_type = VAR_LIST;
11968 rettv->vval.v_list = NULL;
11969 }
11970 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011971 if (!error)
11972 {
11973 ExpandInit(&xpc);
11974 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011975 if (p_wic)
11976 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011977 if (rettv->v_type == VAR_STRING)
11978 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011979 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011980 else if (rettv_list_alloc(rettv) != FAIL)
11981 {
11982 int i;
11983
11984 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11985 NULL, options, WILD_ALL_KEEP);
11986 for (i = 0; i < xpc.xp_numfiles; i++)
11987 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11988
11989 ExpandCleanup(&xpc);
11990 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011991 }
11992 else
11993 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994}
11995
11996/*
11997 * "globpath()" function
11998 */
11999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012001 typval_T *argvars;
12002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012004 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012006 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012007 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012009 /* When the optional second argument is non-zero, don't remove matches
12010 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12011 if (argvars[2].v_type != VAR_UNKNOWN
12012 && get_tv_number_chk(&argvars[2], &error))
12013 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012015 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012016 rettv->vval.v_string = NULL;
12017 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012018 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12019 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020}
12021
12022/*
12023 * "has()" function
12024 */
12025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012027 typval_T *argvars;
12028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029{
12030 int i;
12031 char_u *name;
12032 int n = FALSE;
12033 static char *(has_list[]) =
12034 {
12035#ifdef AMIGA
12036 "amiga",
12037# ifdef FEAT_ARP
12038 "arp",
12039# endif
12040#endif
12041#ifdef __BEOS__
12042 "beos",
12043#endif
12044#ifdef MSDOS
12045# ifdef DJGPP
12046 "dos32",
12047# else
12048 "dos16",
12049# endif
12050#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012051#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 "mac",
12053#endif
12054#if defined(MACOS_X_UNIX)
12055 "macunix",
12056#endif
12057#ifdef OS2
12058 "os2",
12059#endif
12060#ifdef __QNX__
12061 "qnx",
12062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063#ifdef UNIX
12064 "unix",
12065#endif
12066#ifdef VMS
12067 "vms",
12068#endif
12069#ifdef WIN16
12070 "win16",
12071#endif
12072#ifdef WIN32
12073 "win32",
12074#endif
12075#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12076 "win32unix",
12077#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012078#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 "win64",
12080#endif
12081#ifdef EBCDIC
12082 "ebcdic",
12083#endif
12084#ifndef CASE_INSENSITIVE_FILENAME
12085 "fname_case",
12086#endif
12087#ifdef FEAT_ARABIC
12088 "arabic",
12089#endif
12090#ifdef FEAT_AUTOCMD
12091 "autocmd",
12092#endif
12093#ifdef FEAT_BEVAL
12094 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012095# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12096 "balloon_multiline",
12097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098#endif
12099#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12100 "builtin_terms",
12101# ifdef ALL_BUILTIN_TCAPS
12102 "all_builtin_terms",
12103# endif
12104#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012105#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12106 || defined(FEAT_GUI_W32) \
12107 || defined(FEAT_GUI_MOTIF))
12108 "browsefilter",
12109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110#ifdef FEAT_BYTEOFF
12111 "byte_offset",
12112#endif
12113#ifdef FEAT_CINDENT
12114 "cindent",
12115#endif
12116#ifdef FEAT_CLIENTSERVER
12117 "clientserver",
12118#endif
12119#ifdef FEAT_CLIPBOARD
12120 "clipboard",
12121#endif
12122#ifdef FEAT_CMDL_COMPL
12123 "cmdline_compl",
12124#endif
12125#ifdef FEAT_CMDHIST
12126 "cmdline_hist",
12127#endif
12128#ifdef FEAT_COMMENTS
12129 "comments",
12130#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012131#ifdef FEAT_CONCEAL
12132 "conceal",
12133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134#ifdef FEAT_CRYPT
12135 "cryptv",
12136#endif
12137#ifdef FEAT_CSCOPE
12138 "cscope",
12139#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012140#ifdef FEAT_CURSORBIND
12141 "cursorbind",
12142#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012143#ifdef CURSOR_SHAPE
12144 "cursorshape",
12145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146#ifdef DEBUG
12147 "debug",
12148#endif
12149#ifdef FEAT_CON_DIALOG
12150 "dialog_con",
12151#endif
12152#ifdef FEAT_GUI_DIALOG
12153 "dialog_gui",
12154#endif
12155#ifdef FEAT_DIFF
12156 "diff",
12157#endif
12158#ifdef FEAT_DIGRAPHS
12159 "digraphs",
12160#endif
12161#ifdef FEAT_DND
12162 "dnd",
12163#endif
12164#ifdef FEAT_EMACS_TAGS
12165 "emacs_tags",
12166#endif
12167 "eval", /* always present, of course! */
12168#ifdef FEAT_EX_EXTRA
12169 "ex_extra",
12170#endif
12171#ifdef FEAT_SEARCH_EXTRA
12172 "extra_search",
12173#endif
12174#ifdef FEAT_FKMAP
12175 "farsi",
12176#endif
12177#ifdef FEAT_SEARCHPATH
12178 "file_in_path",
12179#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012180#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012181 "filterpipe",
12182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183#ifdef FEAT_FIND_ID
12184 "find_in_path",
12185#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012186#ifdef FEAT_FLOAT
12187 "float",
12188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189#ifdef FEAT_FOLDING
12190 "folding",
12191#endif
12192#ifdef FEAT_FOOTER
12193 "footer",
12194#endif
12195#if !defined(USE_SYSTEM) && defined(UNIX)
12196 "fork",
12197#endif
12198#ifdef FEAT_GETTEXT
12199 "gettext",
12200#endif
12201#ifdef FEAT_GUI
12202 "gui",
12203#endif
12204#ifdef FEAT_GUI_ATHENA
12205# ifdef FEAT_GUI_NEXTAW
12206 "gui_neXtaw",
12207# else
12208 "gui_athena",
12209# endif
12210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211#ifdef FEAT_GUI_GTK
12212 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012215#ifdef FEAT_GUI_GNOME
12216 "gui_gnome",
12217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218#ifdef FEAT_GUI_MAC
12219 "gui_mac",
12220#endif
12221#ifdef FEAT_GUI_MOTIF
12222 "gui_motif",
12223#endif
12224#ifdef FEAT_GUI_PHOTON
12225 "gui_photon",
12226#endif
12227#ifdef FEAT_GUI_W16
12228 "gui_win16",
12229#endif
12230#ifdef FEAT_GUI_W32
12231 "gui_win32",
12232#endif
12233#ifdef FEAT_HANGULIN
12234 "hangul_input",
12235#endif
12236#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12237 "iconv",
12238#endif
12239#ifdef FEAT_INS_EXPAND
12240 "insert_expand",
12241#endif
12242#ifdef FEAT_JUMPLIST
12243 "jumplist",
12244#endif
12245#ifdef FEAT_KEYMAP
12246 "keymap",
12247#endif
12248#ifdef FEAT_LANGMAP
12249 "langmap",
12250#endif
12251#ifdef FEAT_LIBCALL
12252 "libcall",
12253#endif
12254#ifdef FEAT_LINEBREAK
12255 "linebreak",
12256#endif
12257#ifdef FEAT_LISP
12258 "lispindent",
12259#endif
12260#ifdef FEAT_LISTCMDS
12261 "listcmds",
12262#endif
12263#ifdef FEAT_LOCALMAP
12264 "localmap",
12265#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012266#ifdef FEAT_LUA
12267# ifndef DYNAMIC_LUA
12268 "lua",
12269# endif
12270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271#ifdef FEAT_MENU
12272 "menu",
12273#endif
12274#ifdef FEAT_SESSION
12275 "mksession",
12276#endif
12277#ifdef FEAT_MODIFY_FNAME
12278 "modify_fname",
12279#endif
12280#ifdef FEAT_MOUSE
12281 "mouse",
12282#endif
12283#ifdef FEAT_MOUSESHAPE
12284 "mouseshape",
12285#endif
12286#if defined(UNIX) || defined(VMS)
12287# ifdef FEAT_MOUSE_DEC
12288 "mouse_dec",
12289# endif
12290# ifdef FEAT_MOUSE_GPM
12291 "mouse_gpm",
12292# endif
12293# ifdef FEAT_MOUSE_JSB
12294 "mouse_jsbterm",
12295# endif
12296# ifdef FEAT_MOUSE_NET
12297 "mouse_netterm",
12298# endif
12299# ifdef FEAT_MOUSE_PTERM
12300 "mouse_pterm",
12301# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012302# ifdef FEAT_MOUSE_SGR
12303 "mouse_sgr",
12304# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012305# ifdef FEAT_SYSMOUSE
12306 "mouse_sysmouse",
12307# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012308# ifdef FEAT_MOUSE_URXVT
12309 "mouse_urxvt",
12310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311# ifdef FEAT_MOUSE_XTERM
12312 "mouse_xterm",
12313# endif
12314#endif
12315#ifdef FEAT_MBYTE
12316 "multi_byte",
12317#endif
12318#ifdef FEAT_MBYTE_IME
12319 "multi_byte_ime",
12320#endif
12321#ifdef FEAT_MULTI_LANG
12322 "multi_lang",
12323#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012324#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012325#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012326 "mzscheme",
12327#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329#ifdef FEAT_OLE
12330 "ole",
12331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332#ifdef FEAT_PATH_EXTRA
12333 "path_extra",
12334#endif
12335#ifdef FEAT_PERL
12336#ifndef DYNAMIC_PERL
12337 "perl",
12338#endif
12339#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012340#ifdef FEAT_PERSISTENT_UNDO
12341 "persistent_undo",
12342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343#ifdef FEAT_PYTHON
12344#ifndef DYNAMIC_PYTHON
12345 "python",
12346#endif
12347#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012348#ifdef FEAT_PYTHON3
12349#ifndef DYNAMIC_PYTHON3
12350 "python3",
12351#endif
12352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353#ifdef FEAT_POSTSCRIPT
12354 "postscript",
12355#endif
12356#ifdef FEAT_PRINTER
12357 "printer",
12358#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012359#ifdef FEAT_PROFILE
12360 "profile",
12361#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012362#ifdef FEAT_RELTIME
12363 "reltime",
12364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365#ifdef FEAT_QUICKFIX
12366 "quickfix",
12367#endif
12368#ifdef FEAT_RIGHTLEFT
12369 "rightleft",
12370#endif
12371#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12372 "ruby",
12373#endif
12374#ifdef FEAT_SCROLLBIND
12375 "scrollbind",
12376#endif
12377#ifdef FEAT_CMDL_INFO
12378 "showcmd",
12379 "cmdline_info",
12380#endif
12381#ifdef FEAT_SIGNS
12382 "signs",
12383#endif
12384#ifdef FEAT_SMARTINDENT
12385 "smartindent",
12386#endif
12387#ifdef FEAT_SNIFF
12388 "sniff",
12389#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012390#ifdef STARTUPTIME
12391 "startuptime",
12392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393#ifdef FEAT_STL_OPT
12394 "statusline",
12395#endif
12396#ifdef FEAT_SUN_WORKSHOP
12397 "sun_workshop",
12398#endif
12399#ifdef FEAT_NETBEANS_INTG
12400 "netbeans_intg",
12401#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012402#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012403 "spell",
12404#endif
12405#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 "syntax",
12407#endif
12408#if defined(USE_SYSTEM) || !defined(UNIX)
12409 "system",
12410#endif
12411#ifdef FEAT_TAG_BINS
12412 "tag_binary",
12413#endif
12414#ifdef FEAT_TAG_OLDSTATIC
12415 "tag_old_static",
12416#endif
12417#ifdef FEAT_TAG_ANYWHITE
12418 "tag_any_white",
12419#endif
12420#ifdef FEAT_TCL
12421# ifndef DYNAMIC_TCL
12422 "tcl",
12423# endif
12424#endif
12425#ifdef TERMINFO
12426 "terminfo",
12427#endif
12428#ifdef FEAT_TERMRESPONSE
12429 "termresponse",
12430#endif
12431#ifdef FEAT_TEXTOBJ
12432 "textobjects",
12433#endif
12434#ifdef HAVE_TGETENT
12435 "tgetent",
12436#endif
12437#ifdef FEAT_TITLE
12438 "title",
12439#endif
12440#ifdef FEAT_TOOLBAR
12441 "toolbar",
12442#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012443#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12444 "unnamedplus",
12445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446#ifdef FEAT_USR_CMDS
12447 "user-commands", /* was accidentally included in 5.4 */
12448 "user_commands",
12449#endif
12450#ifdef FEAT_VIMINFO
12451 "viminfo",
12452#endif
12453#ifdef FEAT_VERTSPLIT
12454 "vertsplit",
12455#endif
12456#ifdef FEAT_VIRTUALEDIT
12457 "virtualedit",
12458#endif
12459#ifdef FEAT_VISUAL
12460 "visual",
12461#endif
12462#ifdef FEAT_VISUALEXTRA
12463 "visualextra",
12464#endif
12465#ifdef FEAT_VREPLACE
12466 "vreplace",
12467#endif
12468#ifdef FEAT_WILDIGN
12469 "wildignore",
12470#endif
12471#ifdef FEAT_WILDMENU
12472 "wildmenu",
12473#endif
12474#ifdef FEAT_WINDOWS
12475 "windows",
12476#endif
12477#ifdef FEAT_WAK
12478 "winaltkeys",
12479#endif
12480#ifdef FEAT_WRITEBACKUP
12481 "writebackup",
12482#endif
12483#ifdef FEAT_XIM
12484 "xim",
12485#endif
12486#ifdef FEAT_XFONTSET
12487 "xfontset",
12488#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012489#ifdef FEAT_XPM_W32
12490 "xpm_w32",
12491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492#ifdef USE_XSMP
12493 "xsmp",
12494#endif
12495#ifdef USE_XSMP_INTERACT
12496 "xsmp_interact",
12497#endif
12498#ifdef FEAT_XCLIPBOARD
12499 "xterm_clipboard",
12500#endif
12501#ifdef FEAT_XTERM_SAVE
12502 "xterm_save",
12503#endif
12504#if defined(UNIX) && defined(FEAT_X11)
12505 "X11",
12506#endif
12507 NULL
12508 };
12509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 for (i = 0; has_list[i] != NULL; ++i)
12512 if (STRICMP(name, has_list[i]) == 0)
12513 {
12514 n = TRUE;
12515 break;
12516 }
12517
12518 if (n == FALSE)
12519 {
12520 if (STRNICMP(name, "patch", 5) == 0)
12521 n = has_patch(atoi((char *)name + 5));
12522 else if (STRICMP(name, "vim_starting") == 0)
12523 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012524#ifdef FEAT_MBYTE
12525 else if (STRICMP(name, "multi_byte_encoding") == 0)
12526 n = has_mbyte;
12527#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012528#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12529 else if (STRICMP(name, "balloon_multiline") == 0)
12530 n = multiline_balloon_available();
12531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532#ifdef DYNAMIC_TCL
12533 else if (STRICMP(name, "tcl") == 0)
12534 n = tcl_enabled(FALSE);
12535#endif
12536#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12537 else if (STRICMP(name, "iconv") == 0)
12538 n = iconv_enabled(FALSE);
12539#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012540#ifdef DYNAMIC_LUA
12541 else if (STRICMP(name, "lua") == 0)
12542 n = lua_enabled(FALSE);
12543#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012544#ifdef DYNAMIC_MZSCHEME
12545 else if (STRICMP(name, "mzscheme") == 0)
12546 n = mzscheme_enabled(FALSE);
12547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548#ifdef DYNAMIC_RUBY
12549 else if (STRICMP(name, "ruby") == 0)
12550 n = ruby_enabled(FALSE);
12551#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012552#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553#ifdef DYNAMIC_PYTHON
12554 else if (STRICMP(name, "python") == 0)
12555 n = python_enabled(FALSE);
12556#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012557#endif
12558#ifdef FEAT_PYTHON3
12559#ifdef DYNAMIC_PYTHON3
12560 else if (STRICMP(name, "python3") == 0)
12561 n = python3_enabled(FALSE);
12562#endif
12563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564#ifdef DYNAMIC_PERL
12565 else if (STRICMP(name, "perl") == 0)
12566 n = perl_enabled(FALSE);
12567#endif
12568#ifdef FEAT_GUI
12569 else if (STRICMP(name, "gui_running") == 0)
12570 n = (gui.in_use || gui.starting);
12571# ifdef FEAT_GUI_W32
12572 else if (STRICMP(name, "gui_win32s") == 0)
12573 n = gui_is_win32s();
12574# endif
12575# ifdef FEAT_BROWSE
12576 else if (STRICMP(name, "browse") == 0)
12577 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12578# endif
12579#endif
12580#ifdef FEAT_SYN_HL
12581 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012582 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583#endif
12584#if defined(WIN3264)
12585 else if (STRICMP(name, "win95") == 0)
12586 n = mch_windows95();
12587#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012588#ifdef FEAT_NETBEANS_INTG
12589 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012590 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 }
12593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012594 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595}
12596
12597/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012598 * "has_key()" function
12599 */
12600 static void
12601f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012602 typval_T *argvars;
12603 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012604{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012605 if (argvars[0].v_type != VAR_DICT)
12606 {
12607 EMSG(_(e_dictreq));
12608 return;
12609 }
12610 if (argvars[0].vval.v_dict == NULL)
12611 return;
12612
12613 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012614 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012615}
12616
12617/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012618 * "haslocaldir()" function
12619 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012620 static void
12621f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012622 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012623 typval_T *rettv;
12624{
12625 rettv->vval.v_number = (curwin->w_localdir != NULL);
12626}
12627
12628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629 * "hasmapto()" function
12630 */
12631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012632f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012633 typval_T *argvars;
12634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635{
12636 char_u *name;
12637 char_u *mode;
12638 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012639 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012641 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012642 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 mode = (char_u *)"nvo";
12644 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012647 if (argvars[2].v_type != VAR_UNKNOWN)
12648 abbr = get_tv_number(&argvars[2]);
12649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650
Bram Moolenaar2c932302006-03-18 21:42:09 +000012651 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012652 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655}
12656
12657/*
12658 * "histadd()" function
12659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012662 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664{
12665#ifdef FEAT_CMDHIST
12666 int histype;
12667 char_u *str;
12668 char_u buf[NUMBUFLEN];
12669#endif
12670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 if (check_restricted() || check_secure())
12673 return;
12674#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012675 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12676 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 if (histype >= 0)
12678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 if (*str != NUL)
12681 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012682 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 return;
12686 }
12687 }
12688#endif
12689}
12690
12691/*
12692 * "histdel()" function
12693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012696 typval_T *argvars UNUSED;
12697 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698{
12699#ifdef FEAT_CMDHIST
12700 int n;
12701 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12705 if (str == NULL)
12706 n = 0;
12707 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012710 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012712 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012713 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714 else
12715 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012716 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 get_tv_string_buf(&argvars[1], buf));
12718 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719#endif
12720}
12721
12722/*
12723 * "histget()" function
12724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012727 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729{
12730#ifdef FEAT_CMDHIST
12731 int type;
12732 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012733 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12736 if (str == NULL)
12737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012739 {
12740 type = get_histtype(str);
12741 if (argvars[1].v_type == VAR_UNKNOWN)
12742 idx = get_history_idx(type);
12743 else
12744 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12745 /* -1 on type error */
12746 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752}
12753
12754/*
12755 * "histnr()" function
12756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761{
12762 int i;
12763
12764#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012765 char_u *history = get_tv_string_chk(&argvars[0]);
12766
12767 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 if (i >= HIST_CMD && i < HIST_COUNT)
12769 i = get_history_idx(i);
12770 else
12771#endif
12772 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774}
12775
12776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 * "highlightID(name)" function
12778 */
12779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012781 typval_T *argvars;
12782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785}
12786
12787/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012788 * "highlight_exists()" function
12789 */
12790 static void
12791f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012794{
12795 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12796}
12797
12798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 * "hostname()" function
12800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012803 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805{
12806 char_u hostname[256];
12807
12808 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->v_type = VAR_STRING;
12810 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811}
12812
12813/*
12814 * iconv() function
12815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012818 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820{
12821#ifdef FEAT_MBYTE
12822 char_u buf1[NUMBUFLEN];
12823 char_u buf2[NUMBUFLEN];
12824 char_u *from, *to, *str;
12825 vimconv_T vimconv;
12826#endif
12827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->v_type = VAR_STRING;
12829 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830
12831#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 str = get_tv_string(&argvars[0]);
12833 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12834 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 vimconv.vc_type = CONV_NONE;
12836 convert_setup(&vimconv, from, to);
12837
12838 /* If the encodings are equal, no conversion needed. */
12839 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843
12844 convert_setup(&vimconv, NULL, NULL);
12845 vim_free(from);
12846 vim_free(to);
12847#endif
12848}
12849
12850/*
12851 * "indent()" function
12852 */
12853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012855 typval_T *argvars;
12856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857{
12858 linenr_T lnum;
12859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865}
12866
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012867/*
12868 * "index()" function
12869 */
12870 static void
12871f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012872 typval_T *argvars;
12873 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012874{
Bram Moolenaar33570922005-01-25 22:26:29 +000012875 list_T *l;
12876 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012877 long idx = 0;
12878 int ic = FALSE;
12879
12880 rettv->vval.v_number = -1;
12881 if (argvars[0].v_type != VAR_LIST)
12882 {
12883 EMSG(_(e_listreq));
12884 return;
12885 }
12886 l = argvars[0].vval.v_list;
12887 if (l != NULL)
12888 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012889 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012890 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012892 int error = FALSE;
12893
Bram Moolenaar758711c2005-02-02 23:11:38 +000012894 /* Start at specified item. Use the cached index that list_find()
12895 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012896 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012897 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012898 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012899 ic = get_tv_number_chk(&argvars[3], &error);
12900 if (error)
12901 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012902 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012903
Bram Moolenaar758711c2005-02-02 23:11:38 +000012904 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012905 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012906 {
12907 rettv->vval.v_number = idx;
12908 break;
12909 }
12910 }
12911}
12912
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913static int inputsecret_flag = 0;
12914
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012915static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12916
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012918 * This function is used by f_input() and f_inputdialog() functions. The third
12919 * argument to f_input() specifies the type of completion to use at the
12920 * prompt. The third argument to f_inputdialog() specifies the value to return
12921 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922 */
12923 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012924get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012925 typval_T *argvars;
12926 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012927 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012929 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930 char_u *p = NULL;
12931 int c;
12932 char_u buf[NUMBUFLEN];
12933 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012935 int xp_type = EXPAND_NOTHING;
12936 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012939 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940
12941#ifdef NO_CONSOLE_INPUT
12942 /* While starting up, there is no place to enter text. */
12943 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945#endif
12946
12947 cmd_silent = FALSE; /* Want to see the prompt. */
12948 if (prompt != NULL)
12949 {
12950 /* Only the part of the message after the last NL is considered as
12951 * prompt for the command line */
12952 p = vim_strrchr(prompt, '\n');
12953 if (p == NULL)
12954 p = prompt;
12955 else
12956 {
12957 ++p;
12958 c = *p;
12959 *p = NUL;
12960 msg_start();
12961 msg_clr_eos();
12962 msg_puts_attr(prompt, echo_attr);
12963 msg_didout = FALSE;
12964 msg_starthere();
12965 *p = c;
12966 }
12967 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012969 if (argvars[1].v_type != VAR_UNKNOWN)
12970 {
12971 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12972 if (defstr != NULL)
12973 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012975 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012976 {
12977 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012978 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012979 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012980
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012981 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012982 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012983
Bram Moolenaar4463f292005-09-25 22:20:24 +000012984 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12985 if (xp_name == NULL)
12986 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012987
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012988 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012989
Bram Moolenaar4463f292005-09-25 22:20:24 +000012990 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12991 &xp_arg) == FAIL)
12992 return;
12993 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012994 }
12995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012996 if (defstr != NULL)
12997 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012998 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12999 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013000 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013001 && argvars[1].v_type != VAR_UNKNOWN
13002 && argvars[2].v_type != VAR_UNKNOWN)
13003 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13004 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013005
13006 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 /* since the user typed this, no need to wait for return */
13009 need_wait_return = FALSE;
13010 msg_didout = FALSE;
13011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 cmd_silent = cmd_silent_save;
13013}
13014
13015/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013016 * "input()" function
13017 * Also handles inputsecret() when inputsecret is set.
13018 */
13019 static void
13020f_input(argvars, rettv)
13021 typval_T *argvars;
13022 typval_T *rettv;
13023{
13024 get_user_input(argvars, rettv, FALSE);
13025}
13026
13027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028 * "inputdialog()" function
13029 */
13030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013031f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013032 typval_T *argvars;
13033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034{
13035#if defined(FEAT_GUI_TEXTDIALOG)
13036 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13037 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13038 {
13039 char_u *message;
13040 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013041 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 message = get_tv_string_chk(&argvars[0]);
13044 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013045 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013046 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047 else
13048 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013049 if (message != NULL && defstr != NULL
13050 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013051 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 else
13054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013055 if (message != NULL && defstr != NULL
13056 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013057 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->vval.v_string = vim_strsave(
13059 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064 }
13065 else
13066#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013067 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068}
13069
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013070/*
13071 * "inputlist()" function
13072 */
13073 static void
13074f_inputlist(argvars, rettv)
13075 typval_T *argvars;
13076 typval_T *rettv;
13077{
13078 listitem_T *li;
13079 int selected;
13080 int mouse_used;
13081
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013082#ifdef NO_CONSOLE_INPUT
13083 /* While starting up, there is no place to enter text. */
13084 if (no_console_input())
13085 return;
13086#endif
13087 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13088 {
13089 EMSG2(_(e_listarg), "inputlist()");
13090 return;
13091 }
13092
13093 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013094 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013095 lines_left = Rows; /* avoid more prompt */
13096 msg_scroll = TRUE;
13097 msg_clr_eos();
13098
13099 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13100 {
13101 msg_puts(get_tv_string(&li->li_tv));
13102 msg_putchar('\n');
13103 }
13104
13105 /* Ask for choice. */
13106 selected = prompt_for_number(&mouse_used);
13107 if (mouse_used)
13108 selected -= lines_left;
13109
13110 rettv->vval.v_number = selected;
13111}
13112
13113
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13115
13116/*
13117 * "inputrestore()" function
13118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013120f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013121 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123{
13124 if (ga_userinput.ga_len > 0)
13125 {
13126 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13128 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013129 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130 }
13131 else if (p_verbose > 1)
13132 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013133 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013134 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 }
13136}
13137
13138/*
13139 * "inputsave()" function
13140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013142f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013143 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013146 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 if (ga_grow(&ga_userinput, 1) == OK)
13148 {
13149 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13150 + ga_userinput.ga_len);
13151 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013152 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 }
13154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156}
13157
13158/*
13159 * "inputsecret()" function
13160 */
13161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *argvars;
13164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165{
13166 ++cmdline_star;
13167 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 --cmdline_star;
13170 --inputsecret_flag;
13171}
13172
13173/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013174 * "insert()" function
13175 */
13176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013178 typval_T *argvars;
13179 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013180{
13181 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013182 listitem_T *item;
13183 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013184 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013185
13186 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013187 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013188 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013189 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013190 {
13191 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013192 before = get_tv_number_chk(&argvars[2], &error);
13193 if (error)
13194 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195
Bram Moolenaar758711c2005-02-02 23:11:38 +000013196 if (before == l->lv_len)
13197 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013198 else
13199 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013200 item = list_find(l, before);
13201 if (item == NULL)
13202 {
13203 EMSGN(_(e_listidx), before);
13204 l = NULL;
13205 }
13206 }
13207 if (l != NULL)
13208 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013209 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013210 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013211 }
13212 }
13213}
13214
13215/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013216 * "invert(expr)" function
13217 */
13218 static void
13219f_invert(argvars, rettv)
13220 typval_T *argvars;
13221 typval_T *rettv;
13222{
13223 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13224}
13225
13226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 * "isdirectory()" function
13228 */
13229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 typval_T *argvars;
13232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235}
13236
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013237/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013238 * "islocked()" function
13239 */
13240 static void
13241f_islocked(argvars, rettv)
13242 typval_T *argvars;
13243 typval_T *rettv;
13244{
13245 lval_T lv;
13246 char_u *end;
13247 dictitem_T *di;
13248
13249 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013250 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13251 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013252 if (end != NULL && lv.ll_name != NULL)
13253 {
13254 if (*end != NUL)
13255 EMSG(_(e_trailing));
13256 else
13257 {
13258 if (lv.ll_tv == NULL)
13259 {
13260 if (check_changedtick(lv.ll_name))
13261 rettv->vval.v_number = 1; /* always locked */
13262 else
13263 {
13264 di = find_var(lv.ll_name, NULL);
13265 if (di != NULL)
13266 {
13267 /* Consider a variable locked when:
13268 * 1. the variable itself is locked
13269 * 2. the value of the variable is locked.
13270 * 3. the List or Dict value is locked.
13271 */
13272 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13273 || tv_islocked(&di->di_tv));
13274 }
13275 }
13276 }
13277 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013278 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013279 else if (lv.ll_newkey != NULL)
13280 EMSG2(_(e_dictkey), lv.ll_newkey);
13281 else if (lv.ll_list != NULL)
13282 /* List item. */
13283 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13284 else
13285 /* Dictionary item. */
13286 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13287 }
13288 }
13289
13290 clear_lval(&lv);
13291}
13292
Bram Moolenaar33570922005-01-25 22:26:29 +000013293static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013294
13295/*
13296 * Turn a dict into a list:
13297 * "what" == 0: list of keys
13298 * "what" == 1: list of values
13299 * "what" == 2: list of items
13300 */
13301 static void
13302dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013303 typval_T *argvars;
13304 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013305 int what;
13306{
Bram Moolenaar33570922005-01-25 22:26:29 +000013307 list_T *l2;
13308 dictitem_T *di;
13309 hashitem_T *hi;
13310 listitem_T *li;
13311 listitem_T *li2;
13312 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013313 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013314
Bram Moolenaar8c711452005-01-14 21:53:12 +000013315 if (argvars[0].v_type != VAR_DICT)
13316 {
13317 EMSG(_(e_dictreq));
13318 return;
13319 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013320 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013321 return;
13322
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013323 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013324 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013325
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013326 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013327 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013329 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013331 --todo;
13332 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013333
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013334 li = listitem_alloc();
13335 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013336 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013337 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013338
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013339 if (what == 0)
13340 {
13341 /* keys() */
13342 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013343 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013344 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13345 }
13346 else if (what == 1)
13347 {
13348 /* values() */
13349 copy_tv(&di->di_tv, &li->li_tv);
13350 }
13351 else
13352 {
13353 /* items() */
13354 l2 = list_alloc();
13355 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013356 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013357 li->li_tv.vval.v_list = l2;
13358 if (l2 == NULL)
13359 break;
13360 ++l2->lv_refcount;
13361
13362 li2 = listitem_alloc();
13363 if (li2 == NULL)
13364 break;
13365 list_append(l2, li2);
13366 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013367 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013368 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13369
13370 li2 = listitem_alloc();
13371 if (li2 == NULL)
13372 break;
13373 list_append(l2, li2);
13374 copy_tv(&di->di_tv, &li2->li_tv);
13375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013376 }
13377 }
13378}
13379
13380/*
13381 * "items(dict)" function
13382 */
13383 static void
13384f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013385 typval_T *argvars;
13386 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013387{
13388 dict_list(argvars, rettv, 2);
13389}
13390
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013392 * "join()" function
13393 */
13394 static void
13395f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013396 typval_T *argvars;
13397 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013398{
13399 garray_T ga;
13400 char_u *sep;
13401
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013402 if (argvars[0].v_type != VAR_LIST)
13403 {
13404 EMSG(_(e_listreq));
13405 return;
13406 }
13407 if (argvars[0].vval.v_list == NULL)
13408 return;
13409 if (argvars[1].v_type == VAR_UNKNOWN)
13410 sep = (char_u *)" ";
13411 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013412 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013413
13414 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415
13416 if (sep != NULL)
13417 {
13418 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013419 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013420 ga_append(&ga, NUL);
13421 rettv->vval.v_string = (char_u *)ga.ga_data;
13422 }
13423 else
13424 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013425}
13426
13427/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013428 * "keys()" function
13429 */
13430 static void
13431f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013432 typval_T *argvars;
13433 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013434{
13435 dict_list(argvars, rettv, 0);
13436}
13437
13438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 * "last_buffer_nr()" function.
13440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013443 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445{
13446 int n = 0;
13447 buf_T *buf;
13448
13449 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13450 if (n < buf->b_fnum)
13451 n = buf->b_fnum;
13452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013453 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013454}
13455
13456/*
13457 * "len()" function
13458 */
13459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013460f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013461 typval_T *argvars;
13462 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013463{
13464 switch (argvars[0].v_type)
13465 {
13466 case VAR_STRING:
13467 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013468 rettv->vval.v_number = (varnumber_T)STRLEN(
13469 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013470 break;
13471 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013473 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013474 case VAR_DICT:
13475 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13476 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013477 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013478 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013479 break;
13480 }
13481}
13482
Bram Moolenaar33570922005-01-25 22:26:29 +000013483static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013484
13485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013486libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013487 typval_T *argvars;
13488 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013489 int type;
13490{
13491#ifdef FEAT_LIBCALL
13492 char_u *string_in;
13493 char_u **string_result;
13494 int nr_result;
13495#endif
13496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013497 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013498 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013500
13501 if (check_restricted() || check_secure())
13502 return;
13503
13504#ifdef FEAT_LIBCALL
13505 /* The first two args must be strings, otherwise its meaningless */
13506 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13507 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013508 string_in = NULL;
13509 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013510 string_in = argvars[2].vval.v_string;
13511 if (type == VAR_NUMBER)
13512 string_result = NULL;
13513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013515 if (mch_libcall(argvars[0].vval.v_string,
13516 argvars[1].vval.v_string,
13517 string_in,
13518 argvars[2].vval.v_number,
13519 string_result,
13520 &nr_result) == OK
13521 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013523 }
13524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525}
13526
13527/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013528 * "libcall()" function
13529 */
13530 static void
13531f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 typval_T *argvars;
13533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013534{
13535 libcall_common(argvars, rettv, VAR_STRING);
13536}
13537
13538/*
13539 * "libcallnr()" function
13540 */
13541 static void
13542f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013543 typval_T *argvars;
13544 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545{
13546 libcall_common(argvars, rettv, VAR_NUMBER);
13547}
13548
13549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 * "line(string)" function
13551 */
13552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *argvars;
13555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556{
13557 linenr_T lnum = 0;
13558 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013559 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013561 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 if (fp != NULL)
13563 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013564 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565}
13566
13567/*
13568 * "line2byte(lnum)" function
13569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013572 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574{
13575#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577#else
13578 linenr_T lnum;
13579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013580 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13585 if (rettv->vval.v_number >= 0)
13586 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587#endif
13588}
13589
13590/*
13591 * "lispindent(lnum)" function
13592 */
13593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013595 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597{
13598#ifdef FEAT_LISP
13599 pos_T pos;
13600 linenr_T lnum;
13601
13602 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13605 {
13606 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 curwin->w_cursor = pos;
13609 }
13610 else
13611#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613}
13614
13615/*
13616 * "localtime()" function
13617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013620 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624}
13625
Bram Moolenaar33570922005-01-25 22:26:29 +000013626static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627
13628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013630 typval_T *argvars;
13631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 int exact;
13633{
13634 char_u *keys;
13635 char_u *which;
13636 char_u buf[NUMBUFLEN];
13637 char_u *keys_buf = NULL;
13638 char_u *rhs;
13639 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013640 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013641 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013642 mapblock_T *mp;
13643 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644
13645 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 rettv->v_type = VAR_STRING;
13647 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 if (*keys == NUL)
13651 return;
13652
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013653 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013656 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013657 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013658 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013659 if (argvars[3].v_type != VAR_UNKNOWN)
13660 get_dict = get_tv_number(&argvars[3]);
13661 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 else
13664 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 if (which == NULL)
13666 return;
13667
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 mode = get_map_mode(&which, 0);
13669
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013670 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013671 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013673
13674 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013676 /* Return a string. */
13677 if (rhs != NULL)
13678 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679
Bram Moolenaarbd743252010-10-20 21:23:33 +020013680 }
13681 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13682 {
13683 /* Return a dictionary. */
13684 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13685 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13686 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
Bram Moolenaarbd743252010-10-20 21:23:33 +020013688 dict_add_nr_str(dict, "lhs", 0L, lhs);
13689 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13690 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13691 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13692 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13693 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13694 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13695 dict_add_nr_str(dict, "mode", 0L, mapmode);
13696
13697 vim_free(lhs);
13698 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 }
13700}
13701
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013702#ifdef FEAT_FLOAT
13703/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013704 * "log()" function
13705 */
13706 static void
13707f_log(argvars, rettv)
13708 typval_T *argvars;
13709 typval_T *rettv;
13710{
13711 float_T f;
13712
13713 rettv->v_type = VAR_FLOAT;
13714 if (get_float_arg(argvars, &f) == OK)
13715 rettv->vval.v_float = log(f);
13716 else
13717 rettv->vval.v_float = 0.0;
13718}
13719
13720/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013721 * "log10()" function
13722 */
13723 static void
13724f_log10(argvars, rettv)
13725 typval_T *argvars;
13726 typval_T *rettv;
13727{
13728 float_T f;
13729
13730 rettv->v_type = VAR_FLOAT;
13731 if (get_float_arg(argvars, &f) == OK)
13732 rettv->vval.v_float = log10(f);
13733 else
13734 rettv->vval.v_float = 0.0;
13735}
13736#endif
13737
Bram Moolenaar1dced572012-04-05 16:54:08 +020013738#ifdef FEAT_LUA
13739/*
13740 * "luaeval()" function
13741 */
13742 static void
13743f_luaeval(argvars, rettv)
13744 typval_T *argvars;
13745 typval_T *rettv;
13746{
13747 char_u *str;
13748 char_u buf[NUMBUFLEN];
13749
13750 str = get_tv_string_buf(&argvars[0], buf);
13751 do_luaeval(str, argvars + 1, rettv);
13752}
13753#endif
13754
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013756 * "map()" function
13757 */
13758 static void
13759f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013760 typval_T *argvars;
13761 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013762{
13763 filter_map(argvars, rettv, TRUE);
13764}
13765
13766/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013767 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 */
13769 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013770f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013771 typval_T *argvars;
13772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013774 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775}
13776
13777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013778 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 */
13780 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013781f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013782 typval_T *argvars;
13783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013785 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786}
13787
Bram Moolenaar33570922005-01-25 22:26:29 +000013788static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789
13790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013791find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 typval_T *argvars;
13793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 int type;
13795{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013796 char_u *str = NULL;
13797 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 char_u *pat;
13799 regmatch_T regmatch;
13800 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013801 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 char_u *save_cpo;
13803 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013804 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013805 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013806 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 list_T *l = NULL;
13808 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013809 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013810 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811
13812 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13813 save_cpo = p_cpo;
13814 p_cpo = (char_u *)"";
13815
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013816 rettv->vval.v_number = -1;
13817 if (type == 3)
13818 {
13819 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013820 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013821 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013822 }
13823 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013825 rettv->v_type = VAR_STRING;
13826 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013829 if (argvars[0].v_type == VAR_LIST)
13830 {
13831 if ((l = argvars[0].vval.v_list) == NULL)
13832 goto theend;
13833 li = l->lv_first;
13834 }
13835 else
13836 expr = str = get_tv_string(&argvars[0]);
13837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013838 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13839 if (pat == NULL)
13840 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013841
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 int error = FALSE;
13845
13846 start = get_tv_number_chk(&argvars[2], &error);
13847 if (error)
13848 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013849 if (l != NULL)
13850 {
13851 li = list_find(l, start);
13852 if (li == NULL)
13853 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013854 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013855 }
13856 else
13857 {
13858 if (start < 0)
13859 start = 0;
13860 if (start > (long)STRLEN(str))
13861 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013862 /* When "count" argument is there ignore matches before "start",
13863 * otherwise skip part of the string. Differs when pattern is "^"
13864 * or "\<". */
13865 if (argvars[3].v_type != VAR_UNKNOWN)
13866 startcol = start;
13867 else
13868 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013869 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013870
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013871 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013872 nth = get_tv_number_chk(&argvars[3], &error);
13873 if (error)
13874 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 }
13876
13877 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13878 if (regmatch.regprog != NULL)
13879 {
13880 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013881
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013882 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013883 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013884 if (l != NULL)
13885 {
13886 if (li == NULL)
13887 {
13888 match = FALSE;
13889 break;
13890 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013891 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013892 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013893 if (str == NULL)
13894 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013895 }
13896
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013897 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013898
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013899 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013900 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901 if (l == NULL && !match)
13902 break;
13903
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013904 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 if (l != NULL)
13906 {
13907 li = li->li_next;
13908 ++idx;
13909 }
13910 else
13911 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013912#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013913 startcol = (colnr_T)(regmatch.startp[0]
13914 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013915#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013916 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013917#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013918 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013919 }
13920
13921 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013923 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013924 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013925 int i;
13926
13927 /* return list with matched string and submatches */
13928 for (i = 0; i < NSUBEXP; ++i)
13929 {
13930 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013931 {
13932 if (list_append_string(rettv->vval.v_list,
13933 (char_u *)"", 0) == FAIL)
13934 break;
13935 }
13936 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013937 regmatch.startp[i],
13938 (int)(regmatch.endp[i] - regmatch.startp[i]))
13939 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013940 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013941 }
13942 }
13943 else if (type == 2)
13944 {
13945 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013946 if (l != NULL)
13947 copy_tv(&li->li_tv, rettv);
13948 else
13949 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013951 }
13952 else if (l != NULL)
13953 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 else
13955 {
13956 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 (varnumber_T)(regmatch.startp[0] - str);
13959 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013962 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 }
13964 }
13965 vim_free(regmatch.regprog);
13966 }
13967
13968theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013969 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 p_cpo = save_cpo;
13971}
13972
13973/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013974 * "match()" function
13975 */
13976 static void
13977f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013978 typval_T *argvars;
13979 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013980{
13981 find_some_match(argvars, rettv, 1);
13982}
13983
13984/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013985 * "matchadd()" function
13986 */
13987 static void
13988f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013989 typval_T *argvars UNUSED;
13990 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013991{
13992#ifdef FEAT_SEARCH_EXTRA
13993 char_u buf[NUMBUFLEN];
13994 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13995 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13996 int prio = 10; /* default priority */
13997 int id = -1;
13998 int error = FALSE;
13999
14000 rettv->vval.v_number = -1;
14001
14002 if (grp == NULL || pat == NULL)
14003 return;
14004 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014005 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014006 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014007 if (argvars[3].v_type != VAR_UNKNOWN)
14008 id = get_tv_number_chk(&argvars[3], &error);
14009 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014010 if (error == TRUE)
14011 return;
14012 if (id >= 1 && id <= 3)
14013 {
14014 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14015 return;
14016 }
14017
14018 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14019#endif
14020}
14021
14022/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014023 * "matcharg()" function
14024 */
14025 static void
14026f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014027 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014028 typval_T *rettv;
14029{
14030 if (rettv_list_alloc(rettv) == OK)
14031 {
14032#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014033 int id = get_tv_number(&argvars[0]);
14034 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014035
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014036 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014037 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014038 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14039 {
14040 list_append_string(rettv->vval.v_list,
14041 syn_id2name(m->hlg_id), -1);
14042 list_append_string(rettv->vval.v_list, m->pattern, -1);
14043 }
14044 else
14045 {
14046 list_append_string(rettv->vval.v_list, NUL, -1);
14047 list_append_string(rettv->vval.v_list, NUL, -1);
14048 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014049 }
14050#endif
14051 }
14052}
14053
14054/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014055 * "matchdelete()" function
14056 */
14057 static void
14058f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014059 typval_T *argvars UNUSED;
14060 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014061{
14062#ifdef FEAT_SEARCH_EXTRA
14063 rettv->vval.v_number = match_delete(curwin,
14064 (int)get_tv_number(&argvars[0]), TRUE);
14065#endif
14066}
14067
14068/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014069 * "matchend()" function
14070 */
14071 static void
14072f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014073 typval_T *argvars;
14074 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014075{
14076 find_some_match(argvars, rettv, 0);
14077}
14078
14079/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014080 * "matchlist()" function
14081 */
14082 static void
14083f_matchlist(argvars, rettv)
14084 typval_T *argvars;
14085 typval_T *rettv;
14086{
14087 find_some_match(argvars, rettv, 3);
14088}
14089
14090/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014091 * "matchstr()" function
14092 */
14093 static void
14094f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014095 typval_T *argvars;
14096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097{
14098 find_some_match(argvars, rettv, 2);
14099}
14100
Bram Moolenaar33570922005-01-25 22:26:29 +000014101static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014102
14103 static void
14104max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014105 typval_T *argvars;
14106 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014107 int domax;
14108{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014109 long n = 0;
14110 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014111 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014112
14113 if (argvars[0].v_type == VAR_LIST)
14114 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014115 list_T *l;
14116 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014117
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014118 l = argvars[0].vval.v_list;
14119 if (l != NULL)
14120 {
14121 li = l->lv_first;
14122 if (li != NULL)
14123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014125 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014126 {
14127 li = li->li_next;
14128 if (li == NULL)
14129 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014131 if (domax ? i > n : i < n)
14132 n = i;
14133 }
14134 }
14135 }
14136 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014137 else if (argvars[0].v_type == VAR_DICT)
14138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014139 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014140 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014141 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014142 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014143
14144 d = argvars[0].vval.v_dict;
14145 if (d != NULL)
14146 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014147 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014148 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014149 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014150 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014151 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014152 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014154 if (first)
14155 {
14156 n = i;
14157 first = FALSE;
14158 }
14159 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014160 n = i;
14161 }
14162 }
14163 }
14164 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014165 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014166 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014168}
14169
14170/*
14171 * "max()" function
14172 */
14173 static void
14174f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014175 typval_T *argvars;
14176 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014177{
14178 max_min(argvars, rettv, TRUE);
14179}
14180
14181/*
14182 * "min()" function
14183 */
14184 static void
14185f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014186 typval_T *argvars;
14187 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014188{
14189 max_min(argvars, rettv, FALSE);
14190}
14191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014192static int mkdir_recurse __ARGS((char_u *dir, int prot));
14193
14194/*
14195 * Create the directory in which "dir" is located, and higher levels when
14196 * needed.
14197 */
14198 static int
14199mkdir_recurse(dir, prot)
14200 char_u *dir;
14201 int prot;
14202{
14203 char_u *p;
14204 char_u *updir;
14205 int r = FAIL;
14206
14207 /* Get end of directory name in "dir".
14208 * We're done when it's "/" or "c:/". */
14209 p = gettail_sep(dir);
14210 if (p <= get_past_head(dir))
14211 return OK;
14212
14213 /* If the directory exists we're done. Otherwise: create it.*/
14214 updir = vim_strnsave(dir, (int)(p - dir));
14215 if (updir == NULL)
14216 return FAIL;
14217 if (mch_isdir(updir))
14218 r = OK;
14219 else if (mkdir_recurse(updir, prot) == OK)
14220 r = vim_mkdir_emsg(updir, prot);
14221 vim_free(updir);
14222 return r;
14223}
14224
14225#ifdef vim_mkdir
14226/*
14227 * "mkdir()" function
14228 */
14229 static void
14230f_mkdir(argvars, rettv)
14231 typval_T *argvars;
14232 typval_T *rettv;
14233{
14234 char_u *dir;
14235 char_u buf[NUMBUFLEN];
14236 int prot = 0755;
14237
14238 rettv->vval.v_number = FAIL;
14239 if (check_restricted() || check_secure())
14240 return;
14241
14242 dir = get_tv_string_buf(&argvars[0], buf);
14243 if (argvars[1].v_type != VAR_UNKNOWN)
14244 {
14245 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 prot = get_tv_number_chk(&argvars[2], NULL);
14247 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014248 mkdir_recurse(dir, prot);
14249 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014251}
14252#endif
14253
Bram Moolenaar0d660222005-01-07 21:51:51 +000014254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 * "mode()" function
14256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014259 typval_T *argvars;
14260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014262 char_u buf[3];
14263
14264 buf[1] = NUL;
14265 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266
14267#ifdef FEAT_VISUAL
14268 if (VIsual_active)
14269 {
14270 if (VIsual_select)
14271 buf[0] = VIsual_mode + 's' - 'v';
14272 else
14273 buf[0] = VIsual_mode;
14274 }
14275 else
14276#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014277 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14278 || State == CONFIRM)
14279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014281 if (State == ASKMORE)
14282 buf[1] = 'm';
14283 else if (State == CONFIRM)
14284 buf[1] = '?';
14285 }
14286 else if (State == EXTERNCMD)
14287 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 else if (State & INSERT)
14289 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014290#ifdef FEAT_VREPLACE
14291 if (State & VREPLACE_FLAG)
14292 {
14293 buf[0] = 'R';
14294 buf[1] = 'v';
14295 }
14296 else
14297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 if (State & REPLACE_FLAG)
14299 buf[0] = 'R';
14300 else
14301 buf[0] = 'i';
14302 }
14303 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014306 if (exmode_active)
14307 buf[1] = 'v';
14308 }
14309 else if (exmode_active)
14310 {
14311 buf[0] = 'c';
14312 buf[1] = 'e';
14313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014317 if (finish_op)
14318 buf[1] = 'o';
14319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014321 /* Clear out the minor mode when the argument is not a non-zero number or
14322 * non-empty string. */
14323 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014324 buf[1] = NUL;
14325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014326 rettv->vval.v_string = vim_strsave(buf);
14327 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328}
14329
Bram Moolenaar429fa852013-04-15 12:27:36 +020014330#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014331/*
14332 * "mzeval()" function
14333 */
14334 static void
14335f_mzeval(argvars, rettv)
14336 typval_T *argvars;
14337 typval_T *rettv;
14338{
14339 char_u *str;
14340 char_u buf[NUMBUFLEN];
14341
14342 str = get_tv_string_buf(&argvars[0], buf);
14343 do_mzeval(str, rettv);
14344}
Bram Moolenaar75676462013-01-30 14:55:42 +010014345
14346 void
14347mzscheme_call_vim(name, args, rettv)
14348 char_u *name;
14349 typval_T *args;
14350 typval_T *rettv;
14351{
14352 typval_T argvars[3];
14353
14354 argvars[0].v_type = VAR_STRING;
14355 argvars[0].vval.v_string = name;
14356 copy_tv(args, &argvars[1]);
14357 argvars[2].v_type = VAR_UNKNOWN;
14358 f_call(argvars, rettv);
14359 clear_tv(&argvars[1]);
14360}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014361#endif
14362
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014364 * "nextnonblank()" function
14365 */
14366 static void
14367f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014368 typval_T *argvars;
14369 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014370{
14371 linenr_T lnum;
14372
14373 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014376 {
14377 lnum = 0;
14378 break;
14379 }
14380 if (*skipwhite(ml_get(lnum)) != NUL)
14381 break;
14382 }
14383 rettv->vval.v_number = lnum;
14384}
14385
14386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 * "nr2char()" function
14388 */
14389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014391 typval_T *argvars;
14392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393{
14394 char_u buf[NUMBUFLEN];
14395
14396#ifdef FEAT_MBYTE
14397 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014398 {
14399 int utf8 = 0;
14400
14401 if (argvars[1].v_type != VAR_UNKNOWN)
14402 utf8 = get_tv_number_chk(&argvars[1], NULL);
14403 if (utf8)
14404 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14405 else
14406 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 else
14409#endif
14410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 buf[1] = NUL;
14413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014414 rettv->v_type = VAR_STRING;
14415 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014416}
14417
14418/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014419 * "or(expr, expr)" function
14420 */
14421 static void
14422f_or(argvars, rettv)
14423 typval_T *argvars;
14424 typval_T *rettv;
14425{
14426 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14427 | get_tv_number_chk(&argvars[1], NULL);
14428}
14429
14430/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014431 * "pathshorten()" function
14432 */
14433 static void
14434f_pathshorten(argvars, rettv)
14435 typval_T *argvars;
14436 typval_T *rettv;
14437{
14438 char_u *p;
14439
14440 rettv->v_type = VAR_STRING;
14441 p = get_tv_string_chk(&argvars[0]);
14442 if (p == NULL)
14443 rettv->vval.v_string = NULL;
14444 else
14445 {
14446 p = vim_strsave(p);
14447 rettv->vval.v_string = p;
14448 if (p != NULL)
14449 shorten_dir(p);
14450 }
14451}
14452
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014453#ifdef FEAT_FLOAT
14454/*
14455 * "pow()" function
14456 */
14457 static void
14458f_pow(argvars, rettv)
14459 typval_T *argvars;
14460 typval_T *rettv;
14461{
14462 float_T fx, fy;
14463
14464 rettv->v_type = VAR_FLOAT;
14465 if (get_float_arg(argvars, &fx) == OK
14466 && get_float_arg(&argvars[1], &fy) == OK)
14467 rettv->vval.v_float = pow(fx, fy);
14468 else
14469 rettv->vval.v_float = 0.0;
14470}
14471#endif
14472
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474 * "prevnonblank()" function
14475 */
14476 static void
14477f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 typval_T *argvars;
14479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480{
14481 linenr_T lnum;
14482
14483 lnum = get_tv_lnum(argvars);
14484 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14485 lnum = 0;
14486 else
14487 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14488 --lnum;
14489 rettv->vval.v_number = lnum;
14490}
14491
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014492#ifdef HAVE_STDARG_H
14493/* This dummy va_list is here because:
14494 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14495 * - locally in the function results in a "used before set" warning
14496 * - using va_start() to initialize it gives "function with fixed args" error */
14497static va_list ap;
14498#endif
14499
Bram Moolenaar8c711452005-01-14 21:53:12 +000014500/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014501 * "printf()" function
14502 */
14503 static void
14504f_printf(argvars, rettv)
14505 typval_T *argvars;
14506 typval_T *rettv;
14507{
14508 rettv->v_type = VAR_STRING;
14509 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014510#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014511 {
14512 char_u buf[NUMBUFLEN];
14513 int len;
14514 char_u *s;
14515 int saved_did_emsg = did_emsg;
14516 char *fmt;
14517
14518 /* Get the required length, allocate the buffer and do it for real. */
14519 did_emsg = FALSE;
14520 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014521 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014522 if (!did_emsg)
14523 {
14524 s = alloc(len + 1);
14525 if (s != NULL)
14526 {
14527 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014528 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014529 }
14530 }
14531 did_emsg |= saved_did_emsg;
14532 }
14533#endif
14534}
14535
14536/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014537 * "pumvisible()" function
14538 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014539 static void
14540f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014541 typval_T *argvars UNUSED;
14542 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014543{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014544#ifdef FEAT_INS_EXPAND
14545 if (pum_visible())
14546 rettv->vval.v_number = 1;
14547#endif
14548}
14549
Bram Moolenaardb913952012-06-29 12:54:53 +020014550#ifdef FEAT_PYTHON3
14551/*
14552 * "py3eval()" function
14553 */
14554 static void
14555f_py3eval(argvars, rettv)
14556 typval_T *argvars;
14557 typval_T *rettv;
14558{
14559 char_u *str;
14560 char_u buf[NUMBUFLEN];
14561
14562 str = get_tv_string_buf(&argvars[0], buf);
14563 do_py3eval(str, rettv);
14564}
14565#endif
14566
14567#ifdef FEAT_PYTHON
14568/*
14569 * "pyeval()" function
14570 */
14571 static void
14572f_pyeval(argvars, rettv)
14573 typval_T *argvars;
14574 typval_T *rettv;
14575{
14576 char_u *str;
14577 char_u buf[NUMBUFLEN];
14578
14579 str = get_tv_string_buf(&argvars[0], buf);
14580 do_pyeval(str, rettv);
14581}
14582#endif
14583
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014584/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014585 * "range()" function
14586 */
14587 static void
14588f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014589 typval_T *argvars;
14590 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014591{
14592 long start;
14593 long end;
14594 long stride = 1;
14595 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014596 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014599 if (argvars[1].v_type == VAR_UNKNOWN)
14600 {
14601 end = start - 1;
14602 start = 0;
14603 }
14604 else
14605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014606 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014607 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014609 }
14610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 if (error)
14612 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014613 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014614 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014615 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014616 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014617 else
14618 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014619 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014620 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014621 if (list_append_number(rettv->vval.v_list,
14622 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014623 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014624 }
14625}
14626
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014627/*
14628 * "readfile()" function
14629 */
14630 static void
14631f_readfile(argvars, rettv)
14632 typval_T *argvars;
14633 typval_T *rettv;
14634{
14635 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014636 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014637 char_u *fname;
14638 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014639 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14640 int io_size = sizeof(buf);
14641 int readlen; /* size of last fread() */
14642 char_u *prev = NULL; /* previously read bytes, if any */
14643 long prevlen = 0; /* length of data in prev */
14644 long prevsize = 0; /* size of prev buffer */
14645 long maxline = MAXLNUM;
14646 long cnt = 0;
14647 char_u *p; /* position in buf */
14648 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014649
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014650 if (argvars[1].v_type != VAR_UNKNOWN)
14651 {
14652 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14653 binary = TRUE;
14654 if (argvars[2].v_type != VAR_UNKNOWN)
14655 maxline = get_tv_number(&argvars[2]);
14656 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014657
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014658 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014659 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014660
14661 /* Always open the file in binary mode, library functions have a mind of
14662 * their own about CR-LF conversion. */
14663 fname = get_tv_string(&argvars[0]);
14664 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14665 {
14666 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14667 return;
14668 }
14669
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014670 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014671 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014672 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014673
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014674 /* This for loop processes what was read, but is also entered at end
14675 * of file so that either:
14676 * - an incomplete line gets written
14677 * - a "binary" file gets an empty line at the end if it ends in a
14678 * newline. */
14679 for (p = buf, start = buf;
14680 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14681 ++p)
14682 {
14683 if (*p == '\n' || readlen <= 0)
14684 {
14685 listitem_T *li;
14686 char_u *s = NULL;
14687 long_u len = p - start;
14688
14689 /* Finished a line. Remove CRs before NL. */
14690 if (readlen > 0 && !binary)
14691 {
14692 while (len > 0 && start[len - 1] == '\r')
14693 --len;
14694 /* removal may cross back to the "prev" string */
14695 if (len == 0)
14696 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14697 --prevlen;
14698 }
14699 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014700 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014701 else
14702 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014703 /* Change "prev" buffer to be the right size. This way
14704 * the bytes are only copied once, and very long lines are
14705 * allocated only once. */
14706 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014707 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014708 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014709 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014710 prev = NULL; /* the list will own the string */
14711 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014712 }
14713 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014714 if (s == NULL)
14715 {
14716 do_outofmem_msg((long_u) prevlen + len + 1);
14717 failed = TRUE;
14718 break;
14719 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014720
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014721 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014722 {
14723 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014724 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014725 break;
14726 }
14727 li->li_tv.v_type = VAR_STRING;
14728 li->li_tv.v_lock = 0;
14729 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014730 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014731
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014732 start = p + 1; /* step over newline */
14733 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014734 break;
14735 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014736 else if (*p == NUL)
14737 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014738#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014739 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14740 * when finding the BF and check the previous two bytes. */
14741 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014742 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014743 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14744 * + 1, these may be in the "prev" string. */
14745 char_u back1 = p >= buf + 1 ? p[-1]
14746 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14747 char_u back2 = p >= buf + 2 ? p[-2]
14748 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14749 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014750
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014751 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 char_u *dest = p - 2;
14754
14755 /* Usually a BOM is at the beginning of a file, and so at
14756 * the beginning of a line; then we can just step over it.
14757 */
14758 if (start == dest)
14759 start = p + 1;
14760 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014761 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014762 /* have to shuffle buf to close gap */
14763 int adjust_prevlen = 0;
14764
14765 if (dest < buf)
14766 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014767 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014768 dest = buf;
14769 }
14770 if (readlen > p - buf + 1)
14771 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14772 readlen -= 3 - adjust_prevlen;
14773 prevlen -= adjust_prevlen;
14774 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014775 }
14776 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014777 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014778#endif
14779 } /* for */
14780
14781 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14782 break;
14783 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014784 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014785 /* There's part of a line in buf, store it in "prev". */
14786 if (p - start + prevlen >= prevsize)
14787 {
14788 /* need bigger "prev" buffer */
14789 char_u *newprev;
14790
14791 /* A common use case is ordinary text files and "prev" gets a
14792 * fragment of a line, so the first allocation is made
14793 * small, to avoid repeatedly 'allocing' large and
14794 * 'reallocing' small. */
14795 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014796 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014797 else
14798 {
14799 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014800 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014801 prevsize = grow50pc > growmin ? grow50pc : growmin;
14802 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014803 newprev = prev == NULL ? alloc(prevsize)
14804 : vim_realloc(prev, prevsize);
14805 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014806 {
14807 do_outofmem_msg((long_u)prevsize);
14808 failed = TRUE;
14809 break;
14810 }
14811 prev = newprev;
14812 }
14813 /* Add the line part to end of "prev". */
14814 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014815 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014816 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014817 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014818
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014819 /*
14820 * For a negative line count use only the lines at the end of the file,
14821 * free the rest.
14822 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014823 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014824 while (cnt > -maxline)
14825 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014826 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014827 --cnt;
14828 }
14829
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014830 if (failed)
14831 {
14832 list_free(rettv->vval.v_list, TRUE);
14833 /* readfile doc says an empty list is returned on error */
14834 rettv->vval.v_list = list_alloc();
14835 }
14836
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014837 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014838 fclose(fd);
14839}
14840
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014841#if defined(FEAT_RELTIME)
14842static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14843
14844/*
14845 * Convert a List to proftime_T.
14846 * Return FAIL when there is something wrong.
14847 */
14848 static int
14849list2proftime(arg, tm)
14850 typval_T *arg;
14851 proftime_T *tm;
14852{
14853 long n1, n2;
14854 int error = FALSE;
14855
14856 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14857 || arg->vval.v_list->lv_len != 2)
14858 return FAIL;
14859 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14860 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14861# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014862 tm->HighPart = n1;
14863 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014864# else
14865 tm->tv_sec = n1;
14866 tm->tv_usec = n2;
14867# endif
14868 return error ? FAIL : OK;
14869}
14870#endif /* FEAT_RELTIME */
14871
14872/*
14873 * "reltime()" function
14874 */
14875 static void
14876f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014877 typval_T *argvars UNUSED;
14878 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014879{
14880#ifdef FEAT_RELTIME
14881 proftime_T res;
14882 proftime_T start;
14883
14884 if (argvars[0].v_type == VAR_UNKNOWN)
14885 {
14886 /* No arguments: get current time. */
14887 profile_start(&res);
14888 }
14889 else if (argvars[1].v_type == VAR_UNKNOWN)
14890 {
14891 if (list2proftime(&argvars[0], &res) == FAIL)
14892 return;
14893 profile_end(&res);
14894 }
14895 else
14896 {
14897 /* Two arguments: compute the difference. */
14898 if (list2proftime(&argvars[0], &start) == FAIL
14899 || list2proftime(&argvars[1], &res) == FAIL)
14900 return;
14901 profile_sub(&res, &start);
14902 }
14903
14904 if (rettv_list_alloc(rettv) == OK)
14905 {
14906 long n1, n2;
14907
14908# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014909 n1 = res.HighPart;
14910 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014911# else
14912 n1 = res.tv_sec;
14913 n2 = res.tv_usec;
14914# endif
14915 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14916 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14917 }
14918#endif
14919}
14920
14921/*
14922 * "reltimestr()" function
14923 */
14924 static void
14925f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014926 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014927 typval_T *rettv;
14928{
14929#ifdef FEAT_RELTIME
14930 proftime_T tm;
14931#endif
14932
14933 rettv->v_type = VAR_STRING;
14934 rettv->vval.v_string = NULL;
14935#ifdef FEAT_RELTIME
14936 if (list2proftime(&argvars[0], &tm) == OK)
14937 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14938#endif
14939}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014940
Bram Moolenaar0d660222005-01-07 21:51:51 +000014941#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14942static void make_connection __ARGS((void));
14943static int check_connection __ARGS((void));
14944
14945 static void
14946make_connection()
14947{
14948 if (X_DISPLAY == NULL
14949# ifdef FEAT_GUI
14950 && !gui.in_use
14951# endif
14952 )
14953 {
14954 x_force_connect = TRUE;
14955 setup_term_clip();
14956 x_force_connect = FALSE;
14957 }
14958}
14959
14960 static int
14961check_connection()
14962{
14963 make_connection();
14964 if (X_DISPLAY == NULL)
14965 {
14966 EMSG(_("E240: No connection to Vim server"));
14967 return FAIL;
14968 }
14969 return OK;
14970}
14971#endif
14972
14973#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014974static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014975
14976 static void
14977remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014978 typval_T *argvars;
14979 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980 int expr;
14981{
14982 char_u *server_name;
14983 char_u *keys;
14984 char_u *r = NULL;
14985 char_u buf[NUMBUFLEN];
14986# ifdef WIN32
14987 HWND w;
14988# else
14989 Window w;
14990# endif
14991
14992 if (check_restricted() || check_secure())
14993 return;
14994
14995# ifdef FEAT_X11
14996 if (check_connection() == FAIL)
14997 return;
14998# endif
14999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015000 server_name = get_tv_string_chk(&argvars[0]);
15001 if (server_name == NULL)
15002 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003 keys = get_tv_string_buf(&argvars[1], buf);
15004# ifdef WIN32
15005 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15006# else
15007 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15008 < 0)
15009# endif
15010 {
15011 if (r != NULL)
15012 EMSG(r); /* sending worked but evaluation failed */
15013 else
15014 EMSG2(_("E241: Unable to send to %s"), server_name);
15015 return;
15016 }
15017
15018 rettv->vval.v_string = r;
15019
15020 if (argvars[2].v_type != VAR_UNKNOWN)
15021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015022 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015023 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015024 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015026 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 v.di_tv.v_type = VAR_STRING;
15028 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015029 idvar = get_tv_string_chk(&argvars[2]);
15030 if (idvar != NULL)
15031 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015033 }
15034}
15035#endif
15036
15037/*
15038 * "remote_expr()" function
15039 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040 static void
15041f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015042 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015043 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044{
15045 rettv->v_type = VAR_STRING;
15046 rettv->vval.v_string = NULL;
15047#ifdef FEAT_CLIENTSERVER
15048 remote_common(argvars, rettv, TRUE);
15049#endif
15050}
15051
15052/*
15053 * "remote_foreground()" function
15054 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 static void
15056f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015057 typval_T *argvars UNUSED;
15058 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060#ifdef FEAT_CLIENTSERVER
15061# ifdef WIN32
15062 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015063 {
15064 char_u *server_name = get_tv_string_chk(&argvars[0]);
15065
15066 if (server_name != NULL)
15067 serverForeground(server_name);
15068 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069# else
15070 /* Send a foreground() expression to the server. */
15071 argvars[1].v_type = VAR_STRING;
15072 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15073 argvars[2].v_type = VAR_UNKNOWN;
15074 remote_common(argvars, rettv, TRUE);
15075 vim_free(argvars[1].vval.v_string);
15076# endif
15077#endif
15078}
15079
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080 static void
15081f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015082 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015083 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084{
15085#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015086 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087 char_u *s = NULL;
15088# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015089 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015091 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092
15093 if (check_restricted() || check_secure())
15094 {
15095 rettv->vval.v_number = -1;
15096 return;
15097 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 serverid = get_tv_string_chk(&argvars[0]);
15099 if (serverid == NULL)
15100 {
15101 rettv->vval.v_number = -1;
15102 return; /* type error; errmsg already given */
15103 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015105 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106 if (n == 0)
15107 rettv->vval.v_number = -1;
15108 else
15109 {
15110 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15111 rettv->vval.v_number = (s != NULL);
15112 }
15113# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015114 if (check_connection() == FAIL)
15115 return;
15116
15117 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015118 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119# endif
15120
15121 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015123 char_u *retvar;
15124
Bram Moolenaar33570922005-01-25 22:26:29 +000015125 v.di_tv.v_type = VAR_STRING;
15126 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015127 retvar = get_tv_string_chk(&argvars[1]);
15128 if (retvar != NULL)
15129 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015130 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131 }
15132#else
15133 rettv->vval.v_number = -1;
15134#endif
15135}
15136
Bram Moolenaar0d660222005-01-07 21:51:51 +000015137 static void
15138f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015139 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015141{
15142 char_u *r = NULL;
15143
15144#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015145 char_u *serverid = get_tv_string_chk(&argvars[0]);
15146
15147 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015148 {
15149# ifdef WIN32
15150 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015151 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015153 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154 if (n != 0)
15155 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15156 if (r == NULL)
15157# else
15158 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015159 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160# endif
15161 EMSG(_("E277: Unable to read a server reply"));
15162 }
15163#endif
15164 rettv->v_type = VAR_STRING;
15165 rettv->vval.v_string = r;
15166}
15167
15168/*
15169 * "remote_send()" function
15170 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 static void
15172f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015173 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015174 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175{
15176 rettv->v_type = VAR_STRING;
15177 rettv->vval.v_string = NULL;
15178#ifdef FEAT_CLIENTSERVER
15179 remote_common(argvars, rettv, FALSE);
15180#endif
15181}
15182
15183/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015184 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015185 */
15186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015187f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015188 typval_T *argvars;
15189 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015190{
Bram Moolenaar33570922005-01-25 22:26:29 +000015191 list_T *l;
15192 listitem_T *item, *item2;
15193 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015194 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015195 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015196 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015197 dict_T *d;
15198 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015199 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015200
Bram Moolenaar8c711452005-01-14 21:53:12 +000015201 if (argvars[0].v_type == VAR_DICT)
15202 {
15203 if (argvars[2].v_type != VAR_UNKNOWN)
15204 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015205 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015206 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015208 key = get_tv_string_chk(&argvars[1]);
15209 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 di = dict_find(d, key, -1);
15212 if (di == NULL)
15213 EMSG2(_(e_dictkey), key);
15214 else
15215 {
15216 *rettv = di->di_tv;
15217 init_tv(&di->di_tv);
15218 dictitem_remove(d, di);
15219 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015220 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015221 }
15222 }
15223 else if (argvars[0].v_type != VAR_LIST)
15224 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015225 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015226 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015228 int error = FALSE;
15229
15230 idx = get_tv_number_chk(&argvars[1], &error);
15231 if (error)
15232 ; /* type error: do nothing, errmsg already given */
15233 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015234 EMSGN(_(e_listidx), idx);
15235 else
15236 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015237 if (argvars[2].v_type == VAR_UNKNOWN)
15238 {
15239 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015240 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015241 *rettv = item->li_tv;
15242 vim_free(item);
15243 }
15244 else
15245 {
15246 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 end = get_tv_number_chk(&argvars[2], &error);
15248 if (error)
15249 ; /* type error: do nothing */
15250 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015251 EMSGN(_(e_listidx), end);
15252 else
15253 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015254 int cnt = 0;
15255
15256 for (li = item; li != NULL; li = li->li_next)
15257 {
15258 ++cnt;
15259 if (li == item2)
15260 break;
15261 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015262 if (li == NULL) /* didn't find "item2" after "item" */
15263 EMSG(_(e_invrange));
15264 else
15265 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015266 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015267 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015268 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015269 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015270 l->lv_first = item;
15271 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015272 item->li_prev = NULL;
15273 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015274 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015275 }
15276 }
15277 }
15278 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015279 }
15280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281}
15282
15283/*
15284 * "rename({from}, {to})" function
15285 */
15286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015287f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015288 typval_T *argvars;
15289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290{
15291 char_u buf[NUMBUFLEN];
15292
15293 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015296 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15297 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298}
15299
15300/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015301 * "repeat()" function
15302 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015303 static void
15304f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015305 typval_T *argvars;
15306 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015307{
15308 char_u *p;
15309 int n;
15310 int slen;
15311 int len;
15312 char_u *r;
15313 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015314
15315 n = get_tv_number(&argvars[1]);
15316 if (argvars[0].v_type == VAR_LIST)
15317 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015318 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015319 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015320 if (list_extend(rettv->vval.v_list,
15321 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015322 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015323 }
15324 else
15325 {
15326 p = get_tv_string(&argvars[0]);
15327 rettv->v_type = VAR_STRING;
15328 rettv->vval.v_string = NULL;
15329
15330 slen = (int)STRLEN(p);
15331 len = slen * n;
15332 if (len <= 0)
15333 return;
15334
15335 r = alloc(len + 1);
15336 if (r != NULL)
15337 {
15338 for (i = 0; i < n; i++)
15339 mch_memmove(r + i * slen, p, (size_t)slen);
15340 r[len] = NUL;
15341 }
15342
15343 rettv->vval.v_string = r;
15344 }
15345}
15346
15347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348 * "resolve()" function
15349 */
15350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015352 typval_T *argvars;
15353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354{
15355 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015356#ifdef HAVE_READLINK
15357 char_u *buf = NULL;
15358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015360 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361#ifdef FEAT_SHORTCUT
15362 {
15363 char_u *v = NULL;
15364
15365 v = mch_resolve_shortcut(p);
15366 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015367 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015369 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 }
15371#else
15372# ifdef HAVE_READLINK
15373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 char_u *cpy;
15375 int len;
15376 char_u *remain = NULL;
15377 char_u *q;
15378 int is_relative_to_current = FALSE;
15379 int has_trailing_pathsep = FALSE;
15380 int limit = 100;
15381
15382 p = vim_strsave(p);
15383
15384 if (p[0] == '.' && (vim_ispathsep(p[1])
15385 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15386 is_relative_to_current = TRUE;
15387
15388 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015389 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015392 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394
15395 q = getnextcomp(p);
15396 if (*q != NUL)
15397 {
15398 /* Separate the first path component in "p", and keep the
15399 * remainder (beginning with the path separator). */
15400 remain = vim_strsave(q - 1);
15401 q[-1] = NUL;
15402 }
15403
Bram Moolenaard9462e32011-04-11 21:35:11 +020015404 buf = alloc(MAXPATHL + 1);
15405 if (buf == NULL)
15406 goto fail;
15407
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 for (;;)
15409 {
15410 for (;;)
15411 {
15412 len = readlink((char *)p, (char *)buf, MAXPATHL);
15413 if (len <= 0)
15414 break;
15415 buf[len] = NUL;
15416
15417 if (limit-- == 0)
15418 {
15419 vim_free(p);
15420 vim_free(remain);
15421 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015422 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 goto fail;
15424 }
15425
15426 /* Ensure that the result will have a trailing path separator
15427 * if the argument has one. */
15428 if (remain == NULL && has_trailing_pathsep)
15429 add_pathsep(buf);
15430
15431 /* Separate the first path component in the link value and
15432 * concatenate the remainders. */
15433 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15434 if (*q != NUL)
15435 {
15436 if (remain == NULL)
15437 remain = vim_strsave(q - 1);
15438 else
15439 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015440 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 if (cpy != NULL)
15442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 vim_free(remain);
15444 remain = cpy;
15445 }
15446 }
15447 q[-1] = NUL;
15448 }
15449
15450 q = gettail(p);
15451 if (q > p && *q == NUL)
15452 {
15453 /* Ignore trailing path separator. */
15454 q[-1] = NUL;
15455 q = gettail(p);
15456 }
15457 if (q > p && !mch_isFullName(buf))
15458 {
15459 /* symlink is relative to directory of argument */
15460 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15461 if (cpy != NULL)
15462 {
15463 STRCPY(cpy, p);
15464 STRCPY(gettail(cpy), buf);
15465 vim_free(p);
15466 p = cpy;
15467 }
15468 }
15469 else
15470 {
15471 vim_free(p);
15472 p = vim_strsave(buf);
15473 }
15474 }
15475
15476 if (remain == NULL)
15477 break;
15478
15479 /* Append the first path component of "remain" to "p". */
15480 q = getnextcomp(remain + 1);
15481 len = q - remain - (*q != NUL);
15482 cpy = vim_strnsave(p, STRLEN(p) + len);
15483 if (cpy != NULL)
15484 {
15485 STRNCAT(cpy, remain, len);
15486 vim_free(p);
15487 p = cpy;
15488 }
15489 /* Shorten "remain". */
15490 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015491 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 else
15493 {
15494 vim_free(remain);
15495 remain = NULL;
15496 }
15497 }
15498
15499 /* If the result is a relative path name, make it explicitly relative to
15500 * the current directory if and only if the argument had this form. */
15501 if (!vim_ispathsep(*p))
15502 {
15503 if (is_relative_to_current
15504 && *p != NUL
15505 && !(p[0] == '.'
15506 && (p[1] == NUL
15507 || vim_ispathsep(p[1])
15508 || (p[1] == '.'
15509 && (p[2] == NUL
15510 || vim_ispathsep(p[2]))))))
15511 {
15512 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015513 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 if (cpy != NULL)
15515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 vim_free(p);
15517 p = cpy;
15518 }
15519 }
15520 else if (!is_relative_to_current)
15521 {
15522 /* Strip leading "./". */
15523 q = p;
15524 while (q[0] == '.' && vim_ispathsep(q[1]))
15525 q += 2;
15526 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015527 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 }
15529 }
15530
15531 /* Ensure that the result will have no trailing path separator
15532 * if the argument had none. But keep "/" or "//". */
15533 if (!has_trailing_pathsep)
15534 {
15535 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015536 if (after_pathsep(p, q))
15537 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 }
15539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015540 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 }
15542# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015543 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544# endif
15545#endif
15546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548
15549#ifdef HAVE_READLINK
15550fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015551 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554}
15555
15556/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015557 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 */
15559 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015560f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015561 typval_T *argvars;
15562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563{
Bram Moolenaar33570922005-01-25 22:26:29 +000015564 list_T *l;
15565 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567 if (argvars[0].v_type != VAR_LIST)
15568 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015569 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015570 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571 {
15572 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015573 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015574 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015575 while (li != NULL)
15576 {
15577 ni = li->li_prev;
15578 list_append(l, li);
15579 li = ni;
15580 }
15581 rettv->vval.v_list = l;
15582 rettv->v_type = VAR_LIST;
15583 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015584 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586}
15587
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015588#define SP_NOMOVE 0x01 /* don't move cursor */
15589#define SP_REPEAT 0x02 /* repeat to find outer pair */
15590#define SP_RETCOUNT 0x04 /* return matchcount */
15591#define SP_SETPCMARK 0x08 /* set previous context mark */
15592#define SP_START 0x10 /* accept match at start position */
15593#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15594#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015595
Bram Moolenaar33570922005-01-25 22:26:29 +000015596static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597
15598/*
15599 * Get flags for a search function.
15600 * Possibly sets "p_ws".
15601 * Returns BACKWARD, FORWARD or zero (for an error).
15602 */
15603 static int
15604get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015605 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606 int *flagsp;
15607{
15608 int dir = FORWARD;
15609 char_u *flags;
15610 char_u nbuf[NUMBUFLEN];
15611 int mask;
15612
15613 if (varp->v_type != VAR_UNKNOWN)
15614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015615 flags = get_tv_string_buf_chk(varp, nbuf);
15616 if (flags == NULL)
15617 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015618 while (*flags != NUL)
15619 {
15620 switch (*flags)
15621 {
15622 case 'b': dir = BACKWARD; break;
15623 case 'w': p_ws = TRUE; break;
15624 case 'W': p_ws = FALSE; break;
15625 default: mask = 0;
15626 if (flagsp != NULL)
15627 switch (*flags)
15628 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015629 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015630 case 'e': mask = SP_END; break;
15631 case 'm': mask = SP_RETCOUNT; break;
15632 case 'n': mask = SP_NOMOVE; break;
15633 case 'p': mask = SP_SUBPAT; break;
15634 case 'r': mask = SP_REPEAT; break;
15635 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636 }
15637 if (mask == 0)
15638 {
15639 EMSG2(_(e_invarg2), flags);
15640 dir = 0;
15641 }
15642 else
15643 *flagsp |= mask;
15644 }
15645 if (dir == 0)
15646 break;
15647 ++flags;
15648 }
15649 }
15650 return dir;
15651}
15652
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015654 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015656 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015657search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015658 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015659 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015660 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015662 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 char_u *pat;
15664 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015665 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 int save_p_ws = p_ws;
15667 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015668 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015669 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015670 proftime_T tm;
15671#ifdef FEAT_RELTIME
15672 long time_limit = 0;
15673#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015674 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015675 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015677 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015678 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015679 if (dir == 0)
15680 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015681 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015682 if (flags & SP_START)
15683 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015684 if (flags & SP_END)
15685 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015686
Bram Moolenaar76929292008-01-06 19:07:36 +000015687 /* Optional arguments: line number to stop searching and timeout. */
15688 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015689 {
15690 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15691 if (lnum_stop < 0)
15692 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015693#ifdef FEAT_RELTIME
15694 if (argvars[3].v_type != VAR_UNKNOWN)
15695 {
15696 time_limit = get_tv_number_chk(&argvars[3], NULL);
15697 if (time_limit < 0)
15698 goto theend;
15699 }
15700#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015701 }
15702
Bram Moolenaar76929292008-01-06 19:07:36 +000015703#ifdef FEAT_RELTIME
15704 /* Set the time limit, if there is one. */
15705 profile_setlimit(time_limit, &tm);
15706#endif
15707
Bram Moolenaar231334e2005-07-25 20:46:57 +000015708 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015709 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015710 * Check to make sure only those flags are set.
15711 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15712 * flags cannot be set. Check for that condition also.
15713 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015714 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015715 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015717 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015718 goto theend;
15719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015721 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015722 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015723 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015724 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015726 if (flags & SP_SUBPAT)
15727 retval = subpatnum;
15728 else
15729 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015730 if (flags & SP_SETPCMARK)
15731 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015733 if (match_pos != NULL)
15734 {
15735 /* Store the match cursor position */
15736 match_pos->lnum = pos.lnum;
15737 match_pos->col = pos.col + 1;
15738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 /* "/$" will put the cursor after the end of the line, may need to
15740 * correct that here */
15741 check_cursor();
15742 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015743
15744 /* If 'n' flag is used: restore cursor position. */
15745 if (flags & SP_NOMOVE)
15746 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015747 else
15748 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015749theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015751
15752 return retval;
15753}
15754
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015755#ifdef FEAT_FLOAT
15756/*
15757 * "round({float})" function
15758 */
15759 static void
15760f_round(argvars, rettv)
15761 typval_T *argvars;
15762 typval_T *rettv;
15763{
15764 float_T f;
15765
15766 rettv->v_type = VAR_FLOAT;
15767 if (get_float_arg(argvars, &f) == OK)
15768 /* round() is not in C90, use ceil() or floor() instead. */
15769 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15770 else
15771 rettv->vval.v_float = 0.0;
15772}
15773#endif
15774
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015775/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015776 * "screencol()" function
15777 *
15778 * First column is 1 to be consistent with virtcol().
15779 */
15780 static void
15781f_screencol(argvars, rettv)
15782 typval_T *argvars UNUSED;
15783 typval_T *rettv;
15784{
15785 rettv->vval.v_number = screen_screencol() + 1;
15786}
15787
15788/*
15789 * "screenrow()" function
15790 */
15791 static void
15792f_screenrow(argvars, rettv)
15793 typval_T *argvars UNUSED;
15794 typval_T *rettv;
15795{
15796 rettv->vval.v_number = screen_screenrow() + 1;
15797}
15798
15799/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015800 * "search()" function
15801 */
15802 static void
15803f_search(argvars, rettv)
15804 typval_T *argvars;
15805 typval_T *rettv;
15806{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015807 int flags = 0;
15808
15809 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810}
15811
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015813 * "searchdecl()" function
15814 */
15815 static void
15816f_searchdecl(argvars, rettv)
15817 typval_T *argvars;
15818 typval_T *rettv;
15819{
15820 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015821 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015822 int error = FALSE;
15823 char_u *name;
15824
15825 rettv->vval.v_number = 1; /* default: FAIL */
15826
15827 name = get_tv_string_chk(&argvars[0]);
15828 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015829 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015830 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015831 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15832 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15833 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015834 if (!error && name != NULL)
15835 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015836 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015837}
15838
15839/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015840 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015842 static int
15843searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015844 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015845 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846{
15847 char_u *spat, *mpat, *epat;
15848 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 int dir;
15851 int flags = 0;
15852 char_u nbuf1[NUMBUFLEN];
15853 char_u nbuf2[NUMBUFLEN];
15854 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015855 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015856 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015857 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015860 spat = get_tv_string_chk(&argvars[0]);
15861 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15862 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15863 if (spat == NULL || mpat == NULL || epat == NULL)
15864 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 /* Handle the optional fourth argument: flags */
15867 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015868 if (dir == 0)
15869 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015870
15871 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015872 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15873 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015874 if ((flags & (SP_END | SP_SUBPAT)) != 0
15875 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015876 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015877 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015878 goto theend;
15879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015881 /* Using 'r' implies 'W', otherwise it doesn't work. */
15882 if (flags & SP_REPEAT)
15883 p_ws = FALSE;
15884
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015885 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015886 if (argvars[3].v_type == VAR_UNKNOWN
15887 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 skip = (char_u *)"";
15889 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015891 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015892 if (argvars[5].v_type != VAR_UNKNOWN)
15893 {
15894 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15895 if (lnum_stop < 0)
15896 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015897#ifdef FEAT_RELTIME
15898 if (argvars[6].v_type != VAR_UNKNOWN)
15899 {
15900 time_limit = get_tv_number_chk(&argvars[6], NULL);
15901 if (time_limit < 0)
15902 goto theend;
15903 }
15904#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015905 }
15906 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015907 if (skip == NULL)
15908 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015910 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015911 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015912
15913theend:
15914 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015915
15916 return retval;
15917}
15918
15919/*
15920 * "searchpair()" function
15921 */
15922 static void
15923f_searchpair(argvars, rettv)
15924 typval_T *argvars;
15925 typval_T *rettv;
15926{
15927 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15928}
15929
15930/*
15931 * "searchpairpos()" function
15932 */
15933 static void
15934f_searchpairpos(argvars, rettv)
15935 typval_T *argvars;
15936 typval_T *rettv;
15937{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015938 pos_T match_pos;
15939 int lnum = 0;
15940 int col = 0;
15941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015942 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015943 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015944
15945 if (searchpair_cmn(argvars, &match_pos) > 0)
15946 {
15947 lnum = match_pos.lnum;
15948 col = match_pos.col;
15949 }
15950
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015951 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15952 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015953}
15954
15955/*
15956 * Search for a start/middle/end thing.
15957 * Used by searchpair(), see its documentation for the details.
15958 * Returns 0 or -1 for no match,
15959 */
15960 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015961do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15962 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015963 char_u *spat; /* start pattern */
15964 char_u *mpat; /* middle pattern */
15965 char_u *epat; /* end pattern */
15966 int dir; /* BACKWARD or FORWARD */
15967 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015968 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015969 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015970 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015971 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015972{
15973 char_u *save_cpo;
15974 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15975 long retval = 0;
15976 pos_T pos;
15977 pos_T firstpos;
15978 pos_T foundpos;
15979 pos_T save_cursor;
15980 pos_T save_pos;
15981 int n;
15982 int r;
15983 int nest = 1;
15984 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015985 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015986 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015987
15988 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15989 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015990 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015991
Bram Moolenaar76929292008-01-06 19:07:36 +000015992#ifdef FEAT_RELTIME
15993 /* Set the time limit, if there is one. */
15994 profile_setlimit(time_limit, &tm);
15995#endif
15996
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015997 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15998 * start/middle/end (pat3, for the top pair). */
15999 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16000 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16001 if (pat2 == NULL || pat3 == NULL)
16002 goto theend;
16003 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16004 if (*mpat == NUL)
16005 STRCPY(pat3, pat2);
16006 else
16007 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16008 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016009 if (flags & SP_START)
16010 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016011
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 save_cursor = curwin->w_cursor;
16013 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016014 clearpos(&firstpos);
16015 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 pat = pat3;
16017 for (;;)
16018 {
16019 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016020 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16022 /* didn't find it or found the first match again: FAIL */
16023 break;
16024
16025 if (firstpos.lnum == 0)
16026 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016027 if (equalpos(pos, foundpos))
16028 {
16029 /* Found the same position again. Can happen with a pattern that
16030 * has "\zs" at the end and searching backwards. Advance one
16031 * character and try again. */
16032 if (dir == BACKWARD)
16033 decl(&pos);
16034 else
16035 incl(&pos);
16036 }
16037 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016039 /* clear the start flag to avoid getting stuck here */
16040 options &= ~SEARCH_START;
16041
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 /* If the skip pattern matches, ignore this match. */
16043 if (*skip != NUL)
16044 {
16045 save_pos = curwin->w_cursor;
16046 curwin->w_cursor = pos;
16047 r = eval_to_bool(skip, &err, NULL, FALSE);
16048 curwin->w_cursor = save_pos;
16049 if (err)
16050 {
16051 /* Evaluating {skip} caused an error, break here. */
16052 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016053 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 break;
16055 }
16056 if (r)
16057 continue;
16058 }
16059
16060 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16061 {
16062 /* Found end when searching backwards or start when searching
16063 * forward: nested pair. */
16064 ++nest;
16065 pat = pat2; /* nested, don't search for middle */
16066 }
16067 else
16068 {
16069 /* Found end when searching forward or start when searching
16070 * backward: end of (nested) pair; or found middle in outer pair. */
16071 if (--nest == 1)
16072 pat = pat3; /* outer level, search for middle */
16073 }
16074
16075 if (nest == 0)
16076 {
16077 /* Found the match: return matchcount or line number. */
16078 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016079 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016081 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016082 if (flags & SP_SETPCMARK)
16083 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016084 curwin->w_cursor = pos;
16085 if (!(flags & SP_REPEAT))
16086 break;
16087 nest = 1; /* search for next unmatched */
16088 }
16089 }
16090
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016091 if (match_pos != NULL)
16092 {
16093 /* Store the match cursor position */
16094 match_pos->lnum = curwin->w_cursor.lnum;
16095 match_pos->col = curwin->w_cursor.col + 1;
16096 }
16097
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016099 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 curwin->w_cursor = save_cursor;
16101
16102theend:
16103 vim_free(pat2);
16104 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016105 if (p_cpo == empty_option)
16106 p_cpo = save_cpo;
16107 else
16108 /* Darn, evaluating the {skip} expression changed the value. */
16109 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016110
16111 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112}
16113
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016114/*
16115 * "searchpos()" function
16116 */
16117 static void
16118f_searchpos(argvars, rettv)
16119 typval_T *argvars;
16120 typval_T *rettv;
16121{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016122 pos_T match_pos;
16123 int lnum = 0;
16124 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016125 int n;
16126 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016128 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016129 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016130
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016131 n = search_cmn(argvars, &match_pos, &flags);
16132 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016133 {
16134 lnum = match_pos.lnum;
16135 col = match_pos.col;
16136 }
16137
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016138 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16139 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016140 if (flags & SP_SUBPAT)
16141 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016142}
16143
16144
Bram Moolenaar0d660222005-01-07 21:51:51 +000016145 static void
16146f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150#ifdef FEAT_CLIENTSERVER
16151 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016152 char_u *server = get_tv_string_chk(&argvars[0]);
16153 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154
Bram Moolenaar0d660222005-01-07 21:51:51 +000016155 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016156 if (server == NULL || reply == NULL)
16157 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158 if (check_restricted() || check_secure())
16159 return;
16160# ifdef FEAT_X11
16161 if (check_connection() == FAIL)
16162 return;
16163# endif
16164
16165 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167 EMSG(_("E258: Unable to send to client"));
16168 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016170 rettv->vval.v_number = 0;
16171#else
16172 rettv->vval.v_number = -1;
16173#endif
16174}
16175
Bram Moolenaar0d660222005-01-07 21:51:51 +000016176 static void
16177f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016180{
16181 char_u *r = NULL;
16182
16183#ifdef FEAT_CLIENTSERVER
16184# ifdef WIN32
16185 r = serverGetVimNames();
16186# else
16187 make_connection();
16188 if (X_DISPLAY != NULL)
16189 r = serverGetVimNames(X_DISPLAY);
16190# endif
16191#endif
16192 rettv->v_type = VAR_STRING;
16193 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194}
16195
16196/*
16197 * "setbufvar()" function
16198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016200f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016201 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016202 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203{
16204 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016207 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 char_u nbuf[NUMBUFLEN];
16209
16210 if (check_restricted() || check_secure())
16211 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016212 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16213 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016214 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215 varp = &argvars[2];
16216
16217 if (buf != NULL && varname != NULL && varp != NULL)
16218 {
16219 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221
16222 if (*varname == '&')
16223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016224 long numval;
16225 char_u *strval;
16226 int error = FALSE;
16227
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016229 numval = get_tv_number_chk(varp, &error);
16230 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016231 if (!error && strval != NULL)
16232 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 }
16234 else
16235 {
16236 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16237 if (bufvarname != NULL)
16238 {
16239 STRCPY(bufvarname, "b:");
16240 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016241 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 vim_free(bufvarname);
16243 }
16244 }
16245
16246 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249}
16250
16251/*
16252 * "setcmdpos()" function
16253 */
16254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016255f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016256 typval_T *argvars;
16257 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 int pos = (int)get_tv_number(&argvars[0]) - 1;
16260
16261 if (pos >= 0)
16262 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263}
16264
16265/*
16266 * "setline()" function
16267 */
16268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016269f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016270 typval_T *argvars;
16271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272{
16273 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016274 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016275 list_T *l = NULL;
16276 listitem_T *li = NULL;
16277 long added = 0;
16278 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016280 lnum = get_tv_lnum(&argvars[0]);
16281 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016283 l = argvars[1].vval.v_list;
16284 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016286 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016287 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016288
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016289 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016290 for (;;)
16291 {
16292 if (l != NULL)
16293 {
16294 /* list argument, get next string */
16295 if (li == NULL)
16296 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016297 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016298 li = li->li_next;
16299 }
16300
16301 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016302 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016303 break;
16304 if (lnum <= curbuf->b_ml.ml_line_count)
16305 {
16306 /* existing line, replace it */
16307 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16308 {
16309 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016310 if (lnum == curwin->w_cursor.lnum)
16311 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016312 rettv->vval.v_number = 0; /* OK */
16313 }
16314 }
16315 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16316 {
16317 /* lnum is one past the last line, append the line */
16318 ++added;
16319 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16320 rettv->vval.v_number = 0; /* OK */
16321 }
16322
16323 if (l == NULL) /* only one string argument */
16324 break;
16325 ++lnum;
16326 }
16327
16328 if (added > 0)
16329 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330}
16331
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016332static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16333
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016335 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016336 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016337 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016338set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016339 win_T *wp UNUSED;
16340 typval_T *list_arg UNUSED;
16341 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016342 typval_T *rettv;
16343{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016344#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016345 char_u *act;
16346 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016347#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016348
Bram Moolenaar2641f772005-03-25 21:58:17 +000016349 rettv->vval.v_number = -1;
16350
16351#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016352 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016353 EMSG(_(e_listreq));
16354 else
16355 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016356 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016357
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016358 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016359 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016360 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016361 if (act == NULL)
16362 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016363 if (*act == 'a' || *act == 'r')
16364 action = *act;
16365 }
16366
Bram Moolenaar81484f42012-12-05 15:16:47 +010016367 if (l != NULL && set_errorlist(wp, l, action,
16368 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016369 rettv->vval.v_number = 0;
16370 }
16371#endif
16372}
16373
16374/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016375 * "setloclist()" function
16376 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016377 static void
16378f_setloclist(argvars, rettv)
16379 typval_T *argvars;
16380 typval_T *rettv;
16381{
16382 win_T *win;
16383
16384 rettv->vval.v_number = -1;
16385
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016386 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016387 if (win != NULL)
16388 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16389}
16390
16391/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016392 * "setmatches()" function
16393 */
16394 static void
16395f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016396 typval_T *argvars UNUSED;
16397 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016398{
16399#ifdef FEAT_SEARCH_EXTRA
16400 list_T *l;
16401 listitem_T *li;
16402 dict_T *d;
16403
16404 rettv->vval.v_number = -1;
16405 if (argvars[0].v_type != VAR_LIST)
16406 {
16407 EMSG(_(e_listreq));
16408 return;
16409 }
16410 if ((l = argvars[0].vval.v_list) != NULL)
16411 {
16412
16413 /* To some extent make sure that we are dealing with a list from
16414 * "getmatches()". */
16415 li = l->lv_first;
16416 while (li != NULL)
16417 {
16418 if (li->li_tv.v_type != VAR_DICT
16419 || (d = li->li_tv.vval.v_dict) == NULL)
16420 {
16421 EMSG(_(e_invarg));
16422 return;
16423 }
16424 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16425 && dict_find(d, (char_u *)"pattern", -1) != NULL
16426 && dict_find(d, (char_u *)"priority", -1) != NULL
16427 && dict_find(d, (char_u *)"id", -1) != NULL))
16428 {
16429 EMSG(_(e_invarg));
16430 return;
16431 }
16432 li = li->li_next;
16433 }
16434
16435 clear_matches(curwin);
16436 li = l->lv_first;
16437 while (li != NULL)
16438 {
16439 d = li->li_tv.vval.v_dict;
16440 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16441 get_dict_string(d, (char_u *)"pattern", FALSE),
16442 (int)get_dict_number(d, (char_u *)"priority"),
16443 (int)get_dict_number(d, (char_u *)"id"));
16444 li = li->li_next;
16445 }
16446 rettv->vval.v_number = 0;
16447 }
16448#endif
16449}
16450
16451/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016452 * "setpos()" function
16453 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016454 static void
16455f_setpos(argvars, rettv)
16456 typval_T *argvars;
16457 typval_T *rettv;
16458{
16459 pos_T pos;
16460 int fnum;
16461 char_u *name;
16462
Bram Moolenaar08250432008-02-13 11:42:46 +000016463 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016464 name = get_tv_string_chk(argvars);
16465 if (name != NULL)
16466 {
16467 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16468 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016469 if (--pos.col < 0)
16470 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016471 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016472 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016473 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016474 if (fnum == curbuf->b_fnum)
16475 {
16476 curwin->w_cursor = pos;
16477 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016478 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016479 }
16480 else
16481 EMSG(_(e_invarg));
16482 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016483 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16484 {
16485 /* set mark */
16486 if (setmark_pos(name[1], &pos, fnum) == OK)
16487 rettv->vval.v_number = 0;
16488 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016489 else
16490 EMSG(_(e_invarg));
16491 }
16492 }
16493}
16494
16495/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016496 * "setqflist()" function
16497 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016498 static void
16499f_setqflist(argvars, rettv)
16500 typval_T *argvars;
16501 typval_T *rettv;
16502{
16503 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16504}
16505
16506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507 * "setreg()" function
16508 */
16509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016511 typval_T *argvars;
16512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513{
16514 int regname;
16515 char_u *strregname;
16516 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 int append;
16519 char_u yank_type;
16520 long block_len;
16521
16522 block_len = -1;
16523 yank_type = MAUTO;
16524 append = FALSE;
16525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016526 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016527 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016529 if (strregname == NULL)
16530 return; /* type error; errmsg already given */
16531 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 if (regname == 0 || regname == '@')
16533 regname = '"';
16534 else if (regname == '=')
16535 return;
16536
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 stropt = get_tv_string_chk(&argvars[2]);
16540 if (stropt == NULL)
16541 return; /* type error */
16542 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 switch (*stropt)
16544 {
16545 case 'a': case 'A': /* append */
16546 append = TRUE;
16547 break;
16548 case 'v': case 'c': /* character-wise selection */
16549 yank_type = MCHAR;
16550 break;
16551 case 'V': case 'l': /* line-wise selection */
16552 yank_type = MLINE;
16553 break;
16554#ifdef FEAT_VISUAL
16555 case 'b': case Ctrl_V: /* block-wise selection */
16556 yank_type = MBLOCK;
16557 if (VIM_ISDIGIT(stropt[1]))
16558 {
16559 ++stropt;
16560 block_len = getdigits(&stropt) - 1;
16561 --stropt;
16562 }
16563 break;
16564#endif
16565 }
16566 }
16567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016568 strval = get_tv_string_chk(&argvars[1]);
16569 if (strval != NULL)
16570 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016571 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016572 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573}
16574
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016575/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016576 * "settabvar()" function
16577 */
16578 static void
16579f_settabvar(argvars, rettv)
16580 typval_T *argvars;
16581 typval_T *rettv;
16582{
16583 tabpage_T *save_curtab;
16584 char_u *varname, *tabvarname;
16585 typval_T *varp;
16586 tabpage_T *tp;
16587
16588 rettv->vval.v_number = 0;
16589
16590 if (check_restricted() || check_secure())
16591 return;
16592
16593 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16594 varname = get_tv_string_chk(&argvars[1]);
16595 varp = &argvars[2];
16596
16597 if (tp != NULL && varname != NULL && varp != NULL)
16598 {
16599 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016600 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016601
16602 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16603 if (tabvarname != NULL)
16604 {
16605 STRCPY(tabvarname, "t:");
16606 STRCPY(tabvarname + 2, varname);
16607 set_var(tabvarname, varp, TRUE);
16608 vim_free(tabvarname);
16609 }
16610
16611 /* Restore current tabpage */
16612 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016613 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016614 }
16615}
16616
16617/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016618 * "settabwinvar()" function
16619 */
16620 static void
16621f_settabwinvar(argvars, rettv)
16622 typval_T *argvars;
16623 typval_T *rettv;
16624{
16625 setwinvar(argvars, rettv, 1);
16626}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627
16628/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016629 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016632f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016633 typval_T *argvars;
16634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016636 setwinvar(argvars, rettv, 0);
16637}
16638
16639/*
16640 * "setwinvar()" and "settabwinvar()" functions
16641 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016642
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016643 static void
16644setwinvar(argvars, rettv, off)
16645 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016646 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016647 int off;
16648{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 win_T *win;
16650#ifdef FEAT_WINDOWS
16651 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016652 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653#endif
16654 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016655 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016657 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658
16659 if (check_restricted() || check_secure())
16660 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016661
16662#ifdef FEAT_WINDOWS
16663 if (off == 1)
16664 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16665 else
16666 tp = curtab;
16667#endif
16668 win = find_win_by_nr(&argvars[off], tp);
16669 varname = get_tv_string_chk(&argvars[off + 1]);
16670 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671
16672 if (win != NULL && varname != NULL && varp != NULL)
16673 {
16674#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016675 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677#endif
16678
16679 if (*varname == '&')
16680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016681 long numval;
16682 char_u *strval;
16683 int error = FALSE;
16684
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016686 numval = get_tv_number_chk(varp, &error);
16687 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016688 if (!error && strval != NULL)
16689 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690 }
16691 else
16692 {
16693 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16694 if (winvarname != NULL)
16695 {
16696 STRCPY(winvarname, "w:");
16697 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016698 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 vim_free(winvarname);
16700 }
16701 }
16702
16703#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016704 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705#endif
16706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707}
16708
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016709#ifdef FEAT_CRYPT
16710/*
16711 * "sha256({string})" function
16712 */
16713 static void
16714f_sha256(argvars, rettv)
16715 typval_T *argvars;
16716 typval_T *rettv;
16717{
16718 char_u *p;
16719
16720 p = get_tv_string(&argvars[0]);
16721 rettv->vval.v_string = vim_strsave(
16722 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16723 rettv->v_type = VAR_STRING;
16724}
16725#endif /* FEAT_CRYPT */
16726
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016728 * "shellescape({string})" function
16729 */
16730 static void
16731f_shellescape(argvars, rettv)
16732 typval_T *argvars;
16733 typval_T *rettv;
16734{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016735 rettv->vval.v_string = vim_strsave_shellescape(
16736 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016737 rettv->v_type = VAR_STRING;
16738}
16739
16740/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016741 * shiftwidth() function
16742 */
16743 static void
16744f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016745 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016746 typval_T *rettv;
16747{
16748 rettv->vval.v_number = get_sw_value();
16749}
16750
16751/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016752 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 */
16754 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016755f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016756 typval_T *argvars;
16757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016759 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760
Bram Moolenaar0d660222005-01-07 21:51:51 +000016761 p = get_tv_string(&argvars[0]);
16762 rettv->vval.v_string = vim_strsave(p);
16763 simplify_filename(rettv->vval.v_string); /* simplify in place */
16764 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765}
16766
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016767#ifdef FEAT_FLOAT
16768/*
16769 * "sin()" function
16770 */
16771 static void
16772f_sin(argvars, rettv)
16773 typval_T *argvars;
16774 typval_T *rettv;
16775{
16776 float_T f;
16777
16778 rettv->v_type = VAR_FLOAT;
16779 if (get_float_arg(argvars, &f) == OK)
16780 rettv->vval.v_float = sin(f);
16781 else
16782 rettv->vval.v_float = 0.0;
16783}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016784
16785/*
16786 * "sinh()" function
16787 */
16788 static void
16789f_sinh(argvars, rettv)
16790 typval_T *argvars;
16791 typval_T *rettv;
16792{
16793 float_T f;
16794
16795 rettv->v_type = VAR_FLOAT;
16796 if (get_float_arg(argvars, &f) == OK)
16797 rettv->vval.v_float = sinh(f);
16798 else
16799 rettv->vval.v_float = 0.0;
16800}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016801#endif
16802
Bram Moolenaar0d660222005-01-07 21:51:51 +000016803static int
16804#ifdef __BORLANDC__
16805 _RTLENTRYF
16806#endif
16807 item_compare __ARGS((const void *s1, const void *s2));
16808static int
16809#ifdef __BORLANDC__
16810 _RTLENTRYF
16811#endif
16812 item_compare2 __ARGS((const void *s1, const void *s2));
16813
16814static int item_compare_ic;
16815static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016816static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016817static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016818#define ITEM_COMPARE_FAIL 999
16819
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016821 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016823 static int
16824#ifdef __BORLANDC__
16825_RTLENTRYF
16826#endif
16827item_compare(s1, s2)
16828 const void *s1;
16829 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016831 char_u *p1, *p2;
16832 char_u *tofree1, *tofree2;
16833 int res;
16834 char_u numbuf1[NUMBUFLEN];
16835 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016837 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16838 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016839 if (p1 == NULL)
16840 p1 = (char_u *)"";
16841 if (p2 == NULL)
16842 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016843 if (item_compare_ic)
16844 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016846 res = STRCMP(p1, p2);
16847 vim_free(tofree1);
16848 vim_free(tofree2);
16849 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850}
16851
16852 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016853#ifdef __BORLANDC__
16854_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016856item_compare2(s1, s2)
16857 const void *s1;
16858 const void *s2;
16859{
16860 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016861 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016862 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 /* shortcut after failure in previous call; compare all items equal */
16866 if (item_compare_func_err)
16867 return 0;
16868
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016869 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16870 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016871 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16872 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873
16874 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016875 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016876 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16877 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878 clear_tv(&argv[0]);
16879 clear_tv(&argv[1]);
16880
16881 if (res == FAIL)
16882 res = ITEM_COMPARE_FAIL;
16883 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016884 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16885 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016886 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887 clear_tv(&rettv);
16888 return res;
16889}
16890
16891/*
16892 * "sort({list})" function
16893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016896 typval_T *argvars;
16897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898{
Bram Moolenaar33570922005-01-25 22:26:29 +000016899 list_T *l;
16900 listitem_T *li;
16901 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902 long len;
16903 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905 if (argvars[0].v_type != VAR_LIST)
16906 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 else
16908 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016910 if (l == NULL || tv_check_lock(l->lv_lock,
16911 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912 return;
16913 rettv->vval.v_list = l;
16914 rettv->v_type = VAR_LIST;
16915 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917 len = list_len(l);
16918 if (len <= 1)
16919 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920
Bram Moolenaar0d660222005-01-07 21:51:51 +000016921 item_compare_ic = FALSE;
16922 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016923 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016924 if (argvars[1].v_type != VAR_UNKNOWN)
16925 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016926 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016928 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929 else
16930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016931 int error = FALSE;
16932
16933 i = get_tv_number_chk(&argvars[1], &error);
16934 if (error)
16935 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 if (i == 1)
16937 item_compare_ic = TRUE;
16938 else
16939 item_compare_func = get_tv_string(&argvars[1]);
16940 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016941
16942 if (argvars[2].v_type != VAR_UNKNOWN)
16943 {
16944 /* optional third argument: {dict} */
16945 if (argvars[2].v_type != VAR_DICT)
16946 {
16947 EMSG(_(e_dictreq));
16948 return;
16949 }
16950 item_compare_selfdict = argvars[2].vval.v_dict;
16951 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016955 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956 if (ptrs == NULL)
16957 return;
16958 i = 0;
16959 for (li = l->lv_first; li != NULL; li = li->li_next)
16960 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016962 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963 /* test the compare function */
16964 if (item_compare_func != NULL
16965 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16966 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016967 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 {
16970 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016971 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 item_compare_func == NULL ? item_compare : item_compare2);
16973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016974 if (!item_compare_func_err)
16975 {
16976 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016977 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016978 l->lv_len = 0;
16979 for (i = 0; i < len; ++i)
16980 list_append(l, ptrs[i]);
16981 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 }
16983
16984 vim_free(ptrs);
16985 }
16986}
16987
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016988/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016989 * "soundfold({word})" function
16990 */
16991 static void
16992f_soundfold(argvars, rettv)
16993 typval_T *argvars;
16994 typval_T *rettv;
16995{
16996 char_u *s;
16997
16998 rettv->v_type = VAR_STRING;
16999 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017000#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017001 rettv->vval.v_string = eval_soundfold(s);
17002#else
17003 rettv->vval.v_string = vim_strsave(s);
17004#endif
17005}
17006
17007/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017008 * "spellbadword()" function
17009 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017010 static void
17011f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017012 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017013 typval_T *rettv;
17014{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017015 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017016 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017017 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017018
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017019 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017020 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017021
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017022#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017023 if (argvars[0].v_type == VAR_UNKNOWN)
17024 {
17025 /* Find the start and length of the badly spelled word. */
17026 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17027 if (len != 0)
17028 word = ml_get_cursor();
17029 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017030 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017031 {
17032 char_u *str = get_tv_string_chk(&argvars[0]);
17033 int capcol = -1;
17034
17035 if (str != NULL)
17036 {
17037 /* Check the argument for spelling. */
17038 while (*str != NUL)
17039 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017040 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017041 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017042 {
17043 word = str;
17044 break;
17045 }
17046 str += len;
17047 }
17048 }
17049 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017050#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017051
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017052 list_append_string(rettv->vval.v_list, word, len);
17053 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017054 attr == HLF_SPB ? "bad" :
17055 attr == HLF_SPR ? "rare" :
17056 attr == HLF_SPL ? "local" :
17057 attr == HLF_SPC ? "caps" :
17058 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017059}
17060
17061/*
17062 * "spellsuggest()" function
17063 */
17064 static void
17065f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017066 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017067 typval_T *rettv;
17068{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017069#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017070 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017071 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017072 int maxcount;
17073 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017074 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017075 listitem_T *li;
17076 int need_capital = FALSE;
17077#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017078
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017079 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017080 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017081
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017082#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017083 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017084 {
17085 str = get_tv_string(&argvars[0]);
17086 if (argvars[1].v_type != VAR_UNKNOWN)
17087 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017088 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017089 if (maxcount <= 0)
17090 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017091 if (argvars[2].v_type != VAR_UNKNOWN)
17092 {
17093 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17094 if (typeerr)
17095 return;
17096 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017097 }
17098 else
17099 maxcount = 25;
17100
Bram Moolenaar4770d092006-01-12 23:22:24 +000017101 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017102
17103 for (i = 0; i < ga.ga_len; ++i)
17104 {
17105 str = ((char_u **)ga.ga_data)[i];
17106
17107 li = listitem_alloc();
17108 if (li == NULL)
17109 vim_free(str);
17110 else
17111 {
17112 li->li_tv.v_type = VAR_STRING;
17113 li->li_tv.v_lock = 0;
17114 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017115 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017116 }
17117 }
17118 ga_clear(&ga);
17119 }
17120#endif
17121}
17122
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017124f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017125 typval_T *argvars;
17126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017127{
17128 char_u *str;
17129 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017130 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017131 regmatch_T regmatch;
17132 char_u patbuf[NUMBUFLEN];
17133 char_u *save_cpo;
17134 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017136 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138
17139 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17140 save_cpo = p_cpo;
17141 p_cpo = (char_u *)"";
17142
17143 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017144 if (argvars[1].v_type != VAR_UNKNOWN)
17145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017146 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17147 if (pat == NULL)
17148 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017149 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017150 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017151 }
17152 if (pat == NULL || *pat == NUL)
17153 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017154
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017155 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017157 if (typeerr)
17158 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17161 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017164 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017166 if (*str == NUL)
17167 match = FALSE; /* empty item at the end */
17168 else
17169 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170 if (match)
17171 end = regmatch.startp[0];
17172 else
17173 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017174 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17175 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017177 if (list_append_string(rettv->vval.v_list, str,
17178 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 }
17181 if (!match)
17182 break;
17183 /* Advance to just after the match. */
17184 if (regmatch.endp[0] > str)
17185 col = 0;
17186 else
17187 {
17188 /* Don't get stuck at the same match. */
17189#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017190 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191#else
17192 col = 1;
17193#endif
17194 }
17195 str = regmatch.endp[0];
17196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202}
17203
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017204#ifdef FEAT_FLOAT
17205/*
17206 * "sqrt()" function
17207 */
17208 static void
17209f_sqrt(argvars, rettv)
17210 typval_T *argvars;
17211 typval_T *rettv;
17212{
17213 float_T f;
17214
17215 rettv->v_type = VAR_FLOAT;
17216 if (get_float_arg(argvars, &f) == OK)
17217 rettv->vval.v_float = sqrt(f);
17218 else
17219 rettv->vval.v_float = 0.0;
17220}
17221
17222/*
17223 * "str2float()" function
17224 */
17225 static void
17226f_str2float(argvars, rettv)
17227 typval_T *argvars;
17228 typval_T *rettv;
17229{
17230 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17231
17232 if (*p == '+')
17233 p = skipwhite(p + 1);
17234 (void)string2float(p, &rettv->vval.v_float);
17235 rettv->v_type = VAR_FLOAT;
17236}
17237#endif
17238
Bram Moolenaar2c932302006-03-18 21:42:09 +000017239/*
17240 * "str2nr()" function
17241 */
17242 static void
17243f_str2nr(argvars, rettv)
17244 typval_T *argvars;
17245 typval_T *rettv;
17246{
17247 int base = 10;
17248 char_u *p;
17249 long n;
17250
17251 if (argvars[1].v_type != VAR_UNKNOWN)
17252 {
17253 base = get_tv_number(&argvars[1]);
17254 if (base != 8 && base != 10 && base != 16)
17255 {
17256 EMSG(_(e_invarg));
17257 return;
17258 }
17259 }
17260
17261 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017262 if (*p == '+')
17263 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017264 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17265 rettv->vval.v_number = n;
17266}
17267
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268#ifdef HAVE_STRFTIME
17269/*
17270 * "strftime({format}[, {time}])" function
17271 */
17272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017273f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017274 typval_T *argvars;
17275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276{
17277 char_u result_buf[256];
17278 struct tm *curtime;
17279 time_t seconds;
17280 char_u *p;
17281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017282 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017284 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017285 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 seconds = time(NULL);
17287 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017288 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 curtime = localtime(&seconds);
17290 /* MSVC returns NULL for an invalid value of seconds. */
17291 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017292 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 else
17294 {
17295# ifdef FEAT_MBYTE
17296 vimconv_T conv;
17297 char_u *enc;
17298
17299 conv.vc_type = CONV_NONE;
17300 enc = enc_locale();
17301 convert_setup(&conv, p_enc, enc);
17302 if (conv.vc_type != CONV_NONE)
17303 p = string_convert(&conv, p, NULL);
17304# endif
17305 if (p != NULL)
17306 (void)strftime((char *)result_buf, sizeof(result_buf),
17307 (char *)p, curtime);
17308 else
17309 result_buf[0] = NUL;
17310
17311# ifdef FEAT_MBYTE
17312 if (conv.vc_type != CONV_NONE)
17313 vim_free(p);
17314 convert_setup(&conv, enc, p_enc);
17315 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017316 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 else
17318# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320
17321# ifdef FEAT_MBYTE
17322 /* Release conversion descriptors */
17323 convert_setup(&conv, NULL, NULL);
17324 vim_free(enc);
17325# endif
17326 }
17327}
17328#endif
17329
17330/*
17331 * "stridx()" function
17332 */
17333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017334f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017335 typval_T *argvars;
17336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337{
17338 char_u buf[NUMBUFLEN];
17339 char_u *needle;
17340 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017341 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017343 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017345 needle = get_tv_string_chk(&argvars[1]);
17346 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017347 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017348 if (needle == NULL || haystack == NULL)
17349 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350
Bram Moolenaar33570922005-01-25 22:26:29 +000017351 if (argvars[2].v_type != VAR_UNKNOWN)
17352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353 int error = FALSE;
17354
17355 start_idx = get_tv_number_chk(&argvars[2], &error);
17356 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017357 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017358 if (start_idx >= 0)
17359 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 }
17361
17362 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17363 if (pos != NULL)
17364 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365}
17366
17367/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017368 * "string()" function
17369 */
17370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017371f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 typval_T *argvars;
17373 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017374{
17375 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017376 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017379 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017380 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017381 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017382 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383}
17384
17385/*
17386 * "strlen()" function
17387 */
17388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017389f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 typval_T *argvars;
17391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393 rettv->vval.v_number = (varnumber_T)(STRLEN(
17394 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395}
17396
17397/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017398 * "strchars()" function
17399 */
17400 static void
17401f_strchars(argvars, rettv)
17402 typval_T *argvars;
17403 typval_T *rettv;
17404{
17405 char_u *s = get_tv_string(&argvars[0]);
17406#ifdef FEAT_MBYTE
17407 varnumber_T len = 0;
17408
17409 while (*s != NUL)
17410 {
17411 mb_cptr2char_adv(&s);
17412 ++len;
17413 }
17414 rettv->vval.v_number = len;
17415#else
17416 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17417#endif
17418}
17419
17420/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017421 * "strdisplaywidth()" function
17422 */
17423 static void
17424f_strdisplaywidth(argvars, rettv)
17425 typval_T *argvars;
17426 typval_T *rettv;
17427{
17428 char_u *s = get_tv_string(&argvars[0]);
17429 int col = 0;
17430
17431 if (argvars[1].v_type != VAR_UNKNOWN)
17432 col = get_tv_number(&argvars[1]);
17433
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017434 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017435}
17436
17437/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017438 * "strwidth()" function
17439 */
17440 static void
17441f_strwidth(argvars, rettv)
17442 typval_T *argvars;
17443 typval_T *rettv;
17444{
17445 char_u *s = get_tv_string(&argvars[0]);
17446
17447 rettv->vval.v_number = (varnumber_T)(
17448#ifdef FEAT_MBYTE
17449 mb_string2cells(s, -1)
17450#else
17451 STRLEN(s)
17452#endif
17453 );
17454}
17455
17456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 * "strpart()" function
17458 */
17459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017460f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 typval_T *argvars;
17462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463{
17464 char_u *p;
17465 int n;
17466 int len;
17467 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017468 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017470 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 slen = (int)STRLEN(p);
17472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017473 n = get_tv_number_chk(&argvars[1], &error);
17474 if (error)
17475 len = 0;
17476 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017477 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 else
17479 len = slen - n; /* default len: all bytes that are available. */
17480
17481 /*
17482 * Only return the overlap between the specified part and the actual
17483 * string.
17484 */
17485 if (n < 0)
17486 {
17487 len += n;
17488 n = 0;
17489 }
17490 else if (n > slen)
17491 n = slen;
17492 if (len < 0)
17493 len = 0;
17494 else if (n + len > slen)
17495 len = slen - n;
17496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017497 rettv->v_type = VAR_STRING;
17498 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499}
17500
17501/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017502 * "strridx()" function
17503 */
17504 static void
17505f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 typval_T *argvars;
17507 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017508{
17509 char_u buf[NUMBUFLEN];
17510 char_u *needle;
17511 char_u *haystack;
17512 char_u *rest;
17513 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017514 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017516 needle = get_tv_string_chk(&argvars[1]);
17517 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017518
17519 rettv->vval.v_number = -1;
17520 if (needle == NULL || haystack == NULL)
17521 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017522
17523 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017524 if (argvars[2].v_type != VAR_UNKNOWN)
17525 {
17526 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017527 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017528 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017529 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017530 }
17531 else
17532 end_idx = haystack_len;
17533
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017535 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017536 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017537 lastmatch = haystack + end_idx;
17538 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017539 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017540 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017541 for (rest = haystack; *rest != '\0'; ++rest)
17542 {
17543 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017544 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545 break;
17546 lastmatch = rest;
17547 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017548 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017549
17550 if (lastmatch == NULL)
17551 rettv->vval.v_number = -1;
17552 else
17553 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17554}
17555
17556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 * "strtrans()" function
17558 */
17559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 typval_T *argvars;
17562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017564 rettv->v_type = VAR_STRING;
17565 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566}
17567
17568/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569 * "submatch()" function
17570 */
17571 static void
17572f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017573 typval_T *argvars;
17574 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575{
17576 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017577 rettv->vval.v_string =
17578 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579}
17580
17581/*
17582 * "substitute()" function
17583 */
17584 static void
17585f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017586 typval_T *argvars;
17587 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588{
17589 char_u patbuf[NUMBUFLEN];
17590 char_u subbuf[NUMBUFLEN];
17591 char_u flagsbuf[NUMBUFLEN];
17592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017593 char_u *str = get_tv_string_chk(&argvars[0]);
17594 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17595 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17596 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17597
Bram Moolenaar0d660222005-01-07 21:51:51 +000017598 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017599 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17600 rettv->vval.v_string = NULL;
17601 else
17602 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603}
17604
17605/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017606 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017609f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612{
17613 int id = 0;
17614#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017615 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 long col;
17617 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017618 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017620 lnum = get_tv_lnum(argvars); /* -1 on type error */
17621 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17622 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017624 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017625 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017626 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627#endif
17628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017629 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630}
17631
17632/*
17633 * "synIDattr(id, what [, mode])" function
17634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017636f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017637 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639{
17640 char_u *p = NULL;
17641#ifdef FEAT_SYN_HL
17642 int id;
17643 char_u *what;
17644 char_u *mode;
17645 char_u modebuf[NUMBUFLEN];
17646 int modec;
17647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017648 id = get_tv_number(&argvars[0]);
17649 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017652 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017654 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 modec = 0; /* replace invalid with current */
17656 }
17657 else
17658 {
17659#ifdef FEAT_GUI
17660 if (gui.in_use)
17661 modec = 'g';
17662 else
17663#endif
17664 if (t_colors > 1)
17665 modec = 'c';
17666 else
17667 modec = 't';
17668 }
17669
17670
17671 switch (TOLOWER_ASC(what[0]))
17672 {
17673 case 'b':
17674 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17675 p = highlight_color(id, what, modec);
17676 else /* bold */
17677 p = highlight_has_attr(id, HL_BOLD, modec);
17678 break;
17679
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017680 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 p = highlight_color(id, what, modec);
17682 break;
17683
17684 case 'i':
17685 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17686 p = highlight_has_attr(id, HL_INVERSE, modec);
17687 else /* italic */
17688 p = highlight_has_attr(id, HL_ITALIC, modec);
17689 break;
17690
17691 case 'n': /* name */
17692 p = get_highlight_name(NULL, id - 1);
17693 break;
17694
17695 case 'r': /* reverse */
17696 p = highlight_has_attr(id, HL_INVERSE, modec);
17697 break;
17698
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017699 case 's':
17700 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17701 p = highlight_color(id, what, modec);
17702 else /* standout */
17703 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 break;
17705
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017706 case 'u':
17707 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17708 /* underline */
17709 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17710 else
17711 /* undercurl */
17712 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 break;
17714 }
17715
17716 if (p != NULL)
17717 p = vim_strsave(p);
17718#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 rettv->v_type = VAR_STRING;
17720 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721}
17722
17723/*
17724 * "synIDtrans(id)" function
17725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017727f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730{
17731 int id;
17732
17733#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017734 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735
17736 if (id > 0)
17737 id = syn_get_final_id(id);
17738 else
17739#endif
17740 id = 0;
17741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017742 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743}
17744
17745/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017746 * "synconcealed(lnum, col)" function
17747 */
17748 static void
17749f_synconcealed(argvars, rettv)
17750 typval_T *argvars UNUSED;
17751 typval_T *rettv;
17752{
17753#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17754 long lnum;
17755 long col;
17756 int syntax_flags = 0;
17757 int cchar;
17758 int matchid = 0;
17759 char_u str[NUMBUFLEN];
17760#endif
17761
17762 rettv->v_type = VAR_LIST;
17763 rettv->vval.v_list = NULL;
17764
17765#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17766 lnum = get_tv_lnum(argvars); /* -1 on type error */
17767 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17768
17769 vim_memset(str, NUL, sizeof(str));
17770
17771 if (rettv_list_alloc(rettv) != FAIL)
17772 {
17773 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17774 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17775 && curwin->w_p_cole > 0)
17776 {
17777 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17778 syntax_flags = get_syntax_info(&matchid);
17779
17780 /* get the conceal character */
17781 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17782 {
17783 cchar = syn_get_sub_char();
17784 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17785 cchar = lcs_conceal;
17786 if (cchar != NUL)
17787 {
17788# ifdef FEAT_MBYTE
17789 if (has_mbyte)
17790 (*mb_char2bytes)(cchar, str);
17791 else
17792# endif
17793 str[0] = cchar;
17794 }
17795 }
17796 }
17797
17798 list_append_number(rettv->vval.v_list,
17799 (syntax_flags & HL_CONCEAL) != 0);
17800 /* -1 to auto-determine strlen */
17801 list_append_string(rettv->vval.v_list, str, -1);
17802 list_append_number(rettv->vval.v_list, matchid);
17803 }
17804#endif
17805}
17806
17807/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017808 * "synstack(lnum, col)" function
17809 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017810 static void
17811f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017812 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017813 typval_T *rettv;
17814{
17815#ifdef FEAT_SYN_HL
17816 long lnum;
17817 long col;
17818 int i;
17819 int id;
17820#endif
17821
17822 rettv->v_type = VAR_LIST;
17823 rettv->vval.v_list = NULL;
17824
17825#ifdef FEAT_SYN_HL
17826 lnum = get_tv_lnum(argvars); /* -1 on type error */
17827 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17828
17829 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017830 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017831 && rettv_list_alloc(rettv) != FAIL)
17832 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017833 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017834 for (i = 0; ; ++i)
17835 {
17836 id = syn_get_stack_item(i);
17837 if (id < 0)
17838 break;
17839 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17840 break;
17841 }
17842 }
17843#endif
17844}
17845
17846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 * "system()" function
17848 */
17849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017850f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 typval_T *argvars;
17852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017854 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017856 char_u *infile = NULL;
17857 char_u buf[NUMBUFLEN];
17858 int err = FALSE;
17859 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017861 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017862 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017863
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017864 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017865 {
17866 /*
17867 * Write the string to a temp file, to be used for input of the shell
17868 * command.
17869 */
17870 if ((infile = vim_tempname('i')) == NULL)
17871 {
17872 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017873 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017874 }
17875
17876 fd = mch_fopen((char *)infile, WRITEBIN);
17877 if (fd == NULL)
17878 {
17879 EMSG2(_(e_notopen), infile);
17880 goto done;
17881 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017882 p = get_tv_string_buf_chk(&argvars[1], buf);
17883 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017884 {
17885 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017886 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017887 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017888 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17889 err = TRUE;
17890 if (fclose(fd) != 0)
17891 err = TRUE;
17892 if (err)
17893 {
17894 EMSG(_("E677: Error writing temp file"));
17895 goto done;
17896 }
17897 }
17898
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017899 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17900 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017901
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902#ifdef USE_CR
17903 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017904 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 {
17906 char_u *s;
17907
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017908 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 {
17910 if (*s == CAR)
17911 *s = NL;
17912 }
17913 }
17914#else
17915# ifdef USE_CRNL
17916 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017917 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 {
17919 char_u *s, *d;
17920
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017921 d = res;
17922 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 {
17924 if (s[0] == CAR && s[1] == NL)
17925 ++s;
17926 *d++ = *s;
17927 }
17928 *d = NUL;
17929 }
17930# endif
17931#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017932
17933done:
17934 if (infile != NULL)
17935 {
17936 mch_remove(infile);
17937 vim_free(infile);
17938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017939 rettv->v_type = VAR_STRING;
17940 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941}
17942
17943/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017944 * "tabpagebuflist()" function
17945 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017946 static void
17947f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017948 typval_T *argvars UNUSED;
17949 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017950{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017951#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017952 tabpage_T *tp;
17953 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017954
17955 if (argvars[0].v_type == VAR_UNKNOWN)
17956 wp = firstwin;
17957 else
17958 {
17959 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17960 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017961 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017962 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017963 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017964 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017965 for (; wp != NULL; wp = wp->w_next)
17966 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017967 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017968 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017969 }
17970#endif
17971}
17972
17973
17974/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017975 * "tabpagenr()" function
17976 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017977 static void
17978f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017979 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017980 typval_T *rettv;
17981{
17982 int nr = 1;
17983#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017984 char_u *arg;
17985
17986 if (argvars[0].v_type != VAR_UNKNOWN)
17987 {
17988 arg = get_tv_string_chk(&argvars[0]);
17989 nr = 0;
17990 if (arg != NULL)
17991 {
17992 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017993 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017994 else
17995 EMSG2(_(e_invexpr2), arg);
17996 }
17997 }
17998 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017999 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018000#endif
18001 rettv->vval.v_number = nr;
18002}
18003
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018004
18005#ifdef FEAT_WINDOWS
18006static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18007
18008/*
18009 * Common code for tabpagewinnr() and winnr().
18010 */
18011 static int
18012get_winnr(tp, argvar)
18013 tabpage_T *tp;
18014 typval_T *argvar;
18015{
18016 win_T *twin;
18017 int nr = 1;
18018 win_T *wp;
18019 char_u *arg;
18020
18021 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18022 if (argvar->v_type != VAR_UNKNOWN)
18023 {
18024 arg = get_tv_string_chk(argvar);
18025 if (arg == NULL)
18026 nr = 0; /* type error; errmsg already given */
18027 else if (STRCMP(arg, "$") == 0)
18028 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18029 else if (STRCMP(arg, "#") == 0)
18030 {
18031 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18032 if (twin == NULL)
18033 nr = 0;
18034 }
18035 else
18036 {
18037 EMSG2(_(e_invexpr2), arg);
18038 nr = 0;
18039 }
18040 }
18041
18042 if (nr > 0)
18043 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18044 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018045 {
18046 if (wp == NULL)
18047 {
18048 /* didn't find it in this tabpage */
18049 nr = 0;
18050 break;
18051 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018052 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018053 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018054 return nr;
18055}
18056#endif
18057
18058/*
18059 * "tabpagewinnr()" function
18060 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018061 static void
18062f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018063 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018064 typval_T *rettv;
18065{
18066 int nr = 1;
18067#ifdef FEAT_WINDOWS
18068 tabpage_T *tp;
18069
18070 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18071 if (tp == NULL)
18072 nr = 0;
18073 else
18074 nr = get_winnr(tp, &argvars[1]);
18075#endif
18076 rettv->vval.v_number = nr;
18077}
18078
18079
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018080/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018081 * "tagfiles()" function
18082 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018083 static void
18084f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018085 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018086 typval_T *rettv;
18087{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018088 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018089 tagname_T tn;
18090 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018091
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018092 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018093 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018094 fname = alloc(MAXPATHL);
18095 if (fname == NULL)
18096 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018097
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018098 for (first = TRUE; ; first = FALSE)
18099 if (get_tagfname(&tn, first, fname) == FAIL
18100 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018101 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018102 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018103 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018104}
18105
18106/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018107 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018108 */
18109 static void
18110f_taglist(argvars, rettv)
18111 typval_T *argvars;
18112 typval_T *rettv;
18113{
18114 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018115
18116 tag_pattern = get_tv_string(&argvars[0]);
18117
18118 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018119 if (*tag_pattern == NUL)
18120 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018121
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018122 if (rettv_list_alloc(rettv) == OK)
18123 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018124}
18125
18126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 * "tempname()" function
18128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018130f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018131 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133{
18134 static int x = 'A';
18135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136 rettv->v_type = VAR_STRING;
18137 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138
18139 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18140 * names. Skip 'I' and 'O', they are used for shell redirection. */
18141 do
18142 {
18143 if (x == 'Z')
18144 x = '0';
18145 else if (x == '9')
18146 x = 'A';
18147 else
18148 {
18149#ifdef EBCDIC
18150 if (x == 'I')
18151 x = 'J';
18152 else if (x == 'R')
18153 x = 'S';
18154 else
18155#endif
18156 ++x;
18157 }
18158 } while (x == 'I' || x == 'O');
18159}
18160
18161/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018162 * "test(list)" function: Just checking the walls...
18163 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018164 static void
18165f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018166 typval_T *argvars UNUSED;
18167 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018168{
18169 /* Used for unit testing. Change the code below to your liking. */
18170#if 0
18171 listitem_T *li;
18172 list_T *l;
18173 char_u *bad, *good;
18174
18175 if (argvars[0].v_type != VAR_LIST)
18176 return;
18177 l = argvars[0].vval.v_list;
18178 if (l == NULL)
18179 return;
18180 li = l->lv_first;
18181 if (li == NULL)
18182 return;
18183 bad = get_tv_string(&li->li_tv);
18184 li = li->li_next;
18185 if (li == NULL)
18186 return;
18187 good = get_tv_string(&li->li_tv);
18188 rettv->vval.v_number = test_edit_score(bad, good);
18189#endif
18190}
18191
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018192#ifdef FEAT_FLOAT
18193/*
18194 * "tan()" function
18195 */
18196 static void
18197f_tan(argvars, rettv)
18198 typval_T *argvars;
18199 typval_T *rettv;
18200{
18201 float_T f;
18202
18203 rettv->v_type = VAR_FLOAT;
18204 if (get_float_arg(argvars, &f) == OK)
18205 rettv->vval.v_float = tan(f);
18206 else
18207 rettv->vval.v_float = 0.0;
18208}
18209
18210/*
18211 * "tanh()" function
18212 */
18213 static void
18214f_tanh(argvars, rettv)
18215 typval_T *argvars;
18216 typval_T *rettv;
18217{
18218 float_T f;
18219
18220 rettv->v_type = VAR_FLOAT;
18221 if (get_float_arg(argvars, &f) == OK)
18222 rettv->vval.v_float = tanh(f);
18223 else
18224 rettv->vval.v_float = 0.0;
18225}
18226#endif
18227
Bram Moolenaard52d9742005-08-21 22:20:28 +000018228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 * "tolower(string)" function
18230 */
18231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018232f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018233 typval_T *argvars;
18234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235{
18236 char_u *p;
18237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238 p = vim_strsave(get_tv_string(&argvars[0]));
18239 rettv->v_type = VAR_STRING;
18240 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241
18242 if (p != NULL)
18243 while (*p != NUL)
18244 {
18245#ifdef FEAT_MBYTE
18246 int l;
18247
18248 if (enc_utf8)
18249 {
18250 int c, lc;
18251
18252 c = utf_ptr2char(p);
18253 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018254 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 /* TODO: reallocate string when byte count changes. */
18256 if (utf_char2len(lc) == l)
18257 utf_char2bytes(lc, p);
18258 p += l;
18259 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018260 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 p += l; /* skip multi-byte character */
18262 else
18263#endif
18264 {
18265 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18266 ++p;
18267 }
18268 }
18269}
18270
18271/*
18272 * "toupper(string)" function
18273 */
18274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018275f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018276 typval_T *argvars;
18277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018279 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018280 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281}
18282
18283/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018284 * "tr(string, fromstr, tostr)" function
18285 */
18286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018287f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018288 typval_T *argvars;
18289 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018290{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018291 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018292 char_u *fromstr;
18293 char_u *tostr;
18294 char_u *p;
18295#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018296 int inlen;
18297 int fromlen;
18298 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018299 int idx;
18300 char_u *cpstr;
18301 int cplen;
18302 int first = TRUE;
18303#endif
18304 char_u buf[NUMBUFLEN];
18305 char_u buf2[NUMBUFLEN];
18306 garray_T ga;
18307
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018308 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018309 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18310 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018311
18312 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018313 rettv->v_type = VAR_STRING;
18314 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018315 if (fromstr == NULL || tostr == NULL)
18316 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018317 ga_init2(&ga, (int)sizeof(char), 80);
18318
18319#ifdef FEAT_MBYTE
18320 if (!has_mbyte)
18321#endif
18322 /* not multi-byte: fromstr and tostr must be the same length */
18323 if (STRLEN(fromstr) != STRLEN(tostr))
18324 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018325#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018326error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018327#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018328 EMSG2(_(e_invarg2), fromstr);
18329 ga_clear(&ga);
18330 return;
18331 }
18332
18333 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018334 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018335 {
18336#ifdef FEAT_MBYTE
18337 if (has_mbyte)
18338 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018339 inlen = (*mb_ptr2len)(in_str);
18340 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018341 cplen = inlen;
18342 idx = 0;
18343 for (p = fromstr; *p != NUL; p += fromlen)
18344 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018345 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018346 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018347 {
18348 for (p = tostr; *p != NUL; p += tolen)
18349 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018350 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018351 if (idx-- == 0)
18352 {
18353 cplen = tolen;
18354 cpstr = p;
18355 break;
18356 }
18357 }
18358 if (*p == NUL) /* tostr is shorter than fromstr */
18359 goto error;
18360 break;
18361 }
18362 ++idx;
18363 }
18364
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018365 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018366 {
18367 /* Check that fromstr and tostr have the same number of
18368 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018369 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018370 first = FALSE;
18371 for (p = tostr; *p != NUL; p += tolen)
18372 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018373 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018374 --idx;
18375 }
18376 if (idx != 0)
18377 goto error;
18378 }
18379
18380 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018381 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018382 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018383
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018384 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018385 }
18386 else
18387#endif
18388 {
18389 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018390 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018391 if (p != NULL)
18392 ga_append(&ga, tostr[p - fromstr]);
18393 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018394 ga_append(&ga, *in_str);
18395 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018396 }
18397 }
18398
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018399 /* add a terminating NUL */
18400 ga_grow(&ga, 1);
18401 ga_append(&ga, NUL);
18402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018403 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018404}
18405
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018406#ifdef FEAT_FLOAT
18407/*
18408 * "trunc({float})" function
18409 */
18410 static void
18411f_trunc(argvars, rettv)
18412 typval_T *argvars;
18413 typval_T *rettv;
18414{
18415 float_T f;
18416
18417 rettv->v_type = VAR_FLOAT;
18418 if (get_float_arg(argvars, &f) == OK)
18419 /* trunc() is not in C90, use floor() or ceil() instead. */
18420 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18421 else
18422 rettv->vval.v_float = 0.0;
18423}
18424#endif
18425
Bram Moolenaar8299df92004-07-10 09:47:34 +000018426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 * "type(expr)" function
18428 */
18429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018430f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018431 typval_T *argvars;
18432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018434 int n;
18435
18436 switch (argvars[0].v_type)
18437 {
18438 case VAR_NUMBER: n = 0; break;
18439 case VAR_STRING: n = 1; break;
18440 case VAR_FUNC: n = 2; break;
18441 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018442 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018443#ifdef FEAT_FLOAT
18444 case VAR_FLOAT: n = 5; break;
18445#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018446 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18447 }
18448 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449}
18450
18451/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018452 * "undofile(name)" function
18453 */
18454 static void
18455f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018456 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018457 typval_T *rettv;
18458{
18459 rettv->v_type = VAR_STRING;
18460#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018461 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018462 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018463
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018464 if (*fname == NUL)
18465 {
18466 /* If there is no file name there will be no undo file. */
18467 rettv->vval.v_string = NULL;
18468 }
18469 else
18470 {
18471 char_u *ffname = FullName_save(fname, FALSE);
18472
18473 if (ffname != NULL)
18474 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18475 vim_free(ffname);
18476 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018477 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018478#else
18479 rettv->vval.v_string = NULL;
18480#endif
18481}
18482
18483/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018484 * "undotree()" function
18485 */
18486 static void
18487f_undotree(argvars, rettv)
18488 typval_T *argvars UNUSED;
18489 typval_T *rettv;
18490{
18491 if (rettv_dict_alloc(rettv) == OK)
18492 {
18493 dict_T *dict = rettv->vval.v_dict;
18494 list_T *list;
18495
Bram Moolenaar730cde92010-06-27 05:18:54 +020018496 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018497 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018498 dict_add_nr_str(dict, "save_last",
18499 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018500 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18501 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018502 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018503
18504 list = list_alloc();
18505 if (list != NULL)
18506 {
18507 u_eval_tree(curbuf->b_u_oldhead, list);
18508 dict_add_list(dict, "entries", list);
18509 }
18510 }
18511}
18512
18513/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018514 * "values(dict)" function
18515 */
18516 static void
18517f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018518 typval_T *argvars;
18519 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018520{
18521 dict_list(argvars, rettv, 1);
18522}
18523
18524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 * "virtcol(string)" function
18526 */
18527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018528f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018529 typval_T *argvars;
18530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531{
18532 colnr_T vcol = 0;
18533 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018534 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018536 fp = var2fpos(&argvars[0], FALSE, &fnum);
18537 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18538 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 {
18540 getvvcol(curwin, fp, NULL, NULL, &vcol);
18541 ++vcol;
18542 }
18543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018544 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545}
18546
18547/*
18548 * "visualmode()" function
18549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018551f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018552 typval_T *argvars UNUSED;
18553 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554{
18555#ifdef FEAT_VISUAL
18556 char_u str[2];
18557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 str[0] = curbuf->b_visual_mode_eval;
18560 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018561 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562
18563 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018564 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566#endif
18567}
18568
18569/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018570 * "wildmenumode()" function
18571 */
18572 static void
18573f_wildmenumode(argvars, rettv)
18574 typval_T *argvars UNUSED;
18575 typval_T *rettv UNUSED;
18576{
18577#ifdef FEAT_WILDMENU
18578 if (wild_menu_showing)
18579 rettv->vval.v_number = 1;
18580#endif
18581}
18582
18583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 * "winbufnr(nr)" function
18585 */
18586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018588 typval_T *argvars;
18589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590{
18591 win_T *wp;
18592
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018593 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598}
18599
18600/*
18601 * "wincol()" function
18602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018604f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607{
18608 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018609 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610}
18611
18612/*
18613 * "winheight(nr)" function
18614 */
18615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018617 typval_T *argvars;
18618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619{
18620 win_T *wp;
18621
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018622 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627}
18628
18629/*
18630 * "winline()" function
18631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636{
18637 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018638 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639}
18640
18641/*
18642 * "winnr()" function
18643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018645f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018646 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648{
18649 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018650
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018652 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018654 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655}
18656
18657/*
18658 * "winrestcmd()" function
18659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018662 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664{
18665#ifdef FEAT_WINDOWS
18666 win_T *wp;
18667 int winnr = 1;
18668 garray_T ga;
18669 char_u buf[50];
18670
18671 ga_init2(&ga, (int)sizeof(char), 70);
18672 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18673 {
18674 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18675 ga_concat(&ga, buf);
18676# ifdef FEAT_VERTSPLIT
18677 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18678 ga_concat(&ga, buf);
18679# endif
18680 ++winnr;
18681 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018682 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018686 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689}
18690
18691/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018692 * "winrestview()" function
18693 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018694 static void
18695f_winrestview(argvars, rettv)
18696 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018697 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018698{
18699 dict_T *dict;
18700
18701 if (argvars[0].v_type != VAR_DICT
18702 || (dict = argvars[0].vval.v_dict) == NULL)
18703 EMSG(_(e_invarg));
18704 else
18705 {
18706 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18707 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18708#ifdef FEAT_VIRTUALEDIT
18709 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18710#endif
18711 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018712 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018713
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018714 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018715#ifdef FEAT_DIFF
18716 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18717#endif
18718 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18719 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18720
18721 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018722 win_new_height(curwin, curwin->w_height);
18723# ifdef FEAT_VERTSPLIT
18724 win_new_width(curwin, W_WIDTH(curwin));
18725# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018726 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018727
18728 if (curwin->w_topline == 0)
18729 curwin->w_topline = 1;
18730 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18731 curwin->w_topline = curbuf->b_ml.ml_line_count;
18732#ifdef FEAT_DIFF
18733 check_topfill(curwin, TRUE);
18734#endif
18735 }
18736}
18737
18738/*
18739 * "winsaveview()" function
18740 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018741 static void
18742f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018743 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018744 typval_T *rettv;
18745{
18746 dict_T *dict;
18747
Bram Moolenaara800b422010-06-27 01:15:55 +020018748 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018749 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018750 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018751
18752 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18753 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18754#ifdef FEAT_VIRTUALEDIT
18755 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18756#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018757 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018758 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18759
18760 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18761#ifdef FEAT_DIFF
18762 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18763#endif
18764 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18765 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18766}
18767
18768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 * "winwidth(nr)" function
18770 */
18771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018772f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018773 typval_T *argvars;
18774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775{
18776 win_T *wp;
18777
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018778 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 else
18782#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018783 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018785 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786#endif
18787}
18788
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018790 * "writefile()" function
18791 */
18792 static void
18793f_writefile(argvars, rettv)
18794 typval_T *argvars;
18795 typval_T *rettv;
18796{
18797 int binary = FALSE;
18798 char_u *fname;
18799 FILE *fd;
18800 listitem_T *li;
18801 char_u *s;
18802 int ret = 0;
18803 int c;
18804
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018805 if (check_restricted() || check_secure())
18806 return;
18807
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018808 if (argvars[0].v_type != VAR_LIST)
18809 {
18810 EMSG2(_(e_listarg), "writefile()");
18811 return;
18812 }
18813 if (argvars[0].vval.v_list == NULL)
18814 return;
18815
18816 if (argvars[2].v_type != VAR_UNKNOWN
18817 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18818 binary = TRUE;
18819
18820 /* Always open the file in binary mode, library functions have a mind of
18821 * their own about CR-LF conversion. */
18822 fname = get_tv_string(&argvars[1]);
18823 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18824 {
18825 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18826 ret = -1;
18827 }
18828 else
18829 {
18830 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18831 li = li->li_next)
18832 {
18833 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18834 {
18835 if (*s == '\n')
18836 c = putc(NUL, fd);
18837 else
18838 c = putc(*s, fd);
18839 if (c == EOF)
18840 {
18841 ret = -1;
18842 break;
18843 }
18844 }
18845 if (!binary || li->li_next != NULL)
18846 if (putc('\n', fd) == EOF)
18847 {
18848 ret = -1;
18849 break;
18850 }
18851 if (ret < 0)
18852 {
18853 EMSG(_(e_write));
18854 break;
18855 }
18856 }
18857 fclose(fd);
18858 }
18859
18860 rettv->vval.v_number = ret;
18861}
18862
18863/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018864 * "xor(expr, expr)" function
18865 */
18866 static void
18867f_xor(argvars, rettv)
18868 typval_T *argvars;
18869 typval_T *rettv;
18870{
18871 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18872 ^ get_tv_number_chk(&argvars[1], NULL);
18873}
18874
18875
18876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018878 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 */
18880 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018881var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018882 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018883 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018884 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018886 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018888 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889
Bram Moolenaara5525202006-03-02 22:52:09 +000018890 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018891 if (varp->v_type == VAR_LIST)
18892 {
18893 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018894 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018895 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018896 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018897
18898 l = varp->vval.v_list;
18899 if (l == NULL)
18900 return NULL;
18901
18902 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018903 pos.lnum = list_find_nr(l, 0L, &error);
18904 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018905 return NULL; /* invalid line number */
18906
18907 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018908 pos.col = list_find_nr(l, 1L, &error);
18909 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018910 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018911 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018912
18913 /* We accept "$" for the column number: last column. */
18914 li = list_find(l, 1L);
18915 if (li != NULL && li->li_tv.v_type == VAR_STRING
18916 && li->li_tv.vval.v_string != NULL
18917 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18918 pos.col = len + 1;
18919
Bram Moolenaara5525202006-03-02 22:52:09 +000018920 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018921 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018922 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018923 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018924
Bram Moolenaara5525202006-03-02 22:52:09 +000018925#ifdef FEAT_VIRTUALEDIT
18926 /* Get the virtual offset. Defaults to zero. */
18927 pos.coladd = list_find_nr(l, 2L, &error);
18928 if (error)
18929 pos.coladd = 0;
18930#endif
18931
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018932 return &pos;
18933 }
18934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018935 name = get_tv_string_chk(varp);
18936 if (name == NULL)
18937 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018938 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018940#ifdef FEAT_VISUAL
18941 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18942 {
18943 if (VIsual_active)
18944 return &VIsual;
18945 return &curwin->w_cursor;
18946 }
18947#endif
18948 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018950 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18952 return NULL;
18953 return pp;
18954 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018955
18956#ifdef FEAT_VIRTUALEDIT
18957 pos.coladd = 0;
18958#endif
18959
Bram Moolenaar477933c2007-07-17 14:32:23 +000018960 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018961 {
18962 pos.col = 0;
18963 if (name[1] == '0') /* "w0": first visible line */
18964 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018965 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018966 pos.lnum = curwin->w_topline;
18967 return &pos;
18968 }
18969 else if (name[1] == '$') /* "w$": last visible line */
18970 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018971 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018972 pos.lnum = curwin->w_botline - 1;
18973 return &pos;
18974 }
18975 }
18976 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018978 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 {
18980 pos.lnum = curbuf->b_ml.ml_line_count;
18981 pos.col = 0;
18982 }
18983 else
18984 {
18985 pos.lnum = curwin->w_cursor.lnum;
18986 pos.col = (colnr_T)STRLEN(ml_get_curline());
18987 }
18988 return &pos;
18989 }
18990 return NULL;
18991}
18992
18993/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018994 * Convert list in "arg" into a position and optional file number.
18995 * When "fnump" is NULL there is no file number, only 3 items.
18996 * Note that the column is passed on as-is, the caller may want to decrement
18997 * it to use 1 for the first column.
18998 * Return FAIL when conversion is not possible, doesn't check the position for
18999 * validity.
19000 */
19001 static int
19002list2fpos(arg, posp, fnump)
19003 typval_T *arg;
19004 pos_T *posp;
19005 int *fnump;
19006{
19007 list_T *l = arg->vval.v_list;
19008 long i = 0;
19009 long n;
19010
Bram Moolenaarbde35262006-07-23 20:12:24 +000019011 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19012 * when "fnump" isn't NULL and "coladd" is optional. */
19013 if (arg->v_type != VAR_LIST
19014 || l == NULL
19015 || l->lv_len < (fnump == NULL ? 2 : 3)
19016 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019017 return FAIL;
19018
19019 if (fnump != NULL)
19020 {
19021 n = list_find_nr(l, i++, NULL); /* fnum */
19022 if (n < 0)
19023 return FAIL;
19024 if (n == 0)
19025 n = curbuf->b_fnum; /* current buffer */
19026 *fnump = n;
19027 }
19028
19029 n = list_find_nr(l, i++, NULL); /* lnum */
19030 if (n < 0)
19031 return FAIL;
19032 posp->lnum = n;
19033
19034 n = list_find_nr(l, i++, NULL); /* col */
19035 if (n < 0)
19036 return FAIL;
19037 posp->col = n;
19038
19039#ifdef FEAT_VIRTUALEDIT
19040 n = list_find_nr(l, i, NULL);
19041 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019042 posp->coladd = 0;
19043 else
19044 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019045#endif
19046
19047 return OK;
19048}
19049
19050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 * Get the length of an environment variable name.
19052 * Advance "arg" to the first character after the name.
19053 * Return 0 for error.
19054 */
19055 static int
19056get_env_len(arg)
19057 char_u **arg;
19058{
19059 char_u *p;
19060 int len;
19061
19062 for (p = *arg; vim_isIDc(*p); ++p)
19063 ;
19064 if (p == *arg) /* no name found */
19065 return 0;
19066
19067 len = (int)(p - *arg);
19068 *arg = p;
19069 return len;
19070}
19071
19072/*
19073 * Get the length of the name of a function or internal variable.
19074 * "arg" is advanced to the first non-white character after the name.
19075 * Return 0 if something is wrong.
19076 */
19077 static int
19078get_id_len(arg)
19079 char_u **arg;
19080{
19081 char_u *p;
19082 int len;
19083
19084 /* Find the end of the name. */
19085 for (p = *arg; eval_isnamec(*p); ++p)
19086 ;
19087 if (p == *arg) /* no name found */
19088 return 0;
19089
19090 len = (int)(p - *arg);
19091 *arg = skipwhite(p);
19092
19093 return len;
19094}
19095
19096/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019097 * Get the length of the name of a variable or function.
19098 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019100 * Return -1 if curly braces expansion failed.
19101 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 * If the name contains 'magic' {}'s, expand them and return the
19103 * expanded name in an allocated string via 'alias' - caller must free.
19104 */
19105 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019106get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 char_u **arg;
19108 char_u **alias;
19109 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019110 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111{
19112 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 char_u *p;
19114 char_u *expr_start;
19115 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116
19117 *alias = NULL; /* default to no alias */
19118
19119 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19120 && (*arg)[2] == (int)KE_SNR)
19121 {
19122 /* hard coded <SNR>, already translated */
19123 *arg += 3;
19124 return get_id_len(arg) + 3;
19125 }
19126 len = eval_fname_script(*arg);
19127 if (len > 0)
19128 {
19129 /* literal "<SID>", "s:" or "<SNR>" */
19130 *arg += len;
19131 }
19132
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019136 p = find_name_end(*arg, &expr_start, &expr_end,
19137 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 if (expr_start != NULL)
19139 {
19140 char_u *temp_string;
19141
19142 if (!evaluate)
19143 {
19144 len += (int)(p - *arg);
19145 *arg = skipwhite(p);
19146 return len;
19147 }
19148
19149 /*
19150 * Include any <SID> etc in the expanded string:
19151 * Thus the -len here.
19152 */
19153 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19154 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019155 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 *alias = temp_string;
19157 *arg = skipwhite(p);
19158 return (int)STRLEN(temp_string);
19159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160
19161 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019162 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 EMSG2(_(e_invexpr2), *arg);
19164
19165 return len;
19166}
19167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168/*
19169 * Find the end of a variable or function name, taking care of magic braces.
19170 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19171 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019172 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173 * Return a pointer to just after the name. Equal to "arg" if there is no
19174 * valid name.
19175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019177find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 char_u *arg;
19179 char_u **expr_start;
19180 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019181 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 int mb_nest = 0;
19184 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 char_u *p;
19186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019187 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019189 *expr_start = NULL;
19190 *expr_end = NULL;
19191 }
19192
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019193 /* Quick check for valid starting character. */
19194 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19195 return arg;
19196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019197 for (p = arg; *p != NUL
19198 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019199 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019200 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019202 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019204 if (*p == '\'')
19205 {
19206 /* skip over 'string' to avoid counting [ and ] inside it. */
19207 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19208 ;
19209 if (*p == NUL)
19210 break;
19211 }
19212 else if (*p == '"')
19213 {
19214 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19215 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19216 if (*p == '\\' && p[1] != NUL)
19217 ++p;
19218 if (*p == NUL)
19219 break;
19220 }
19221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019222 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019224 if (*p == '[')
19225 ++br_nest;
19226 else if (*p == ']')
19227 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019230 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232 if (*p == '{')
19233 {
19234 mb_nest++;
19235 if (expr_start != NULL && *expr_start == NULL)
19236 *expr_start = p;
19237 }
19238 else if (*p == '}')
19239 {
19240 mb_nest--;
19241 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19242 *expr_end = p;
19243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 }
19246
19247 return p;
19248}
19249
19250/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019251 * Expands out the 'magic' {}'s in a variable/function name.
19252 * Note that this can call itself recursively, to deal with
19253 * constructs like foo{bar}{baz}{bam}
19254 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19255 * "in_start" ^
19256 * "expr_start" ^
19257 * "expr_end" ^
19258 * "in_end" ^
19259 *
19260 * Returns a new allocated string, which the caller must free.
19261 * Returns NULL for failure.
19262 */
19263 static char_u *
19264make_expanded_name(in_start, expr_start, expr_end, in_end)
19265 char_u *in_start;
19266 char_u *expr_start;
19267 char_u *expr_end;
19268 char_u *in_end;
19269{
19270 char_u c1;
19271 char_u *retval = NULL;
19272 char_u *temp_result;
19273 char_u *nextcmd = NULL;
19274
19275 if (expr_end == NULL || in_end == NULL)
19276 return NULL;
19277 *expr_start = NUL;
19278 *expr_end = NUL;
19279 c1 = *in_end;
19280 *in_end = NUL;
19281
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019282 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019283 if (temp_result != NULL && nextcmd == NULL)
19284 {
19285 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19286 + (in_end - expr_end) + 1));
19287 if (retval != NULL)
19288 {
19289 STRCPY(retval, in_start);
19290 STRCAT(retval, temp_result);
19291 STRCAT(retval, expr_end + 1);
19292 }
19293 }
19294 vim_free(temp_result);
19295
19296 *in_end = c1; /* put char back for error messages */
19297 *expr_start = '{';
19298 *expr_end = '}';
19299
19300 if (retval != NULL)
19301 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019302 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019303 if (expr_start != NULL)
19304 {
19305 /* Further expansion! */
19306 temp_result = make_expanded_name(retval, expr_start,
19307 expr_end, temp_result);
19308 vim_free(retval);
19309 retval = temp_result;
19310 }
19311 }
19312
19313 return retval;
19314}
19315
19316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019318 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 */
19320 static int
19321eval_isnamec(c)
19322 int c;
19323{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019324 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19325}
19326
19327/*
19328 * Return TRUE if character "c" can be used as the first character in a
19329 * variable or function name (excluding '{' and '}').
19330 */
19331 static int
19332eval_isnamec1(c)
19333 int c;
19334{
19335 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336}
19337
19338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 * Set number v: variable to "val".
19340 */
19341 void
19342set_vim_var_nr(idx, val)
19343 int idx;
19344 long val;
19345{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019346 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347}
19348
19349/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019350 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 */
19352 long
19353get_vim_var_nr(idx)
19354 int idx;
19355{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019356 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357}
19358
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019359/*
19360 * Get string v: variable value. Uses a static buffer, can only be used once.
19361 */
19362 char_u *
19363get_vim_var_str(idx)
19364 int idx;
19365{
19366 return get_tv_string(&vimvars[idx].vv_tv);
19367}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019368
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019370 * Get List v: variable value. Caller must take care of reference count when
19371 * needed.
19372 */
19373 list_T *
19374get_vim_var_list(idx)
19375 int idx;
19376{
19377 return vimvars[idx].vv_list;
19378}
19379
19380/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019381 * Set v:char to character "c".
19382 */
19383 void
19384set_vim_var_char(c)
19385 int c;
19386{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019387 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019388
19389#ifdef FEAT_MBYTE
19390 if (has_mbyte)
19391 buf[(*mb_char2bytes)(c, buf)] = NUL;
19392 else
19393#endif
19394 {
19395 buf[0] = c;
19396 buf[1] = NUL;
19397 }
19398 set_vim_var_string(VV_CHAR, buf, -1);
19399}
19400
19401/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019402 * Set v:count to "count" and v:count1 to "count1".
19403 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 */
19405 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019406set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 long count;
19408 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019409 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019411 if (set_prevcount)
19412 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019413 vimvars[VV_COUNT].vv_nr = count;
19414 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415}
19416
19417/*
19418 * Set string v: variable to a copy of "val".
19419 */
19420 void
19421set_vim_var_string(idx, val, len)
19422 int idx;
19423 char_u *val;
19424 int len; /* length of "val" to use or -1 (whole string) */
19425{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019426 /* Need to do this (at least) once, since we can't initialize a union.
19427 * Will always be invoked when "v:progname" is set. */
19428 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19429
Bram Moolenaare9a41262005-01-15 22:18:47 +000019430 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019432 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019434 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019436 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437}
19438
19439/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019440 * Set List v: variable to "val".
19441 */
19442 void
19443set_vim_var_list(idx, val)
19444 int idx;
19445 list_T *val;
19446{
19447 list_unref(vimvars[idx].vv_list);
19448 vimvars[idx].vv_list = val;
19449 if (val != NULL)
19450 ++val->lv_refcount;
19451}
19452
19453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 * Set v:register if needed.
19455 */
19456 void
19457set_reg_var(c)
19458 int c;
19459{
19460 char_u regname;
19461
19462 if (c == 0 || c == ' ')
19463 regname = '"';
19464 else
19465 regname = c;
19466 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019467 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 set_vim_var_string(VV_REG, &regname, 1);
19469}
19470
19471/*
19472 * Get or set v:exception. If "oldval" == NULL, return the current value.
19473 * Otherwise, restore the value to "oldval" and return NULL.
19474 * Must always be called in pairs to save and restore v:exception! Does not
19475 * take care of memory allocations.
19476 */
19477 char_u *
19478v_exception(oldval)
19479 char_u *oldval;
19480{
19481 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019482 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483
Bram Moolenaare9a41262005-01-15 22:18:47 +000019484 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 return NULL;
19486}
19487
19488/*
19489 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19490 * Otherwise, restore the value to "oldval" and return NULL.
19491 * Must always be called in pairs to save and restore v:throwpoint! Does not
19492 * take care of memory allocations.
19493 */
19494 char_u *
19495v_throwpoint(oldval)
19496 char_u *oldval;
19497{
19498 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019499 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500
Bram Moolenaare9a41262005-01-15 22:18:47 +000019501 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 return NULL;
19503}
19504
19505#if defined(FEAT_AUTOCMD) || defined(PROTO)
19506/*
19507 * Set v:cmdarg.
19508 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19509 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19510 * Must always be called in pairs!
19511 */
19512 char_u *
19513set_cmdarg(eap, oldarg)
19514 exarg_T *eap;
19515 char_u *oldarg;
19516{
19517 char_u *oldval;
19518 char_u *newval;
19519 unsigned len;
19520
Bram Moolenaare9a41262005-01-15 22:18:47 +000019521 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019522 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019524 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019525 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019526 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 }
19528
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019529 if (eap->force_bin == FORCE_BIN)
19530 len = 6;
19531 else if (eap->force_bin == FORCE_NOBIN)
19532 len = 8;
19533 else
19534 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019535
19536 if (eap->read_edit)
19537 len += 7;
19538
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019539 if (eap->force_ff != 0)
19540 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19541# ifdef FEAT_MBYTE
19542 if (eap->force_enc != 0)
19543 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019544 if (eap->bad_char != 0)
19545 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019546# endif
19547
19548 newval = alloc(len + 1);
19549 if (newval == NULL)
19550 return NULL;
19551
19552 if (eap->force_bin == FORCE_BIN)
19553 sprintf((char *)newval, " ++bin");
19554 else if (eap->force_bin == FORCE_NOBIN)
19555 sprintf((char *)newval, " ++nobin");
19556 else
19557 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019558
19559 if (eap->read_edit)
19560 STRCAT(newval, " ++edit");
19561
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019562 if (eap->force_ff != 0)
19563 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19564 eap->cmd + eap->force_ff);
19565# ifdef FEAT_MBYTE
19566 if (eap->force_enc != 0)
19567 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19568 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019569 if (eap->bad_char == BAD_KEEP)
19570 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19571 else if (eap->bad_char == BAD_DROP)
19572 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19573 else if (eap->bad_char != 0)
19574 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019575# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019576 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019577 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578}
19579#endif
19580
19581/*
19582 * Get the value of internal variable "name".
19583 * Return OK or FAIL.
19584 */
19585 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019586get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 char_u *name;
19588 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019589 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019590 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591{
19592 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 typval_T *tv = NULL;
19594 typval_T atv;
19595 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597
19598 /* truncate the name, so that we can use strcmp() */
19599 cc = name[len];
19600 name[len] = NUL;
19601
19602 /*
19603 * Check for "b:changedtick".
19604 */
19605 if (STRCMP(name, "b:changedtick") == 0)
19606 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019607 atv.v_type = VAR_NUMBER;
19608 atv.vval.v_number = curbuf->b_changedtick;
19609 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 }
19611
19612 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 * Check for user-defined variables.
19614 */
19615 else
19616 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019617 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 }
19621
Bram Moolenaare9a41262005-01-15 22:18:47 +000019622 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019624 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019625 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 ret = FAIL;
19627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019629 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630
19631 name[len] = cc;
19632
19633 return ret;
19634}
19635
19636/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019637 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19638 * Also handle function call with Funcref variable: func(expr)
19639 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19640 */
19641 static int
19642handle_subscript(arg, rettv, evaluate, verbose)
19643 char_u **arg;
19644 typval_T *rettv;
19645 int evaluate; /* do more than finding the end */
19646 int verbose; /* give error messages */
19647{
19648 int ret = OK;
19649 dict_T *selfdict = NULL;
19650 char_u *s;
19651 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019652 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019653
19654 while (ret == OK
19655 && (**arg == '['
19656 || (**arg == '.' && rettv->v_type == VAR_DICT)
19657 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19658 && !vim_iswhite(*(*arg - 1)))
19659 {
19660 if (**arg == '(')
19661 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019662 /* need to copy the funcref so that we can clear rettv */
19663 functv = *rettv;
19664 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019665
19666 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019667 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019668 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019669 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19670 &len, evaluate, selfdict);
19671
19672 /* Clear the funcref afterwards, so that deleting it while
19673 * evaluating the arguments is possible (see test55). */
19674 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019675
19676 /* Stop the expression evaluation when immediately aborting on
19677 * error, or when an interrupt occurred or an exception was thrown
19678 * but not caught. */
19679 if (aborting())
19680 {
19681 if (ret == OK)
19682 clear_tv(rettv);
19683 ret = FAIL;
19684 }
19685 dict_unref(selfdict);
19686 selfdict = NULL;
19687 }
19688 else /* **arg == '[' || **arg == '.' */
19689 {
19690 dict_unref(selfdict);
19691 if (rettv->v_type == VAR_DICT)
19692 {
19693 selfdict = rettv->vval.v_dict;
19694 if (selfdict != NULL)
19695 ++selfdict->dv_refcount;
19696 }
19697 else
19698 selfdict = NULL;
19699 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19700 {
19701 clear_tv(rettv);
19702 ret = FAIL;
19703 }
19704 }
19705 }
19706 dict_unref(selfdict);
19707 return ret;
19708}
19709
19710/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019711 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019712 * value).
19713 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019714 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019716{
Bram Moolenaar33570922005-01-25 22:26:29 +000019717 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019718}
19719
19720/*
19721 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 * The string "s" must have been allocated, it is consumed.
19723 * Return NULL for out of memory, the variable otherwise.
19724 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019725 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 char_u *s;
19728{
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019731 rettv = alloc_tv();
19732 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 rettv->v_type = VAR_STRING;
19735 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 }
19737 else
19738 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019739 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740}
19741
19742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019743 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019745 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748{
19749 if (varp != NULL)
19750 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019751 switch (varp->v_type)
19752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019753 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019754 func_unref(varp->vval.v_string);
19755 /*FALLTHROUGH*/
19756 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019757 vim_free(varp->vval.v_string);
19758 break;
19759 case VAR_LIST:
19760 list_unref(varp->vval.v_list);
19761 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019762 case VAR_DICT:
19763 dict_unref(varp->vval.v_dict);
19764 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019765 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019766#ifdef FEAT_FLOAT
19767 case VAR_FLOAT:
19768#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019769 case VAR_UNKNOWN:
19770 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019771 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019772 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019773 break;
19774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 vim_free(varp);
19776 }
19777}
19778
19779/*
19780 * Free the memory for a variable value and set the value to NULL or 0.
19781 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019782 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019783clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019784 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785{
19786 if (varp != NULL)
19787 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019788 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019790 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019791 func_unref(varp->vval.v_string);
19792 /*FALLTHROUGH*/
19793 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019794 vim_free(varp->vval.v_string);
19795 varp->vval.v_string = NULL;
19796 break;
19797 case VAR_LIST:
19798 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019799 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019800 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019801 case VAR_DICT:
19802 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019803 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019804 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019806 varp->vval.v_number = 0;
19807 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019808#ifdef FEAT_FLOAT
19809 case VAR_FLOAT:
19810 varp->vval.v_float = 0.0;
19811 break;
19812#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019813 case VAR_UNKNOWN:
19814 break;
19815 default:
19816 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019818 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 }
19820}
19821
19822/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 * Set the value of a variable to NULL without freeing items.
19824 */
19825 static void
19826init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019827 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828{
19829 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019830 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831}
19832
19833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 * Get the number value of a variable.
19835 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019836 * For incompatible types, return 0.
19837 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19838 * caller of incompatible types: it sets *denote to TRUE if "denote"
19839 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 */
19841 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019843 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019845 int error = FALSE;
19846
19847 return get_tv_number_chk(varp, &error); /* return 0L on error */
19848}
19849
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019850 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019851get_tv_number_chk(varp, denote)
19852 typval_T *varp;
19853 int *denote;
19854{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019855 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019857 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019859 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019860 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019861#ifdef FEAT_FLOAT
19862 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019863 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019864 break;
19865#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019866 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019867 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019868 break;
19869 case VAR_STRING:
19870 if (varp->vval.v_string != NULL)
19871 vim_str2nr(varp->vval.v_string, NULL, NULL,
19872 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019873 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019874 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019875 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019876 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019877 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019878 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019879 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019880 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019881 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019882 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019884 if (denote == NULL) /* useful for values that must be unsigned */
19885 n = -1;
19886 else
19887 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019888 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889}
19890
19891/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019892 * Get the lnum from the first argument.
19893 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019894 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 */
19896 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019897get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019898 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899{
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 linenr_T lnum;
19902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019903 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 if (lnum == 0) /* no valid number, try using line() */
19905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019906 rettv.v_type = VAR_NUMBER;
19907 f_line(argvars, &rettv);
19908 lnum = rettv.vval.v_number;
19909 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 }
19911 return lnum;
19912}
19913
19914/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019915 * Get the lnum from the first argument.
19916 * Also accepts "$", then "buf" is used.
19917 * Returns 0 on error.
19918 */
19919 static linenr_T
19920get_tv_lnum_buf(argvars, buf)
19921 typval_T *argvars;
19922 buf_T *buf;
19923{
19924 if (argvars[0].v_type == VAR_STRING
19925 && argvars[0].vval.v_string != NULL
19926 && argvars[0].vval.v_string[0] == '$'
19927 && buf != NULL)
19928 return buf->b_ml.ml_line_count;
19929 return get_tv_number_chk(&argvars[0], NULL);
19930}
19931
19932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 * Get the string value of a variable.
19934 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019935 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19936 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 * If the String variable has never been set, return an empty string.
19938 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019939 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19940 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 */
19942 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019943get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019944 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945{
19946 static char_u mybuf[NUMBUFLEN];
19947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019948 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949}
19950
19951 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019952get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019953 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019956 char_u *res = get_tv_string_buf_chk(varp, buf);
19957
19958 return res != NULL ? res : (char_u *)"";
19959}
19960
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019961 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019962get_tv_string_chk(varp)
19963 typval_T *varp;
19964{
19965 static char_u mybuf[NUMBUFLEN];
19966
19967 return get_tv_string_buf_chk(varp, mybuf);
19968}
19969
19970 static char_u *
19971get_tv_string_buf_chk(varp, buf)
19972 typval_T *varp;
19973 char_u *buf;
19974{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019975 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019977 case VAR_NUMBER:
19978 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19979 return buf;
19980 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019981 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019982 break;
19983 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019984 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019985 break;
19986 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019987 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019988 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019989#ifdef FEAT_FLOAT
19990 case VAR_FLOAT:
19991 EMSG(_("E806: using Float as a String"));
19992 break;
19993#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019994 case VAR_STRING:
19995 if (varp->vval.v_string != NULL)
19996 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019997 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019998 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020000 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020002 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003}
20004
20005/*
20006 * Find variable "name" in the list of variables.
20007 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020008 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020009 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020010 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020012 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020013find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020015 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020018 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019
Bram Moolenaara7043832005-01-21 11:56:39 +000020020 ht = find_var_ht(name, &varname);
20021 if (htp != NULL)
20022 *htp = ht;
20023 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020025 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026}
20027
20028/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020029 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020030 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020032 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020033find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020035 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020036 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020037 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020038{
Bram Moolenaar33570922005-01-25 22:26:29 +000020039 hashitem_T *hi;
20040
20041 if (*varname == NUL)
20042 {
20043 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020044 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020045 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020046 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020047 case 'g': return &globvars_var;
20048 case 'v': return &vimvars_var;
20049 case 'b': return &curbuf->b_bufvar;
20050 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020051#ifdef FEAT_WINDOWS
20052 case 't': return &curtab->tp_winvar;
20053#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020054 case 'l': return current_funccal == NULL
20055 ? NULL : &current_funccal->l_vars_var;
20056 case 'a': return current_funccal == NULL
20057 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 }
20059 return NULL;
20060 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020061
20062 hi = hash_find(ht, varname);
20063 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020064 {
20065 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020066 * worked find the variable again. Don't auto-load a script if it was
20067 * loaded already, otherwise it would be loaded every time when
20068 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020069 if (ht == &globvarht && !writing)
20070 {
20071 /* Note: script_autoload() may make "hi" invalid. It must either
20072 * be obtained again or not used. */
20073 if (!script_autoload(varname, FALSE) || aborting())
20074 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020075 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020076 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020077 if (HASHITEM_EMPTY(hi))
20078 return NULL;
20079 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020080 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020081}
20082
20083/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020084 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020085 * Set "varname" to the start of name without ':'.
20086 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020087 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020088find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 char_u *name;
20090 char_u **varname;
20091{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020092 hashitem_T *hi;
20093
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 if (name[1] != ':')
20095 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020096 /* The name must not start with a colon or #. */
20097 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 return NULL;
20099 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020100
20101 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020102 hi = hash_find(&compat_hashtab, name);
20103 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020104 return &compat_hashtab;
20105
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 return &globvarht; /* global variable */
20108 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 }
20110 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020111 if (*name == 'g') /* global variable */
20112 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020113 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20114 */
20115 if (vim_strchr(name + 2, ':') != NULL
20116 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020117 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020119 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020121 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020122#ifdef FEAT_WINDOWS
20123 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020124 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020125#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020126 if (*name == 'v') /* v: variable */
20127 return &vimvarht;
20128 if (*name == 'a' && current_funccal != NULL) /* function argument */
20129 return &current_funccal->l_avars.dv_hashtab;
20130 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20131 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 if (*name == 's' /* script variable */
20133 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20134 return &SCRIPT_VARS(current_SID);
20135 return NULL;
20136}
20137
20138/*
20139 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020140 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 * Returns NULL when it doesn't exist.
20142 */
20143 char_u *
20144get_var_value(name)
20145 char_u *name;
20146{
Bram Moolenaar33570922005-01-25 22:26:29 +000020147 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148
Bram Moolenaara7043832005-01-21 11:56:39 +000020149 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 if (v == NULL)
20151 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153}
20154
20155/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 * sourcing this script and when executing functions defined in the script.
20158 */
20159 void
20160new_script_vars(id)
20161 scid_T id;
20162{
Bram Moolenaara7043832005-01-21 11:56:39 +000020163 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 hashtab_T *ht;
20165 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020166
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20168 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020169 /* Re-allocating ga_data means that an ht_array pointing to
20170 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020171 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020172 for (i = 1; i <= ga_scripts.ga_len; ++i)
20173 {
20174 ht = &SCRIPT_VARS(i);
20175 if (ht->ht_mask == HT_INIT_SIZE - 1)
20176 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020177 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020178 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020179 }
20180
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 while (ga_scripts.ga_len < id)
20182 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020183 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020184 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020185 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 }
20188 }
20189}
20190
20191/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020192 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20193 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194 */
20195 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020196init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020197 dict_T *dict;
20198 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020199 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200{
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020202 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020203 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020204 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020205 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020206 dict_var->di_tv.vval.v_dict = dict;
20207 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020208 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20210 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211}
20212
20213/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020214 * Unreference a dictionary initialized by init_var_dict().
20215 */
20216 void
20217unref_var_dict(dict)
20218 dict_T *dict;
20219{
20220 /* Now the dict needs to be freed if no one else is using it, go back to
20221 * normal reference counting. */
20222 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20223 dict_unref(dict);
20224}
20225
20226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 * Frees all allocated variables and the value they contain.
20229 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 */
20231 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020232vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020233 hashtab_T *ht;
20234{
20235 vars_clear_ext(ht, TRUE);
20236}
20237
20238/*
20239 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20240 */
20241 static void
20242vars_clear_ext(ht, free_val)
20243 hashtab_T *ht;
20244 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245{
Bram Moolenaara7043832005-01-21 11:56:39 +000020246 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020247 hashitem_T *hi;
20248 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249
Bram Moolenaar33570922005-01-25 22:26:29 +000020250 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020251 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020252 for (hi = ht->ht_array; todo > 0; ++hi)
20253 {
20254 if (!HASHITEM_EMPTY(hi))
20255 {
20256 --todo;
20257
Bram Moolenaar33570922005-01-25 22:26:29 +000020258 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020259 * ht_array might change then. hash_clear() takes care of it
20260 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020261 v = HI2DI(hi);
20262 if (free_val)
20263 clear_tv(&v->di_tv);
20264 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20265 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020266 }
20267 }
20268 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020269 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270}
20271
Bram Moolenaara7043832005-01-21 11:56:39 +000020272/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020273 * Delete a variable from hashtab "ht" at item "hi".
20274 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020277delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020278 hashtab_T *ht;
20279 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280{
Bram Moolenaar33570922005-01-25 22:26:29 +000020281 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020282
20283 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020284 clear_tv(&di->di_tv);
20285 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286}
20287
20288/*
20289 * List the value of one internal variable.
20290 */
20291 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020292list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020293 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020295 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020297 char_u *tofree;
20298 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020299 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020300
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020301 current_copyID += COPYID_INC;
20302 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020304 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020305 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306}
20307
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020309list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 char_u *prefix;
20311 char_u *name;
20312 int type;
20313 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020314 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315{
Bram Moolenaar31859182007-08-14 20:41:13 +000020316 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20317 msg_start();
20318 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 if (name != NULL) /* "a:" vars don't have a name stored */
20320 msg_puts(name);
20321 msg_putchar(' ');
20322 msg_advance(22);
20323 if (type == VAR_NUMBER)
20324 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020325 else if (type == VAR_FUNC)
20326 msg_putchar('*');
20327 else if (type == VAR_LIST)
20328 {
20329 msg_putchar('[');
20330 if (*string == '[')
20331 ++string;
20332 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020333 else if (type == VAR_DICT)
20334 {
20335 msg_putchar('{');
20336 if (*string == '{')
20337 ++string;
20338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 else
20340 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020341
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020343
20344 if (type == VAR_FUNC)
20345 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020346 if (*first)
20347 {
20348 msg_clr_eos();
20349 *first = FALSE;
20350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351}
20352
20353/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020354 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 * If the variable already exists, the value is updated.
20356 * Otherwise the variable is created.
20357 */
20358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020359set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020362 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363{
Bram Moolenaar33570922005-01-25 22:26:29 +000020364 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020368 ht = find_var_ht(name, &varname);
20369 if (ht == NULL || *varname == NUL)
20370 {
20371 EMSG2(_(e_illvar), name);
20372 return;
20373 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020374 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020375
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020376 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20377 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020378
Bram Moolenaar33570922005-01-25 22:26:29 +000020379 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020382 if (var_check_ro(v->di_flags, name)
20383 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020384 return;
20385 if (v->di_tv.v_type != tv->v_type
20386 && !((v->di_tv.v_type == VAR_STRING
20387 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020388 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020389 || tv->v_type == VAR_NUMBER))
20390#ifdef FEAT_FLOAT
20391 && !((v->di_tv.v_type == VAR_NUMBER
20392 || v->di_tv.v_type == VAR_FLOAT)
20393 && (tv->v_type == VAR_NUMBER
20394 || tv->v_type == VAR_FLOAT))
20395#endif
20396 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020397 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020398 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020399 return;
20400 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020401
20402 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020403 * Handle setting internal v: variables separately: we don't change
20404 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020405 */
20406 if (ht == &vimvarht)
20407 {
20408 if (v->di_tv.v_type == VAR_STRING)
20409 {
20410 vim_free(v->di_tv.vval.v_string);
20411 if (copy || tv->v_type != VAR_STRING)
20412 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20413 else
20414 {
20415 /* Take over the string to avoid an extra alloc/free. */
20416 v->di_tv.vval.v_string = tv->vval.v_string;
20417 tv->vval.v_string = NULL;
20418 }
20419 }
20420 else if (v->di_tv.v_type != VAR_NUMBER)
20421 EMSG2(_(e_intern2), "set_var()");
20422 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020423 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020424 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020425 if (STRCMP(varname, "searchforward") == 0)
20426 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20427 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020428 return;
20429 }
20430
20431 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 }
20433 else /* add a new variable */
20434 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020435 /* Can't add "v:" variable. */
20436 if (ht == &vimvarht)
20437 {
20438 EMSG2(_(e_illvar), name);
20439 return;
20440 }
20441
Bram Moolenaar92124a32005-06-17 22:03:40 +000020442 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020443 if (!valid_varname(varname))
20444 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020445
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020446 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20447 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020448 if (v == NULL)
20449 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020450 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020451 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020453 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 return;
20455 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020456 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020458
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020459 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020460 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020461 else
20462 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020463 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020464 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020465 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467}
20468
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020469/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020470 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020471 * Also give an error message.
20472 */
20473 static int
20474var_check_ro(flags, name)
20475 int flags;
20476 char_u *name;
20477{
20478 if (flags & DI_FLAGS_RO)
20479 {
20480 EMSG2(_(e_readonlyvar), name);
20481 return TRUE;
20482 }
20483 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20484 {
20485 EMSG2(_(e_readonlysbx), name);
20486 return TRUE;
20487 }
20488 return FALSE;
20489}
20490
20491/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020492 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20493 * Also give an error message.
20494 */
20495 static int
20496var_check_fixed(flags, name)
20497 int flags;
20498 char_u *name;
20499{
20500 if (flags & DI_FLAGS_FIX)
20501 {
20502 EMSG2(_("E795: Cannot delete variable %s"), name);
20503 return TRUE;
20504 }
20505 return FALSE;
20506}
20507
20508/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020509 * Check if a funcref is assigned to a valid variable name.
20510 * Return TRUE and give an error if not.
20511 */
20512 static int
20513var_check_func_name(name, new_var)
20514 char_u *name; /* points to start of variable name */
20515 int new_var; /* TRUE when creating the variable */
20516{
20517 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20518 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20519 ? name[2] : name[0]))
20520 {
20521 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20522 name);
20523 return TRUE;
20524 }
20525 /* Don't allow hiding a function. When "v" is not NULL we might be
20526 * assigning another function to the same var, the type is checked
20527 * below. */
20528 if (new_var && function_exists(name))
20529 {
20530 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20531 name);
20532 return TRUE;
20533 }
20534 return FALSE;
20535}
20536
20537/*
20538 * Check if a variable name is valid.
20539 * Return FALSE and give an error if not.
20540 */
20541 static int
20542valid_varname(varname)
20543 char_u *varname;
20544{
20545 char_u *p;
20546
20547 for (p = varname; *p != NUL; ++p)
20548 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20549 && *p != AUTOLOAD_CHAR)
20550 {
20551 EMSG2(_(e_illvar), varname);
20552 return FALSE;
20553 }
20554 return TRUE;
20555}
20556
20557/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020558 * Return TRUE if typeval "tv" is set to be locked (immutable).
20559 * Also give an error message, using "name".
20560 */
20561 static int
20562tv_check_lock(lock, name)
20563 int lock;
20564 char_u *name;
20565{
20566 if (lock & VAR_LOCKED)
20567 {
20568 EMSG2(_("E741: Value is locked: %s"),
20569 name == NULL ? (char_u *)_("Unknown") : name);
20570 return TRUE;
20571 }
20572 if (lock & VAR_FIXED)
20573 {
20574 EMSG2(_("E742: Cannot change value of %s"),
20575 name == NULL ? (char_u *)_("Unknown") : name);
20576 return TRUE;
20577 }
20578 return FALSE;
20579}
20580
20581/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020582 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020583 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020584 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020585 * It is OK for "from" and "to" to point to the same item. This is used to
20586 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020587 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020588 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020589copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020590 typval_T *from;
20591 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020593 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020594 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020595 switch (from->v_type)
20596 {
20597 case VAR_NUMBER:
20598 to->vval.v_number = from->vval.v_number;
20599 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020600#ifdef FEAT_FLOAT
20601 case VAR_FLOAT:
20602 to->vval.v_float = from->vval.v_float;
20603 break;
20604#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020605 case VAR_STRING:
20606 case VAR_FUNC:
20607 if (from->vval.v_string == NULL)
20608 to->vval.v_string = NULL;
20609 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020611 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020612 if (from->v_type == VAR_FUNC)
20613 func_ref(to->vval.v_string);
20614 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020615 break;
20616 case VAR_LIST:
20617 if (from->vval.v_list == NULL)
20618 to->vval.v_list = NULL;
20619 else
20620 {
20621 to->vval.v_list = from->vval.v_list;
20622 ++to->vval.v_list->lv_refcount;
20623 }
20624 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020625 case VAR_DICT:
20626 if (from->vval.v_dict == NULL)
20627 to->vval.v_dict = NULL;
20628 else
20629 {
20630 to->vval.v_dict = from->vval.v_dict;
20631 ++to->vval.v_dict->dv_refcount;
20632 }
20633 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020634 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020635 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020636 break;
20637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638}
20639
20640/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020641 * Make a copy of an item.
20642 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020643 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20644 * reference to an already copied list/dict can be used.
20645 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020646 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020647 static int
20648item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020649 typval_T *from;
20650 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020651 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020652 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020653{
20654 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020655 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020656
Bram Moolenaar33570922005-01-25 22:26:29 +000020657 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020658 {
20659 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020660 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020661 }
20662 ++recurse;
20663
20664 switch (from->v_type)
20665 {
20666 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020667#ifdef FEAT_FLOAT
20668 case VAR_FLOAT:
20669#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020670 case VAR_STRING:
20671 case VAR_FUNC:
20672 copy_tv(from, to);
20673 break;
20674 case VAR_LIST:
20675 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020676 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020677 if (from->vval.v_list == NULL)
20678 to->vval.v_list = NULL;
20679 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20680 {
20681 /* use the copy made earlier */
20682 to->vval.v_list = from->vval.v_list->lv_copylist;
20683 ++to->vval.v_list->lv_refcount;
20684 }
20685 else
20686 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20687 if (to->vval.v_list == NULL)
20688 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020689 break;
20690 case VAR_DICT:
20691 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020692 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020693 if (from->vval.v_dict == NULL)
20694 to->vval.v_dict = NULL;
20695 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20696 {
20697 /* use the copy made earlier */
20698 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20699 ++to->vval.v_dict->dv_refcount;
20700 }
20701 else
20702 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20703 if (to->vval.v_dict == NULL)
20704 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020705 break;
20706 default:
20707 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020708 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020709 }
20710 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020711 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020712}
20713
20714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 * ":echo expr1 ..." print each argument separated with a space, add a
20716 * newline at the end.
20717 * ":echon expr1 ..." print each argument plain.
20718 */
20719 void
20720ex_echo(eap)
20721 exarg_T *eap;
20722{
20723 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020724 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020725 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 char_u *p;
20727 int needclr = TRUE;
20728 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020729 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730
20731 if (eap->skip)
20732 ++emsg_skip;
20733 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20734 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020735 /* If eval1() causes an error message the text from the command may
20736 * still need to be cleared. E.g., "echo 22,44". */
20737 need_clr_eos = needclr;
20738
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020740 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 {
20742 /*
20743 * Report the invalid expression unless the expression evaluation
20744 * has been cancelled due to an aborting error, an interrupt, or an
20745 * exception.
20746 */
20747 if (!aborting())
20748 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020749 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 break;
20751 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020752 need_clr_eos = FALSE;
20753
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 if (!eap->skip)
20755 {
20756 if (atstart)
20757 {
20758 atstart = FALSE;
20759 /* Call msg_start() after eval1(), evaluating the expression
20760 * may cause a message to appear. */
20761 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020762 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020763 /* Mark the saved text as finishing the line, so that what
20764 * follows is displayed on a new line when scrolling back
20765 * at the more prompt. */
20766 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 }
20770 else if (eap->cmdidx == CMD_echo)
20771 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020772 current_copyID += COPYID_INC;
20773 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020774 if (p != NULL)
20775 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020777 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020779 if (*p != TAB && needclr)
20780 {
20781 /* remove any text still there from the command */
20782 msg_clr_eos();
20783 needclr = FALSE;
20784 }
20785 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 }
20787 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020788 {
20789#ifdef FEAT_MBYTE
20790 if (has_mbyte)
20791 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020792 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020793
20794 (void)msg_outtrans_len_attr(p, i, echo_attr);
20795 p += i - 1;
20796 }
20797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020799 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020802 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 arg = skipwhite(arg);
20806 }
20807 eap->nextcmd = check_nextcmd(arg);
20808
20809 if (eap->skip)
20810 --emsg_skip;
20811 else
20812 {
20813 /* remove text that may still be there from the command */
20814 if (needclr)
20815 msg_clr_eos();
20816 if (eap->cmdidx == CMD_echo)
20817 msg_end();
20818 }
20819}
20820
20821/*
20822 * ":echohl {name}".
20823 */
20824 void
20825ex_echohl(eap)
20826 exarg_T *eap;
20827{
20828 int id;
20829
20830 id = syn_name2id(eap->arg);
20831 if (id == 0)
20832 echo_attr = 0;
20833 else
20834 echo_attr = syn_id2attr(id);
20835}
20836
20837/*
20838 * ":execute expr1 ..." execute the result of an expression.
20839 * ":echomsg expr1 ..." Print a message
20840 * ":echoerr expr1 ..." Print an error
20841 * Each gets spaces around each argument and a newline at the end for
20842 * echo commands
20843 */
20844 void
20845ex_execute(eap)
20846 exarg_T *eap;
20847{
20848 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020849 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 int ret = OK;
20851 char_u *p;
20852 garray_T ga;
20853 int len;
20854 int save_did_emsg;
20855
20856 ga_init2(&ga, 1, 80);
20857
20858 if (eap->skip)
20859 ++emsg_skip;
20860 while (*arg != NUL && *arg != '|' && *arg != '\n')
20861 {
20862 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020863 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 {
20865 /*
20866 * Report the invalid expression unless the expression evaluation
20867 * has been cancelled due to an aborting error, an interrupt, or an
20868 * exception.
20869 */
20870 if (!aborting())
20871 EMSG2(_(e_invexpr2), p);
20872 ret = FAIL;
20873 break;
20874 }
20875
20876 if (!eap->skip)
20877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020878 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 len = (int)STRLEN(p);
20880 if (ga_grow(&ga, len + 2) == FAIL)
20881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020882 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 ret = FAIL;
20884 break;
20885 }
20886 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 ga.ga_len += len;
20890 }
20891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 arg = skipwhite(arg);
20894 }
20895
20896 if (ret != FAIL && ga.ga_data != NULL)
20897 {
20898 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020901 out_flush();
20902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 else if (eap->cmdidx == CMD_echoerr)
20904 {
20905 /* We don't want to abort following commands, restore did_emsg. */
20906 save_did_emsg = did_emsg;
20907 EMSG((char_u *)ga.ga_data);
20908 if (!force_abort)
20909 did_emsg = save_did_emsg;
20910 }
20911 else if (eap->cmdidx == CMD_execute)
20912 do_cmdline((char_u *)ga.ga_data,
20913 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20914 }
20915
20916 ga_clear(&ga);
20917
20918 if (eap->skip)
20919 --emsg_skip;
20920
20921 eap->nextcmd = check_nextcmd(arg);
20922}
20923
20924/*
20925 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20926 * "arg" points to the "&" or '+' when called, to "option" when returning.
20927 * Returns NULL when no option name found. Otherwise pointer to the char
20928 * after the option name.
20929 */
20930 static char_u *
20931find_option_end(arg, opt_flags)
20932 char_u **arg;
20933 int *opt_flags;
20934{
20935 char_u *p = *arg;
20936
20937 ++p;
20938 if (*p == 'g' && p[1] == ':')
20939 {
20940 *opt_flags = OPT_GLOBAL;
20941 p += 2;
20942 }
20943 else if (*p == 'l' && p[1] == ':')
20944 {
20945 *opt_flags = OPT_LOCAL;
20946 p += 2;
20947 }
20948 else
20949 *opt_flags = 0;
20950
20951 if (!ASCII_ISALPHA(*p))
20952 return NULL;
20953 *arg = p;
20954
20955 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20956 p += 4; /* termcap option */
20957 else
20958 while (ASCII_ISALPHA(*p))
20959 ++p;
20960 return p;
20961}
20962
20963/*
20964 * ":function"
20965 */
20966 void
20967ex_function(eap)
20968 exarg_T *eap;
20969{
20970 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020971 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 int j;
20973 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 char_u *name = NULL;
20976 char_u *p;
20977 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020978 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 garray_T newargs;
20980 garray_T newlines;
20981 int varargs = FALSE;
20982 int mustend = FALSE;
20983 int flags = 0;
20984 ufunc_T *fp;
20985 int indent;
20986 int nesting;
20987 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020988 dictitem_T *v;
20989 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020990 static int func_nr = 0; /* number for nameless function */
20991 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020992 hashtab_T *ht;
20993 int todo;
20994 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020995 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996
20997 /*
20998 * ":function" without argument: list functions.
20999 */
21000 if (ends_excmd(*eap->arg))
21001 {
21002 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021003 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021004 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021005 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021006 {
21007 if (!HASHITEM_EMPTY(hi))
21008 {
21009 --todo;
21010 fp = HI2UF(hi);
21011 if (!isdigit(*fp->uf_name))
21012 list_func_head(fp, FALSE);
21013 }
21014 }
21015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 eap->nextcmd = check_nextcmd(eap->arg);
21017 return;
21018 }
21019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021020 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021021 * ":function /pat": list functions matching pattern.
21022 */
21023 if (*eap->arg == '/')
21024 {
21025 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21026 if (!eap->skip)
21027 {
21028 regmatch_T regmatch;
21029
21030 c = *p;
21031 *p = NUL;
21032 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21033 *p = c;
21034 if (regmatch.regprog != NULL)
21035 {
21036 regmatch.rm_ic = p_ic;
21037
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021038 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021039 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21040 {
21041 if (!HASHITEM_EMPTY(hi))
21042 {
21043 --todo;
21044 fp = HI2UF(hi);
21045 if (!isdigit(*fp->uf_name)
21046 && vim_regexec(&regmatch, fp->uf_name, 0))
21047 list_func_head(fp, FALSE);
21048 }
21049 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021050 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021051 }
21052 }
21053 if (*p == '/')
21054 ++p;
21055 eap->nextcmd = check_nextcmd(p);
21056 return;
21057 }
21058
21059 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021060 * Get the function name. There are these situations:
21061 * func normal function name
21062 * "name" == func, "fudi.fd_dict" == NULL
21063 * dict.func new dictionary entry
21064 * "name" == NULL, "fudi.fd_dict" set,
21065 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21066 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021067 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021068 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21069 * dict.func existing dict entry that's not a Funcref
21070 * "name" == NULL, "fudi.fd_dict" set,
21071 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021074 name = trans_function_name(&p, eap->skip, 0, &fudi);
21075 paren = (vim_strchr(p, '(') != NULL);
21076 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 {
21078 /*
21079 * Return on an invalid expression in braces, unless the expression
21080 * evaluation has been cancelled due to an aborting error, an
21081 * interrupt, or an exception.
21082 */
21083 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021085 if (!eap->skip && fudi.fd_newkey != NULL)
21086 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021087 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 else
21091 eap->skip = TRUE;
21092 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021093
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 /* An error in a function call during evaluation of an expression in magic
21095 * braces should not cause the function not to be defined. */
21096 saved_did_emsg = did_emsg;
21097 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098
21099 /*
21100 * ":function func" with only function name: list function.
21101 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021102 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 {
21104 if (!ends_excmd(*skipwhite(p)))
21105 {
21106 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021107 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 }
21109 eap->nextcmd = check_nextcmd(p);
21110 if (eap->nextcmd != NULL)
21111 *p = NUL;
21112 if (!eap->skip && !got_int)
21113 {
21114 fp = find_func(name);
21115 if (fp != NULL)
21116 {
21117 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021118 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021120 if (FUNCLINE(fp, j) == NULL)
21121 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 msg_putchar('\n');
21123 msg_outnum((long)(j + 1));
21124 if (j < 9)
21125 msg_putchar(' ');
21126 if (j < 99)
21127 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021128 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 out_flush(); /* show a line at a time */
21130 ui_breakcheck();
21131 }
21132 if (!got_int)
21133 {
21134 msg_putchar('\n');
21135 msg_puts((char_u *)" endfunction");
21136 }
21137 }
21138 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021139 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021141 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 }
21143
21144 /*
21145 * ":function name(arg1, arg2)" Define function.
21146 */
21147 p = skipwhite(p);
21148 if (*p != '(')
21149 {
21150 if (!eap->skip)
21151 {
21152 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021153 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 }
21155 /* attempt to continue by skipping some text */
21156 if (vim_strchr(p, '(') != NULL)
21157 p = vim_strchr(p, '(');
21158 }
21159 p = skipwhite(p + 1);
21160
21161 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21162 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21163
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021164 if (!eap->skip)
21165 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021166 /* Check the name of the function. Unless it's a dictionary function
21167 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021168 if (name != NULL)
21169 arg = name;
21170 else
21171 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021172 if (arg != NULL && (fudi.fd_di == NULL
21173 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021174 {
21175 if (*arg == K_SPECIAL)
21176 j = 3;
21177 else
21178 j = 0;
21179 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21180 : eval_isnamec(arg[j])))
21181 ++j;
21182 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021183 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021184 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021185 /* Disallow using the g: dict. */
21186 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21187 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021188 }
21189
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 /*
21191 * Isolate the arguments: "arg1, arg2, ...)"
21192 */
21193 while (*p != ')')
21194 {
21195 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21196 {
21197 varargs = TRUE;
21198 p += 3;
21199 mustend = TRUE;
21200 }
21201 else
21202 {
21203 arg = p;
21204 while (ASCII_ISALNUM(*p) || *p == '_')
21205 ++p;
21206 if (arg == p || isdigit(*arg)
21207 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21208 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21209 {
21210 if (!eap->skip)
21211 EMSG2(_("E125: Illegal argument: %s"), arg);
21212 break;
21213 }
21214 if (ga_grow(&newargs, 1) == FAIL)
21215 goto erret;
21216 c = *p;
21217 *p = NUL;
21218 arg = vim_strsave(arg);
21219 if (arg == NULL)
21220 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021221
21222 /* Check for duplicate argument name. */
21223 for (i = 0; i < newargs.ga_len; ++i)
21224 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21225 {
21226 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21227 goto erret;
21228 }
21229
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21231 *p = c;
21232 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 if (*p == ',')
21234 ++p;
21235 else
21236 mustend = TRUE;
21237 }
21238 p = skipwhite(p);
21239 if (mustend && *p != ')')
21240 {
21241 if (!eap->skip)
21242 EMSG2(_(e_invarg2), eap->arg);
21243 break;
21244 }
21245 }
21246 ++p; /* skip the ')' */
21247
Bram Moolenaare9a41262005-01-15 22:18:47 +000021248 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 for (;;)
21250 {
21251 p = skipwhite(p);
21252 if (STRNCMP(p, "range", 5) == 0)
21253 {
21254 flags |= FC_RANGE;
21255 p += 5;
21256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021257 else if (STRNCMP(p, "dict", 4) == 0)
21258 {
21259 flags |= FC_DICT;
21260 p += 4;
21261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 else if (STRNCMP(p, "abort", 5) == 0)
21263 {
21264 flags |= FC_ABORT;
21265 p += 5;
21266 }
21267 else
21268 break;
21269 }
21270
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021271 /* When there is a line break use what follows for the function body.
21272 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21273 if (*p == '\n')
21274 line_arg = p + 1;
21275 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 EMSG(_(e_trailing));
21277
21278 /*
21279 * Read the body of the function, until ":endfunction" is found.
21280 */
21281 if (KeyTyped)
21282 {
21283 /* Check if the function already exists, don't let the user type the
21284 * whole function before telling him it doesn't work! For a script we
21285 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021286 if (!eap->skip && !eap->forceit)
21287 {
21288 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21289 EMSG(_(e_funcdict));
21290 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021291 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021294 if (!eap->skip && did_emsg)
21295 goto erret;
21296
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 msg_putchar('\n'); /* don't overwrite the function name */
21298 cmdline_row = msg_row;
21299 }
21300
21301 indent = 2;
21302 nesting = 0;
21303 for (;;)
21304 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021305 if (KeyTyped)
21306 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021308 sourcing_lnum_off = sourcing_lnum;
21309
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021310 if (line_arg != NULL)
21311 {
21312 /* Use eap->arg, split up in parts by line breaks. */
21313 theline = line_arg;
21314 p = vim_strchr(theline, '\n');
21315 if (p == NULL)
21316 line_arg += STRLEN(line_arg);
21317 else
21318 {
21319 *p = NUL;
21320 line_arg = p + 1;
21321 }
21322 }
21323 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 theline = getcmdline(':', 0L, indent);
21325 else
21326 theline = eap->getline(':', eap->cookie, indent);
21327 if (KeyTyped)
21328 lines_left = Rows - 1;
21329 if (theline == NULL)
21330 {
21331 EMSG(_("E126: Missing :endfunction"));
21332 goto erret;
21333 }
21334
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021335 /* Detect line continuation: sourcing_lnum increased more than one. */
21336 if (sourcing_lnum > sourcing_lnum_off + 1)
21337 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21338 else
21339 sourcing_lnum_off = 0;
21340
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 if (skip_until != NULL)
21342 {
21343 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21344 * don't check for ":endfunc". */
21345 if (STRCMP(theline, skip_until) == 0)
21346 {
21347 vim_free(skip_until);
21348 skip_until = NULL;
21349 }
21350 }
21351 else
21352 {
21353 /* skip ':' and blanks*/
21354 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21355 ;
21356
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021357 /* Check for "endfunction". */
21358 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021360 if (line_arg == NULL)
21361 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 break;
21363 }
21364
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021365 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 * at "end". */
21367 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21368 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021369 else if (STRNCMP(p, "if", 2) == 0
21370 || STRNCMP(p, "wh", 2) == 0
21371 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 || STRNCMP(p, "try", 3) == 0)
21373 indent += 2;
21374
21375 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021376 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021378 if (*p == '!')
21379 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 p += eval_fname_script(p);
21381 if (ASCII_ISALPHA(*p))
21382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021383 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 if (*skipwhite(p) == '(')
21385 {
21386 ++nesting;
21387 indent += 2;
21388 }
21389 }
21390 }
21391
21392 /* Check for ":append" or ":insert". */
21393 p = skip_range(p, NULL);
21394 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21395 || (p[0] == 'i'
21396 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21397 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21398 skip_until = vim_strsave((char_u *)".");
21399
21400 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21401 arg = skipwhite(skiptowhite(p));
21402 if (arg[0] == '<' && arg[1] =='<'
21403 && ((p[0] == 'p' && p[1] == 'y'
21404 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21405 || (p[0] == 'p' && p[1] == 'e'
21406 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21407 || (p[0] == 't' && p[1] == 'c'
21408 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021409 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21410 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21412 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021413 || (p[0] == 'm' && p[1] == 'z'
21414 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 ))
21416 {
21417 /* ":python <<" continues until a dot, like ":append" */
21418 p = skipwhite(arg + 2);
21419 if (*p == NUL)
21420 skip_until = vim_strsave((char_u *)".");
21421 else
21422 skip_until = vim_strsave(p);
21423 }
21424 }
21425
21426 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021427 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021428 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021429 if (line_arg == NULL)
21430 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021432 }
21433
21434 /* Copy the line to newly allocated memory. get_one_sourceline()
21435 * allocates 250 bytes per line, this saves 80% on average. The cost
21436 * is an extra alloc/free. */
21437 p = vim_strsave(theline);
21438 if (p != NULL)
21439 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021440 if (line_arg == NULL)
21441 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021442 theline = p;
21443 }
21444
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021445 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21446
21447 /* Add NULL lines for continuation lines, so that the line count is
21448 * equal to the index in the growarray. */
21449 while (sourcing_lnum_off-- > 0)
21450 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021451
21452 /* Check for end of eap->arg. */
21453 if (line_arg != NULL && *line_arg == NUL)
21454 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 }
21456
21457 /* Don't define the function when skipping commands or when an error was
21458 * detected. */
21459 if (eap->skip || did_emsg)
21460 goto erret;
21461
21462 /*
21463 * If there are no errors, add the function
21464 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021465 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021466 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021467 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021468 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021469 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021470 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021471 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021472 goto erret;
21473 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021474
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021475 fp = find_func(name);
21476 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021478 if (!eap->forceit)
21479 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021480 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021481 goto erret;
21482 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021483 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021484 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021485 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021486 name);
21487 goto erret;
21488 }
21489 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021490 ga_clear_strings(&(fp->uf_args));
21491 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021492 vim_free(name);
21493 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 }
21496 else
21497 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021498 char numbuf[20];
21499
21500 fp = NULL;
21501 if (fudi.fd_newkey == NULL && !eap->forceit)
21502 {
21503 EMSG(_(e_funcdict));
21504 goto erret;
21505 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021506 if (fudi.fd_di == NULL)
21507 {
21508 /* Can't add a function to a locked dictionary */
21509 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21510 goto erret;
21511 }
21512 /* Can't change an existing function if it is locked */
21513 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21514 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021515
21516 /* Give the function a sequential number. Can only be used with a
21517 * Funcref! */
21518 vim_free(name);
21519 sprintf(numbuf, "%d", ++func_nr);
21520 name = vim_strsave((char_u *)numbuf);
21521 if (name == NULL)
21522 goto erret;
21523 }
21524
21525 if (fp == NULL)
21526 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021527 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021528 {
21529 int slen, plen;
21530 char_u *scriptname;
21531
21532 /* Check that the autoload name matches the script name. */
21533 j = FAIL;
21534 if (sourcing_name != NULL)
21535 {
21536 scriptname = autoload_name(name);
21537 if (scriptname != NULL)
21538 {
21539 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021540 plen = (int)STRLEN(p);
21541 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021542 if (slen > plen && fnamecmp(p,
21543 sourcing_name + slen - plen) == 0)
21544 j = OK;
21545 vim_free(scriptname);
21546 }
21547 }
21548 if (j == FAIL)
21549 {
21550 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21551 goto erret;
21552 }
21553 }
21554
21555 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 if (fp == NULL)
21557 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021558
21559 if (fudi.fd_dict != NULL)
21560 {
21561 if (fudi.fd_di == NULL)
21562 {
21563 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021564 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021565 if (fudi.fd_di == NULL)
21566 {
21567 vim_free(fp);
21568 goto erret;
21569 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021570 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21571 {
21572 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021573 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021574 goto erret;
21575 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021576 }
21577 else
21578 /* overwrite existing dict entry */
21579 clear_tv(&fudi.fd_di->di_tv);
21580 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021581 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021582 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021583 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021584
21585 /* behave like "dict" was used */
21586 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 }
21588
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021590 STRCPY(fp->uf_name, name);
21591 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021593 fp->uf_args = newargs;
21594 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021595#ifdef FEAT_PROFILE
21596 fp->uf_tml_count = NULL;
21597 fp->uf_tml_total = NULL;
21598 fp->uf_tml_self = NULL;
21599 fp->uf_profiling = FALSE;
21600 if (prof_def_func())
21601 func_do_profile(fp);
21602#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021603 fp->uf_varargs = varargs;
21604 fp->uf_flags = flags;
21605 fp->uf_calls = 0;
21606 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021607 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608
21609erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 ga_clear_strings(&newargs);
21611 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021612ret_free:
21613 vim_free(skip_until);
21614 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617}
21618
21619/*
21620 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021621 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021623 * flags:
21624 * TFN_INT: internal function name OK
21625 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 * Advances "pp" to just after the function name (if no error).
21627 */
21628 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021629trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 char_u **pp;
21631 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021632 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021633 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021635 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 char_u *start;
21637 char_u *end;
21638 int lead;
21639 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021641 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021642
21643 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021644 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021646
21647 /* Check for hard coded <SNR>: already translated function ID (from a user
21648 * command). */
21649 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21650 && (*pp)[2] == (int)KE_SNR)
21651 {
21652 *pp += 3;
21653 len = get_id_len(pp) + 3;
21654 return vim_strnsave(start, len);
21655 }
21656
21657 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21658 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021660 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021662
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021663 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21664 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021665 if (end == start)
21666 {
21667 if (!skip)
21668 EMSG(_("E129: Function name required"));
21669 goto theend;
21670 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021671 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021672 {
21673 /*
21674 * Report an invalid expression in braces, unless the expression
21675 * evaluation has been cancelled due to an aborting error, an
21676 * interrupt, or an exception.
21677 */
21678 if (!aborting())
21679 {
21680 if (end != NULL)
21681 EMSG2(_(e_invarg2), start);
21682 }
21683 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021684 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021685 goto theend;
21686 }
21687
21688 if (lv.ll_tv != NULL)
21689 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021690 if (fdp != NULL)
21691 {
21692 fdp->fd_dict = lv.ll_dict;
21693 fdp->fd_newkey = lv.ll_newkey;
21694 lv.ll_newkey = NULL;
21695 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021697 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21698 {
21699 name = vim_strsave(lv.ll_tv->vval.v_string);
21700 *pp = end;
21701 }
21702 else
21703 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021704 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21705 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021706 EMSG(_(e_funcref));
21707 else
21708 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021709 name = NULL;
21710 }
21711 goto theend;
21712 }
21713
21714 if (lv.ll_name == NULL)
21715 {
21716 /* Error found, but continue after the function name. */
21717 *pp = end;
21718 goto theend;
21719 }
21720
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021721 /* Check if the name is a Funcref. If so, use the value. */
21722 if (lv.ll_exp_name != NULL)
21723 {
21724 len = (int)STRLEN(lv.ll_exp_name);
21725 name = deref_func_name(lv.ll_exp_name, &len);
21726 if (name == lv.ll_exp_name)
21727 name = NULL;
21728 }
21729 else
21730 {
21731 len = (int)(end - *pp);
21732 name = deref_func_name(*pp, &len);
21733 if (name == *pp)
21734 name = NULL;
21735 }
21736 if (name != NULL)
21737 {
21738 name = vim_strsave(name);
21739 *pp = end;
21740 goto theend;
21741 }
21742
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021743 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021744 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021745 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021746 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21747 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21748 {
21749 /* When there was "s:" already or the name expanded to get a
21750 * leading "s:" then remove it. */
21751 lv.ll_name += 2;
21752 len -= 2;
21753 lead = 2;
21754 }
21755 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021756 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021757 {
21758 if (lead == 2) /* skip over "s:" */
21759 lv.ll_name += 2;
21760 len = (int)(end - lv.ll_name);
21761 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021762
21763 /*
21764 * Copy the function name to allocated memory.
21765 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21766 * Accept <SNR>123_name() outside a script.
21767 */
21768 if (skip)
21769 lead = 0; /* do nothing */
21770 else if (lead > 0)
21771 {
21772 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021773 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21774 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021775 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021776 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021777 if (current_SID <= 0)
21778 {
21779 EMSG(_(e_usingsid));
21780 goto theend;
21781 }
21782 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21783 lead += (int)STRLEN(sid_buf);
21784 }
21785 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021786 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021787 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021788 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021789 goto theend;
21790 }
21791 name = alloc((unsigned)(len + lead + 1));
21792 if (name != NULL)
21793 {
21794 if (lead > 0)
21795 {
21796 name[0] = K_SPECIAL;
21797 name[1] = KS_EXTRA;
21798 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021799 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021800 STRCPY(name + 3, sid_buf);
21801 }
21802 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21803 name[len + lead] = NUL;
21804 }
21805 *pp = end;
21806
21807theend:
21808 clear_lval(&lv);
21809 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810}
21811
21812/*
21813 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21814 * Return 2 if "p" starts with "s:".
21815 * Return 0 otherwise.
21816 */
21817 static int
21818eval_fname_script(p)
21819 char_u *p;
21820{
21821 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21822 || STRNICMP(p + 1, "SNR>", 4) == 0))
21823 return 5;
21824 if (p[0] == 's' && p[1] == ':')
21825 return 2;
21826 return 0;
21827}
21828
21829/*
21830 * Return TRUE if "p" starts with "<SID>" or "s:".
21831 * Only works if eval_fname_script() returned non-zero for "p"!
21832 */
21833 static int
21834eval_fname_sid(p)
21835 char_u *p;
21836{
21837 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21838}
21839
21840/*
21841 * List the head of the function: "name(arg1, arg2)".
21842 */
21843 static void
21844list_func_head(fp, indent)
21845 ufunc_T *fp;
21846 int indent;
21847{
21848 int j;
21849
21850 msg_start();
21851 if (indent)
21852 MSG_PUTS(" ");
21853 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021854 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 {
21856 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021857 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 }
21859 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021860 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021862 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 {
21864 if (j)
21865 MSG_PUTS(", ");
21866 msg_puts(FUNCARG(fp, j));
21867 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021868 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 {
21870 if (j)
21871 MSG_PUTS(", ");
21872 MSG_PUTS("...");
21873 }
21874 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021875 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021876 if (p_verbose > 0)
21877 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878}
21879
21880/*
21881 * Find a function by name, return pointer to it in ufuncs.
21882 * Return NULL for unknown function.
21883 */
21884 static ufunc_T *
21885find_func(name)
21886 char_u *name;
21887{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021888 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021890 hi = hash_find(&func_hashtab, name);
21891 if (!HASHITEM_EMPTY(hi))
21892 return HI2UF(hi);
21893 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894}
21895
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021896#if defined(EXITFREE) || defined(PROTO)
21897 void
21898free_all_functions()
21899{
21900 hashitem_T *hi;
21901
21902 /* Need to start all over every time, because func_free() may change the
21903 * hash table. */
21904 while (func_hashtab.ht_used > 0)
21905 for (hi = func_hashtab.ht_array; ; ++hi)
21906 if (!HASHITEM_EMPTY(hi))
21907 {
21908 func_free(HI2UF(hi));
21909 break;
21910 }
21911}
21912#endif
21913
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021914/*
21915 * Return TRUE if a function "name" exists.
21916 */
21917 static int
21918function_exists(name)
21919 char_u *name;
21920{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021921 char_u *nm = name;
21922 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021923 int n = FALSE;
21924
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021925 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021926 nm = skipwhite(nm);
21927
21928 /* Only accept "funcname", "funcname ", "funcname (..." and
21929 * "funcname(...", not "funcname!...". */
21930 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021931 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021932 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021933 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021934 else
21935 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021936 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021937 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021938 return n;
21939}
21940
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021941/*
21942 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021943 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021944 */
21945 static int
21946builtin_function(name)
21947 char_u *name;
21948{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021949 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21950 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021951}
21952
Bram Moolenaar05159a02005-02-26 23:04:13 +000021953#if defined(FEAT_PROFILE) || defined(PROTO)
21954/*
21955 * Start profiling function "fp".
21956 */
21957 static void
21958func_do_profile(fp)
21959 ufunc_T *fp;
21960{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021961 int len = fp->uf_lines.ga_len;
21962
21963 if (len == 0)
21964 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021965 fp->uf_tm_count = 0;
21966 profile_zero(&fp->uf_tm_self);
21967 profile_zero(&fp->uf_tm_total);
21968 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021969 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021970 if (fp->uf_tml_total == NULL)
21971 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021972 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021973 if (fp->uf_tml_self == NULL)
21974 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021975 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021976 fp->uf_tml_idx = -1;
21977 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21978 || fp->uf_tml_self == NULL)
21979 return; /* out of memory */
21980
21981 fp->uf_profiling = TRUE;
21982}
21983
21984/*
21985 * Dump the profiling results for all functions in file "fd".
21986 */
21987 void
21988func_dump_profile(fd)
21989 FILE *fd;
21990{
21991 hashitem_T *hi;
21992 int todo;
21993 ufunc_T *fp;
21994 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021995 ufunc_T **sorttab;
21996 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021997
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021998 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021999 if (todo == 0)
22000 return; /* nothing to dump */
22001
Bram Moolenaar73830342005-02-28 22:48:19 +000022002 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22003
Bram Moolenaar05159a02005-02-26 23:04:13 +000022004 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22005 {
22006 if (!HASHITEM_EMPTY(hi))
22007 {
22008 --todo;
22009 fp = HI2UF(hi);
22010 if (fp->uf_profiling)
22011 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022012 if (sorttab != NULL)
22013 sorttab[st_len++] = fp;
22014
Bram Moolenaar05159a02005-02-26 23:04:13 +000022015 if (fp->uf_name[0] == K_SPECIAL)
22016 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22017 else
22018 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22019 if (fp->uf_tm_count == 1)
22020 fprintf(fd, "Called 1 time\n");
22021 else
22022 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22023 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22024 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22025 fprintf(fd, "\n");
22026 fprintf(fd, "count total (s) self (s)\n");
22027
22028 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22029 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022030 if (FUNCLINE(fp, i) == NULL)
22031 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022032 prof_func_line(fd, fp->uf_tml_count[i],
22033 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022034 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22035 }
22036 fprintf(fd, "\n");
22037 }
22038 }
22039 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022040
22041 if (sorttab != NULL && st_len > 0)
22042 {
22043 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22044 prof_total_cmp);
22045 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22046 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22047 prof_self_cmp);
22048 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22049 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022050
22051 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022052}
Bram Moolenaar73830342005-02-28 22:48:19 +000022053
22054 static void
22055prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22056 FILE *fd;
22057 ufunc_T **sorttab;
22058 int st_len;
22059 char *title;
22060 int prefer_self; /* when equal print only self time */
22061{
22062 int i;
22063 ufunc_T *fp;
22064
22065 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22066 fprintf(fd, "count total (s) self (s) function\n");
22067 for (i = 0; i < 20 && i < st_len; ++i)
22068 {
22069 fp = sorttab[i];
22070 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22071 prefer_self);
22072 if (fp->uf_name[0] == K_SPECIAL)
22073 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22074 else
22075 fprintf(fd, " %s()\n", fp->uf_name);
22076 }
22077 fprintf(fd, "\n");
22078}
22079
22080/*
22081 * Print the count and times for one function or function line.
22082 */
22083 static void
22084prof_func_line(fd, count, total, self, prefer_self)
22085 FILE *fd;
22086 int count;
22087 proftime_T *total;
22088 proftime_T *self;
22089 int prefer_self; /* when equal print only self time */
22090{
22091 if (count > 0)
22092 {
22093 fprintf(fd, "%5d ", count);
22094 if (prefer_self && profile_equal(total, self))
22095 fprintf(fd, " ");
22096 else
22097 fprintf(fd, "%s ", profile_msg(total));
22098 if (!prefer_self && profile_equal(total, self))
22099 fprintf(fd, " ");
22100 else
22101 fprintf(fd, "%s ", profile_msg(self));
22102 }
22103 else
22104 fprintf(fd, " ");
22105}
22106
22107/*
22108 * Compare function for total time sorting.
22109 */
22110 static int
22111#ifdef __BORLANDC__
22112_RTLENTRYF
22113#endif
22114prof_total_cmp(s1, s2)
22115 const void *s1;
22116 const void *s2;
22117{
22118 ufunc_T *p1, *p2;
22119
22120 p1 = *(ufunc_T **)s1;
22121 p2 = *(ufunc_T **)s2;
22122 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22123}
22124
22125/*
22126 * Compare function for self time sorting.
22127 */
22128 static int
22129#ifdef __BORLANDC__
22130_RTLENTRYF
22131#endif
22132prof_self_cmp(s1, s2)
22133 const void *s1;
22134 const void *s2;
22135{
22136 ufunc_T *p1, *p2;
22137
22138 p1 = *(ufunc_T **)s1;
22139 p2 = *(ufunc_T **)s2;
22140 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22141}
22142
Bram Moolenaar05159a02005-02-26 23:04:13 +000022143#endif
22144
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022145/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022146 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022147 * Return TRUE if a package was loaded.
22148 */
22149 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022150script_autoload(name, reload)
22151 char_u *name;
22152 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022153{
22154 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022155 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022156 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022157 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022158
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022159 /* Return quickly when autoload disabled. */
22160 if (no_autoload)
22161 return FALSE;
22162
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022163 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022164 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022165 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022166 return FALSE;
22167
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022168 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022169
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022170 /* Find the name in the list of previously loaded package names. Skip
22171 * "autoload/", it's always the same. */
22172 for (i = 0; i < ga_loaded.ga_len; ++i)
22173 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22174 break;
22175 if (!reload && i < ga_loaded.ga_len)
22176 ret = FALSE; /* was loaded already */
22177 else
22178 {
22179 /* Remember the name if it wasn't loaded already. */
22180 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22181 {
22182 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22183 tofree = NULL;
22184 }
22185
22186 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022187 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022188 ret = TRUE;
22189 }
22190
22191 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022192 return ret;
22193}
22194
22195/*
22196 * Return the autoload script name for a function or variable name.
22197 * Returns NULL when out of memory.
22198 */
22199 static char_u *
22200autoload_name(name)
22201 char_u *name;
22202{
22203 char_u *p;
22204 char_u *scriptname;
22205
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022206 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022207 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22208 if (scriptname == NULL)
22209 return FALSE;
22210 STRCPY(scriptname, "autoload/");
22211 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022212 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022213 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022214 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022215 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022216 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022217}
22218
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22220
22221/*
22222 * Function given to ExpandGeneric() to obtain the list of user defined
22223 * function names.
22224 */
22225 char_u *
22226get_user_func_name(xp, idx)
22227 expand_T *xp;
22228 int idx;
22229{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022230 static long_u done;
22231 static hashitem_T *hi;
22232 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233
22234 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022236 done = 0;
22237 hi = func_hashtab.ht_array;
22238 }
22239 if (done < func_hashtab.ht_used)
22240 {
22241 if (done++ > 0)
22242 ++hi;
22243 while (HASHITEM_EMPTY(hi))
22244 ++hi;
22245 fp = HI2UF(hi);
22246
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022247 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022248 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022249
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022250 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22251 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252
22253 cat_func_name(IObuff, fp);
22254 if (xp->xp_context != EXPAND_USER_FUNC)
22255 {
22256 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022257 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 STRCAT(IObuff, ")");
22259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 return IObuff;
22261 }
22262 return NULL;
22263}
22264
22265#endif /* FEAT_CMDL_COMPL */
22266
22267/*
22268 * Copy the function name of "fp" to buffer "buf".
22269 * "buf" must be able to hold the function name plus three bytes.
22270 * Takes care of script-local function names.
22271 */
22272 static void
22273cat_func_name(buf, fp)
22274 char_u *buf;
22275 ufunc_T *fp;
22276{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022277 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 {
22279 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022280 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 }
22282 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022283 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284}
22285
22286/*
22287 * ":delfunction {name}"
22288 */
22289 void
22290ex_delfunction(eap)
22291 exarg_T *eap;
22292{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022293 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 char_u *p;
22295 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022296 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297
22298 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022299 name = trans_function_name(&p, eap->skip, 0, &fudi);
22300 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022302 {
22303 if (fudi.fd_dict != NULL && !eap->skip)
22304 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 if (!ends_excmd(*skipwhite(p)))
22308 {
22309 vim_free(name);
22310 EMSG(_(e_trailing));
22311 return;
22312 }
22313 eap->nextcmd = check_nextcmd(p);
22314 if (eap->nextcmd != NULL)
22315 *p = NUL;
22316
22317 if (!eap->skip)
22318 fp = find_func(name);
22319 vim_free(name);
22320
22321 if (!eap->skip)
22322 {
22323 if (fp == NULL)
22324 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022325 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 return;
22327 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022328 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 {
22330 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22331 return;
22332 }
22333
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022334 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022336 /* Delete the dict item that refers to the function, it will
22337 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022338 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340 else
22341 func_free(fp);
22342 }
22343}
22344
22345/*
22346 * Free a function and remove it from the list of functions.
22347 */
22348 static void
22349func_free(fp)
22350 ufunc_T *fp;
22351{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022352 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022353
22354 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022355 ga_clear_strings(&(fp->uf_args));
22356 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022357#ifdef FEAT_PROFILE
22358 vim_free(fp->uf_tml_count);
22359 vim_free(fp->uf_tml_total);
22360 vim_free(fp->uf_tml_self);
22361#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022362
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022363 /* remove the function from the function hashtable */
22364 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22365 if (HASHITEM_EMPTY(hi))
22366 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022367 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022368 hash_remove(&func_hashtab, hi);
22369
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022370 vim_free(fp);
22371}
22372
22373/*
22374 * Unreference a Function: decrement the reference count and free it when it
22375 * becomes zero. Only for numbered functions.
22376 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022377 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022378func_unref(name)
22379 char_u *name;
22380{
22381 ufunc_T *fp;
22382
22383 if (name != NULL && isdigit(*name))
22384 {
22385 fp = find_func(name);
22386 if (fp == NULL)
22387 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022388 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022389 {
22390 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022391 * when "uf_calls" becomes zero. */
22392 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022393 func_free(fp);
22394 }
22395 }
22396}
22397
22398/*
22399 * Count a reference to a Function.
22400 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022401 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022402func_ref(name)
22403 char_u *name;
22404{
22405 ufunc_T *fp;
22406
22407 if (name != NULL && isdigit(*name))
22408 {
22409 fp = find_func(name);
22410 if (fp == NULL)
22411 EMSG2(_(e_intern2), "func_ref()");
22412 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022413 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 }
22415}
22416
22417/*
22418 * Call a user function.
22419 */
22420 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022421call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 ufunc_T *fp; /* pointer to function */
22423 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 typval_T *argvars; /* arguments */
22425 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 linenr_T firstline; /* first line of range */
22427 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022428 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429{
Bram Moolenaar33570922005-01-25 22:26:29 +000022430 char_u *save_sourcing_name;
22431 linenr_T save_sourcing_lnum;
22432 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022433 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 int save_did_emsg;
22435 static int depth = 0;
22436 dictitem_T *v;
22437 int fixvar_idx = 0; /* index in fixvar[] */
22438 int i;
22439 int ai;
22440 char_u numbuf[NUMBUFLEN];
22441 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022442#ifdef FEAT_PROFILE
22443 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022444 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446
22447 /* If depth of calling is getting too high, don't execute the function */
22448 if (depth >= p_mfd)
22449 {
22450 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022451 rettv->v_type = VAR_NUMBER;
22452 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 return;
22454 }
22455 ++depth;
22456
22457 line_breakcheck(); /* check for CTRL-C hit */
22458
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022459 fc = (funccall_T *)alloc(sizeof(funccall_T));
22460 fc->caller = current_funccal;
22461 current_funccal = fc;
22462 fc->func = fp;
22463 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022464 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022465 fc->linenr = 0;
22466 fc->returned = FALSE;
22467 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022469 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22470 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471
Bram Moolenaar33570922005-01-25 22:26:29 +000022472 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022473 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022474 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22475 * each argument variable and saves a lot of time.
22476 */
22477 /*
22478 * Init l: variables.
22479 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022480 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022481 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022482 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022483 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22484 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022485 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022486 name = v->di_key;
22487 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022488 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022489 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022491 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 v->di_tv.vval.v_dict = selfdict;
22493 ++selfdict->dv_refcount;
22494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022495
Bram Moolenaar33570922005-01-25 22:26:29 +000022496 /*
22497 * Init a: variables.
22498 * Set a:0 to "argcount".
22499 * Set a:000 to a list with room for the "..." arguments.
22500 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022501 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022502 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022503 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022504 /* Use "name" to avoid a warning from some compiler that checks the
22505 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022506 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022507 name = v->di_key;
22508 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022509 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022510 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022511 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022512 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022513 v->di_tv.vval.v_list = &fc->l_varlist;
22514 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22515 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22516 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022517
22518 /*
22519 * Set a:firstline to "firstline" and a:lastline to "lastline".
22520 * Set a:name to named arguments.
22521 * Set a:N to the "..." arguments.
22522 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022523 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022524 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022525 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 (varnumber_T)lastline);
22527 for (i = 0; i < argcount; ++i)
22528 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022529 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022530 if (ai < 0)
22531 /* named argument a:name */
22532 name = FUNCARG(fp, i);
22533 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022534 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022535 /* "..." argument a:1, a:2, etc. */
22536 sprintf((char *)numbuf, "%d", ai + 1);
22537 name = numbuf;
22538 }
22539 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22540 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022541 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022542 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22543 }
22544 else
22545 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022546 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22547 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022548 if (v == NULL)
22549 break;
22550 v->di_flags = DI_FLAGS_RO;
22551 }
22552 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022553 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022554
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022555 /* Note: the values are copied directly to avoid alloc/free.
22556 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022558 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022559
22560 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22561 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022562 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22563 fc->l_listitems[ai].li_tv = argvars[i];
22564 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022565 }
22566 }
22567
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 /* Don't redraw while executing the function. */
22569 ++RedrawingDisabled;
22570 save_sourcing_name = sourcing_name;
22571 save_sourcing_lnum = sourcing_lnum;
22572 sourcing_lnum = 1;
22573 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022574 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575 if (sourcing_name != NULL)
22576 {
22577 if (save_sourcing_name != NULL
22578 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22579 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22580 else
22581 STRCPY(sourcing_name, "function ");
22582 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22583
22584 if (p_verbose >= 12)
22585 {
22586 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022587 verbose_enter_scroll();
22588
Bram Moolenaar555b2802005-05-19 21:08:39 +000022589 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 if (p_verbose >= 14)
22591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022593 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022594 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022595 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596
22597 msg_puts((char_u *)"(");
22598 for (i = 0; i < argcount; ++i)
22599 {
22600 if (i > 0)
22601 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022602 if (argvars[i].v_type == VAR_NUMBER)
22603 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 else
22605 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022606 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22607 if (s != NULL)
22608 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022609 if (vim_strsize(s) > MSG_BUF_CLEN)
22610 {
22611 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22612 s = buf;
22613 }
22614 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022615 vim_free(tofree);
22616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 }
22618 }
22619 msg_puts((char_u *)")");
22620 }
22621 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022622
22623 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 --no_wait_return;
22625 }
22626 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022627#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022628 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022629 {
22630 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22631 func_do_profile(fp);
22632 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022633 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022634 {
22635 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022636 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022637 profile_zero(&fp->uf_tm_children);
22638 }
22639 script_prof_save(&wait_start);
22640 }
22641#endif
22642
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022644 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 save_did_emsg = did_emsg;
22646 did_emsg = FALSE;
22647
22648 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022649 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22651
22652 --RedrawingDisabled;
22653
22654 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022655 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022657 clear_tv(rettv);
22658 rettv->v_type = VAR_NUMBER;
22659 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 }
22661
Bram Moolenaar05159a02005-02-26 23:04:13 +000022662#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022663 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022664 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022665 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022666 profile_end(&call_start);
22667 profile_sub_wait(&wait_start, &call_start);
22668 profile_add(&fp->uf_tm_total, &call_start);
22669 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022670 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022671 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022672 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22673 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022674 }
22675 }
22676#endif
22677
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 /* when being verbose, mention the return value */
22679 if (p_verbose >= 12)
22680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022682 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022685 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022686 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022687 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022688 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022691 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022692 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022693 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022694 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022695
Bram Moolenaar555b2802005-05-19 21:08:39 +000022696 /* The value may be very long. Skip the middle part, so that we
22697 * have some idea how it starts and ends. smsg() would always
22698 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022699 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022700 if (s != NULL)
22701 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022702 if (vim_strsize(s) > MSG_BUF_CLEN)
22703 {
22704 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22705 s = buf;
22706 }
22707 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022708 vim_free(tofree);
22709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 }
22711 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022712
22713 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 --no_wait_return;
22715 }
22716
22717 vim_free(sourcing_name);
22718 sourcing_name = save_sourcing_name;
22719 sourcing_lnum = save_sourcing_lnum;
22720 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022721#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022722 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022723 script_prof_restore(&wait_start);
22724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725
22726 if (p_verbose >= 12 && sourcing_name != NULL)
22727 {
22728 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022729 verbose_enter_scroll();
22730
Bram Moolenaar555b2802005-05-19 21:08:39 +000022731 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022733
22734 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 --no_wait_return;
22736 }
22737
22738 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022739 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022741
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022742 /* If the a:000 list and the l: and a: dicts are not referenced we can
22743 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022744 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22745 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22746 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22747 {
22748 free_funccal(fc, FALSE);
22749 }
22750 else
22751 {
22752 hashitem_T *hi;
22753 listitem_T *li;
22754 int todo;
22755
22756 /* "fc" is still in use. This can happen when returning "a:000" or
22757 * assigning "l:" to a global variable.
22758 * Link "fc" in the list for garbage collection later. */
22759 fc->caller = previous_funccal;
22760 previous_funccal = fc;
22761
22762 /* Make a copy of the a: variables, since we didn't do that above. */
22763 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22764 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22765 {
22766 if (!HASHITEM_EMPTY(hi))
22767 {
22768 --todo;
22769 v = HI2DI(hi);
22770 copy_tv(&v->di_tv, &v->di_tv);
22771 }
22772 }
22773
22774 /* Make a copy of the a:000 items, since we didn't do that above. */
22775 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22776 copy_tv(&li->li_tv, &li->li_tv);
22777 }
22778}
22779
22780/*
22781 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022782 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022783 */
22784 static int
22785can_free_funccal(fc, copyID)
22786 funccall_T *fc;
22787 int copyID;
22788{
22789 return (fc->l_varlist.lv_copyID != copyID
22790 && fc->l_vars.dv_copyID != copyID
22791 && fc->l_avars.dv_copyID != copyID);
22792}
22793
22794/*
22795 * Free "fc" and what it contains.
22796 */
22797 static void
22798free_funccal(fc, free_val)
22799 funccall_T *fc;
22800 int free_val; /* a: vars were allocated */
22801{
22802 listitem_T *li;
22803
22804 /* The a: variables typevals may not have been allocated, only free the
22805 * allocated variables. */
22806 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22807
22808 /* free all l: variables */
22809 vars_clear(&fc->l_vars.dv_hashtab);
22810
22811 /* Free the a:000 variables if they were allocated. */
22812 if (free_val)
22813 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22814 clear_tv(&li->li_tv);
22815
22816 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817}
22818
22819/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022820 * Add a number variable "name" to dict "dp" with value "nr".
22821 */
22822 static void
22823add_nr_var(dp, v, name, nr)
22824 dict_T *dp;
22825 dictitem_T *v;
22826 char *name;
22827 varnumber_T nr;
22828{
22829 STRCPY(v->di_key, name);
22830 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22831 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22832 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022833 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 v->di_tv.vval.v_number = nr;
22835}
22836
22837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838 * ":return [expr]"
22839 */
22840 void
22841ex_return(eap)
22842 exarg_T *eap;
22843{
22844 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022845 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 int returning = FALSE;
22847
22848 if (current_funccal == NULL)
22849 {
22850 EMSG(_("E133: :return not inside a function"));
22851 return;
22852 }
22853
22854 if (eap->skip)
22855 ++emsg_skip;
22856
22857 eap->nextcmd = NULL;
22858 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022859 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 {
22861 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022862 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022864 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 }
22866 /* It's safer to return also on error. */
22867 else if (!eap->skip)
22868 {
22869 /*
22870 * Return unless the expression evaluation has been cancelled due to an
22871 * aborting error, an interrupt, or an exception.
22872 */
22873 if (!aborting())
22874 returning = do_return(eap, FALSE, TRUE, NULL);
22875 }
22876
22877 /* When skipping or the return gets pending, advance to the next command
22878 * in this line (!returning). Otherwise, ignore the rest of the line.
22879 * Following lines will be ignored by get_func_line(). */
22880 if (returning)
22881 eap->nextcmd = NULL;
22882 else if (eap->nextcmd == NULL) /* no argument */
22883 eap->nextcmd = check_nextcmd(arg);
22884
22885 if (eap->skip)
22886 --emsg_skip;
22887}
22888
22889/*
22890 * Return from a function. Possibly makes the return pending. Also called
22891 * for a pending return at the ":endtry" or after returning from an extra
22892 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022894 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 * FALSE when the return gets pending.
22896 */
22897 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022898do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 exarg_T *eap;
22900 int reanimate;
22901 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022902 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903{
22904 int idx;
22905 struct condstack *cstack = eap->cstack;
22906
22907 if (reanimate)
22908 /* Undo the return. */
22909 current_funccal->returned = FALSE;
22910
22911 /*
22912 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22913 * not in its finally clause (which then is to be executed next) is found.
22914 * In this case, make the ":return" pending for execution at the ":endtry".
22915 * Otherwise, return normally.
22916 */
22917 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22918 if (idx >= 0)
22919 {
22920 cstack->cs_pending[idx] = CSTP_RETURN;
22921
22922 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022923 /* A pending return again gets pending. "rettv" points to an
22924 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022926 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 else
22928 {
22929 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022930 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022932 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022934 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 {
22936 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022937 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022938 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 else
22940 EMSG(_(e_outofmem));
22941 }
22942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022943 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944
22945 if (reanimate)
22946 {
22947 /* The pending return value could be overwritten by a ":return"
22948 * without argument in a finally clause; reset the default
22949 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022950 current_funccal->rettv->v_type = VAR_NUMBER;
22951 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 }
22953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022954 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 }
22956 else
22957 {
22958 current_funccal->returned = TRUE;
22959
22960 /* If the return is carried out now, store the return value. For
22961 * a return immediately after reanimation, the value is already
22962 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022963 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022965 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022968 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 }
22970 }
22971
22972 return idx < 0;
22973}
22974
22975/*
22976 * Free the variable with a pending return value.
22977 */
22978 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022979discard_pending_return(rettv)
22980 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981{
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983}
22984
22985/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022986 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 * is an allocated string. Used by report_pending() for verbose messages.
22988 */
22989 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022990get_return_cmd(rettv)
22991 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022993 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022994 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022995 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022997 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022998 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022999 if (s == NULL)
23000 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023001
23002 STRCPY(IObuff, ":return ");
23003 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23004 if (STRLEN(s) + 8 >= IOSIZE)
23005 STRCPY(IObuff + IOSIZE - 4, "...");
23006 vim_free(tofree);
23007 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008}
23009
23010/*
23011 * Get next function line.
23012 * Called by do_cmdline() to get the next line.
23013 * Returns allocated string, or NULL for end of function.
23014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 char_u *
23016get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023017 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023019 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020{
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023022 ufunc_T *fp = fcp->func;
23023 char_u *retval;
23024 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025
23026 /* If breakpoints have been added/deleted need to check for it. */
23027 if (fcp->dbg_tick != debug_tick)
23028 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023029 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 sourcing_lnum);
23031 fcp->dbg_tick = debug_tick;
23032 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023033#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023034 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023035 func_line_end(cookie);
23036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037
Bram Moolenaar05159a02005-02-26 23:04:13 +000023038 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023039 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23040 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 retval = NULL;
23042 else
23043 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023044 /* Skip NULL lines (continuation lines). */
23045 while (fcp->linenr < gap->ga_len
23046 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23047 ++fcp->linenr;
23048 if (fcp->linenr >= gap->ga_len)
23049 retval = NULL;
23050 else
23051 {
23052 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23053 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023054#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023055 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023056 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023057#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 }
23060
23061 /* Did we encounter a breakpoint? */
23062 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23063 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023064 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023066 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 sourcing_lnum);
23068 fcp->dbg_tick = debug_tick;
23069 }
23070
23071 return retval;
23072}
23073
Bram Moolenaar05159a02005-02-26 23:04:13 +000023074#if defined(FEAT_PROFILE) || defined(PROTO)
23075/*
23076 * Called when starting to read a function line.
23077 * "sourcing_lnum" must be correct!
23078 * When skipping lines it may not actually be executed, but we won't find out
23079 * until later and we need to store the time now.
23080 */
23081 void
23082func_line_start(cookie)
23083 void *cookie;
23084{
23085 funccall_T *fcp = (funccall_T *)cookie;
23086 ufunc_T *fp = fcp->func;
23087
23088 if (fp->uf_profiling && sourcing_lnum >= 1
23089 && sourcing_lnum <= fp->uf_lines.ga_len)
23090 {
23091 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023092 /* Skip continuation lines. */
23093 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23094 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023095 fp->uf_tml_execed = FALSE;
23096 profile_start(&fp->uf_tml_start);
23097 profile_zero(&fp->uf_tml_children);
23098 profile_get_wait(&fp->uf_tml_wait);
23099 }
23100}
23101
23102/*
23103 * Called when actually executing a function line.
23104 */
23105 void
23106func_line_exec(cookie)
23107 void *cookie;
23108{
23109 funccall_T *fcp = (funccall_T *)cookie;
23110 ufunc_T *fp = fcp->func;
23111
23112 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23113 fp->uf_tml_execed = TRUE;
23114}
23115
23116/*
23117 * Called when done with a function line.
23118 */
23119 void
23120func_line_end(cookie)
23121 void *cookie;
23122{
23123 funccall_T *fcp = (funccall_T *)cookie;
23124 ufunc_T *fp = fcp->func;
23125
23126 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23127 {
23128 if (fp->uf_tml_execed)
23129 {
23130 ++fp->uf_tml_count[fp->uf_tml_idx];
23131 profile_end(&fp->uf_tml_start);
23132 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023133 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023134 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23135 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023136 }
23137 fp->uf_tml_idx = -1;
23138 }
23139}
23140#endif
23141
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142/*
23143 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023144 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 */
23146 int
23147func_has_ended(cookie)
23148 void *cookie;
23149{
Bram Moolenaar33570922005-01-25 22:26:29 +000023150 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151
23152 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23153 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023154 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 || fcp->returned);
23156}
23157
23158/*
23159 * return TRUE if cookie indicates a function which "abort"s on errors.
23160 */
23161 int
23162func_has_abort(cookie)
23163 void *cookie;
23164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023165 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166}
23167
23168#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23169typedef enum
23170{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023171 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23172 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23173 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174} var_flavour_T;
23175
23176static var_flavour_T var_flavour __ARGS((char_u *varname));
23177
23178 static var_flavour_T
23179var_flavour(varname)
23180 char_u *varname;
23181{
23182 char_u *p = varname;
23183
23184 if (ASCII_ISUPPER(*p))
23185 {
23186 while (*(++p))
23187 if (ASCII_ISLOWER(*p))
23188 return VAR_FLAVOUR_SESSION;
23189 return VAR_FLAVOUR_VIMINFO;
23190 }
23191 else
23192 return VAR_FLAVOUR_DEFAULT;
23193}
23194#endif
23195
23196#if defined(FEAT_VIMINFO) || defined(PROTO)
23197/*
23198 * Restore global vars that start with a capital from the viminfo file
23199 */
23200 int
23201read_viminfo_varlist(virp, writing)
23202 vir_T *virp;
23203 int writing;
23204{
23205 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023206 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208
23209 if (!writing && (find_viminfo_parameter('!') != NULL))
23210 {
23211 tab = vim_strchr(virp->vir_line + 1, '\t');
23212 if (tab != NULL)
23213 {
23214 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023215 switch (*tab)
23216 {
23217 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023218#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023219 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023220#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023221 case 'D': type = VAR_DICT; break;
23222 case 'L': type = VAR_LIST; break;
23223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224
23225 tab = vim_strchr(tab, '\t');
23226 if (tab != NULL)
23227 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023228 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023229 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023230 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023232#ifdef FEAT_FLOAT
23233 else if (type == VAR_FLOAT)
23234 (void)string2float(tab + 1, &tv.vval.v_float);
23235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023237 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023238 if (type == VAR_DICT || type == VAR_LIST)
23239 {
23240 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23241
23242 if (etv == NULL)
23243 /* Failed to parse back the dict or list, use it as a
23244 * string. */
23245 tv.v_type = VAR_STRING;
23246 else
23247 {
23248 vim_free(tv.vval.v_string);
23249 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023250 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023251 }
23252 }
23253
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023254 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023255
23256 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023257 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023258 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23259 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 }
23261 }
23262 }
23263
23264 return viminfo_readline(virp);
23265}
23266
23267/*
23268 * Write global vars that start with a capital to the viminfo file
23269 */
23270 void
23271write_viminfo_varlist(fp)
23272 FILE *fp;
23273{
Bram Moolenaar33570922005-01-25 22:26:29 +000023274 hashitem_T *hi;
23275 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023276 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023277 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023278 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023279 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023280 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281
23282 if (find_viminfo_parameter('!') == NULL)
23283 return;
23284
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023285 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023286
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023287 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023288 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023290 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023292 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023293 this_var = HI2DI(hi);
23294 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023295 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023296 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023297 {
23298 case VAR_STRING: s = "STR"; break;
23299 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023300#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023301 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023302#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023303 case VAR_DICT: s = "DIC"; break;
23304 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023305 default: continue;
23306 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023308 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023309 if (p != NULL)
23310 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023311 vim_free(tofree);
23312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 }
23314 }
23315}
23316#endif
23317
23318#if defined(FEAT_SESSION) || defined(PROTO)
23319 int
23320store_session_globals(fd)
23321 FILE *fd;
23322{
Bram Moolenaar33570922005-01-25 22:26:29 +000023323 hashitem_T *hi;
23324 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023325 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 char_u *p, *t;
23327
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 ((this_var->di_tv.v_type == VAR_NUMBER
23336 || this_var->di_tv.v_type == VAR_STRING)
23337 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023338 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023339 /* Escape special characters with a backslash. Turn a LF and
23340 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023341 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023342 (char_u *)"\\\"\n\r");
23343 if (p == NULL) /* out of memory */
23344 break;
23345 for (t = p; *t != NUL; ++t)
23346 if (*t == '\n')
23347 *t = 'n';
23348 else if (*t == '\r')
23349 *t = 'r';
23350 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023351 this_var->di_key,
23352 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23353 : ' ',
23354 p,
23355 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23356 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023357 || put_eol(fd) == FAIL)
23358 {
23359 vim_free(p);
23360 return FAIL;
23361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 vim_free(p);
23363 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023364#ifdef FEAT_FLOAT
23365 else if (this_var->di_tv.v_type == VAR_FLOAT
23366 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23367 {
23368 float_T f = this_var->di_tv.vval.v_float;
23369 int sign = ' ';
23370
23371 if (f < 0)
23372 {
23373 f = -f;
23374 sign = '-';
23375 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023376 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023377 this_var->di_key, sign, f) < 0)
23378 || put_eol(fd) == FAIL)
23379 return FAIL;
23380 }
23381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 }
23383 }
23384 return OK;
23385}
23386#endif
23387
Bram Moolenaar661b1822005-07-28 22:36:45 +000023388/*
23389 * Display script name where an item was last set.
23390 * Should only be invoked when 'verbose' is non-zero.
23391 */
23392 void
23393last_set_msg(scriptID)
23394 scid_T scriptID;
23395{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023396 char_u *p;
23397
Bram Moolenaar661b1822005-07-28 22:36:45 +000023398 if (scriptID != 0)
23399 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023400 p = home_replace_save(NULL, get_scriptname(scriptID));
23401 if (p != NULL)
23402 {
23403 verbose_enter();
23404 MSG_PUTS(_("\n\tLast set from "));
23405 MSG_PUTS(p);
23406 vim_free(p);
23407 verbose_leave();
23408 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023409 }
23410}
23411
Bram Moolenaard812df62008-11-09 12:46:09 +000023412/*
23413 * List v:oldfiles in a nice way.
23414 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023415 void
23416ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023417 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023418{
23419 list_T *l = vimvars[VV_OLDFILES].vv_list;
23420 listitem_T *li;
23421 int nr = 0;
23422
23423 if (l == NULL)
23424 msg((char_u *)_("No old files"));
23425 else
23426 {
23427 msg_start();
23428 msg_scroll = TRUE;
23429 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23430 {
23431 msg_outnum((long)++nr);
23432 MSG_PUTS(": ");
23433 msg_outtrans(get_tv_string(&li->li_tv));
23434 msg_putchar('\n');
23435 out_flush(); /* output one line at a time */
23436 ui_breakcheck();
23437 }
23438 /* Assume "got_int" was set to truncate the listing. */
23439 got_int = FALSE;
23440
23441#ifdef FEAT_BROWSE_CMD
23442 if (cmdmod.browse)
23443 {
23444 quit_more = FALSE;
23445 nr = prompt_for_number(FALSE);
23446 msg_starthere();
23447 if (nr > 0)
23448 {
23449 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23450 (long)nr);
23451
23452 if (p != NULL)
23453 {
23454 p = expand_env_save(p);
23455 eap->arg = p;
23456 eap->cmdidx = CMD_edit;
23457 cmdmod.browse = FALSE;
23458 do_exedit(eap, NULL);
23459 vim_free(p);
23460 }
23461 }
23462 }
23463#endif
23464 }
23465}
23466
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467#endif /* FEAT_EVAL */
23468
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023470#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471
23472#ifdef WIN3264
23473/*
23474 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23475 */
23476static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23477static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23478static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23479
23480/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023481 * Get the short path (8.3) for the filename in "fnamep".
23482 * Only works for a valid file name.
23483 * When the path gets longer "fnamep" is changed and the allocated buffer
23484 * is put in "bufp".
23485 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23486 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 */
23488 static int
23489get_short_pathname(fnamep, bufp, fnamelen)
23490 char_u **fnamep;
23491 char_u **bufp;
23492 int *fnamelen;
23493{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023494 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 char_u *newbuf;
23496
23497 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498 l = GetShortPathName(*fnamep, *fnamep, len);
23499 if (l > len - 1)
23500 {
23501 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023502 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 newbuf = vim_strnsave(*fnamep, l);
23504 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506
23507 vim_free(*bufp);
23508 *fnamep = *bufp = newbuf;
23509
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023510 /* Really should always succeed, as the buffer is big enough. */
23511 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 }
23513
23514 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023515 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516}
23517
23518/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023519 * Get the short path (8.3) for the filename in "fname". The converted
23520 * path is returned in "bufp".
23521 *
23522 * Some of the directories specified in "fname" may not exist. This function
23523 * will shorten the existing directories at the beginning of the path and then
23524 * append the remaining non-existing path.
23525 *
23526 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023527 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023528 * bufp - Pointer to an allocated buffer for the filename.
23529 * fnamelen - Length of the filename pointed to by fname
23530 *
23531 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 */
23533 static int
23534shortpath_for_invalid_fname(fname, bufp, fnamelen)
23535 char_u **fname;
23536 char_u **bufp;
23537 int *fnamelen;
23538{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023539 char_u *short_fname, *save_fname, *pbuf_unused;
23540 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023542 int old_len, len;
23543 int new_len, sfx_len;
23544 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545
23546 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023547 old_len = *fnamelen;
23548 save_fname = vim_strnsave(*fname, old_len);
23549 pbuf_unused = NULL;
23550 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023552 endp = save_fname + old_len - 1; /* Find the end of the copy */
23553 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023555 /*
23556 * Try shortening the supplied path till it succeeds by removing one
23557 * directory at a time from the tail of the path.
23558 */
23559 len = 0;
23560 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023562 /* go back one path-separator */
23563 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23564 --endp;
23565 if (endp <= save_fname)
23566 break; /* processed the complete path */
23567
23568 /*
23569 * Replace the path separator with a NUL and try to shorten the
23570 * resulting path.
23571 */
23572 ch = *endp;
23573 *endp = 0;
23574 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023575 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023576 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23577 {
23578 retval = FAIL;
23579 goto theend;
23580 }
23581 *endp = ch; /* preserve the string */
23582
23583 if (len > 0)
23584 break; /* successfully shortened the path */
23585
23586 /* failed to shorten the path. Skip the path separator */
23587 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588 }
23589
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023590 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023592 /*
23593 * Succeeded in shortening the path. Now concatenate the shortened
23594 * path with the remaining path at the tail.
23595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023597 /* Compute the length of the new path. */
23598 sfx_len = (int)(save_endp - endp) + 1;
23599 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023601 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023603 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023605 /* There is not enough space in the currently allocated string,
23606 * copy it to a buffer big enough. */
23607 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023609 {
23610 retval = FAIL;
23611 goto theend;
23612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 }
23614 else
23615 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023616 /* Transfer short_fname to the main buffer (it's big enough),
23617 * unless get_short_pathname() did its work in-place. */
23618 *fname = *bufp = save_fname;
23619 if (short_fname != save_fname)
23620 vim_strncpy(save_fname, short_fname, len);
23621 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023623
23624 /* concat the not-shortened part of the path */
23625 vim_strncpy(*fname + len, endp, sfx_len);
23626 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023628
23629theend:
23630 vim_free(pbuf_unused);
23631 vim_free(save_fname);
23632
23633 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634}
23635
23636/*
23637 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023638 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 */
23640 static int
23641shortpath_for_partial(fnamep, bufp, fnamelen)
23642 char_u **fnamep;
23643 char_u **bufp;
23644 int *fnamelen;
23645{
23646 int sepcount, len, tflen;
23647 char_u *p;
23648 char_u *pbuf, *tfname;
23649 int hasTilde;
23650
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023651 /* Count up the path separators from the RHS.. so we know which part
23652 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023654 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 if (vim_ispathsep(*p))
23656 ++sepcount;
23657
23658 /* Need full path first (use expand_env() to remove a "~/") */
23659 hasTilde = (**fnamep == '~');
23660 if (hasTilde)
23661 pbuf = tfname = expand_env_save(*fnamep);
23662 else
23663 pbuf = tfname = FullName_save(*fnamep, FALSE);
23664
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023665 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023666
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023667 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669
23670 if (len == 0)
23671 {
23672 /* Don't have a valid filename, so shorten the rest of the
23673 * path if we can. This CAN give us invalid 8.3 filenames, but
23674 * there's not a lot of point in guessing what it might be.
23675 */
23676 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023677 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23678 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 }
23680
23681 /* Count the paths backward to find the beginning of the desired string. */
23682 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023683 {
23684#ifdef FEAT_MBYTE
23685 if (has_mbyte)
23686 p -= mb_head_off(tfname, p);
23687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023688 if (vim_ispathsep(*p))
23689 {
23690 if (sepcount == 0 || (hasTilde && sepcount == 1))
23691 break;
23692 else
23693 sepcount --;
23694 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696 if (hasTilde)
23697 {
23698 --p;
23699 if (p >= tfname)
23700 *p = '~';
23701 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023702 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 }
23704 else
23705 ++p;
23706
23707 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23708 vim_free(*bufp);
23709 *fnamelen = (int)STRLEN(p);
23710 *bufp = pbuf;
23711 *fnamep = p;
23712
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023713 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714}
23715#endif /* WIN3264 */
23716
23717/*
23718 * Adjust a filename, according to a string of modifiers.
23719 * *fnamep must be NUL terminated when called. When returning, the length is
23720 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023721 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 * When there is an error, *fnamep is set to NULL.
23723 */
23724 int
23725modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23726 char_u *src; /* string with modifiers */
23727 int *usedlen; /* characters after src that are used */
23728 char_u **fnamep; /* file name so far */
23729 char_u **bufp; /* buffer for allocated file name or NULL */
23730 int *fnamelen; /* length of fnamep */
23731{
23732 int valid = 0;
23733 char_u *tail;
23734 char_u *s, *p, *pbuf;
23735 char_u dirname[MAXPATHL];
23736 int c;
23737 int has_fullname = 0;
23738#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023739 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740 int has_shortname = 0;
23741#endif
23742
23743repeat:
23744 /* ":p" - full path/file_name */
23745 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23746 {
23747 has_fullname = 1;
23748
23749 valid |= VALID_PATH;
23750 *usedlen += 2;
23751
23752 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23753 if ((*fnamep)[0] == '~'
23754#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23755 && ((*fnamep)[1] == '/'
23756# ifdef BACKSLASH_IN_FILENAME
23757 || (*fnamep)[1] == '\\'
23758# endif
23759 || (*fnamep)[1] == NUL)
23760
23761#endif
23762 )
23763 {
23764 *fnamep = expand_env_save(*fnamep);
23765 vim_free(*bufp); /* free any allocated file name */
23766 *bufp = *fnamep;
23767 if (*fnamep == NULL)
23768 return -1;
23769 }
23770
23771 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023772 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 {
23774 if (vim_ispathsep(*p)
23775 && p[1] == '.'
23776 && (p[2] == NUL
23777 || vim_ispathsep(p[2])
23778 || (p[2] == '.'
23779 && (p[3] == NUL || vim_ispathsep(p[3])))))
23780 break;
23781 }
23782
23783 /* FullName_save() is slow, don't use it when not needed. */
23784 if (*p != NUL || !vim_isAbsName(*fnamep))
23785 {
23786 *fnamep = FullName_save(*fnamep, *p != NUL);
23787 vim_free(*bufp); /* free any allocated file name */
23788 *bufp = *fnamep;
23789 if (*fnamep == NULL)
23790 return -1;
23791 }
23792
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023793#ifdef WIN3264
23794# if _WIN32_WINNT >= 0x0500
23795 if (vim_strchr(*fnamep, '~') != NULL)
23796 {
23797 /* Expand 8.3 filename to full path. Needed to make sure the same
23798 * file does not have two different names.
23799 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23800 p = alloc(_MAX_PATH + 1);
23801 if (p != NULL)
23802 {
23803 if (GetLongPathName(*fnamep, p, MAXPATHL))
23804 {
23805 vim_free(*bufp);
23806 *bufp = *fnamep = p;
23807 }
23808 else
23809 vim_free(p);
23810 }
23811 }
23812# endif
23813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 /* Append a path separator to a directory. */
23815 if (mch_isdir(*fnamep))
23816 {
23817 /* Make room for one or two extra characters. */
23818 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23819 vim_free(*bufp); /* free any allocated file name */
23820 *bufp = *fnamep;
23821 if (*fnamep == NULL)
23822 return -1;
23823 add_pathsep(*fnamep);
23824 }
23825 }
23826
23827 /* ":." - path relative to the current directory */
23828 /* ":~" - path relative to the home directory */
23829 /* ":8" - shortname path - postponed till after */
23830 while (src[*usedlen] == ':'
23831 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23832 {
23833 *usedlen += 2;
23834 if (c == '8')
23835 {
23836#ifdef WIN3264
23837 has_shortname = 1; /* Postpone this. */
23838#endif
23839 continue;
23840 }
23841 pbuf = NULL;
23842 /* Need full path first (use expand_env() to remove a "~/") */
23843 if (!has_fullname)
23844 {
23845 if (c == '.' && **fnamep == '~')
23846 p = pbuf = expand_env_save(*fnamep);
23847 else
23848 p = pbuf = FullName_save(*fnamep, FALSE);
23849 }
23850 else
23851 p = *fnamep;
23852
23853 has_fullname = 0;
23854
23855 if (p != NULL)
23856 {
23857 if (c == '.')
23858 {
23859 mch_dirname(dirname, MAXPATHL);
23860 s = shorten_fname(p, dirname);
23861 if (s != NULL)
23862 {
23863 *fnamep = s;
23864 if (pbuf != NULL)
23865 {
23866 vim_free(*bufp); /* free any allocated file name */
23867 *bufp = pbuf;
23868 pbuf = NULL;
23869 }
23870 }
23871 }
23872 else
23873 {
23874 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23875 /* Only replace it when it starts with '~' */
23876 if (*dirname == '~')
23877 {
23878 s = vim_strsave(dirname);
23879 if (s != NULL)
23880 {
23881 *fnamep = s;
23882 vim_free(*bufp);
23883 *bufp = s;
23884 }
23885 }
23886 }
23887 vim_free(pbuf);
23888 }
23889 }
23890
23891 tail = gettail(*fnamep);
23892 *fnamelen = (int)STRLEN(*fnamep);
23893
23894 /* ":h" - head, remove "/file_name", can be repeated */
23895 /* Don't remove the first "/" or "c:\" */
23896 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23897 {
23898 valid |= VALID_HEAD;
23899 *usedlen += 2;
23900 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023901 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023902 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 *fnamelen = (int)(tail - *fnamep);
23904#ifdef VMS
23905 if (*fnamelen > 0)
23906 *fnamelen += 1; /* the path separator is part of the path */
23907#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023908 if (*fnamelen == 0)
23909 {
23910 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23911 p = vim_strsave((char_u *)".");
23912 if (p == NULL)
23913 return -1;
23914 vim_free(*bufp);
23915 *bufp = *fnamep = tail = p;
23916 *fnamelen = 1;
23917 }
23918 else
23919 {
23920 while (tail > s && !after_pathsep(s, tail))
23921 mb_ptr_back(*fnamep, tail);
23922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 }
23924
23925 /* ":8" - shortname */
23926 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23927 {
23928 *usedlen += 2;
23929#ifdef WIN3264
23930 has_shortname = 1;
23931#endif
23932 }
23933
23934#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023935 /*
23936 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 */
23938 if (has_shortname)
23939 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023940 /* Copy the string if it is shortened by :h and when it wasn't copied
23941 * yet, because we are going to change it in place. Avoids changing
23942 * the buffer name for "%:8". */
23943 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 {
23945 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023946 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 return -1;
23948 vim_free(*bufp);
23949 *bufp = *fnamep = p;
23950 }
23951
23952 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023953 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 if (!has_fullname && !vim_isAbsName(*fnamep))
23955 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023956 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 return -1;
23958 }
23959 else
23960 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023961 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962
Bram Moolenaardc935552011-08-17 15:23:23 +020023963 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023965 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 return -1;
23967
23968 if (l == 0)
23969 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023970 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023972 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 return -1;
23974 }
23975 *fnamelen = l;
23976 }
23977 }
23978#endif /* WIN3264 */
23979
23980 /* ":t" - tail, just the basename */
23981 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23982 {
23983 *usedlen += 2;
23984 *fnamelen -= (int)(tail - *fnamep);
23985 *fnamep = tail;
23986 }
23987
23988 /* ":e" - extension, can be repeated */
23989 /* ":r" - root, without extension, can be repeated */
23990 while (src[*usedlen] == ':'
23991 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23992 {
23993 /* find a '.' in the tail:
23994 * - for second :e: before the current fname
23995 * - otherwise: The last '.'
23996 */
23997 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23998 s = *fnamep - 2;
23999 else
24000 s = *fnamep + *fnamelen - 1;
24001 for ( ; s > tail; --s)
24002 if (s[0] == '.')
24003 break;
24004 if (src[*usedlen + 1] == 'e') /* :e */
24005 {
24006 if (s > tail)
24007 {
24008 *fnamelen += (int)(*fnamep - (s + 1));
24009 *fnamep = s + 1;
24010#ifdef VMS
24011 /* cut version from the extension */
24012 s = *fnamep + *fnamelen - 1;
24013 for ( ; s > *fnamep; --s)
24014 if (s[0] == ';')
24015 break;
24016 if (s > *fnamep)
24017 *fnamelen = s - *fnamep;
24018#endif
24019 }
24020 else if (*fnamep <= tail)
24021 *fnamelen = 0;
24022 }
24023 else /* :r */
24024 {
24025 if (s > tail) /* remove one extension */
24026 *fnamelen = (int)(s - *fnamep);
24027 }
24028 *usedlen += 2;
24029 }
24030
24031 /* ":s?pat?foo?" - substitute */
24032 /* ":gs?pat?foo?" - global substitute */
24033 if (src[*usedlen] == ':'
24034 && (src[*usedlen + 1] == 's'
24035 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24036 {
24037 char_u *str;
24038 char_u *pat;
24039 char_u *sub;
24040 int sep;
24041 char_u *flags;
24042 int didit = FALSE;
24043
24044 flags = (char_u *)"";
24045 s = src + *usedlen + 2;
24046 if (src[*usedlen + 1] == 'g')
24047 {
24048 flags = (char_u *)"g";
24049 ++s;
24050 }
24051
24052 sep = *s++;
24053 if (sep)
24054 {
24055 /* find end of pattern */
24056 p = vim_strchr(s, sep);
24057 if (p != NULL)
24058 {
24059 pat = vim_strnsave(s, (int)(p - s));
24060 if (pat != NULL)
24061 {
24062 s = p + 1;
24063 /* find end of substitution */
24064 p = vim_strchr(s, sep);
24065 if (p != NULL)
24066 {
24067 sub = vim_strnsave(s, (int)(p - s));
24068 str = vim_strnsave(*fnamep, *fnamelen);
24069 if (sub != NULL && str != NULL)
24070 {
24071 *usedlen = (int)(p + 1 - src);
24072 s = do_string_sub(str, pat, sub, flags);
24073 if (s != NULL)
24074 {
24075 *fnamep = s;
24076 *fnamelen = (int)STRLEN(s);
24077 vim_free(*bufp);
24078 *bufp = s;
24079 didit = TRUE;
24080 }
24081 }
24082 vim_free(sub);
24083 vim_free(str);
24084 }
24085 vim_free(pat);
24086 }
24087 }
24088 /* after using ":s", repeat all the modifiers */
24089 if (didit)
24090 goto repeat;
24091 }
24092 }
24093
24094 return valid;
24095}
24096
24097/*
24098 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24099 * "flags" can be "g" to do a global substitute.
24100 * Returns an allocated string, NULL for error.
24101 */
24102 char_u *
24103do_string_sub(str, pat, sub, flags)
24104 char_u *str;
24105 char_u *pat;
24106 char_u *sub;
24107 char_u *flags;
24108{
24109 int sublen;
24110 regmatch_T regmatch;
24111 int i;
24112 int do_all;
24113 char_u *tail;
24114 garray_T ga;
24115 char_u *ret;
24116 char_u *save_cpo;
24117
24118 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24119 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024120 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121
24122 ga_init2(&ga, 1, 200);
24123
24124 do_all = (flags[0] == 'g');
24125
24126 regmatch.rm_ic = p_ic;
24127 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24128 if (regmatch.regprog != NULL)
24129 {
24130 tail = str;
24131 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24132 {
24133 /*
24134 * Get some space for a temporary buffer to do the substitution
24135 * into. It will contain:
24136 * - The text up to where the match is.
24137 * - The substituted text.
24138 * - The text after the match.
24139 */
24140 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24141 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24142 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24143 {
24144 ga_clear(&ga);
24145 break;
24146 }
24147
24148 /* copy the text up to where the match is */
24149 i = (int)(regmatch.startp[0] - tail);
24150 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24151 /* add the substituted text */
24152 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24153 + ga.ga_len + i, TRUE, TRUE, FALSE);
24154 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155 /* avoid getting stuck on a match with an empty string */
24156 if (tail == regmatch.endp[0])
24157 {
24158 if (*tail == NUL)
24159 break;
24160 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24161 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 }
24163 else
24164 {
24165 tail = regmatch.endp[0];
24166 if (*tail == NUL)
24167 break;
24168 }
24169 if (!do_all)
24170 break;
24171 }
24172
24173 if (ga.ga_data != NULL)
24174 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24175
24176 vim_free(regmatch.regprog);
24177 }
24178
24179 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24180 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024181 if (p_cpo == empty_option)
24182 p_cpo = save_cpo;
24183 else
24184 /* Darn, evaluating {sub} expression changed the value. */
24185 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186
24187 return ret;
24188}
24189
24190#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */