blob: 38893c795db43398b223e867e1e4fcb84e1287f6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200125/* When using exists() don't auto-load a script. */
126static int no_autoload = FALSE;
127
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
158 * Structure to hold info for a user function.
159 */
160typedef struct ufunc ufunc_T;
161
162struct ufunc
163{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000164 int uf_varargs; /* variable nr of arguments */
165 int uf_flags;
166 int uf_calls; /* nr of active calls */
167 garray_T uf_args; /* arguments */
168 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169#ifdef FEAT_PROFILE
170 int uf_profiling; /* TRUE when func is being profiled */
171 /* profiling the function as a whole */
172 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000173 proftime_T uf_tm_total; /* time spent in function + children */
174 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175 proftime_T uf_tm_children; /* time spent in children this call */
176 /* profiling the function per line */
177 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T *uf_tml_total; /* time spent in a line + children */
179 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tml_start; /* start time for current line */
181 proftime_T uf_tml_children; /* time spent in children for this line */
182 proftime_T uf_tml_wait; /* start wait time for current line */
183 int uf_tml_idx; /* index of line being timed; -1 if none */
184 int uf_tml_execed; /* line being timed was executed */
185#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000186 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000188 int uf_refcount; /* for numbered function: reference count */
189 char_u uf_name[1]; /* name of function (actually longer); can
190 start with <SNR>123_ (<SNR> is K_SPECIAL
191 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192};
193
194/* function flags */
195#define FC_ABORT 1 /* abort function on error */
196#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000197#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000200 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000202static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000204/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000205static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
206
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207/* list heads for garbage collection */
208static dict_T *first_dict = NULL; /* list of all dicts */
209static list_T *first_list = NULL; /* list of all lists */
210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000211/* From user function to hashitem and back. */
212static ufunc_T dumuf;
213#define UF2HIKEY(fp) ((fp)->uf_name)
214#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
215#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
216
217#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
218#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaar33570922005-01-25 22:26:29 +0000220#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
221#define VAR_SHORT_LEN 20 /* short variable name length */
222#define FIXVAR_CNT 12 /* number of fixed variables */
223
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000225typedef struct funccall_S funccall_T;
226
227struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 ufunc_T *func; /* function being called */
230 int linenr; /* next line to be executed */
231 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000232 struct /* fixed variables for arguments */
233 {
234 dictitem_T var; /* variable (without room for name) */
235 char_u room[VAR_SHORT_LEN]; /* room for the name */
236 } fixvar[FIXVAR_CNT];
237 dict_T l_vars; /* l: local function variables */
238 dictitem_T l_vars_var; /* variable for l: scope */
239 dict_T l_avars; /* a: argument variables */
240 dictitem_T l_avars_var; /* variable for a: scope */
241 list_T l_varlist; /* list for a:000 */
242 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
243 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 linenr_T breakpoint; /* next line with breakpoint or zero */
245 int dbg_tick; /* debug_tick when breakpoint was set */
246 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000247#ifdef FEAT_PROFILE
248 proftime_T prof_child; /* time spent in a child */
249#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000250 funccall_T *caller; /* calling function or NULL */
251};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
253/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000254 * Info used by a ":for" loop.
255 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000256typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257{
258 int fi_semicolon; /* TRUE if ending in '; var]' */
259 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260 listwatch_T fi_lw; /* keep an eye on the item used. */
261 list_T *fi_list; /* list being used */
262} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000265 * Struct used by trans_function_name()
266 */
267typedef struct
268{
Bram Moolenaar33570922005-01-25 22:26:29 +0000269 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000270 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 dictitem_T *fd_di; /* Dictionary item used */
272} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273
Bram Moolenaara7043832005-01-21 11:56:39 +0000274
275/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 * Array to hold the value of v: variables.
277 * The value is in a dictitem, so that it can also be used in the v: scope.
278 * The reason to use this table anyway is for very quick access to the
279 * variables with the VV_ defines.
280 */
281#include "version.h"
282
283/* values for vv_flags: */
284#define VV_COMPAT 1 /* compatible, also used without "v:" */
285#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000286#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000288#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000289
290static struct vimvar
291{
292 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000293 dictitem_T vv_di; /* value and name for key */
294 char vv_filler[16]; /* space for LONGEST name below!!! */
295 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
296} vimvars[VV_LEN] =
297{
298 /*
299 * The order here must match the VV_ defines in vim.h!
300 * Initializing a union does not work, leave tv.vval empty to get zero's.
301 */
302 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("count1", VAR_NUMBER), VV_RO},
304 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
305 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
306 {VV_NAME("warningmsg", VAR_STRING), 0},
307 {VV_NAME("statusmsg", VAR_STRING), 0},
308 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
310 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
312 {VV_NAME("termresponse", VAR_STRING), VV_RO},
313 {VV_NAME("fname", VAR_STRING), VV_RO},
314 {VV_NAME("lang", VAR_STRING), VV_RO},
315 {VV_NAME("lc_time", VAR_STRING), VV_RO},
316 {VV_NAME("ctype", VAR_STRING), VV_RO},
317 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
319 {VV_NAME("fname_in", VAR_STRING), VV_RO},
320 {VV_NAME("fname_out", VAR_STRING), VV_RO},
321 {VV_NAME("fname_new", VAR_STRING), VV_RO},
322 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
323 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
324 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
325 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
327 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("progname", VAR_STRING), VV_RO},
329 {VV_NAME("servername", VAR_STRING), VV_RO},
330 {VV_NAME("dying", VAR_NUMBER), VV_RO},
331 {VV_NAME("exception", VAR_STRING), VV_RO},
332 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
333 {VV_NAME("register", VAR_STRING), VV_RO},
334 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
335 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000336 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
337 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000338 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000339 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
340 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000341 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000346 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000347 {VV_NAME("swapname", VAR_STRING), VV_RO},
348 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000349 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200350 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000351 {VV_NAME("mouse_win", VAR_NUMBER), 0},
352 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
353 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000354 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000355 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000356 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200357 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000358};
359
360/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361#define vv_type vv_di.di_tv.v_type
362#define vv_nr vv_di.di_tv.vval.v_number
363#define vv_float vv_di.di_tv.vval.v_float
364#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000365#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000367
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200368static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000369#define vimvarht vimvardict.dv_hashtab
370
Bram Moolenaara40058a2005-07-11 22:42:07 +0000371static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
372static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000373static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
374static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
375static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000376static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
377static void list_glob_vars __ARGS((int *first));
378static void list_buf_vars __ARGS((int *first));
379static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000380#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000381static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_vim_vars __ARGS((int *first));
384static void list_script_vars __ARGS((int *first));
385static void list_func_vars __ARGS((int *first));
386static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
388static int check_changedtick __ARGS((char_u *arg));
389static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
390static void clear_lval __ARGS((lval_T *lp));
391static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
392static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
394static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
395static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
396static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
397static void item_lock __ARGS((typval_T *tv, int deep, int lock));
398static int tv_islocked __ARGS((typval_T *tv));
399
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
401static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000406static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
407static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000409static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000414static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100416static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
417static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
418static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000419static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000421static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
423static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000424static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000425static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100426static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000427static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000428static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200429static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
431static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
437static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000439#ifdef FEAT_FLOAT
440static int string2float __ARGS((char_u *text, float_T *value));
441#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
443static int find_internal_func __ARGS((char_u *name));
444static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
445static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200446static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000447static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000448static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000450#ifdef FEAT_FLOAT
451static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200452static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100455static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200463static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
478#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000479static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000482static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000484#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000485static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000486static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
488#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#ifdef FEAT_FLOAT
492static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200493static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
498static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200509#ifdef FEAT_FLOAT
510static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000514static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#ifdef FEAT_FLOAT
521static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000524#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000525static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000534static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000536static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000542static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000550static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000551static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000552static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000553static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200556static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000557static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000565static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000579static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100584static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000586static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000598#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200599static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000600static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
601#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200602#ifdef FEAT_LUA
603static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000609static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000610static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000611static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000613static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000617#ifdef vim_mkdir
618static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100621#ifdef FEAT_MZSCHEME
622static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100626static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000628#ifdef FEAT_FLOAT
629static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000632static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000633static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200634#ifdef FEAT_PYTHON3
635static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
637#ifdef FEAT_PYTHON
638static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000642static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
655static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100657static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000660static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000662static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000669static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000670static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000671static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000672static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200674static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000675static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100677#ifdef FEAT_CRYPT
678static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
679#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000680static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200681static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000683#ifdef FEAT_FLOAT
684static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200685static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000688static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000689static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
693static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
695#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000696static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200697static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698#ifdef HAVE_STRFTIME
699static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
701static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200707static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000714static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200715static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000717static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000718static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000719static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000720static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000721static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000723static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200724#ifdef FEAT_FLOAT
725static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
727#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#ifdef FEAT_FLOAT
732static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200735static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200736static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000737static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100740static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000747static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000750static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100751static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000753static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000754static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static int get_env_len __ARGS((char_u **arg));
756static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000758static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
759#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
760#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
761 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000762static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
766static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static typval_T *alloc_tv __ARGS((void));
768static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void init_tv __ARGS((typval_T *varp));
770static long get_tv_number __ARGS((typval_T *varp));
771static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000772static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static char_u *get_tv_string __ARGS((typval_T *varp));
774static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000775static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200777static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
779static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
780static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000781static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
782static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
784static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000785static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200786static int var_check_func_name __ARGS((char_u *name, int new_var));
787static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000788static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000789static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
791static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
792static int eval_fname_script __ARGS((char_u *p));
793static int eval_fname_sid __ARGS((char_u *p));
794static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static ufunc_T *find_func __ARGS((char_u *name));
796static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000797static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798#ifdef FEAT_PROFILE
799static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000800static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
801static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
802static int
803# ifdef __BORLANDC__
804 _RTLENTRYF
805# endif
806 prof_total_cmp __ARGS((const void *s1, const void *s2));
807static int
808# ifdef __BORLANDC__
809 _RTLENTRYF
810# endif
811 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200813static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000814static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000815static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000817static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000818static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
819static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000821static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
822static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000823static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000824static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200827
828#ifdef EBCDIC
829static int compare_func_name __ARGS((const void *s1, const void *s2));
830static void sortFunctions __ARGS(());
831#endif
832
Bram Moolenaar33570922005-01-25 22:26:29 +0000833/*
834 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000835 */
836 void
837eval_init()
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 int i;
840 struct vimvar *p;
841
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200842 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
843 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200844 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000845 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000847
848 for (i = 0; i < VV_LEN; ++i)
849 {
850 p = &vimvars[i];
851 STRCPY(p->vv_di.di_key, p->vv_name);
852 if (p->vv_flags & VV_RO)
853 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
854 else if (p->vv_flags & VV_RO_SBX)
855 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
856 else
857 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000858
859 /* add to v: scope dict, unless the value is not always available */
860 if (p->vv_type != VAR_UNKNOWN)
861 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000863 /* add to compat scope dict */
864 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000866 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200867 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200868
869#ifdef EBCDIC
870 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100871 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200872 */
873 sortFunctions();
874#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000875}
876
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000877#if defined(EXITFREE) || defined(PROTO)
878 void
879eval_clear()
880{
881 int i;
882 struct vimvar *p;
883
884 for (i = 0; i < VV_LEN; ++i)
885 {
886 p = &vimvars[i];
887 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000888 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000889 vim_free(p->vv_str);
890 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000891 }
892 else if (p->vv_di.di_tv.v_type == VAR_LIST)
893 {
894 list_unref(p->vv_list);
895 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000896 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000897 }
898 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000899 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000900 hash_clear(&compat_hashtab);
901
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100903# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200904 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100905# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000906
907 /* global variables */
908 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000909
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000910 /* autoloaded script names */
911 ga_clear_strings(&ga_loaded);
912
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200913 /* script-local variables */
914 for (i = 1; i <= ga_scripts.ga_len; ++i)
915 {
916 vars_clear(&SCRIPT_VARS(i));
917 vim_free(SCRIPT_SV(i));
918 }
919 ga_clear(&ga_scripts);
920
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000921 /* unreferenced lists and dicts */
922 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000923
924 /* functions */
925 free_all_functions();
926 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000927}
928#endif
929
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 * Return the name of the executed function.
932 */
933 char_u *
934func_name(cookie)
935 void *cookie;
936{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000937 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938}
939
940/*
941 * Return the address holding the next breakpoint line for a funccall cookie.
942 */
943 linenr_T *
944func_breakpoint(cookie)
945 void *cookie;
946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/*
951 * Return the address holding the debug tick for a funccall cookie.
952 */
953 int *
954func_dbg_tick(cookie)
955 void *cookie;
956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958}
959
960/*
961 * Return the nesting level for a funccall cookie.
962 */
963 int
964func_level(cookie)
965 void *cookie;
966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000971funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000973/* pointer to list of previously used funccal, still around because some
974 * item in it is still being used. */
975funccall_T *previous_funccal = NULL;
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977/*
978 * Return TRUE when a function was ended by a ":return" command.
979 */
980 int
981current_func_returned()
982{
983 return current_funccal->returned;
984}
985
986
987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 * Set an internal variable to a string value. Creates the variable if it does
989 * not already exist.
990 */
991 void
992set_internal_string_var(name, value)
993 char_u *name;
994 char_u *value;
995{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000996 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000997 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
999 val = vim_strsave(value);
1000 if (val != NULL)
1001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001003 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008 }
1009}
1010
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001012static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013static char_u *redir_endp = NULL;
1014static char_u *redir_varname = NULL;
1015
1016/*
1017 * Start recording command output to a variable
1018 * Returns OK if successfully completed the setup. FAIL otherwise.
1019 */
1020 int
1021var_redir_start(name, append)
1022 char_u *name;
1023 int append; /* append to an existing variable */
1024{
1025 int save_emsg;
1026 int err;
1027 typval_T tv;
1028
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001029 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001030 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 {
1032 EMSG(_(e_invarg));
1033 return FAIL;
1034 }
1035
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001036 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037 redir_varname = vim_strsave(name);
1038 if (redir_varname == NULL)
1039 return FAIL;
1040
1041 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1042 if (redir_lval == NULL)
1043 {
1044 var_redir_stop();
1045 return FAIL;
1046 }
1047
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001048 /* The output is stored in growarray "redir_ga" until redirection ends. */
1049 ga_init2(&redir_ga, (int)sizeof(char), 500);
1050
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001052 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1053 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1055 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001056 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (redir_endp != NULL && *redir_endp != NUL)
1058 /* Trailing characters are present after the variable name */
1059 EMSG(_(e_trailing));
1060 else
1061 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001062 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 var_redir_stop();
1064 return FAIL;
1065 }
1066
1067 /* check if we can write to the variable: set it to or append an empty
1068 * string */
1069 save_emsg = did_emsg;
1070 did_emsg = FALSE;
1071 tv.v_type = VAR_STRING;
1072 tv.vval.v_string = (char_u *)"";
1073 if (append)
1074 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1075 else
1076 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001077 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001079 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 if (err)
1081 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001082 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 var_redir_stop();
1084 return FAIL;
1085 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086
1087 return OK;
1088}
1089
1090/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091 * Append "value[value_len]" to the variable set by var_redir_start().
1092 * The actual appending is postponed until redirection ends, because the value
1093 * appended may in fact be the string we write to, changing it may cause freed
1094 * memory to be used:
1095 * :redir => foo
1096 * :let foo
1097 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 */
1099 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001104 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105
1106 if (redir_lval == NULL)
1107 return;
1108
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 {
1116 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 }
1119 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121}
1122
1123/*
1124 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 */
1127 void
1128var_redir_stop()
1129{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 typval_T tv;
1131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 if (redir_lval != NULL)
1133 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 /* If there was no error: assign the text to the variable. */
1135 if (redir_endp != NULL)
1136 {
1137 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1138 tv.v_type = VAR_STRING;
1139 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001140 /* Call get_lval() again, if it's inside a Dict or List it may
1141 * have changed. */
1142 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1143 FALSE, FALSE, FALSE, FNE_CHECK_START);
1144 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1145 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1146 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* free the collected output */
1150 vim_free(redir_ga.ga_data);
1151 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 vim_free(redir_lval);
1154 redir_lval = NULL;
1155 }
1156 vim_free(redir_varname);
1157 redir_varname = NULL;
1158}
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160# if defined(FEAT_MBYTE) || defined(PROTO)
1161 int
1162eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1163 char_u *enc_from;
1164 char_u *enc_to;
1165 char_u *fname_from;
1166 char_u *fname_to;
1167{
1168 int err = FALSE;
1169
1170 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1171 set_vim_var_string(VV_CC_TO, enc_to, -1);
1172 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1173 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1174 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1175 err = TRUE;
1176 set_vim_var_string(VV_CC_FROM, NULL, -1);
1177 set_vim_var_string(VV_CC_TO, NULL, -1);
1178 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1179 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1180
1181 if (err)
1182 return FAIL;
1183 return OK;
1184}
1185# endif
1186
1187# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1188 int
1189eval_printexpr(fname, args)
1190 char_u *fname;
1191 char_u *args;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_FNAME_IN, fname, -1);
1196 set_vim_var_string(VV_CMDARG, args, -1);
1197 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1198 err = TRUE;
1199 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1200 set_vim_var_string(VV_CMDARG, NULL, -1);
1201
1202 if (err)
1203 {
1204 mch_remove(fname);
1205 return FAIL;
1206 }
1207 return OK;
1208}
1209# endif
1210
1211# if defined(FEAT_DIFF) || defined(PROTO)
1212 void
1213eval_diff(origfile, newfile, outfile)
1214 char_u *origfile;
1215 char_u *newfile;
1216 char_u *outfile;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1221 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1222 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1223 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1226 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1227}
1228
1229 void
1230eval_patch(origfile, difffile, outfile)
1231 char_u *origfile;
1232 char_u *difffile;
1233 char_u *outfile;
1234{
1235 int err;
1236
1237 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1238 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1239 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1240 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1241 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1242 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1243 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1244}
1245# endif
1246
1247/*
1248 * Top level evaluation function, returning a boolean.
1249 * Sets "error" to TRUE if there was an error.
1250 * Return TRUE or FALSE.
1251 */
1252 int
1253eval_to_bool(arg, error, nextcmd, skip)
1254 char_u *arg;
1255 int *error;
1256 char_u **nextcmd;
1257 int skip; /* only parse, don't execute */
1258{
Bram Moolenaar33570922005-01-25 22:26:29 +00001259 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 int retval = FALSE;
1261
1262 if (skip)
1263 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001264 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 else
1267 {
1268 *error = FALSE;
1269 if (!skip)
1270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001271 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 }
1275 if (skip)
1276 --emsg_skip;
1277
1278 return retval;
1279}
1280
1281/*
1282 * Top level evaluation function, returning a string. If "skip" is TRUE,
1283 * only parsing to "nextcmd" is done, without reporting errors. Return
1284 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1285 */
1286 char_u *
1287eval_to_string_skip(arg, nextcmd, skip)
1288 char_u *arg;
1289 char_u **nextcmd;
1290 int skip; /* only parse, don't execute */
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 char_u *retval;
1294
1295 if (skip)
1296 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 retval = NULL;
1299 else
1300 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 retval = vim_strsave(get_tv_string(&tv));
1302 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 if (skip)
1305 --emsg_skip;
1306
1307 return retval;
1308}
1309
1310/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001311 * Skip over an expression at "*pp".
1312 * Return FAIL for an error, OK otherwise.
1313 */
1314 int
1315skip_expr(pp)
1316 char_u **pp;
1317{
Bram Moolenaar33570922005-01-25 22:26:29 +00001318 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319
1320 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001322}
1323
1324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001326 * When "convert" is TRUE convert a List into a sequence of lines and convert
1327 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * Return pointer to allocated memory, or NULL for failure.
1329 */
1330 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 char_u *arg;
1333 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001338 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001339#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 retval = NULL;
1345 else
1346 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001347 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001348 {
1349 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001350 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001351 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001352 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001353 if (tv.vval.v_list->lv_len > 0)
1354 ga_append(&ga, NL);
1355 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 ga_append(&ga, NUL);
1357 retval = (char_u *)ga.ga_data;
1358 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359#ifdef FEAT_FLOAT
1360 else if (convert && tv.v_type == VAR_FLOAT)
1361 {
1362 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1363 retval = vim_strsave(numbuf);
1364 }
1365#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 else
1367 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370
1371 return retval;
1372}
1373
1374/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001375 * Call eval_to_string() without using current local variables and using
1376 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 */
1378 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 char_u *arg;
1381 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 char_u *retval;
1385 void *save_funccalp;
1386
1387 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 if (use_sandbox)
1389 ++sandbox;
1390 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 --sandbox;
1394 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 restore_funccal(save_funccalp);
1396 return retval;
1397}
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399/*
1400 * Top level evaluation function, returning a number.
1401 * Evaluates "expr" silently.
1402 * Returns -1 for an error.
1403 */
1404 int
1405eval_to_number(expr)
1406 char_u *expr;
1407{
Bram Moolenaar33570922005-01-25 22:26:29 +00001408 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001410 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412 ++emsg_off;
1413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 retval = -1;
1416 else
1417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001418 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421 --emsg_off;
1422
1423 return retval;
1424}
1425
Bram Moolenaara40058a2005-07-11 22:42:07 +00001426/*
1427 * Prepare v: variable "idx" to be used.
1428 * Save the current typeval in "save_tv".
1429 * When not used yet add the variable to the v: hashtable.
1430 */
1431 static void
1432prepare_vimvar(idx, save_tv)
1433 int idx;
1434 typval_T *save_tv;
1435{
1436 *save_tv = vimvars[idx].vv_tv;
1437 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1438 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1439}
1440
1441/*
1442 * Restore v: variable "idx" to typeval "save_tv".
1443 * When no longer defined, remove the variable from the v: hashtable.
1444 */
1445 static void
1446restore_vimvar(idx, save_tv)
1447 int idx;
1448 typval_T *save_tv;
1449{
1450 hashitem_T *hi;
1451
Bram Moolenaara40058a2005-07-11 22:42:07 +00001452 vimvars[idx].vv_tv = *save_tv;
1453 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1454 {
1455 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1456 if (HASHITEM_EMPTY(hi))
1457 EMSG2(_(e_intern2), "restore_vimvar()");
1458 else
1459 hash_remove(&vimvarht, hi);
1460 }
1461}
1462
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001463#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001464/*
1465 * Evaluate an expression to a list with suggestions.
1466 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001467 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468 */
1469 list_T *
1470eval_spell_expr(badword, expr)
1471 char_u *badword;
1472 char_u *expr;
1473{
1474 typval_T save_val;
1475 typval_T rettv;
1476 list_T *list = NULL;
1477 char_u *p = skipwhite(expr);
1478
1479 /* Set "v:val" to the bad word. */
1480 prepare_vimvar(VV_VAL, &save_val);
1481 vimvars[VV_VAL].vv_type = VAR_STRING;
1482 vimvars[VV_VAL].vv_str = badword;
1483 if (p_verbose == 0)
1484 ++emsg_off;
1485
1486 if (eval1(&p, &rettv, TRUE) == OK)
1487 {
1488 if (rettv.v_type != VAR_LIST)
1489 clear_tv(&rettv);
1490 else
1491 list = rettv.vval.v_list;
1492 }
1493
1494 if (p_verbose == 0)
1495 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001496 restore_vimvar(VV_VAL, &save_val);
1497
1498 return list;
1499}
1500
1501/*
1502 * "list" is supposed to contain two items: a word and a number. Return the
1503 * word in "pp" and the number as the return value.
1504 * Return -1 if anything isn't right.
1505 * Used to get the good word and score from the eval_spell_expr() result.
1506 */
1507 int
1508get_spellword(list, pp)
1509 list_T *list;
1510 char_u **pp;
1511{
1512 listitem_T *li;
1513
1514 li = list->lv_first;
1515 if (li == NULL)
1516 return -1;
1517 *pp = get_tv_string(&li->li_tv);
1518
1519 li = li->li_next;
1520 if (li == NULL)
1521 return -1;
1522 return get_tv_number(&li->li_tv);
1523}
1524#endif
1525
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001526/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001527 * Top level evaluation function.
1528 * Returns an allocated typval_T with the result.
1529 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530 */
1531 typval_T *
1532eval_expr(arg, nextcmd)
1533 char_u *arg;
1534 char_u **nextcmd;
1535{
1536 typval_T *tv;
1537
1538 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540 {
1541 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 }
1544
1545 return tv;
1546}
1547
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001550 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551 * Uses argv[argc] for the function arguments. Only Number and String
1552 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001555 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001556call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 char_u *func;
1558 int argc;
1559 char_u **argv;
1560 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001561 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 long n;
1566 int len;
1567 int i;
1568 int doesrange;
1569 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001572 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 for (i = 0; i < argc; i++)
1577 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001578 /* Pass a NULL or empty argument as an empty string */
1579 if (argv[i] == NULL || *argv[i] == NUL)
1580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001581 argvars[i].v_type = VAR_STRING;
1582 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 continue;
1584 }
1585
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 if (str_arg_only)
1587 len = 0;
1588 else
1589 /* Recognize a number argument, the others must be strings. */
1590 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (len != 0 && len == (int)STRLEN(argv[i]))
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_NUMBER;
1594 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 else
1597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001598 argvars[i].v_type = VAR_STRING;
1599 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 }
1602
1603 if (safe)
1604 {
1605 save_funccalp = save_funccal();
1606 ++sandbox;
1607 }
1608
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001609 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1610 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (safe)
1614 {
1615 --sandbox;
1616 restore_funccal(save_funccalp);
1617 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 vim_free(argvars);
1619
1620 if (ret == FAIL)
1621 clear_tv(rettv);
1622
1623 return ret;
1624}
1625
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001626/*
1627 * Call vimL function "func" and return the result as a number.
1628 * Returns -1 when calling the function fails.
1629 * Uses argv[argc] for the function arguments.
1630 */
1631 long
1632call_func_retnr(func, argc, argv, safe)
1633 char_u *func;
1634 int argc;
1635 char_u **argv;
1636 int safe; /* use the sandbox */
1637{
1638 typval_T rettv;
1639 long retval;
1640
1641 /* All arguments are passed as strings, no conversion to number. */
1642 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1643 return -1;
1644
1645 retval = get_tv_number_chk(&rettv, NULL);
1646 clear_tv(&rettv);
1647 return retval;
1648}
1649
1650#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1651 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1652
Bram Moolenaar4f688582007-07-24 12:34:30 +00001653# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001655 * Call vimL function "func" and return the result as a string.
1656 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 * Uses argv[argc] for the function arguments.
1658 */
1659 void *
1660call_func_retstr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001669 /* All arguments are passed as strings, no conversion to number. */
1670 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 return NULL;
1672
1673 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 return retval;
1676}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001682 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 */
1684 void *
1685call_func_retlist(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
1692
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001693 /* All arguments are passed as strings, no conversion to number. */
1694 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 return NULL;
1696
1697 if (rettv.v_type != VAR_LIST)
1698 {
1699 clear_tv(&rettv);
1700 return NULL;
1701 }
1702
1703 return rettv.vval.v_list;
1704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#endif
1706
1707/*
1708 * Save the current function call pointer, and set it to NULL.
1709 * Used when executing autocommands and for ":source".
1710 */
1711 void *
1712save_funccal()
1713{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 current_funccal = NULL;
1717 return (void *)fc;
1718}
1719
1720 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001721restore_funccal(vfc)
1722 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724 funccall_T *fc = (funccall_T *)vfc;
1725
1726 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
1728
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729#if defined(FEAT_PROFILE) || defined(PROTO)
1730/*
1731 * Prepare profiling for entering a child or something else that is not
1732 * counted for the script/function itself.
1733 * Should always be called in pair with prof_child_exit().
1734 */
1735 void
1736prof_child_enter(tm)
1737 proftime_T *tm; /* place to store waittime */
1738{
1739 funccall_T *fc = current_funccal;
1740
1741 if (fc != NULL && fc->func->uf_profiling)
1742 profile_start(&fc->prof_child);
1743 script_prof_save(tm);
1744}
1745
1746/*
1747 * Take care of time spent in a child.
1748 * Should always be called after prof_child_enter().
1749 */
1750 void
1751prof_child_exit(tm)
1752 proftime_T *tm; /* where waittime was stored */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 {
1758 profile_end(&fc->prof_child);
1759 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1760 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1761 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1762 }
1763 script_prof_restore(tm);
1764}
1765#endif
1766
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef FEAT_FOLDING
1769/*
1770 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1771 * it in "*cp". Doesn't give error messages.
1772 */
1773 int
1774eval_foldexpr(arg, cp)
1775 char_u *arg;
1776 int *cp;
1777{
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 int retval;
1780 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001781 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1782 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001785 if (use_sandbox)
1786 ++sandbox;
1787 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = 0;
1791 else
1792 {
1793 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (tv.v_type == VAR_NUMBER)
1795 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001796 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 retval = 0;
1798 else
1799 {
1800 /* If the result is a string, check if there is a non-digit before
1801 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (!VIM_ISDIGIT(*s) && *s != '-')
1804 *cp = *s++;
1805 retval = atol((char *)s);
1806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 --sandbox;
1812 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 return retval;
1815}
1816#endif
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 * ":let" list all variable values
1820 * ":let var1 var2" list variable values
1821 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 * ":let var += expr" assignment command.
1823 * ":let var -= expr" assignment command.
1824 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827 void
1828ex_let(eap)
1829 exarg_T *eap;
1830{
1831 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 int var_count = 0;
1836 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001838 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001839 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 argend = skip_var_list(arg, &var_count, &semicolon);
1842 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001844 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1845 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001849 /*
1850 * ":let" without "=": list variables
1851 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (*arg == '[')
1853 EMSG(_(e_invarg));
1854 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_glob_vars(&first);
1861 list_buf_vars(&first);
1862 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001865#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_script_vars(&first);
1867 list_func_vars(&first);
1868 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 eap->nextcmd = check_nextcmd(arg);
1871 }
1872 else
1873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 op[0] = '=';
1875 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001876 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 {
1878 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1879 op[0] = expr[-1]; /* +=, -= or .= */
1880 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (eap->skip)
1884 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (eap->skip)
1887 {
1888 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 --emsg_skip;
1891 }
1892 else if (i != FAIL)
1893 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 }
1899}
1900
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901/*
1902 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1903 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 * When "nextchars" is not NULL it points to a string with characters that
1905 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1906 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 * Returns OK or FAIL;
1908 */
1909 static int
1910ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1911 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 int copy; /* copy values from "tv", don't move */
1914 int semicolon; /* from skip_var_list() */
1915 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917{
1918 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 listitem_T *item;
1922 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923
1924 if (*arg != '[')
1925 {
1926 /*
1927 * ":let var = expr" or ":for var in list"
1928 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 return FAIL;
1931 return OK;
1932 }
1933
1934 /*
1935 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1936 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001937 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 {
1939 EMSG(_(e_listreq));
1940 return FAIL;
1941 }
1942
1943 i = list_len(l);
1944 if (semicolon == 0 && var_count < i)
1945 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001946 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 return FAIL;
1948 }
1949 if (var_count - semicolon > i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954
1955 item = l->lv_first;
1956 while (*arg != ']')
1957 {
1958 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 item = item->li_next;
1961 if (arg == NULL)
1962 return FAIL;
1963
1964 arg = skipwhite(arg);
1965 if (*arg == ';')
1966 {
1967 /* Put the rest of the list (may be empty) in the var after ';'.
1968 * Create a new list for this. */
1969 l = list_alloc();
1970 if (l == NULL)
1971 return FAIL;
1972 while (item != NULL)
1973 {
1974 list_append_tv(l, &item->li_tv);
1975 item = item->li_next;
1976 }
1977
1978 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001979 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 ltv.vval.v_list = l;
1981 l->lv_refcount = 1;
1982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1984 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 clear_tv(&ltv);
1986 if (arg == NULL)
1987 return FAIL;
1988 break;
1989 }
1990 else if (*arg != ',' && *arg != ']')
1991 {
1992 EMSG2(_(e_intern2), "ex_let_vars()");
1993 return FAIL;
1994 }
1995 }
1996
1997 return OK;
1998}
1999
2000/*
2001 * Skip over assignable variable "var" or list of variables "[var, var]".
2002 * Used for ":let varvar = expr" and ":for varvar in expr".
2003 * For "[var, var]" increment "*var_count" for each variable.
2004 * for "[var, var; var]" set "semicolon".
2005 * Return NULL for an error.
2006 */
2007 static char_u *
2008skip_var_list(arg, var_count, semicolon)
2009 char_u *arg;
2010 int *var_count;
2011 int *semicolon;
2012{
2013 char_u *p, *s;
2014
2015 if (*arg == '[')
2016 {
2017 /* "[var, var]": find the matching ']'. */
2018 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002019 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 {
2021 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2022 s = skip_var_one(p);
2023 if (s == p)
2024 {
2025 EMSG2(_(e_invarg2), p);
2026 return NULL;
2027 }
2028 ++*var_count;
2029
2030 p = skipwhite(s);
2031 if (*p == ']')
2032 break;
2033 else if (*p == ';')
2034 {
2035 if (*semicolon == 1)
2036 {
2037 EMSG(_("Double ; in list of variables"));
2038 return NULL;
2039 }
2040 *semicolon = 1;
2041 }
2042 else if (*p != ',')
2043 {
2044 EMSG2(_(e_invarg2), p);
2045 return NULL;
2046 }
2047 }
2048 return p + 1;
2049 }
2050 else
2051 return skip_var_one(arg);
2052}
2053
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002055 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002056 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 static char_u *
2059skip_var_one(arg)
2060 char_u *arg;
2061{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 if (*arg == '@' && arg[1] != NUL)
2063 return arg + 2;
2064 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2065 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066}
2067
Bram Moolenaara7043832005-01-21 11:56:39 +00002068/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 * List variables for hashtab "ht" with prefix "prefix".
2070 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078{
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashitem_T *hi;
2080 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 int todo;
2082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2085 {
2086 if (!HASHITEM_EMPTY(hi))
2087 {
2088 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 di = HI2DI(hi);
2090 if (empty || di->di_tv.v_type != VAR_STRING
2091 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 }
2094 }
2095}
2096
2097/*
2098 * List global variables.
2099 */
2100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_glob_vars(first)
2102 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105}
2106
2107/*
2108 * List buffer variables.
2109 */
2110 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111list_buf_vars(first)
2112 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002113{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114 char_u numbuf[NUMBUFLEN];
2115
Bram Moolenaar429fa852013-04-15 12:27:36 +02002116 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118
2119 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2121 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
2124/*
2125 * List window variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_win_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002131 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135#ifdef FEAT_WINDOWS
2136/*
2137 * List tab page variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_tab_vars(first)
2141 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002142{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145}
2146#endif
2147
Bram Moolenaara7043832005-01-21 11:56:39 +00002148/*
2149 * List Vim variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_vim_vars(first)
2153 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156}
2157
2158/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002159 * List script-local variables, if there is a script.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_script_vars(first)
2163 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164{
2165 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2167 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168}
2169
2170/*
2171 * List function variables, if there is a function.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_func_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_funccal != NULL)
2178 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 * List variables in "arg".
2184 */
2185 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 exarg_T *eap;
2188 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
2191 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 char_u *name_start;
2195 char_u *arg_subsc;
2196 char_u *tofree;
2197 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198
2199 while (!ends_excmd(*arg) && !got_int)
2200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002203 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2205 {
2206 emsg_severe = TRUE;
2207 EMSG(_(e_trailing));
2208 break;
2209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 /* get_name_len() takes care of expanding curly braces */
2214 name_start = name = arg;
2215 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2216 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* This is mainly to keep test 49 working: when expanding
2219 * curly braces fails overrule the exception error message. */
2220 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 emsg_severe = TRUE;
2223 EMSG2(_(e_invarg2), arg);
2224 break;
2225 }
2226 error = TRUE;
2227 }
2228 else
2229 {
2230 if (tofree != NULL)
2231 name = tofree;
2232 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 else
2235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* handle d.key, l[idx], f(expr) */
2237 arg_subsc = arg;
2238 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 'g': list_glob_vars(first); break;
2247 case 'b': list_buf_vars(first); break;
2248 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'v': list_vim_vars(first); break;
2253 case 's': list_script_vars(first); break;
2254 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 default:
2256 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 }
2259 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 {
2261 char_u numbuf[NUMBUFLEN];
2262 char_u *tf;
2263 int c;
2264 char_u *s;
2265
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002266 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 c = *arg;
2268 *arg = NUL;
2269 list_one_var_a((char_u *)"",
2270 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 tv.v_type,
2272 s == NULL ? (char_u *)"" : s,
2273 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 *arg = c;
2275 vim_free(tf);
2276 }
2277 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281
2282 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284
2285 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
2287
2288 return arg;
2289}
2290
2291/*
2292 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2293 * Returns a pointer to the char just after the var name.
2294 * Returns NULL if there is an error.
2295 */
2296 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002299 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002301 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303{
2304 int c1;
2305 char_u *name;
2306 char_u *p;
2307 char_u *arg_end = NULL;
2308 int len;
2309 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311
2312 /*
2313 * ":let $VAR = expr": Set environment variable.
2314 */
2315 if (*arg == '$')
2316 {
2317 /* Find the end of the name. */
2318 ++arg;
2319 name = arg;
2320 len = get_env_len(&arg);
2321 if (len == 0)
2322 EMSG2(_(e_invarg2), name - 1);
2323 else
2324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 if (op != NULL && (*op == '+' || *op == '-'))
2326 EMSG2(_(e_letwrong), op);
2327 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002330 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
2332 c1 = name[len];
2333 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334 p = get_tv_string_chk(tv);
2335 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 {
2337 int mustfree = FALSE;
2338 char_u *s = vim_getenv(name, &mustfree);
2339
2340 if (s != NULL)
2341 {
2342 p = tofree = concat_str(s, p);
2343 if (mustfree)
2344 vim_free(s);
2345 }
2346 }
2347 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 if (STRICMP(name, "HOME") == 0)
2351 init_homedir();
2352 else if (didset_vim && STRICMP(name, "VIM") == 0)
2353 didset_vim = FALSE;
2354 else if (didset_vimruntime
2355 && STRICMP(name, "VIMRUNTIME") == 0)
2356 didset_vimruntime = FALSE;
2357 arg_end = arg;
2358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362 }
2363 }
2364
2365 /*
2366 * ":let &option = expr": Set option value.
2367 * ":let &l:option = expr": Set local option value.
2368 * ":let &g:option = expr": Set global option value.
2369 */
2370 else if (*arg == '&')
2371 {
2372 /* Find the end of the name. */
2373 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 if (p == NULL || (endchars != NULL
2375 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 EMSG(_(e_letunexp));
2377 else
2378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 long n;
2380 int opt_type;
2381 long numval;
2382 char_u *stringval = NULL;
2383 char_u *s;
2384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 c1 = *p;
2386 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387
2388 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 s = get_tv_string_chk(tv); /* != NULL if number or string */
2390 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 opt_type = get_option_value(arg, &numval,
2393 &stringval, opt_flags);
2394 if ((opt_type == 1 && *op == '.')
2395 || (opt_type == 0 && *op != '.'))
2396 EMSG2(_(e_letwrong), op);
2397 else
2398 {
2399 if (opt_type == 1) /* number */
2400 {
2401 if (*op == '+')
2402 n = numval + n;
2403 else
2404 n = numval - n;
2405 }
2406 else if (opt_type == 0 && stringval != NULL) /* string */
2407 {
2408 s = concat_str(stringval, s);
2409 vim_free(stringval);
2410 stringval = s;
2411 }
2412 }
2413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 if (s != NULL)
2415 {
2416 set_option_value(arg, n, s, opt_flags);
2417 arg_end = p;
2418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 }
2422 }
2423
2424 /*
2425 * ":let @r = expr": Set register contents.
2426 */
2427 else if (*arg == '@')
2428 {
2429 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 if (op != NULL && (*op == '+' || *op == '-'))
2431 EMSG2(_(e_letwrong), op);
2432 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002437 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 char_u *s;
2439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 p = get_tv_string_chk(tv);
2441 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002443 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 if (s != NULL)
2445 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002446 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 arg_end = arg + 1;
2454 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
2457 }
2458
2459 /*
2460 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002465 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480
2481 else
2482 EMSG2(_(e_invarg2), arg);
2483
2484 return arg_end;
2485}
2486
2487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2489 */
2490 static int
2491check_changedtick(arg)
2492 char_u *arg;
2493{
2494 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2495 {
2496 EMSG2(_(e_readonlyvar), arg);
2497 return TRUE;
2498 }
2499 return FALSE;
2500}
2501
2502/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * Get an lval: variable, Dict item or List item that can be assigned a value
2504 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2505 * "name.key", "name.key[expr]" etc.
2506 * Indexing only works if "name" is an existing List or Dictionary.
2507 * "name" points to the start of the name.
2508 * If "rettv" is not NULL it points to the value to be assigned.
2509 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2510 * wrong; must end in space or cmd separator.
2511 *
2512 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002513 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 */
2516 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 typval_T *rettv;
2520 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 int unlet;
2522 int skip;
2523 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 char_u *expr_start, *expr_end;
2528 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 dictitem_T *v;
2530 typval_T var1;
2531 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540
2541 if (skip)
2542 {
2543 /* When skipping just find the end of the name. */
2544 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 }
2547
2548 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (expr_start != NULL)
2551 {
2552 /* Don't expand the name when we already know there is an error. */
2553 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2554 && *p != '[' && *p != '.')
2555 {
2556 EMSG(_(e_trailing));
2557 return NULL;
2558 }
2559
2560 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2561 if (lp->ll_exp_name == NULL)
2562 {
2563 /* Report an invalid expression in braces, unless the
2564 * expression evaluation has been cancelled due to an
2565 * aborting error, an interrupt, or an exception. */
2566 if (!aborting() && !quiet)
2567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002568 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 EMSG2(_(e_invarg2), name);
2570 return NULL;
2571 }
2572 }
2573 lp->ll_name = lp->ll_exp_name;
2574 }
2575 else
2576 lp->ll_name = name;
2577
2578 /* Without [idx] or .key we are done. */
2579 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2580 return p;
2581
2582 cc = *p;
2583 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002584 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (v == NULL && !quiet)
2586 EMSG2(_(e_undefvar), lp->ll_name);
2587 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 if (v == NULL)
2589 return NULL;
2590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 /*
2592 * Loop until no more [idx] or .key is following.
2593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2598 && !(lp->ll_tv->v_type == VAR_DICT
2599 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_("E689: Can only index a List or Dictionary"));
2603 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E708: [:] must come last"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 len = -1;
2613 if (*p == '.')
2614 {
2615 key = p + 1;
2616 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2617 ;
2618 if (len == 0)
2619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!quiet)
2621 EMSG(_(e_emptykey));
2622 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 }
2624 p = key + len;
2625 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 else
2627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 if (*p == ':')
2631 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 empty1 = FALSE;
2635 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 if (get_tv_string_chk(&var1) == NULL)
2638 {
2639 /* not a number or string */
2640 clear_tv(&var1);
2641 return NULL;
2642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 }
2644
2645 /* Optionally get the second index [ :expr]. */
2646 if (*p == ':')
2647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 if (!empty1)
2653 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (rettv != NULL && (rettv->v_type != VAR_LIST
2657 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (!empty1)
2662 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = skipwhite(p + 1);
2666 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 else
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 if (!empty1)
2674 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002677 if (get_tv_string_chk(&var2) == NULL)
2678 {
2679 /* not a number or string */
2680 if (!empty1)
2681 clear_tv(&var1);
2682 clear_tv(&var2);
2683 return NULL;
2684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (!quiet)
2694 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (!empty1)
2696 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Skip to past ']'. */
2703 ++p;
2704 }
2705
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
2708 if (len == -1)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (*key == NUL)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 lp->ll_list = NULL;
2721 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002722 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002723
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002724 /* When assigning to a scope dictionary check that a function and
2725 * variable name is valid (only variable name unless it is l: or
2726 * g: dictionary). Disallow overwriting a builtin function. */
2727 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002729 int prevval;
2730 int wrong;
2731
2732 if (len != -1)
2733 {
2734 prevval = key[len];
2735 key[len] = NUL;
2736 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002737 else
2738 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002739 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2740 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002742 || !valid_varname(key);
2743 if (len != -1)
2744 key[len] = prevval;
2745 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 return NULL;
2747 }
2748
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 /* Can't add "v:" variable. */
2752 if (lp->ll_dict == &vimvardict)
2753 {
2754 EMSG2(_(e_illvar), name);
2755 return NULL;
2756 }
2757
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002758 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 if (len == -1)
2764 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 }
2767 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (len == -1)
2772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 p = NULL;
2775 break;
2776 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 /* existing variable, need to check if it can be changed */
2778 else if (var_check_ro(lp->ll_di->di_flags, name))
2779 return NULL;
2780
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 if (len == -1)
2782 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 }
2785 else
2786 {
2787 /*
2788 * Get the number and item for the only or first index of the List.
2789 */
2790 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 else
2793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002794 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 clear_tv(&var1);
2796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002797 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 lp->ll_list = lp->ll_tv->vval.v_list;
2799 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2800 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002802 if (lp->ll_n1 < 0)
2803 {
2804 lp->ll_n1 = 0;
2805 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2806 }
2807 }
2808 if (lp->ll_li == NULL)
2809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002812 if (!quiet)
2813 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816
2817 /*
2818 * May need to find the item or absolute index for the second
2819 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 * When no index given: "lp->ll_empty2" is TRUE.
2821 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002825 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002831 {
2832 if (!quiet)
2833 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002835 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 }
2838
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2840 if (lp->ll_n1 < 0)
2841 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2842 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002843 {
2844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002848 }
2849
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002852 }
2853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 return p;
2855}
2856
2857/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002858 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 */
2860 static void
2861clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863{
2864 vim_free(lp->ll_exp_name);
2865 vim_free(lp->ll_newkey);
2866}
2867
2868/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 */
2873 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002874set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880{
2881 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 listitem_T *ri;
2883 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884
2885 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 cc = *endp;
2890 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891 if (op != NULL && *op != '=')
2892 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002896 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002897 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002898 {
2899 if (tv_op(&tv, rettv, op) == OK)
2900 set_var(lp->ll_name, &tv, FALSE);
2901 clear_tv(&tv);
2902 }
2903 }
2904 else
2905 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002909 else if (tv_check_lock(lp->ll_newkey == NULL
2910 ? lp->ll_tv->v_lock
2911 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2912 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 else if (lp->ll_range)
2914 {
2915 /*
2916 * Assign the List values to the list items.
2917 */
2918 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002919 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 if (op != NULL && *op != '=')
2921 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2922 else
2923 {
2924 clear_tv(&lp->ll_li->li_tv);
2925 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2926 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 ri = ri->li_next;
2928 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2929 break;
2930 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002933 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 ri = NULL;
2936 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002937 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002938 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 lp->ll_li = lp->ll_li->li_next;
2940 ++lp->ll_n1;
2941 }
2942 if (ri != NULL)
2943 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 else if (lp->ll_empty2
2945 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 : lp->ll_n1 != lp->ll_n2)
2947 EMSG(_("E711: List value has not enough items"));
2948 }
2949 else
2950 {
2951 /*
2952 * Assign to a List or Dictionary item.
2953 */
2954 if (lp->ll_newkey != NULL)
2955 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 if (op != NULL && *op != '=')
2957 {
2958 EMSG2(_(e_letwrong), op);
2959 return;
2960 }
2961
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002963 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 if (di == NULL)
2965 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002966 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2967 {
2968 vim_free(di);
2969 return;
2970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002972 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 else if (op != NULL && *op != '=')
2974 {
2975 tv_op(lp->ll_tv, rettv, op);
2976 return;
2977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002980
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 /*
2982 * Assign the value to the variable or list item.
2983 */
2984 if (copy)
2985 copy_tv(rettv, lp->ll_tv);
2986 else
2987 {
2988 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002989 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002991 }
2992 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002993}
2994
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002995/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2997 * Returns OK or FAIL.
2998 */
2999 static int
3000tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 typval_T *tv1;
3002 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 char_u *op;
3004{
3005 long n;
3006 char_u numbuf[NUMBUFLEN];
3007 char_u *s;
3008
3009 /* Can't do anything with a Funcref or a Dict on the right. */
3010 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3011 {
3012 switch (tv1->v_type)
3013 {
3014 case VAR_DICT:
3015 case VAR_FUNC:
3016 break;
3017
3018 case VAR_LIST:
3019 if (*op != '+' || tv2->v_type != VAR_LIST)
3020 break;
3021 /* List += List */
3022 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3023 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3024 return OK;
3025
3026 case VAR_NUMBER:
3027 case VAR_STRING:
3028 if (tv2->v_type == VAR_LIST)
3029 break;
3030 if (*op == '+' || *op == '-')
3031 {
3032 /* nr += nr or nr -= nr*/
3033 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034#ifdef FEAT_FLOAT
3035 if (tv2->v_type == VAR_FLOAT)
3036 {
3037 float_T f = n;
3038
3039 if (*op == '+')
3040 f += tv2->vval.v_float;
3041 else
3042 f -= tv2->vval.v_float;
3043 clear_tv(tv1);
3044 tv1->v_type = VAR_FLOAT;
3045 tv1->vval.v_float = f;
3046 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003047 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048#endif
3049 {
3050 if (*op == '+')
3051 n += get_tv_number(tv2);
3052 else
3053 n -= get_tv_number(tv2);
3054 clear_tv(tv1);
3055 tv1->v_type = VAR_NUMBER;
3056 tv1->vval.v_number = n;
3057 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 }
3059 else
3060 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003061 if (tv2->v_type == VAR_FLOAT)
3062 break;
3063
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 /* str .= str */
3065 s = get_tv_string(tv1);
3066 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3067 clear_tv(tv1);
3068 tv1->v_type = VAR_STRING;
3069 tv1->vval.v_string = s;
3070 }
3071 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003072
3073#ifdef FEAT_FLOAT
3074 case VAR_FLOAT:
3075 {
3076 float_T f;
3077
3078 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3079 && tv2->v_type != VAR_NUMBER
3080 && tv2->v_type != VAR_STRING))
3081 break;
3082 if (tv2->v_type == VAR_FLOAT)
3083 f = tv2->vval.v_float;
3084 else
3085 f = get_tv_number(tv2);
3086 if (*op == '+')
3087 tv1->vval.v_float += f;
3088 else
3089 tv1->vval.v_float -= f;
3090 }
3091 return OK;
3092#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 }
3094 }
3095
3096 EMSG2(_(e_letwrong), op);
3097 return FAIL;
3098}
3099
3100/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003101 * Add a watcher to a list.
3102 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003103 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003105 list_T *l;
3106 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107{
3108 lw->lw_next = l->lv_watch;
3109 l->lv_watch = lw;
3110}
3111
3112/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003113 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 * No warning when it isn't found...
3115 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003116 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 list_T *l;
3119 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120{
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122
3123 lwp = &l->lv_watch;
3124 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3125 {
3126 if (lw == lwrem)
3127 {
3128 *lwp = lw->lw_next;
3129 break;
3130 }
3131 lwp = &lw->lw_next;
3132 }
3133}
3134
3135/*
3136 * Just before removing an item from a list: advance watchers to the next
3137 * item.
3138 */
3139 static void
3140list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 list_T *l;
3142 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143{
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145
3146 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3147 if (lw->lw_item == item)
3148 lw->lw_item = item->li_next;
3149}
3150
3151/*
3152 * Evaluate the expression used in a ":for var in expr" command.
3153 * "arg" points to "var".
3154 * Set "*errp" to TRUE for an error, FALSE otherwise;
3155 * Return a pointer that holds the info. Null when there is an error.
3156 */
3157 void *
3158eval_for_line(arg, errp, nextcmdp, skip)
3159 char_u *arg;
3160 int *errp;
3161 char_u **nextcmdp;
3162 int skip;
3163{
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 typval_T tv;
3167 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168
3169 *errp = TRUE; /* default: there is an error */
3170
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 if (fi == NULL)
3173 return NULL;
3174
3175 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3176 if (expr == NULL)
3177 return fi;
3178
3179 expr = skipwhite(expr);
3180 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3181 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003182 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 return fi;
3184 }
3185
3186 if (skip)
3187 ++emsg_skip;
3188 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3189 {
3190 *errp = FALSE;
3191 if (!skip)
3192 {
3193 l = tv.vval.v_list;
3194 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003195 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003197 clear_tv(&tv);
3198 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 else
3200 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003201 /* No need to increment the refcount, it's already set for the
3202 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203 fi->fi_list = l;
3204 list_add_watch(l, &fi->fi_lw);
3205 fi->fi_lw.lw_item = l->lv_first;
3206 }
3207 }
3208 }
3209 if (skip)
3210 --emsg_skip;
3211
3212 return fi;
3213}
3214
3215/*
3216 * Use the first item in a ":for" list. Advance to the next.
3217 * Assign the values to the variable (list). "arg" points to the first one.
3218 * Return TRUE when a valid item was found, FALSE when at end of list or
3219 * something wrong.
3220 */
3221 int
3222next_for_item(fi_void, arg)
3223 void *fi_void;
3224 char_u *arg;
3225{
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229
3230 item = fi->fi_lw.lw_item;
3231 if (item == NULL)
3232 result = FALSE;
3233 else
3234 {
3235 fi->fi_lw.lw_item = item->li_next;
3236 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3237 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3238 }
3239 return result;
3240}
3241
3242/*
3243 * Free the structure used to store info used by ":for".
3244 */
3245 void
3246free_for_info(fi_void)
3247 void *fi_void;
3248{
Bram Moolenaar33570922005-01-25 22:26:29 +00003249 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003251 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003252 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 list_unref(fi->fi_list);
3255 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 vim_free(fi);
3257}
3258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3260
3261 void
3262set_context_for_expression(xp, arg, cmdidx)
3263 expand_T *xp;
3264 char_u *arg;
3265 cmdidx_T cmdidx;
3266{
3267 int got_eq = FALSE;
3268 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 if (cmdidx == CMD_let)
3272 {
3273 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003274 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 {
3276 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003277 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 {
3279 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 if (vim_iswhite(*p))
3282 break;
3283 }
3284 return;
3285 }
3286 }
3287 else
3288 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3289 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 while ((xp->xp_pattern = vim_strpbrk(arg,
3291 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3292 {
3293 c = *xp->xp_pattern;
3294 if (c == '&')
3295 {
3296 c = xp->xp_pattern[1];
3297 if (c == '&')
3298 {
3299 ++xp->xp_pattern;
3300 xp->xp_context = cmdidx != CMD_let || got_eq
3301 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3302 }
3303 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003306 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3307 xp->xp_pattern += 2;
3308
3309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311 else if (c == '$')
3312 {
3313 /* environment variable */
3314 xp->xp_context = EXPAND_ENV_VARS;
3315 }
3316 else if (c == '=')
3317 {
3318 got_eq = TRUE;
3319 xp->xp_context = EXPAND_EXPRESSION;
3320 }
3321 else if (c == '<'
3322 && xp->xp_context == EXPAND_FUNCTIONS
3323 && vim_strchr(xp->xp_pattern, '(') == NULL)
3324 {
3325 /* Function name can start with "<SNR>" */
3326 break;
3327 }
3328 else if (cmdidx != CMD_let || got_eq)
3329 {
3330 if (c == '"') /* string */
3331 {
3332 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3333 if (c == '\\' && xp->xp_pattern[1] != NUL)
3334 ++xp->xp_pattern;
3335 xp->xp_context = EXPAND_NOTHING;
3336 }
3337 else if (c == '\'') /* literal string */
3338 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003339 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3341 /* skip */ ;
3342 xp->xp_context = EXPAND_NOTHING;
3343 }
3344 else if (c == '|')
3345 {
3346 if (xp->xp_pattern[1] == '|')
3347 {
3348 ++xp->xp_pattern;
3349 xp->xp_context = EXPAND_EXPRESSION;
3350 }
3351 else
3352 xp->xp_context = EXPAND_COMMANDS;
3353 }
3354 else
3355 xp->xp_context = EXPAND_EXPRESSION;
3356 }
3357 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 /* Doesn't look like something valid, expand as an expression
3359 * anyway. */
3360 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 arg = xp->xp_pattern;
3362 if (*arg != NUL)
3363 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3364 /* skip */ ;
3365 }
3366 xp->xp_pattern = arg;
3367}
3368
3369#endif /* FEAT_CMDL_COMPL */
3370
3371/*
3372 * ":1,25call func(arg1, arg2)" function call.
3373 */
3374 void
3375ex_call(eap)
3376 exarg_T *eap;
3377{
3378 char_u *arg = eap->arg;
3379 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003381 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 linenr_T lnum;
3385 int doesrange;
3386 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003387 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003389 if (eap->skip)
3390 {
3391 /* trans_function_name() doesn't work well when skipping, use eval0()
3392 * instead to skip to any following command, e.g. for:
3393 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003394 ++emsg_skip;
3395 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3396 clear_tv(&rettv);
3397 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003398 return;
3399 }
3400
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003401 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003402 if (fudi.fd_newkey != NULL)
3403 {
3404 /* Still need to give an error message for missing key. */
3405 EMSG2(_(e_dictkey), fudi.fd_newkey);
3406 vim_free(fudi.fd_newkey);
3407 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003408 if (tofree == NULL)
3409 return;
3410
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003411 /* Increase refcount on dictionary, it could get deleted when evaluating
3412 * the arguments. */
3413 if (fudi.fd_dict != NULL)
3414 ++fudi.fd_dict->dv_refcount;
3415
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003416 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003417 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003418 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaar532c7802005-01-27 14:44:31 +00003420 /* Skip white space to allow ":call func ()". Not good, but required for
3421 * backward compatibility. */
3422 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003423 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 if (*startarg != '(')
3426 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003427 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 goto end;
3429 }
3430
3431 /*
3432 * When skipping, evaluate the function once, to find the end of the
3433 * arguments.
3434 * When the function takes a range, this is discovered after the first
3435 * call, and the loop is broken.
3436 */
3437 if (eap->skip)
3438 {
3439 ++emsg_skip;
3440 lnum = eap->line2; /* do it once, also with an invalid range */
3441 }
3442 else
3443 lnum = eap->line1;
3444 for ( ; lnum <= eap->line2; ++lnum)
3445 {
3446 if (!eap->skip && eap->addr_count > 0)
3447 {
3448 curwin->w_cursor.lnum = lnum;
3449 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003450#ifdef FEAT_VIRTUALEDIT
3451 curwin->w_cursor.coladd = 0;
3452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003455 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003456 eap->line1, eap->line2, &doesrange,
3457 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 failed = TRUE;
3460 break;
3461 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003462
3463 /* Handle a function returning a Funcref, Dictionary or List. */
3464 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3465 {
3466 failed = TRUE;
3467 break;
3468 }
3469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (doesrange || eap->skip)
3472 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003475 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003477 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (aborting())
3479 break;
3480 }
3481 if (eap->skip)
3482 --emsg_skip;
3483
3484 if (!failed)
3485 {
3486 /* Check for trailing illegal characters and a following command. */
3487 if (!ends_excmd(*arg))
3488 {
3489 emsg_severe = TRUE;
3490 EMSG(_(e_trailing));
3491 }
3492 else
3493 eap->nextcmd = check_nextcmd(arg);
3494 }
3495
3496end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003497 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003498 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499}
3500
3501/*
3502 * ":unlet[!] var1 ... " command.
3503 */
3504 void
3505ex_unlet(eap)
3506 exarg_T *eap;
3507{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003508 ex_unletlock(eap, eap->arg, 0);
3509}
3510
3511/*
3512 * ":lockvar" and ":unlockvar" commands
3513 */
3514 void
3515ex_lockvar(eap)
3516 exarg_T *eap;
3517{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519 int deep = 2;
3520
3521 if (eap->forceit)
3522 deep = -1;
3523 else if (vim_isdigit(*arg))
3524 {
3525 deep = getdigits(&arg);
3526 arg = skipwhite(arg);
3527 }
3528
3529 ex_unletlock(eap, arg, deep);
3530}
3531
3532/*
3533 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3534 */
3535 static void
3536ex_unletlock(eap, argstart, deep)
3537 exarg_T *eap;
3538 char_u *argstart;
3539 int deep;
3540{
3541 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003544 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 do
3547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003548 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003549 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3550 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 if (lv.ll_name == NULL)
3552 error = TRUE; /* error but continue parsing */
3553 if (name_end == NULL || (!vim_iswhite(*name_end)
3554 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 if (name_end != NULL)
3557 {
3558 emsg_severe = TRUE;
3559 EMSG(_(e_trailing));
3560 }
3561 if (!(eap->skip || error))
3562 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 break;
3564 }
3565
3566 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003567 {
3568 if (eap->cmdidx == CMD_unlet)
3569 {
3570 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3571 error = TRUE;
3572 }
3573 else
3574 {
3575 if (do_lock_var(&lv, name_end, deep,
3576 eap->cmdidx == CMD_lockvar) == FAIL)
3577 error = TRUE;
3578 }
3579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003581 if (!eap->skip)
3582 clear_lval(&lv);
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 arg = skipwhite(name_end);
3585 } while (!ends_excmd(*arg));
3586
3587 eap->nextcmd = check_nextcmd(arg);
3588}
3589
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003592 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 int forceit;
3595{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 int ret = OK;
3597 int cc;
3598
3599 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 cc = *name_end;
3602 *name_end = NUL;
3603
3604 /* Normal name or expanded name. */
3605 if (check_changedtick(lp->ll_name))
3606 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3612 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 else if (lp->ll_range)
3614 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616
3617 /* Delete a range of List items. */
3618 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3619 {
3620 li = lp->ll_li->li_next;
3621 listitem_remove(lp->ll_list, lp->ll_li);
3622 lp->ll_li = li;
3623 ++lp->ll_n1;
3624 }
3625 }
3626 else
3627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 /* unlet a List item. */
3630 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003633 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 }
3635
3636 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003637}
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639/*
3640 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
3643 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003644do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 hashtab_T *ht;
3649 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003650 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003651 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 ht = find_var_ht(name, &varname);
3654 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003656 hi = hash_find(ht, varname);
3657 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003658 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003659 di = HI2DI(hi);
3660 if (var_check_fixed(di->di_flags, name)
3661 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 return FAIL;
3663 delete_var(ht, hi);
3664 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 if (forceit)
3668 return OK;
3669 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 return FAIL;
3671}
3672
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673/*
3674 * Lock or unlock variable indicated by "lp".
3675 * "deep" is the levels to go (-1 for unlimited);
3676 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3677 */
3678 static int
3679do_lock_var(lp, name_end, deep, lock)
3680 lval_T *lp;
3681 char_u *name_end;
3682 int deep;
3683 int lock;
3684{
3685 int ret = OK;
3686 int cc;
3687 dictitem_T *di;
3688
3689 if (deep == 0) /* nothing to do */
3690 return OK;
3691
3692 if (lp->ll_tv == NULL)
3693 {
3694 cc = *name_end;
3695 *name_end = NUL;
3696
3697 /* Normal name or expanded name. */
3698 if (check_changedtick(lp->ll_name))
3699 ret = FAIL;
3700 else
3701 {
3702 di = find_var(lp->ll_name, NULL);
3703 if (di == NULL)
3704 ret = FAIL;
3705 else
3706 {
3707 if (lock)
3708 di->di_flags |= DI_FLAGS_LOCK;
3709 else
3710 di->di_flags &= ~DI_FLAGS_LOCK;
3711 item_lock(&di->di_tv, deep, lock);
3712 }
3713 }
3714 *name_end = cc;
3715 }
3716 else if (lp->ll_range)
3717 {
3718 listitem_T *li = lp->ll_li;
3719
3720 /* (un)lock a range of List items. */
3721 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3722 {
3723 item_lock(&li->li_tv, deep, lock);
3724 li = li->li_next;
3725 ++lp->ll_n1;
3726 }
3727 }
3728 else if (lp->ll_list != NULL)
3729 /* (un)lock a List item. */
3730 item_lock(&lp->ll_li->li_tv, deep, lock);
3731 else
3732 /* un(lock) a Dictionary item. */
3733 item_lock(&lp->ll_di->di_tv, deep, lock);
3734
3735 return ret;
3736}
3737
3738/*
3739 * Lock or unlock an item. "deep" is nr of levels to go.
3740 */
3741 static void
3742item_lock(tv, deep, lock)
3743 typval_T *tv;
3744 int deep;
3745 int lock;
3746{
3747 static int recurse = 0;
3748 list_T *l;
3749 listitem_T *li;
3750 dict_T *d;
3751 hashitem_T *hi;
3752 int todo;
3753
3754 if (recurse >= DICT_MAXNEST)
3755 {
3756 EMSG(_("E743: variable nested too deep for (un)lock"));
3757 return;
3758 }
3759 if (deep == 0)
3760 return;
3761 ++recurse;
3762
3763 /* lock/unlock the item itself */
3764 if (lock)
3765 tv->v_lock |= VAR_LOCKED;
3766 else
3767 tv->v_lock &= ~VAR_LOCKED;
3768
3769 switch (tv->v_type)
3770 {
3771 case VAR_LIST:
3772 if ((l = tv->vval.v_list) != NULL)
3773 {
3774 if (lock)
3775 l->lv_lock |= VAR_LOCKED;
3776 else
3777 l->lv_lock &= ~VAR_LOCKED;
3778 if (deep < 0 || deep > 1)
3779 /* recursive: lock/unlock the items the List contains */
3780 for (li = l->lv_first; li != NULL; li = li->li_next)
3781 item_lock(&li->li_tv, deep - 1, lock);
3782 }
3783 break;
3784 case VAR_DICT:
3785 if ((d = tv->vval.v_dict) != NULL)
3786 {
3787 if (lock)
3788 d->dv_lock |= VAR_LOCKED;
3789 else
3790 d->dv_lock &= ~VAR_LOCKED;
3791 if (deep < 0 || deep > 1)
3792 {
3793 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003794 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003795 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3796 {
3797 if (!HASHITEM_EMPTY(hi))
3798 {
3799 --todo;
3800 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3801 }
3802 }
3803 }
3804 }
3805 }
3806 --recurse;
3807}
3808
Bram Moolenaara40058a2005-07-11 22:42:07 +00003809/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003810 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3811 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003812 */
3813 static int
3814tv_islocked(tv)
3815 typval_T *tv;
3816{
3817 return (tv->v_lock & VAR_LOCKED)
3818 || (tv->v_type == VAR_LIST
3819 && tv->vval.v_list != NULL
3820 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3821 || (tv->v_type == VAR_DICT
3822 && tv->vval.v_dict != NULL
3823 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3824}
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3827/*
3828 * Delete all "menutrans_" variables.
3829 */
3830 void
3831del_menutrans_vars()
3832{
Bram Moolenaar33570922005-01-25 22:26:29 +00003833 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003834 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
Bram Moolenaar33570922005-01-25 22:26:29 +00003836 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003837 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003839 {
3840 if (!HASHITEM_EMPTY(hi))
3841 {
3842 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3844 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003845 }
3846 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848}
3849#endif
3850
3851#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3852
3853/*
3854 * Local string buffer for the next two functions to store a variable name
3855 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3856 * get_user_var_name().
3857 */
3858
3859static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3860
3861static char_u *varnamebuf = NULL;
3862static int varnamebuflen = 0;
3863
3864/*
3865 * Function to concatenate a prefix and a variable name.
3866 */
3867 static char_u *
3868cat_prefix_varname(prefix, name)
3869 int prefix;
3870 char_u *name;
3871{
3872 int len;
3873
3874 len = (int)STRLEN(name) + 3;
3875 if (len > varnamebuflen)
3876 {
3877 vim_free(varnamebuf);
3878 len += 10; /* some additional space */
3879 varnamebuf = alloc(len);
3880 if (varnamebuf == NULL)
3881 {
3882 varnamebuflen = 0;
3883 return NULL;
3884 }
3885 varnamebuflen = len;
3886 }
3887 *varnamebuf = prefix;
3888 varnamebuf[1] = ':';
3889 STRCPY(varnamebuf + 2, name);
3890 return varnamebuf;
3891}
3892
3893/*
3894 * Function given to ExpandGeneric() to obtain the list of user defined
3895 * (global/buffer/window/built-in) variable names.
3896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 char_u *
3898get_user_var_name(xp, idx)
3899 expand_T *xp;
3900 int idx;
3901{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003902 static long_u gdone;
3903 static long_u bdone;
3904 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003905#ifdef FEAT_WINDOWS
3906 static long_u tdone;
3907#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003908 static int vidx;
3909 static hashitem_T *hi;
3910 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003913 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003914 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003915#ifdef FEAT_WINDOWS
3916 tdone = 0;
3917#endif
3918 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003919
3920 /* Global variables */
3921 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003924 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 else
3926 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003927 while (HASHITEM_EMPTY(hi))
3928 ++hi;
3929 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3930 return cat_prefix_varname('g', hi->hi_key);
3931 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003933
3934 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003935 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003940 else
3941 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 while (HASHITEM_EMPTY(hi))
3943 ++hi;
3944 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 return (char_u *)"b:changedtick";
3950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951
3952 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003953 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003956 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003958 else
3959 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 while (HASHITEM_EMPTY(hi))
3961 ++hi;
3962 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003964
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003965#ifdef FEAT_WINDOWS
3966 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003967 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003968 if (tdone < ht->ht_used)
3969 {
3970 if (tdone++ == 0)
3971 hi = ht->ht_array;
3972 else
3973 ++hi;
3974 while (HASHITEM_EMPTY(hi))
3975 ++hi;
3976 return cat_prefix_varname('t', hi->hi_key);
3977 }
3978#endif
3979
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 /* v: variables */
3981 if (vidx < VV_LEN)
3982 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 vim_free(varnamebuf);
3985 varnamebuf = NULL;
3986 varnamebuflen = 0;
3987 return NULL;
3988}
3989
3990#endif /* FEAT_CMDL_COMPL */
3991
3992/*
3993 * types for expressions.
3994 */
3995typedef enum
3996{
3997 TYPE_UNKNOWN = 0
3998 , TYPE_EQUAL /* == */
3999 , TYPE_NEQUAL /* != */
4000 , TYPE_GREATER /* > */
4001 , TYPE_GEQUAL /* >= */
4002 , TYPE_SMALLER /* < */
4003 , TYPE_SEQUAL /* <= */
4004 , TYPE_MATCH /* =~ */
4005 , TYPE_NOMATCH /* !~ */
4006} exptype_T;
4007
4008/*
4009 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4012 */
4013
4014/*
4015 * Handle zero level expression.
4016 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004017 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004018 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * Return OK or FAIL.
4020 */
4021 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 char_u **nextcmd;
4026 int evaluate;
4027{
4028 int ret;
4029 char_u *p;
4030
4031 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (ret == FAIL || !ends_excmd(*p))
4034 {
4035 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 /*
4038 * Report the invalid expression unless the expression evaluation has
4039 * been cancelled due to an aborting error, an interrupt, or an
4040 * exception.
4041 */
4042 if (!aborting())
4043 EMSG2(_(e_invexpr2), arg);
4044 ret = FAIL;
4045 }
4046 if (nextcmd != NULL)
4047 *nextcmd = check_nextcmd(p);
4048
4049 return ret;
4050}
4051
4052/*
4053 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004054 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 *
4056 * "arg" must point to the first non-white of the expression.
4057 * "arg" is advanced to the next non-white after the recognized expression.
4058 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004059 * Note: "rettv.v_lock" is not set.
4060 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 * Return OK or FAIL.
4062 */
4063 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 int evaluate;
4068{
4069 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 /*
4073 * Get the first variable.
4074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 return FAIL;
4077
4078 if ((*arg)[0] == '?')
4079 {
4080 result = FALSE;
4081 if (evaluate)
4082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083 int error = FALSE;
4084
4085 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 if (error)
4089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091
4092 /*
4093 * Get the second variable.
4094 */
4095 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 return FAIL;
4098
4099 /*
4100 * Check for the ":".
4101 */
4102 if ((*arg)[0] != ':')
4103 {
4104 EMSG(_("E109: Missing ':' after '?'"));
4105 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 return FAIL;
4108 }
4109
4110 /*
4111 * Get the third variable.
4112 */
4113 *arg = skipwhite(*arg + 1);
4114 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4115 {
4116 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 return FAIL;
4119 }
4120 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123
4124 return OK;
4125}
4126
4127/*
4128 * Handle first level expression:
4129 * expr2 || expr2 || expr2 logical OR
4130 *
4131 * "arg" must point to the first non-white of the expression.
4132 * "arg" is advanced to the next non-white after the recognized expression.
4133 *
4134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int evaluate;
4141{
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 long result;
4144 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004145 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 /*
4148 * Get the first variable.
4149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 return FAIL;
4152
4153 /*
4154 * Repeat until there is no following "||".
4155 */
4156 first = TRUE;
4157 result = FALSE;
4158 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4159 {
4160 if (evaluate && first)
4161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (error)
4166 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 first = FALSE;
4168 }
4169
4170 /*
4171 * Get the second variable.
4172 */
4173 *arg = skipwhite(*arg + 2);
4174 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4175 return FAIL;
4176
4177 /*
4178 * Compute the result.
4179 */
4180 if (evaluate && !result)
4181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (error)
4186 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 if (evaluate)
4189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 rettv->v_type = VAR_NUMBER;
4191 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 }
4194
4195 return OK;
4196}
4197
4198/*
4199 * Handle second level expression:
4200 * expr3 && expr3 && expr3 logical AND
4201 *
4202 * "arg" must point to the first non-white of the expression.
4203 * "arg" is advanced to the next non-white after the recognized expression.
4204 *
4205 * Return OK or FAIL.
4206 */
4207 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 int evaluate;
4212{
Bram Moolenaar33570922005-01-25 22:26:29 +00004213 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 long result;
4215 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218 /*
4219 * Get the first variable.
4220 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return FAIL;
4223
4224 /*
4225 * Repeat until there is no following "&&".
4226 */
4227 first = TRUE;
4228 result = TRUE;
4229 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4230 {
4231 if (evaluate && first)
4232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 if (error)
4237 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 first = FALSE;
4239 }
4240
4241 /*
4242 * Get the second variable.
4243 */
4244 *arg = skipwhite(*arg + 2);
4245 if (eval4(arg, &var2, evaluate && result) == FAIL)
4246 return FAIL;
4247
4248 /*
4249 * Compute the result.
4250 */
4251 if (evaluate && result)
4252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (error)
4257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 if (evaluate)
4260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 rettv->v_type = VAR_NUMBER;
4262 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 }
4265
4266 return OK;
4267}
4268
4269/*
4270 * Handle third level expression:
4271 * var1 == var2
4272 * var1 =~ var2
4273 * var1 != var2
4274 * var1 !~ var2
4275 * var1 > var2
4276 * var1 >= var2
4277 * var1 < var2
4278 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004279 * var1 is var2
4280 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 *
4282 * "arg" must point to the first non-white of the expression.
4283 * "arg" is advanced to the next non-white after the recognized expression.
4284 *
4285 * Return OK or FAIL.
4286 */
4287 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 int evaluate;
4292{
Bram Moolenaar33570922005-01-25 22:26:29 +00004293 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 char_u *p;
4295 int i;
4296 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004297 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int len = 2;
4299 long n1, n2;
4300 char_u *s1, *s2;
4301 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4302 regmatch_T regmatch;
4303 int ic;
4304 char_u *save_cpo;
4305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 p = *arg;
4313 switch (p[0])
4314 {
4315 case '=': if (p[1] == '=')
4316 type = TYPE_EQUAL;
4317 else if (p[1] == '~')
4318 type = TYPE_MATCH;
4319 break;
4320 case '!': if (p[1] == '=')
4321 type = TYPE_NEQUAL;
4322 else if (p[1] == '~')
4323 type = TYPE_NOMATCH;
4324 break;
4325 case '>': if (p[1] != '=')
4326 {
4327 type = TYPE_GREATER;
4328 len = 1;
4329 }
4330 else
4331 type = TYPE_GEQUAL;
4332 break;
4333 case '<': if (p[1] != '=')
4334 {
4335 type = TYPE_SMALLER;
4336 len = 1;
4337 }
4338 else
4339 type = TYPE_SEQUAL;
4340 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004341 case 'i': if (p[1] == 's')
4342 {
4343 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4344 len = 5;
4345 if (!vim_isIDc(p[len]))
4346 {
4347 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4348 type_is = TRUE;
4349 }
4350 }
4351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353
4354 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004355 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 */
4357 if (type != TYPE_UNKNOWN)
4358 {
4359 /* extra question mark appended: ignore case */
4360 if (p[len] == '?')
4361 {
4362 ic = TRUE;
4363 ++len;
4364 }
4365 /* extra '#' appended: match case */
4366 else if (p[len] == '#')
4367 {
4368 ic = FALSE;
4369 ++len;
4370 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 else
4373 ic = p_ic;
4374
4375 /*
4376 * Get the second variable.
4377 */
4378 *arg = skipwhite(p + len);
4379 if (eval5(arg, &var2, evaluate) == FAIL)
4380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 return FAIL;
4383 }
4384
4385 if (evaluate)
4386 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 if (type_is && rettv->v_type != var2.v_type)
4388 {
4389 /* For "is" a different type always means FALSE, for "notis"
4390 * it means TRUE. */
4391 n1 = (type == TYPE_NEQUAL);
4392 }
4393 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4394 {
4395 if (type_is)
4396 {
4397 n1 = (rettv->v_type == var2.v_type
4398 && rettv->vval.v_list == var2.vval.v_list);
4399 if (type == TYPE_NEQUAL)
4400 n1 = !n1;
4401 }
4402 else if (rettv->v_type != var2.v_type
4403 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4404 {
4405 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004406 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004407 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004408 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409 clear_tv(rettv);
4410 clear_tv(&var2);
4411 return FAIL;
4412 }
4413 else
4414 {
4415 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004416 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4417 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004418 if (type == TYPE_NEQUAL)
4419 n1 = !n1;
4420 }
4421 }
4422
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004423 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4424 {
4425 if (type_is)
4426 {
4427 n1 = (rettv->v_type == var2.v_type
4428 && rettv->vval.v_dict == var2.vval.v_dict);
4429 if (type == TYPE_NEQUAL)
4430 n1 = !n1;
4431 }
4432 else if (rettv->v_type != var2.v_type
4433 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4434 {
4435 if (rettv->v_type != var2.v_type)
4436 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4437 else
4438 EMSG(_("E736: Invalid operation for Dictionary"));
4439 clear_tv(rettv);
4440 clear_tv(&var2);
4441 return FAIL;
4442 }
4443 else
4444 {
4445 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004446 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4447 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004448 if (type == TYPE_NEQUAL)
4449 n1 = !n1;
4450 }
4451 }
4452
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004453 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4454 {
4455 if (rettv->v_type != var2.v_type
4456 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4457 {
4458 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004459 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004460 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004461 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 clear_tv(rettv);
4463 clear_tv(&var2);
4464 return FAIL;
4465 }
4466 else
4467 {
4468 /* Compare two Funcrefs for being equal or unequal. */
4469 if (rettv->vval.v_string == NULL
4470 || var2.vval.v_string == NULL)
4471 n1 = FALSE;
4472 else
4473 n1 = STRCMP(rettv->vval.v_string,
4474 var2.vval.v_string) == 0;
4475 if (type == TYPE_NEQUAL)
4476 n1 = !n1;
4477 }
4478 }
4479
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004480#ifdef FEAT_FLOAT
4481 /*
4482 * If one of the two variables is a float, compare as a float.
4483 * When using "=~" or "!~", always compare as string.
4484 */
4485 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4486 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4487 {
4488 float_T f1, f2;
4489
4490 if (rettv->v_type == VAR_FLOAT)
4491 f1 = rettv->vval.v_float;
4492 else
4493 f1 = get_tv_number(rettv);
4494 if (var2.v_type == VAR_FLOAT)
4495 f2 = var2.vval.v_float;
4496 else
4497 f2 = get_tv_number(&var2);
4498 n1 = FALSE;
4499 switch (type)
4500 {
4501 case TYPE_EQUAL: n1 = (f1 == f2); break;
4502 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4503 case TYPE_GREATER: n1 = (f1 > f2); break;
4504 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4505 case TYPE_SMALLER: n1 = (f1 < f2); break;
4506 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4507 case TYPE_UNKNOWN:
4508 case TYPE_MATCH:
4509 case TYPE_NOMATCH: break; /* avoid gcc warning */
4510 }
4511 }
4512#endif
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /*
4515 * If one of the two variables is a number, compare as a number.
4516 * When using "=~" or "!~", always compare as string.
4517 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 n1 = get_tv_number(rettv);
4522 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 switch (type)
4524 {
4525 case TYPE_EQUAL: n1 = (n1 == n2); break;
4526 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4527 case TYPE_GREATER: n1 = (n1 > n2); break;
4528 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4529 case TYPE_SMALLER: n1 = (n1 < n2); break;
4530 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4531 case TYPE_UNKNOWN:
4532 case TYPE_MATCH:
4533 case TYPE_NOMATCH: break; /* avoid gcc warning */
4534 }
4535 }
4536 else
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 s1 = get_tv_string_buf(rettv, buf1);
4539 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4541 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4542 else
4543 i = 0;
4544 n1 = FALSE;
4545 switch (type)
4546 {
4547 case TYPE_EQUAL: n1 = (i == 0); break;
4548 case TYPE_NEQUAL: n1 = (i != 0); break;
4549 case TYPE_GREATER: n1 = (i > 0); break;
4550 case TYPE_GEQUAL: n1 = (i >= 0); break;
4551 case TYPE_SMALLER: n1 = (i < 0); break;
4552 case TYPE_SEQUAL: n1 = (i <= 0); break;
4553
4554 case TYPE_MATCH:
4555 case TYPE_NOMATCH:
4556 /* avoid 'l' flag in 'cpoptions' */
4557 save_cpo = p_cpo;
4558 p_cpo = (char_u *)"";
4559 regmatch.regprog = vim_regcomp(s2,
4560 RE_MAGIC + RE_STRING);
4561 regmatch.rm_ic = ic;
4562 if (regmatch.regprog != NULL)
4563 {
4564 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004565 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (type == TYPE_NOMATCH)
4567 n1 = !n1;
4568 }
4569 p_cpo = save_cpo;
4570 break;
4571
4572 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4573 }
4574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 rettv->v_type = VAR_NUMBER;
4578 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 }
4581
4582 return OK;
4583}
4584
4585/*
4586 * Handle fourth level expression:
4587 * + number addition
4588 * - number subtraction
4589 * . string concatenation
4590 *
4591 * "arg" must point to the first non-white of the expression.
4592 * "arg" is advanced to the next non-white after the recognized expression.
4593 *
4594 * Return OK or FAIL.
4595 */
4596 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 int evaluate;
4601{
Bram Moolenaar33570922005-01-25 22:26:29 +00004602 typval_T var2;
4603 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 int op;
4605 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004606#ifdef FEAT_FLOAT
4607 float_T f1 = 0, f2 = 0;
4608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 char_u *s1, *s2;
4610 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4611 char_u *p;
4612
4613 /*
4614 * Get the first variable.
4615 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004616 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 return FAIL;
4618
4619 /*
4620 * Repeat computing, until no '+', '-' or '.' is following.
4621 */
4622 for (;;)
4623 {
4624 op = **arg;
4625 if (op != '+' && op != '-' && op != '.')
4626 break;
4627
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004628 if ((op != '+' || rettv->v_type != VAR_LIST)
4629#ifdef FEAT_FLOAT
4630 && (op == '.' || rettv->v_type != VAR_FLOAT)
4631#endif
4632 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004633 {
4634 /* For "list + ...", an illegal use of the first operand as
4635 * a number cannot be determined before evaluating the 2nd
4636 * operand: if this is also a list, all is ok.
4637 * For "something . ...", "something - ..." or "non-list + ...",
4638 * we know that the first operand needs to be a string or number
4639 * without evaluating the 2nd operand. So check before to avoid
4640 * side effects after an error. */
4641 if (evaluate && get_tv_string_chk(rettv) == NULL)
4642 {
4643 clear_tv(rettv);
4644 return FAIL;
4645 }
4646 }
4647
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 /*
4649 * Get the second variable.
4650 */
4651 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004652 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 return FAIL;
4656 }
4657
4658 if (evaluate)
4659 {
4660 /*
4661 * Compute the result.
4662 */
4663 if (op == '.')
4664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004665 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4666 s2 = get_tv_string_buf_chk(&var2, buf2);
4667 if (s2 == NULL) /* type error ? */
4668 {
4669 clear_tv(rettv);
4670 clear_tv(&var2);
4671 return FAIL;
4672 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004673 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
4675 rettv->v_type = VAR_STRING;
4676 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004678 else if (op == '+' && rettv->v_type == VAR_LIST
4679 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004680 {
4681 /* concatenate Lists */
4682 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4683 &var3) == FAIL)
4684 {
4685 clear_tv(rettv);
4686 clear_tv(&var2);
4687 return FAIL;
4688 }
4689 clear_tv(rettv);
4690 *rettv = var3;
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 else
4693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004694 int error = FALSE;
4695
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004699 f1 = rettv->vval.v_float;
4700 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702 else
4703#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705 n1 = get_tv_number_chk(rettv, &error);
4706 if (error)
4707 {
4708 /* This can only happen for "list + non-list". For
4709 * "non-list + ..." or "something - ...", we returned
4710 * before evaluating the 2nd operand. */
4711 clear_tv(rettv);
4712 return FAIL;
4713 }
4714#ifdef FEAT_FLOAT
4715 if (var2.v_type == VAR_FLOAT)
4716 f1 = n1;
4717#endif
4718 }
4719#ifdef FEAT_FLOAT
4720 if (var2.v_type == VAR_FLOAT)
4721 {
4722 f2 = var2.vval.v_float;
4723 n2 = 0;
4724 }
4725 else
4726#endif
4727 {
4728 n2 = get_tv_number_chk(&var2, &error);
4729 if (error)
4730 {
4731 clear_tv(rettv);
4732 clear_tv(&var2);
4733 return FAIL;
4734 }
4735#ifdef FEAT_FLOAT
4736 if (rettv->v_type == VAR_FLOAT)
4737 f2 = n2;
4738#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004740 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004741
4742#ifdef FEAT_FLOAT
4743 /* If there is a float on either side the result is a float. */
4744 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4745 {
4746 if (op == '+')
4747 f1 = f1 + f2;
4748 else
4749 f1 = f1 - f2;
4750 rettv->v_type = VAR_FLOAT;
4751 rettv->vval.v_float = f1;
4752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004754#endif
4755 {
4756 if (op == '+')
4757 n1 = n1 + n2;
4758 else
4759 n1 = n1 - n2;
4760 rettv->v_type = VAR_NUMBER;
4761 rettv->vval.v_number = n1;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 }
4767 return OK;
4768}
4769
4770/*
4771 * Handle fifth level expression:
4772 * * number multiplication
4773 * / number division
4774 * % number modulo
4775 *
4776 * "arg" must point to the first non-white of the expression.
4777 * "arg" is advanced to the next non-white after the recognized expression.
4778 *
4779 * Return OK or FAIL.
4780 */
4781 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004782eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004786 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
Bram Moolenaar33570922005-01-25 22:26:29 +00004788 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 int op;
4790 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791#ifdef FEAT_FLOAT
4792 int use_float = FALSE;
4793 float_T f1 = 0, f2;
4794#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797 /*
4798 * Get the first variable.
4799 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004800 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 return FAIL;
4802
4803 /*
4804 * Repeat computing, until no '*', '/' or '%' is following.
4805 */
4806 for (;;)
4807 {
4808 op = **arg;
4809 if (op != '*' && op != '/' && op != '%')
4810 break;
4811
4812 if (evaluate)
4813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814#ifdef FEAT_FLOAT
4815 if (rettv->v_type == VAR_FLOAT)
4816 {
4817 f1 = rettv->vval.v_float;
4818 use_float = TRUE;
4819 n1 = 0;
4820 }
4821 else
4822#endif
4823 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 if (error)
4826 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828 else
4829 n1 = 0;
4830
4831 /*
4832 * Get the second variable.
4833 */
4834 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004835 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 return FAIL;
4837
4838 if (evaluate)
4839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840#ifdef FEAT_FLOAT
4841 if (var2.v_type == VAR_FLOAT)
4842 {
4843 if (!use_float)
4844 {
4845 f1 = n1;
4846 use_float = TRUE;
4847 }
4848 f2 = var2.vval.v_float;
4849 n2 = 0;
4850 }
4851 else
4852#endif
4853 {
4854 n2 = get_tv_number_chk(&var2, &error);
4855 clear_tv(&var2);
4856 if (error)
4857 return FAIL;
4858#ifdef FEAT_FLOAT
4859 if (use_float)
4860 f2 = n2;
4861#endif
4862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
4864 /*
4865 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868#ifdef FEAT_FLOAT
4869 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871 if (op == '*')
4872 f1 = f1 * f2;
4873 else if (op == '/')
4874 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004875# ifdef VMS
4876 /* VMS crashes on divide by zero, work around it */
4877 if (f2 == 0.0)
4878 {
4879 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004880 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004881 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004882 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004884 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004885 }
4886 else
4887 f1 = f1 / f2;
4888# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889 /* We rely on the floating point library to handle divide
4890 * by zero to result in "inf" and not a crash. */
4891 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004892# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004896 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 return FAIL;
4898 }
4899 rettv->v_type = VAR_FLOAT;
4900 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 if (op == '*')
4906 n1 = n1 * n2;
4907 else if (op == '/')
4908 {
4909 if (n2 == 0) /* give an error message? */
4910 {
4911 if (n1 == 0)
4912 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4913 else if (n1 < 0)
4914 n1 = -0x7fffffffL;
4915 else
4916 n1 = 0x7fffffffL;
4917 }
4918 else
4919 n1 = n1 / n2;
4920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 {
4923 if (n2 == 0) /* give an error message? */
4924 n1 = 0;
4925 else
4926 n1 = n1 % n2;
4927 }
4928 rettv->v_type = VAR_NUMBER;
4929 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 }
4933
4934 return OK;
4935}
4936
4937/*
4938 * Handle sixth level expression:
4939 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004940 * "string" string constant
4941 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 * &option-name option value
4943 * @r register contents
4944 * identifier variable value
4945 * function() function call
4946 * $VAR environment variable
4947 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004948 * [expr, expr] List
4949 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 *
4951 * Also handle:
4952 * ! in front logical NOT
4953 * - in front unary minus
4954 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 * trailing [] subscript in String or List
4956 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 *
4958 * "arg" must point to the first non-white of the expression.
4959 * "arg" is advanced to the next non-white after the recognized expression.
4960 *
4961 * Return OK or FAIL.
4962 */
4963 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004964eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004968 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 long n;
4971 int len;
4972 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 char_u *start_leader, *end_leader;
4974 int ret = OK;
4975 char_u *alias;
4976
4977 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004979 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004981 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 /*
4984 * Skip '!' and '-' characters. They are handled later.
4985 */
4986 start_leader = *arg;
4987 while (**arg == '!' || **arg == '-' || **arg == '+')
4988 *arg = skipwhite(*arg + 1);
4989 end_leader = *arg;
4990
4991 switch (**arg)
4992 {
4993 /*
4994 * Number constant.
4995 */
4996 case '0':
4997 case '1':
4998 case '2':
4999 case '3':
5000 case '4':
5001 case '5':
5002 case '6':
5003 case '7':
5004 case '8':
5005 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 {
5007#ifdef FEAT_FLOAT
5008 char_u *p = skipdigits(*arg + 1);
5009 int get_float = FALSE;
5010
5011 /* We accept a float when the format matches
5012 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005013 * strict to avoid backwards compatibility problems.
5014 * Don't look for a float after the "." operator, so that
5015 * ":let vers = 1.2.3" doesn't fail. */
5016 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 get_float = TRUE;
5019 p = skipdigits(p + 2);
5020 if (*p == 'e' || *p == 'E')
5021 {
5022 ++p;
5023 if (*p == '-' || *p == '+')
5024 ++p;
5025 if (!vim_isdigit(*p))
5026 get_float = FALSE;
5027 else
5028 p = skipdigits(p + 1);
5029 }
5030 if (ASCII_ISALPHA(*p) || *p == '.')
5031 get_float = FALSE;
5032 }
5033 if (get_float)
5034 {
5035 float_T f;
5036
5037 *arg += string2float(*arg, &f);
5038 if (evaluate)
5039 {
5040 rettv->v_type = VAR_FLOAT;
5041 rettv->vval.v_float = f;
5042 }
5043 }
5044 else
5045#endif
5046 {
5047 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5048 *arg += len;
5049 if (evaluate)
5050 {
5051 rettv->v_type = VAR_NUMBER;
5052 rettv->vval.v_number = n;
5053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
5058 /*
5059 * String constant: "string".
5060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 break;
5063
5064 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068 break;
5069
5070 /*
5071 * List: [expr, expr]
5072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 break;
5075
5076 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 * Dictionary: {key: val, key: val}
5078 */
5079 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5080 break;
5081
5082 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005083 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005085 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087
5088 /*
5089 * Environment variable: $VAR.
5090 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
5093
5094 /*
5095 * Register contents: @r.
5096 */
5097 case '@': ++*arg;
5098 if (evaluate)
5099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005101 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103 if (**arg != NUL)
5104 ++*arg;
5105 break;
5106
5107 /*
5108 * nested expression: (expression).
5109 */
5110 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (**arg == ')')
5113 ++*arg;
5114 else if (ret == OK)
5115 {
5116 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 ret = FAIL;
5119 }
5120 break;
5121
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 break;
5124 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125
5126 if (ret == NOTDONE)
5127 {
5128 /*
5129 * Must be a variable or function name.
5130 * Can also be a curly-braces kind of name: {expr}.
5131 */
5132 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005133 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 if (alias != NULL)
5135 s = alias;
5136
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005137 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 ret = FAIL;
5139 else
5140 {
5141 if (**arg == '(') /* recursive! */
5142 {
5143 /* If "s" is the name of a variable of type VAR_FUNC
5144 * use its contents. */
5145 s = deref_func_name(s, &len);
5146
5147 /* Invoke the function. */
5148 ret = get_func_tv(s, len, rettv, arg,
5149 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005150 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005151
5152 /* If evaluate is FALSE rettv->v_type was not set in
5153 * get_func_tv, but it's needed in handle_subscript() to parse
5154 * what follows. So set it here. */
5155 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5156 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005157 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005158 rettv->v_type = VAR_FUNC;
5159 }
5160
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 /* Stop the expression evaluation when immediately
5162 * aborting on error, or when an interrupt occurred or
5163 * an exception was thrown but not caught. */
5164 if (aborting())
5165 {
5166 if (ret == OK)
5167 clear_tv(rettv);
5168 ret = FAIL;
5169 }
5170 }
5171 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005172 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005173 else
5174 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005176 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 }
5178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 *arg = skipwhite(*arg);
5180
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5182 * expr(expr). */
5183 if (ret == OK)
5184 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186 /*
5187 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5188 */
5189 if (ret == OK && evaluate && end_leader > start_leader)
5190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005191 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005192 int val = 0;
5193#ifdef FEAT_FLOAT
5194 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005195
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 if (rettv->v_type == VAR_FLOAT)
5197 f = rettv->vval.v_float;
5198 else
5199#endif
5200 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 clear_tv(rettv);
5204 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 else
5207 {
5208 while (end_leader > start_leader)
5209 {
5210 --end_leader;
5211 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 {
5213#ifdef FEAT_FLOAT
5214 if (rettv->v_type == VAR_FLOAT)
5215 f = !f;
5216 else
5217#endif
5218 val = !val;
5219 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005221 {
5222#ifdef FEAT_FLOAT
5223 if (rettv->v_type == VAR_FLOAT)
5224 f = -f;
5225 else
5226#endif
5227 val = -val;
5228 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 {
5233 clear_tv(rettv);
5234 rettv->vval.v_float = f;
5235 }
5236 else
5237#endif
5238 {
5239 clear_tv(rettv);
5240 rettv->v_type = VAR_NUMBER;
5241 rettv->vval.v_number = val;
5242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245
5246 return ret;
5247}
5248
5249/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005250 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5251 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5253 */
5254 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005255eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260{
5261 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 long len = -1;
5265 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005269 if (rettv->v_type == VAR_FUNC
5270#ifdef FEAT_FLOAT
5271 || rettv->v_type == VAR_FLOAT
5272#endif
5273 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 if (verbose)
5276 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 return FAIL;
5278 }
5279
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 /*
5283 * dict.name
5284 */
5285 key = *arg + 1;
5286 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5287 ;
5288 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 /*
5295 * something[idx]
5296 *
5297 * Get the (first) variable from inside the [].
5298 */
5299 *arg = skipwhite(*arg + 1);
5300 if (**arg == ':')
5301 empty1 = TRUE;
5302 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5303 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5305 {
5306 /* not a number or string */
5307 clear_tv(&var1);
5308 return FAIL;
5309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310
5311 /*
5312 * Get the second variable from inside the [:].
5313 */
5314 if (**arg == ':')
5315 {
5316 range = TRUE;
5317 *arg = skipwhite(*arg + 1);
5318 if (**arg == ']')
5319 empty2 = TRUE;
5320 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 if (!empty1)
5323 clear_tv(&var1);
5324 return FAIL;
5325 }
5326 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5327 {
5328 /* not a number or string */
5329 if (!empty1)
5330 clear_tv(&var1);
5331 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 return FAIL;
5333 }
5334 }
5335
5336 /* Check for the ']'. */
5337 if (**arg != ']')
5338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005339 if (verbose)
5340 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 clear_tv(&var1);
5342 if (range)
5343 clear_tv(&var2);
5344 return FAIL;
5345 }
5346 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 }
5348
5349 if (evaluate)
5350 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 n1 = 0;
5352 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 n1 = get_tv_number(&var1);
5355 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 if (range)
5358 {
5359 if (empty2)
5360 n2 = -1;
5361 else
5362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 n2 = get_tv_number(&var2);
5364 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 }
5366 }
5367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 {
5370 case VAR_NUMBER:
5371 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 len = (long)STRLEN(s);
5374 if (range)
5375 {
5376 /* The resulting variable is a substring. If the indexes
5377 * are out of range the result is empty. */
5378 if (n1 < 0)
5379 {
5380 n1 = len + n1;
5381 if (n1 < 0)
5382 n1 = 0;
5383 }
5384 if (n2 < 0)
5385 n2 = len + n2;
5386 else if (n2 >= len)
5387 n2 = len;
5388 if (n1 >= len || n2 < 0 || n1 > n2)
5389 s = NULL;
5390 else
5391 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5392 }
5393 else
5394 {
5395 /* The resulting variable is a string of a single
5396 * character. If the index is too big or negative the
5397 * result is empty. */
5398 if (n1 >= len || n1 < 0)
5399 s = NULL;
5400 else
5401 s = vim_strnsave(s + n1, 1);
5402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 clear_tv(rettv);
5404 rettv->v_type = VAR_STRING;
5405 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 break;
5407
5408 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 if (n1 < 0)
5411 n1 = len + n1;
5412 if (!empty1 && (n1 < 0 || n1 >= len))
5413 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005414 /* For a range we allow invalid values and return an empty
5415 * list. A list index out of range is an error. */
5416 if (!range)
5417 {
5418 if (verbose)
5419 EMSGN(_(e_listidx), n1);
5420 return FAIL;
5421 }
5422 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 }
5424 if (range)
5425 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 list_T *l;
5427 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428
5429 if (n2 < 0)
5430 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005431 else if (n2 >= len)
5432 n2 = len - 1;
5433 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005434 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 l = list_alloc();
5436 if (l == NULL)
5437 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 n1 <= n2; ++n1)
5440 {
5441 if (list_append_tv(l, &item->li_tv) == FAIL)
5442 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005443 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 return FAIL;
5445 }
5446 item = item->li_next;
5447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 clear_tv(rettv);
5449 rettv->v_type = VAR_LIST;
5450 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005451 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453 else
5454 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005455 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 clear_tv(rettv);
5457 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460
5461 case VAR_DICT:
5462 if (range)
5463 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005464 if (verbose)
5465 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 if (len == -1)
5467 clear_tv(&var1);
5468 return FAIL;
5469 }
5470 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005471 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472
5473 if (len == -1)
5474 {
5475 key = get_tv_string(&var1);
5476 if (*key == NUL)
5477 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005478 if (verbose)
5479 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 clear_tv(&var1);
5481 return FAIL;
5482 }
5483 }
5484
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005485 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005487 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005488 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 if (len == -1)
5490 clear_tv(&var1);
5491 if (item == NULL)
5492 return FAIL;
5493
5494 copy_tv(&item->di_tv, &var1);
5495 clear_tv(rettv);
5496 *rettv = var1;
5497 }
5498 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 }
5500 }
5501
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 return OK;
5503}
5504
5505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * Get an option value.
5507 * "arg" points to the '&' or '+' before the option name.
5508 * "arg" is advanced to character after the option name.
5509 * Return OK or FAIL.
5510 */
5511 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 int evaluate;
5516{
5517 char_u *option_end;
5518 long numval;
5519 char_u *stringval;
5520 int opt_type;
5521 int c;
5522 int working = (**arg == '+'); /* has("+option") */
5523 int ret = OK;
5524 int opt_flags;
5525
5526 /*
5527 * Isolate the option name and find its value.
5528 */
5529 option_end = find_option_end(arg, &opt_flags);
5530 if (option_end == NULL)
5531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 EMSG2(_("E112: Option name missing: %s"), *arg);
5534 return FAIL;
5535 }
5536
5537 if (!evaluate)
5538 {
5539 *arg = option_end;
5540 return OK;
5541 }
5542
5543 c = *option_end;
5544 *option_end = NUL;
5545 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 if (opt_type == -3) /* invalid name */
5549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 EMSG2(_("E113: Unknown option: %s"), *arg);
5552 ret = FAIL;
5553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
5556 if (opt_type == -2) /* hidden string option */
5557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 rettv->v_type = VAR_STRING;
5559 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561 else if (opt_type == -1) /* hidden number option */
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 rettv->v_type = VAR_NUMBER;
5564 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566 else if (opt_type == 1) /* number option */
5567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 rettv->v_type = VAR_NUMBER;
5569 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 else /* string option */
5572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 rettv->v_type = VAR_STRING;
5574 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576 }
5577 else if (working && (opt_type == -2 || opt_type == -1))
5578 ret = FAIL;
5579
5580 *option_end = c; /* put back for error messages */
5581 *arg = option_end;
5582
5583 return ret;
5584}
5585
5586/*
5587 * Allocate a variable for a string constant.
5588 * Return OK or FAIL.
5589 */
5590 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 int evaluate;
5595{
5596 char_u *p;
5597 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 int extra = 0;
5599
5600 /*
5601 * Find the end of the string, skipping backslashed characters.
5602 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005603 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
5605 if (*p == '\\' && p[1] != NUL)
5606 {
5607 ++p;
5608 /* A "\<x>" form occupies at least 4 characters, and produces up
5609 * to 6 characters: reserve space for 2 extra */
5610 if (*p == '<')
5611 extra += 2;
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614
5615 if (*p != '"')
5616 {
5617 EMSG2(_("E114: Missing quote: %s"), *arg);
5618 return FAIL;
5619 }
5620
5621 /* If only parsing, set *arg and return here */
5622 if (!evaluate)
5623 {
5624 *arg = p + 1;
5625 return OK;
5626 }
5627
5628 /*
5629 * Copy the string into allocated memory, handling backslashed
5630 * characters.
5631 */
5632 name = alloc((unsigned)(p - *arg + extra));
5633 if (name == NULL)
5634 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 rettv->v_type = VAR_STRING;
5636 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
5640 if (*p == '\\')
5641 {
5642 switch (*++p)
5643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 case 'b': *name++ = BS; ++p; break;
5645 case 'e': *name++ = ESC; ++p; break;
5646 case 'f': *name++ = FF; ++p; break;
5647 case 'n': *name++ = NL; ++p; break;
5648 case 'r': *name++ = CAR; ++p; break;
5649 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 case 'X': /* hex: "\x1", "\x12" */
5652 case 'x':
5653 case 'u': /* Unicode: "\u0023" */
5654 case 'U':
5655 if (vim_isxdigit(p[1]))
5656 {
5657 int n, nr;
5658 int c = toupper(*p);
5659
5660 if (c == 'X')
5661 n = 2;
5662 else
5663 n = 4;
5664 nr = 0;
5665 while (--n >= 0 && vim_isxdigit(p[1]))
5666 {
5667 ++p;
5668 nr = (nr << 4) + hex2nr(*p);
5669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_MBYTE
5672 /* For "\u" store the number according to
5673 * 'encoding'. */
5674 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 else
5677#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 break;
5681
5682 /* octal: "\1", "\12", "\123" */
5683 case '0':
5684 case '1':
5685 case '2':
5686 case '3':
5687 case '4':
5688 case '5':
5689 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 case '7': *name = *p++ - '0';
5691 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 *name = (*name << 3) + *p++ - '0';
5694 if (*p >= '0' && *p <= '7')
5695 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 break;
5699
5700 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 if (extra != 0)
5703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 break;
5706 }
5707 /* FALLTHROUGH */
5708
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 break;
5711 }
5712 }
5713 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 *arg = p + 1;
5719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 return OK;
5721}
5722
5723/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * Return OK or FAIL.
5726 */
5727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 int evaluate;
5732{
5733 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 int reduce = 0;
5736
5737 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 */
5740 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 break;
5746 ++reduce;
5747 ++p;
5748 }
5749 }
5750
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 return FAIL;
5755 }
5756
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 if (!evaluate)
5759 {
5760 *arg = p + 1;
5761 return OK;
5762 }
5763
5764 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 */
5767 str = alloc((unsigned)((p - *arg) - reduce));
5768 if (str == NULL)
5769 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 rettv->v_type = VAR_STRING;
5771 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 break;
5779 ++p;
5780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 *arg = p + 1;
5785
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 return OK;
5787}
5788
5789/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 * Allocate a variable for a List and fill it from "*arg".
5791 * Return OK or FAIL.
5792 */
5793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005794get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005796 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 int evaluate;
5798{
Bram Moolenaar33570922005-01-25 22:26:29 +00005799 list_T *l = NULL;
5800 typval_T tv;
5801 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802
5803 if (evaluate)
5804 {
5805 l = list_alloc();
5806 if (l == NULL)
5807 return FAIL;
5808 }
5809
5810 *arg = skipwhite(*arg + 1);
5811 while (**arg != ']' && **arg != NUL)
5812 {
5813 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5814 goto failret;
5815 if (evaluate)
5816 {
5817 item = listitem_alloc();
5818 if (item != NULL)
5819 {
5820 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005821 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 list_append(l, item);
5823 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005824 else
5825 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 }
5827
5828 if (**arg == ']')
5829 break;
5830 if (**arg != ',')
5831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 goto failret;
5834 }
5835 *arg = skipwhite(*arg + 1);
5836 }
5837
5838 if (**arg != ']')
5839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841failret:
5842 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005843 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 return FAIL;
5845 }
5846
5847 *arg = skipwhite(*arg + 1);
5848 if (evaluate)
5849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005850 rettv->v_type = VAR_LIST;
5851 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 ++l->lv_refcount;
5853 }
5854
5855 return OK;
5856}
5857
5858/*
5859 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005860 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005862 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863list_alloc()
5864{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005865 list_T *l;
5866
5867 l = (list_T *)alloc_clear(sizeof(list_T));
5868 if (l != NULL)
5869 {
5870 /* Prepend the list to the list of lists for garbage collection. */
5871 if (first_list != NULL)
5872 first_list->lv_used_prev = l;
5873 l->lv_used_prev = NULL;
5874 l->lv_used_next = first_list;
5875 first_list = l;
5876 }
5877 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878}
5879
5880/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005881 * Allocate an empty list for a return value.
5882 * Returns OK or FAIL.
5883 */
5884 static int
5885rettv_list_alloc(rettv)
5886 typval_T *rettv;
5887{
5888 list_T *l = list_alloc();
5889
5890 if (l == NULL)
5891 return FAIL;
5892
5893 rettv->vval.v_list = l;
5894 rettv->v_type = VAR_LIST;
5895 ++l->lv_refcount;
5896 return OK;
5897}
5898
5899/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 * Unreference a list: decrement the reference count and free it when it
5901 * becomes zero.
5902 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005903 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005907 if (l != NULL && --l->lv_refcount <= 0)
5908 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909}
5910
5911/*
5912 * Free a list, including all items it points to.
5913 * Ignores the reference count.
5914 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005915 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005916list_free(l, recurse)
5917 list_T *l;
5918 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919{
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005922 /* Remove the list from the list of lists for garbage collection. */
5923 if (l->lv_used_prev == NULL)
5924 first_list = l->lv_used_next;
5925 else
5926 l->lv_used_prev->lv_used_next = l->lv_used_next;
5927 if (l->lv_used_next != NULL)
5928 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5929
Bram Moolenaard9fba312005-06-26 22:34:35 +00005930 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 /* Remove the item before deleting it. */
5933 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005934 if (recurse || (item->li_tv.v_type != VAR_LIST
5935 && item->li_tv.v_type != VAR_DICT))
5936 clear_tv(&item->li_tv);
5937 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 }
5939 vim_free(l);
5940}
5941
5942/*
5943 * Allocate a list item.
5944 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005945 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946listitem_alloc()
5947{
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949}
5950
5951/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005952 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005954 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 vim_free(item);
5960}
5961
5962/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005963 * Remove a list item from a List and free it. Also clears the value.
5964 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005965 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005966listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005967 list_T *l;
5968 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005969{
5970 list_remove(l, item, item);
5971 listitem_free(item);
5972}
5973
5974/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 * Get the number of items in a list.
5976 */
5977 static long
5978list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 if (l == NULL)
5982 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005983 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984}
5985
5986/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005987 * Return TRUE when two lists have exactly the same values.
5988 */
5989 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005990list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005991 list_T *l1;
5992 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005993 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005994 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995{
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005998 if (l1 == NULL || l2 == NULL)
5999 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006000 if (l1 == l2)
6001 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006002 if (list_len(l1) != list_len(l2))
6003 return FALSE;
6004
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005 for (item1 = l1->lv_first, item2 = l2->lv_first;
6006 item1 != NULL && item2 != NULL;
6007 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006008 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 return FALSE;
6010 return item1 == NULL && item2 == NULL;
6011}
6012
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006013#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6014 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006015/*
6016 * Return the dictitem that an entry in a hashtable points to.
6017 */
6018 dictitem_T *
6019dict_lookup(hi)
6020 hashitem_T *hi;
6021{
6022 return HI2DI(hi);
6023}
6024#endif
6025
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006027 * Return TRUE when two dictionaries have exactly the same key/values.
6028 */
6029 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006030dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 dict_T *d1;
6032 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006034 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 hashitem_T *hi;
6037 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006040 if (d1 == NULL || d2 == NULL)
6041 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006042 if (d1 == d2)
6043 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044 if (dict_len(d1) != dict_len(d2))
6045 return FALSE;
6046
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006047 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006050 if (!HASHITEM_EMPTY(hi))
6051 {
6052 item2 = dict_find(d2, hi->hi_key, -1);
6053 if (item2 == NULL)
6054 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006056 return FALSE;
6057 --todo;
6058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006059 }
6060 return TRUE;
6061}
6062
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006063static int tv_equal_recurse_limit;
6064
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006066 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006067 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006068 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006069 */
6070 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 typval_T *tv1;
6073 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006074 int ic; /* ignore case */
6075 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076{
6077 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006078 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006080 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006081
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006082 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006083 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006085 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 * recursiveness to a limit. We guess they are equal then.
6087 * A fixed limit has the problem of still taking an awful long time.
6088 * Reduce the limit every time running into it. That should work fine for
6089 * deeply linked structures that are not recursively linked and catch
6090 * recursiveness quickly. */
6091 if (!recursive)
6092 tv_equal_recurse_limit = 1000;
6093 if (recursive_cnt >= tv_equal_recurse_limit)
6094 {
6095 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006096 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006097 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098
6099 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006101 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 ++recursive_cnt;
6103 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6104 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006105 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006106
6107 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108 ++recursive_cnt;
6109 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6110 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112
6113 case VAR_FUNC:
6114 return (tv1->vval.v_string != NULL
6115 && tv2->vval.v_string != NULL
6116 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6117
6118 case VAR_NUMBER:
6119 return tv1->vval.v_number == tv2->vval.v_number;
6120
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006121#ifdef FEAT_FLOAT
6122 case VAR_FLOAT:
6123 return tv1->vval.v_float == tv2->vval.v_float;
6124#endif
6125
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006126 case VAR_STRING:
6127 s1 = get_tv_string_buf(tv1, buf1);
6128 s2 = get_tv_string_buf(tv2, buf2);
6129 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006131
6132 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 return TRUE;
6134}
6135
6136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 * Locate item with index "n" in list "l" and return it.
6138 * A negative index is counted from the end; -1 is the last item.
6139 * Returns NULL when "n" is out of range.
6140 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006141 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144 long n;
6145{
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 long idx;
6148
6149 if (l == NULL)
6150 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006151
6152 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006154 n = l->lv_len + n;
6155
6156 /* Check for index out of range. */
6157 if (n < 0 || n >= l->lv_len)
6158 return NULL;
6159
6160 /* When there is a cached index may start search from there. */
6161 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163 if (n < l->lv_idx / 2)
6164 {
6165 /* closest to the start of the list */
6166 item = l->lv_first;
6167 idx = 0;
6168 }
6169 else if (n > (l->lv_idx + l->lv_len) / 2)
6170 {
6171 /* closest to the end of the list */
6172 item = l->lv_last;
6173 idx = l->lv_len - 1;
6174 }
6175 else
6176 {
6177 /* closest to the cached index */
6178 item = l->lv_idx_item;
6179 idx = l->lv_idx;
6180 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 }
6182 else
6183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 if (n < l->lv_len / 2)
6185 {
6186 /* closest to the start of the list */
6187 item = l->lv_first;
6188 idx = 0;
6189 }
6190 else
6191 {
6192 /* closest to the end of the list */
6193 item = l->lv_last;
6194 idx = l->lv_len - 1;
6195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006197
6198 while (n > idx)
6199 {
6200 /* search forward */
6201 item = item->li_next;
6202 ++idx;
6203 }
6204 while (n < idx)
6205 {
6206 /* search backward */
6207 item = item->li_prev;
6208 --idx;
6209 }
6210
6211 /* cache the used index */
6212 l->lv_idx = idx;
6213 l->lv_idx_item = item;
6214
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006215 return item;
6216}
6217
6218/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006219 * Get list item "l[idx]" as a number.
6220 */
6221 static long
6222list_find_nr(l, idx, errorp)
6223 list_T *l;
6224 long idx;
6225 int *errorp; /* set to TRUE when something wrong */
6226{
6227 listitem_T *li;
6228
6229 li = list_find(l, idx);
6230 if (li == NULL)
6231 {
6232 if (errorp != NULL)
6233 *errorp = TRUE;
6234 return -1L;
6235 }
6236 return get_tv_number_chk(&li->li_tv, errorp);
6237}
6238
6239/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006240 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6241 */
6242 char_u *
6243list_find_str(l, idx)
6244 list_T *l;
6245 long idx;
6246{
6247 listitem_T *li;
6248
6249 li = list_find(l, idx - 1);
6250 if (li == NULL)
6251 {
6252 EMSGN(_(e_listidx), idx);
6253 return NULL;
6254 }
6255 return get_tv_string(&li->li_tv);
6256}
6257
6258/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006259 * Locate "item" list "l" and return its index.
6260 * Returns -1 when "item" is not in the list.
6261 */
6262 static long
6263list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 list_T *l;
6265 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006266{
6267 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006269
6270 if (l == NULL)
6271 return -1;
6272 idx = 0;
6273 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6274 ++idx;
6275 if (li == NULL)
6276 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006277 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006278}
6279
6280/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 * Append item "item" to the end of list "l".
6282 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287{
6288 if (l->lv_last == NULL)
6289 {
6290 /* empty list */
6291 l->lv_first = item;
6292 l->lv_last = item;
6293 item->li_prev = NULL;
6294 }
6295 else
6296 {
6297 l->lv_last->li_next = item;
6298 item->li_prev = l->lv_last;
6299 l->lv_last = item;
6300 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 item->li_next = NULL;
6303}
6304
6305/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006309 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 list_T *l;
6312 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006314 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315
Bram Moolenaar05159a02005-02-26 23:04:13 +00006316 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006318 copy_tv(tv, &li->li_tv);
6319 list_append(l, li);
6320 return OK;
6321}
6322
6323/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006324 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 * Return FAIL when out of memory.
6326 */
6327 int
6328list_append_dict(list, dict)
6329 list_T *list;
6330 dict_T *dict;
6331{
6332 listitem_T *li = listitem_alloc();
6333
6334 if (li == NULL)
6335 return FAIL;
6336 li->li_tv.v_type = VAR_DICT;
6337 li->li_tv.v_lock = 0;
6338 li->li_tv.vval.v_dict = dict;
6339 list_append(list, li);
6340 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341 return OK;
6342}
6343
6344/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006345 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006346 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 * Returns FAIL when out of memory.
6348 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006349 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006350list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006351 list_T *l;
6352 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006353 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006354{
6355 listitem_T *li = listitem_alloc();
6356
6357 if (li == NULL)
6358 return FAIL;
6359 list_append(l, li);
6360 li->li_tv.v_type = VAR_STRING;
6361 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006362 if (str == NULL)
6363 li->li_tv.vval.v_string = NULL;
6364 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006365 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366 return FAIL;
6367 return OK;
6368}
6369
6370/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006371 * Append "n" to list "l".
6372 * Returns FAIL when out of memory.
6373 */
6374 static int
6375list_append_number(l, n)
6376 list_T *l;
6377 varnumber_T n;
6378{
6379 listitem_T *li;
6380
6381 li = listitem_alloc();
6382 if (li == NULL)
6383 return FAIL;
6384 li->li_tv.v_type = VAR_NUMBER;
6385 li->li_tv.v_lock = 0;
6386 li->li_tv.vval.v_number = n;
6387 list_append(l, li);
6388 return OK;
6389}
6390
6391/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006393 * If "item" is NULL append at the end.
6394 * Return FAIL when out of memory.
6395 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006396 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *l;
6399 typval_T *tv;
6400 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401{
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403
6404 if (ni == NULL)
6405 return FAIL;
6406 copy_tv(tv, &ni->li_tv);
6407 if (item == NULL)
6408 /* Append new item at end of list. */
6409 list_append(l, ni);
6410 else
6411 {
6412 /* Insert new item before existing item. */
6413 ni->li_prev = item->li_prev;
6414 ni->li_next = item;
6415 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006416 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 ++l->lv_idx;
6419 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006421 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 l->lv_idx_item = NULL;
6424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006426 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 }
6428 return OK;
6429}
6430
6431/*
6432 * Extend "l1" with "l2".
6433 * If "bef" is NULL append at the end, otherwise insert before this item.
6434 * Returns FAIL when out of memory.
6435 */
6436 static int
6437list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 list_T *l1;
6439 list_T *l2;
6440 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441{
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006443 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006445 /* We also quit the loop when we have inserted the original item count of
6446 * the list, avoid a hang when we extend a list with itself. */
6447 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6449 return FAIL;
6450 return OK;
6451}
6452
6453/*
6454 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6455 * Return FAIL when out of memory.
6456 */
6457 static int
6458list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 list_T *l1;
6460 list_T *l2;
6461 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462{
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006465 if (l1 == NULL || l2 == NULL)
6466 return FAIL;
6467
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 if (l == NULL)
6471 return FAIL;
6472 tv->v_type = VAR_LIST;
6473 tv->vval.v_list = l;
6474
6475 /* append all items from the second list */
6476 return list_extend(l, l2, NULL);
6477}
6478
6479/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006480 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * Returns NULL when out of memory.
6484 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490{
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 list_T *copy;
6492 listitem_T *item;
6493 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494
6495 if (orig == NULL)
6496 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497
6498 copy = list_alloc();
6499 if (copy != NULL)
6500 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006501 if (copyID != 0)
6502 {
6503 /* Do this before adding the items, because one of the items may
6504 * refer back to this list. */
6505 orig->lv_copyID = copyID;
6506 orig->lv_copylist = copy;
6507 }
6508 for (item = orig->lv_first; item != NULL && !got_int;
6509 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 {
6511 ni = listitem_alloc();
6512 if (ni == NULL)
6513 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006514 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 {
6516 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6517 {
6518 vim_free(ni);
6519 break;
6520 }
6521 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006523 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 list_append(copy, ni);
6525 }
6526 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006527 if (item != NULL)
6528 {
6529 list_unref(copy);
6530 copy = NULL;
6531 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 }
6533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 return copy;
6535}
6536
6537/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006539 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006541 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006542list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 list_T *l;
6544 listitem_T *item;
6545 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546{
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 /* notify watchers */
6550 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006551 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 list_fix_watch(l, ip);
6554 if (ip == item2)
6555 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557
6558 if (item2->li_next == NULL)
6559 l->lv_last = item->li_prev;
6560 else
6561 item2->li_next->li_prev = item->li_prev;
6562 if (item->li_prev == NULL)
6563 l->lv_first = item2->li_next;
6564 else
6565 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006566 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567}
6568
6569/*
6570 * Return an allocated string with the string representation of a list.
6571 * May return NULL.
6572 */
6573 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006574list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577{
6578 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579
6580 if (tv->vval.v_list == NULL)
6581 return NULL;
6582 ga_init2(&ga, (int)sizeof(char), 80);
6583 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006584 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 {
6586 vim_free(ga.ga_data);
6587 return NULL;
6588 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 ga_append(&ga, ']');
6590 ga_append(&ga, NUL);
6591 return (char_u *)ga.ga_data;
6592}
6593
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006594typedef struct join_S {
6595 char_u *s;
6596 char_u *tofree;
6597} join_T;
6598
6599 static int
6600list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6601 garray_T *gap; /* to store the result in */
6602 list_T *l;
6603 char_u *sep;
6604 int echo_style;
6605 int copyID;
6606 garray_T *join_gap; /* to keep each list item string */
6607{
6608 int i;
6609 join_T *p;
6610 int len;
6611 int sumlen = 0;
6612 int first = TRUE;
6613 char_u *tofree;
6614 char_u numbuf[NUMBUFLEN];
6615 listitem_T *item;
6616 char_u *s;
6617
6618 /* Stringify each item in the list. */
6619 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6620 {
6621 if (echo_style)
6622 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6623 else
6624 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6625 if (s == NULL)
6626 return FAIL;
6627
6628 len = (int)STRLEN(s);
6629 sumlen += len;
6630
6631 ga_grow(join_gap, 1);
6632 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6633 if (tofree != NULL || s != numbuf)
6634 {
6635 p->s = s;
6636 p->tofree = tofree;
6637 }
6638 else
6639 {
6640 p->s = vim_strnsave(s, len);
6641 p->tofree = p->s;
6642 }
6643
6644 line_breakcheck();
6645 }
6646
6647 /* Allocate result buffer with its total size, avoid re-allocation and
6648 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6649 if (join_gap->ga_len >= 2)
6650 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6651 if (ga_grow(gap, sumlen + 2) == FAIL)
6652 return FAIL;
6653
6654 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6655 {
6656 if (first)
6657 first = FALSE;
6658 else
6659 ga_concat(gap, sep);
6660 p = ((join_T *)join_gap->ga_data) + i;
6661
6662 if (p->s != NULL)
6663 ga_concat(gap, p->s);
6664 line_breakcheck();
6665 }
6666
6667 return OK;
6668}
6669
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006672 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006673 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006676list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006677 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006680 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006681 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006683 garray_T join_ga;
6684 int retval;
6685 join_T *p;
6686 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006688 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6689 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6690
6691 /* Dispose each item in join_ga. */
6692 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694 p = (join_T *)join_ga.ga_data;
6695 for (i = 0; i < join_ga.ga_len; ++i)
6696 {
6697 vim_free(p->tofree);
6698 ++p;
6699 }
6700 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006702
6703 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704}
6705
6706/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006707 * Garbage collection for lists and dictionaries.
6708 *
6709 * We use reference counts to be able to free most items right away when they
6710 * are no longer used. But for composite items it's possible that it becomes
6711 * unused while the reference count is > 0: When there is a recursive
6712 * reference. Example:
6713 * :let l = [1, 2, 3]
6714 * :let d = {9: l}
6715 * :let l[1] = d
6716 *
6717 * Since this is quite unusual we handle this with garbage collection: every
6718 * once in a while find out which lists and dicts are not referenced from any
6719 * variable.
6720 *
6721 * Here is a good reference text about garbage collection (refers to Python
6722 * but it applies to all reference-counting mechanisms):
6723 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006724 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006725
6726/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006727 * Do garbage collection for lists and dicts.
6728 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006729 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006730 int
6731garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006732{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006733 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006734 buf_T *buf;
6735 win_T *wp;
6736 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006737 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006738 int did_free;
6739 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006740#ifdef FEAT_WINDOWS
6741 tabpage_T *tp;
6742#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006744 /* Only do this once. */
6745 want_garbage_collect = FALSE;
6746 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006747 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006748
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006749 /* We advance by two because we add one for items referenced through
6750 * previous_funccal. */
6751 current_copyID += COPYID_INC;
6752 copyID = current_copyID;
6753
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006754 /*
6755 * 1. Go through all accessible variables and mark all lists and dicts
6756 * with copyID.
6757 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006758
6759 /* Don't free variables in the previous_funccal list unless they are only
6760 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006761 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006762 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6763 {
6764 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6765 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6766 }
6767
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006768 /* script-local variables */
6769 for (i = 1; i <= ga_scripts.ga_len; ++i)
6770 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6771
6772 /* buffer-local variables */
6773 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006774 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775
6776 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006777 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006778 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006779#ifdef FEAT_AUTOCMD
6780 if (aucmd_win != NULL)
6781 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6782#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006784#ifdef FEAT_WINDOWS
6785 /* tabpage-local variables */
6786 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006787 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006788#endif
6789
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006790 /* global variables */
6791 set_ref_in_ht(&globvarht, copyID);
6792
6793 /* function-local variables */
6794 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6795 {
6796 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6797 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6798 }
6799
Bram Moolenaard812df62008-11-09 12:46:09 +00006800 /* v: vars */
6801 set_ref_in_ht(&vimvarht, copyID);
6802
Bram Moolenaar1dced572012-04-05 16:54:08 +02006803#ifdef FEAT_LUA
6804 set_ref_in_lua(copyID);
6805#endif
6806
Bram Moolenaardb913952012-06-29 12:54:53 +02006807#ifdef FEAT_PYTHON
6808 set_ref_in_python(copyID);
6809#endif
6810
6811#ifdef FEAT_PYTHON3
6812 set_ref_in_python3(copyID);
6813#endif
6814
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006815 /*
6816 * 2. Free lists and dictionaries that are not referenced.
6817 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006818 did_free = free_unref_items(copyID);
6819
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006820 /*
6821 * 3. Check if any funccal can be freed now.
6822 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006823 for (pfc = &previous_funccal; *pfc != NULL; )
6824 {
6825 if (can_free_funccal(*pfc, copyID))
6826 {
6827 fc = *pfc;
6828 *pfc = fc->caller;
6829 free_funccal(fc, TRUE);
6830 did_free = TRUE;
6831 did_free_funccal = TRUE;
6832 }
6833 else
6834 pfc = &(*pfc)->caller;
6835 }
6836 if (did_free_funccal)
6837 /* When a funccal was freed some more items might be garbage
6838 * collected, so run again. */
6839 (void)garbage_collect();
6840
6841 return did_free;
6842}
6843
6844/*
6845 * Free lists and dictionaries that are no longer referenced.
6846 */
6847 static int
6848free_unref_items(copyID)
6849 int copyID;
6850{
6851 dict_T *dd;
6852 list_T *ll;
6853 int did_free = FALSE;
6854
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006856 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 */
6858 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006861 /* Free the Dictionary and ordinary items it contains, but don't
6862 * recurse into Lists and Dictionaries, they will be in the list
6863 * of dicts or list of lists. */
6864 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 did_free = TRUE;
6866
6867 /* restart, next dict may also have been freed */
6868 dd = first_dict;
6869 }
6870 else
6871 dd = dd->dv_used_next;
6872
6873 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 * Go through the list of lists and free items without the copyID.
6875 * But don't free a list that has a watcher (used in a for loop), these
6876 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 */
6878 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6880 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006882 /* Free the List and ordinary items it contains, but don't recurse
6883 * into Lists and Dictionaries, they will be in the list of dicts
6884 * or list of lists. */
6885 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 did_free = TRUE;
6887
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006888 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 ll = first_list;
6890 }
6891 else
6892 ll = ll->lv_used_next;
6893
6894 return did_free;
6895}
6896
6897/*
6898 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6899 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006900 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901set_ref_in_ht(ht, copyID)
6902 hashtab_T *ht;
6903 int copyID;
6904{
6905 int todo;
6906 hashitem_T *hi;
6907
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006908 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 for (hi = ht->ht_array; todo > 0; ++hi)
6910 if (!HASHITEM_EMPTY(hi))
6911 {
6912 --todo;
6913 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6914 }
6915}
6916
6917/*
6918 * Mark all lists and dicts referenced through list "l" with "copyID".
6919 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006920 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921set_ref_in_list(l, copyID)
6922 list_T *l;
6923 int copyID;
6924{
6925 listitem_T *li;
6926
6927 for (li = l->lv_first; li != NULL; li = li->li_next)
6928 set_ref_in_item(&li->li_tv, copyID);
6929}
6930
6931/*
6932 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6933 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006934 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935set_ref_in_item(tv, copyID)
6936 typval_T *tv;
6937 int copyID;
6938{
6939 dict_T *dd;
6940 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006941
6942 switch (tv->v_type)
6943 {
6944 case VAR_DICT:
6945 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006946 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006947 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006948 /* Didn't see this dict yet. */
6949 dd->dv_copyID = copyID;
6950 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953
6954 case VAR_LIST:
6955 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006956 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006957 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 /* Didn't see this list yet. */
6959 ll->lv_copyID = copyID;
6960 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965}
6966
6967/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968 * Allocate an empty header for a dictionary.
6969 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006970 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006971dict_alloc()
6972{
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006976 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006977 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006978 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 if (first_dict != NULL)
6980 first_dict->dv_used_prev = d;
6981 d->dv_used_next = first_dict;
6982 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006983 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006987 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006989 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006990 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006991 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006992}
6993
6994/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006995 * Allocate an empty dict for a return value.
6996 * Returns OK or FAIL.
6997 */
6998 static int
6999rettv_dict_alloc(rettv)
7000 typval_T *rettv;
7001{
7002 dict_T *d = dict_alloc();
7003
7004 if (d == NULL)
7005 return FAIL;
7006
7007 rettv->vval.v_dict = d;
7008 rettv->v_type = VAR_DICT;
7009 ++d->dv_refcount;
7010 return OK;
7011}
7012
7013
7014/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015 * Unreference a Dictionary: decrement the reference count and free it when it
7016 * becomes zero.
7017 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007018 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007022 if (d != NULL && --d->dv_refcount <= 0)
7023 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024}
7025
7026/*
7027 * Free a Dictionary, including all items it contains.
7028 * Ignores the reference count.
7029 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007030 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007031dict_free(d, recurse)
7032 dict_T *d;
7033 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007035 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007037 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 /* Remove the dict from the list of dicts for garbage collection. */
7040 if (d->dv_used_prev == NULL)
7041 first_dict = d->dv_used_next;
7042 else
7043 d->dv_used_prev->dv_used_next = d->dv_used_next;
7044 if (d->dv_used_next != NULL)
7045 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7046
7047 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007048 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007049 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052 if (!HASHITEM_EMPTY(hi))
7053 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007054 /* Remove the item before deleting it, just in case there is
7055 * something recursive causing trouble. */
7056 di = HI2DI(hi);
7057 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007058 if (recurse || (di->di_tv.v_type != VAR_LIST
7059 && di->di_tv.v_type != VAR_DICT))
7060 clear_tv(&di->di_tv);
7061 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 --todo;
7063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007065 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 vim_free(d);
7067}
7068
7069/*
7070 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 * The "key" is copied to the new item.
7072 * Note that the value of the item "di_tv" still needs to be initialized!
7073 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007075 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007076dictitem_alloc(key)
7077 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078{
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007081 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007083 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007084 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007085 di->di_flags = 0;
7086 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007087 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088}
7089
7090/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007091 * Make a copy of a Dictionary item.
7092 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096{
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007098
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007099 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7100 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007101 if (di != NULL)
7102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007103 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007104 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105 copy_tv(&org->di_tv, &di->di_tv);
7106 }
7107 return di;
7108}
7109
7110/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007111 * Remove item "item" from Dictionary "dict" and free it.
7112 */
7113 static void
7114dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 dict_T *dict;
7116 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117{
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 if (HASHITEM_EMPTY(hi))
7122 EMSG2(_(e_intern2), "dictitem_remove()");
7123 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125 dictitem_free(item);
7126}
7127
7128/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007129 * Free a dict item. Also clears the value.
7130 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007131 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135 clear_tv(&item->di_tv);
7136 vim_free(item);
7137}
7138
7139/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7141 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007142 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 * Returns NULL when out of memory.
7144 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007146dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007149 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150{
Bram Moolenaar33570922005-01-25 22:26:29 +00007151 dict_T *copy;
7152 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007153 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155
7156 if (orig == NULL)
7157 return NULL;
7158
7159 copy = dict_alloc();
7160 if (copy != NULL)
7161 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007162 if (copyID != 0)
7163 {
7164 orig->dv_copyID = copyID;
7165 orig->dv_copydict = copy;
7166 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007167 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007168 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 --todo;
7173
7174 di = dictitem_alloc(hi->hi_key);
7175 if (di == NULL)
7176 break;
7177 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 {
7179 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7180 copyID) == FAIL)
7181 {
7182 vim_free(di);
7183 break;
7184 }
7185 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 else
7187 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7188 if (dict_add(copy, di) == FAIL)
7189 {
7190 dictitem_free(di);
7191 break;
7192 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007193 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195
Bram Moolenaare9a41262005-01-15 22:18:47 +00007196 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 if (todo > 0)
7198 {
7199 dict_unref(copy);
7200 copy = NULL;
7201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 }
7203
7204 return copy;
7205}
7206
7207/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007209 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007211 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 dict_T *d;
7214 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215{
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217}
7218
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007220 * Add a number or string entry to dictionary "d".
7221 * When "str" is NULL use number "nr", otherwise use "str".
7222 * Returns FAIL when out of memory and when key already exists.
7223 */
7224 int
7225dict_add_nr_str(d, key, nr, str)
7226 dict_T *d;
7227 char *key;
7228 long nr;
7229 char_u *str;
7230{
7231 dictitem_T *item;
7232
7233 item = dictitem_alloc((char_u *)key);
7234 if (item == NULL)
7235 return FAIL;
7236 item->di_tv.v_lock = 0;
7237 if (str == NULL)
7238 {
7239 item->di_tv.v_type = VAR_NUMBER;
7240 item->di_tv.vval.v_number = nr;
7241 }
7242 else
7243 {
7244 item->di_tv.v_type = VAR_STRING;
7245 item->di_tv.vval.v_string = vim_strsave(str);
7246 }
7247 if (dict_add(d, item) == FAIL)
7248 {
7249 dictitem_free(item);
7250 return FAIL;
7251 }
7252 return OK;
7253}
7254
7255/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007256 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007257 * Returns FAIL when out of memory and when key already exists.
7258 */
7259 int
7260dict_add_list(d, key, list)
7261 dict_T *d;
7262 char *key;
7263 list_T *list;
7264{
7265 dictitem_T *item;
7266
7267 item = dictitem_alloc((char_u *)key);
7268 if (item == NULL)
7269 return FAIL;
7270 item->di_tv.v_lock = 0;
7271 item->di_tv.v_type = VAR_LIST;
7272 item->di_tv.vval.v_list = list;
7273 if (dict_add(d, item) == FAIL)
7274 {
7275 dictitem_free(item);
7276 return FAIL;
7277 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007278 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007279 return OK;
7280}
7281
7282/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283 * Get the number of items in a Dictionary.
7284 */
7285 static long
7286dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007288{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289 if (d == NULL)
7290 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007291 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292}
7293
7294/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295 * Find item "key[len]" in Dictionary "d".
7296 * If "len" is negative use strlen(key).
7297 * Returns NULL when not found.
7298 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007299 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 char_u *key;
7303 int len;
7304{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305#define AKEYLEN 200
7306 char_u buf[AKEYLEN];
7307 char_u *akey;
7308 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 if (len < 0)
7312 akey = key;
7313 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007314 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 tofree = akey = vim_strnsave(key, len);
7316 if (akey == NULL)
7317 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007318 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 else
7320 {
7321 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007322 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 akey = buf;
7324 }
7325
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 vim_free(tofree);
7328 if (HASHITEM_EMPTY(hi))
7329 return NULL;
7330 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331}
7332
7333/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007334 * Get a string item from a dictionary.
7335 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007336 * Returns NULL if the entry doesn't exist or out of memory.
7337 */
7338 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007339get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007340 dict_T *d;
7341 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007342 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007343{
7344 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007345 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007346
7347 di = dict_find(d, key, -1);
7348 if (di == NULL)
7349 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007350 s = get_tv_string(&di->di_tv);
7351 if (save && s != NULL)
7352 s = vim_strsave(s);
7353 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007354}
7355
7356/*
7357 * Get a number item from a dictionary.
7358 * Returns 0 if the entry doesn't exist or out of memory.
7359 */
7360 long
7361get_dict_number(d, key)
7362 dict_T *d;
7363 char_u *key;
7364{
7365 dictitem_T *di;
7366
7367 di = dict_find(d, key, -1);
7368 if (di == NULL)
7369 return 0;
7370 return get_tv_number(&di->di_tv);
7371}
7372
7373/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007374 * Return an allocated string with the string representation of a Dictionary.
7375 * May return NULL.
7376 */
7377 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007378dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007380 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007381{
7382 garray_T ga;
7383 int first = TRUE;
7384 char_u *tofree;
7385 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392 return NULL;
7393 ga_init2(&ga, (int)sizeof(char), 80);
7394 ga_append(&ga, '{');
7395
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007396 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 --todo;
7402
7403 if (first)
7404 first = FALSE;
7405 else
7406 ga_concat(&ga, (char_u *)", ");
7407
7408 tofree = string_quote(hi->hi_key, FALSE);
7409 if (tofree != NULL)
7410 {
7411 ga_concat(&ga, tofree);
7412 vim_free(tofree);
7413 }
7414 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007415 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007416 if (s != NULL)
7417 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007419 if (s == NULL)
7420 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007423 if (todo > 0)
7424 {
7425 vim_free(ga.ga_data);
7426 return NULL;
7427 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428
7429 ga_append(&ga, '}');
7430 ga_append(&ga, NUL);
7431 return (char_u *)ga.ga_data;
7432}
7433
7434/*
7435 * Allocate a variable for a Dictionary and fill it from "*arg".
7436 * Return OK or FAIL. Returns NOTDONE for {expr}.
7437 */
7438 static int
7439get_dict_tv(arg, rettv, evaluate)
7440 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 int evaluate;
7443{
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 dict_T *d = NULL;
7445 typval_T tvkey;
7446 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007447 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451
7452 /*
7453 * First check if it's not a curly-braces thing: {expr}.
7454 * Must do this without evaluating, otherwise a function may be called
7455 * twice. Unfortunately this means we need to call eval1() twice for the
7456 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 if (*start != '}')
7460 {
7461 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7462 return FAIL;
7463 if (*start == '}')
7464 return NOTDONE;
7465 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007466
7467 if (evaluate)
7468 {
7469 d = dict_alloc();
7470 if (d == NULL)
7471 return FAIL;
7472 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007473 tvkey.v_type = VAR_UNKNOWN;
7474 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475
7476 *arg = skipwhite(*arg + 1);
7477 while (**arg != '}' && **arg != NUL)
7478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 goto failret;
7481 if (**arg != ':')
7482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007483 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007484 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 goto failret;
7486 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007487 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007489 key = get_tv_string_buf_chk(&tvkey, buf);
7490 if (key == NULL || *key == NUL)
7491 {
7492 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7493 if (key != NULL)
7494 EMSG(_(e_emptykey));
7495 clear_tv(&tvkey);
7496 goto failret;
7497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499
7500 *arg = skipwhite(*arg + 1);
7501 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7502 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007503 if (evaluate)
7504 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 goto failret;
7506 }
7507 if (evaluate)
7508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 if (item != NULL)
7511 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007512 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 clear_tv(&tv);
7515 goto failret;
7516 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 item = dictitem_alloc(key);
7518 clear_tv(&tvkey);
7519 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007522 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 if (dict_add(d, item) == FAIL)
7524 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 }
7526 }
7527
7528 if (**arg == '}')
7529 break;
7530 if (**arg != ',')
7531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007532 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 goto failret;
7534 }
7535 *arg = skipwhite(*arg + 1);
7536 }
7537
7538 if (**arg != '}')
7539 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007540 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541failret:
7542 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007543 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 return FAIL;
7545 }
7546
7547 *arg = skipwhite(*arg + 1);
7548 if (evaluate)
7549 {
7550 rettv->v_type = VAR_DICT;
7551 rettv->vval.v_dict = d;
7552 ++d->dv_refcount;
7553 }
7554
7555 return OK;
7556}
7557
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007559 * Return a string with the string representation of a variable.
7560 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007561 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007562 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007563 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007564 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 */
7566 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007570 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007571 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007572{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573 static int recurse = 0;
7574 char_u *r = NULL;
7575
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007577 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007578 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 *tofree = NULL;
7580 return NULL;
7581 }
7582 ++recurse;
7583
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007584 switch (tv->v_type)
7585 {
7586 case VAR_FUNC:
7587 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588 r = tv->vval.v_string;
7589 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007591 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592 if (tv->vval.v_list == NULL)
7593 {
7594 *tofree = NULL;
7595 r = NULL;
7596 }
7597 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7598 {
7599 *tofree = NULL;
7600 r = (char_u *)"[...]";
7601 }
7602 else
7603 {
7604 tv->vval.v_list->lv_copyID = copyID;
7605 *tofree = list2string(tv, copyID);
7606 r = *tofree;
7607 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007608 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611 if (tv->vval.v_dict == NULL)
7612 {
7613 *tofree = NULL;
7614 r = NULL;
7615 }
7616 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7617 {
7618 *tofree = NULL;
7619 r = (char_u *)"{...}";
7620 }
7621 else
7622 {
7623 tv->vval.v_dict->dv_copyID = copyID;
7624 *tofree = dict2string(tv, copyID);
7625 r = *tofree;
7626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007627 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629 case VAR_STRING:
7630 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007631 *tofree = NULL;
7632 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007634
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007635#ifdef FEAT_FLOAT
7636 case VAR_FLOAT:
7637 *tofree = NULL;
7638 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7639 r = numbuf;
7640 break;
7641#endif
7642
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007644 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007646 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647
7648 --recurse;
7649 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007650}
7651
7652/*
7653 * Return a string with the string representation of a variable.
7654 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7655 * "numbuf" is used for a number.
7656 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007657 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 */
7659 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007660tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007662 char_u **tofree;
7663 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007665{
7666 switch (tv->v_type)
7667 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007668 case VAR_FUNC:
7669 *tofree = string_quote(tv->vval.v_string, TRUE);
7670 return *tofree;
7671 case VAR_STRING:
7672 *tofree = string_quote(tv->vval.v_string, FALSE);
7673 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007674#ifdef FEAT_FLOAT
7675 case VAR_FLOAT:
7676 *tofree = NULL;
7677 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7678 return numbuf;
7679#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007680 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007681 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007683 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007687 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007688}
7689
7690/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 * Return string "str" in ' quotes, doubling ' characters.
7692 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007694 */
7695 static char_u *
7696string_quote(str, function)
7697 char_u *str;
7698 int function;
7699{
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701 char_u *p, *r, *s;
7702
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 len = (function ? 13 : 3);
7704 if (str != NULL)
7705 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007706 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 for (p = str; *p != NUL; mb_ptr_adv(p))
7708 if (*p == '\'')
7709 ++len;
7710 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 s = r = alloc(len);
7712 if (r != NULL)
7713 {
7714 if (function)
7715 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 r += 10;
7718 }
7719 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 if (str != NULL)
7722 for (p = str; *p != NUL; )
7723 {
7724 if (*p == '\'')
7725 *r++ = '\'';
7726 MB_COPY_CHAR(p, r);
7727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 if (function)
7730 *r++ = ')';
7731 *r++ = NUL;
7732 }
7733 return s;
7734}
7735
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007736#ifdef FEAT_FLOAT
7737/*
7738 * Convert the string "text" to a floating point number.
7739 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7740 * this always uses a decimal point.
7741 * Returns the length of the text that was consumed.
7742 */
7743 static int
7744string2float(text, value)
7745 char_u *text;
7746 float_T *value; /* result stored here */
7747{
7748 char *s = (char *)text;
7749 float_T f;
7750
7751 f = strtod(s, &s);
7752 *value = f;
7753 return (int)((char_u *)s - text);
7754}
7755#endif
7756
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 * Get the value of an environment variable.
7759 * "arg" is pointing to the '$'. It is advanced to after the name.
7760 * If the environment variable was not set, silently assume it is empty.
7761 * Always return OK.
7762 */
7763 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 int evaluate;
7768{
7769 char_u *string = NULL;
7770 int len;
7771 int cc;
7772 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007773 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 ++*arg;
7776 name = *arg;
7777 len = get_env_len(arg);
7778 if (evaluate)
7779 {
7780 if (len != 0)
7781 {
7782 cc = name[len];
7783 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007784 /* first try vim_getenv(), fast for normal environment vars */
7785 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007787 {
7788 if (!mustfree)
7789 string = vim_strsave(string);
7790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 else
7792 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007793 if (mustfree)
7794 vim_free(string);
7795
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 /* next try expanding things like $VIM and ${HOME} */
7797 string = expand_env_save(name - 1);
7798 if (string != NULL && *string == '$')
7799 {
7800 vim_free(string);
7801 string = NULL;
7802 }
7803 }
7804 name[len] = cc;
7805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806 rettv->v_type = VAR_STRING;
7807 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809
7810 return OK;
7811}
7812
7813/*
7814 * Array with names and number of arguments of all internal functions
7815 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7816 */
7817static struct fst
7818{
7819 char *f_name; /* function name */
7820 char f_min_argc; /* minimal number of arguments */
7821 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007822 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007823 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824} functions[] =
7825{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007826#ifdef FEAT_FLOAT
7827 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007828 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007829#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007830 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007831 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {"append", 2, 2, f_append},
7833 {"argc", 0, 0, f_argc},
7834 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007835 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007839 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007842 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"bufexists", 1, 1, f_bufexists},
7844 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7845 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7846 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7847 {"buflisted", 1, 1, f_buflisted},
7848 {"bufloaded", 1, 1, f_bufloaded},
7849 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007850 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {"bufwinnr", 1, 1, f_bufwinnr},
7852 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007853 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007855#ifdef FEAT_FLOAT
7856 {"ceil", 1, 1, f_ceil},
7857#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007858 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007859 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007861 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007863#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007864 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007865 {"complete_add", 1, 1, f_complete_add},
7866 {"complete_check", 0, 0, f_complete_check},
7867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#ifdef FEAT_FLOAT
7871 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007872 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007874 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007876 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007877 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"delete", 1, 1, f_delete},
7879 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007880 {"diff_filler", 1, 1, f_diff_filler},
7881 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007882 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"eventhandler", 0, 0, f_eventhandler},
7886 {"executable", 1, 1, f_executable},
7887 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007888#ifdef FEAT_FLOAT
7889 {"exp", 1, 1, f_exp},
7890#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007891 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007892 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007893 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7895 {"filereadable", 1, 1, f_filereadable},
7896 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007898 {"finddir", 1, 3, f_finddir},
7899 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007900#ifdef FEAT_FLOAT
7901 {"float2nr", 1, 1, f_float2nr},
7902 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007903 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007905 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"fnamemodify", 2, 2, f_fnamemodify},
7907 {"foldclosed", 1, 1, f_foldclosed},
7908 {"foldclosedend", 1, 1, f_foldclosedend},
7909 {"foldlevel", 1, 1, f_foldlevel},
7910 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007911 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007914 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007915 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007916 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007917 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"getchar", 0, 1, f_getchar},
7919 {"getcharmod", 0, 0, f_getcharmod},
7920 {"getcmdline", 0, 0, f_getcmdline},
7921 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007922 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007924 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007925 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"getfsize", 1, 1, f_getfsize},
7927 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007928 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007929 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007930 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007931 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007932 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007933 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007934 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007935 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007937 {"gettabvar", 2, 3, f_gettabvar},
7938 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"getwinposx", 0, 0, f_getwinposx},
7940 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007941 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007942 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007943 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007945 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007946 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007947 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7949 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7950 {"histadd", 2, 2, f_histadd},
7951 {"histdel", 1, 2, f_histdel},
7952 {"histget", 1, 2, f_histget},
7953 {"histnr", 1, 1, f_histnr},
7954 {"hlID", 1, 1, f_hlID},
7955 {"hlexists", 1, 1, f_hlexists},
7956 {"hostname", 0, 0, f_hostname},
7957 {"iconv", 3, 3, f_iconv},
7958 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007960 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007962 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"inputrestore", 0, 0, f_inputrestore},
7964 {"inputsave", 0, 0, f_inputsave},
7965 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007966 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007967 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007969 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007974 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"libcall", 3, 3, f_libcall},
7976 {"libcallnr", 3, 3, f_libcallnr},
7977 {"line", 1, 1, f_line},
7978 {"line2byte", 1, 1, f_line2byte},
7979 {"lispindent", 1, 1, f_lispindent},
7980 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007981#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007982 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983 {"log10", 1, 1, f_log10},
7984#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007985#ifdef FEAT_LUA
7986 {"luaeval", 1, 2, f_luaeval},
7987#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007988 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007989 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007990 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007991 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007992 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007993 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007994 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007995 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007996 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007997 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007998 {"max", 1, 1, f_max},
7999 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008000#ifdef vim_mkdir
8001 {"mkdir", 1, 3, f_mkdir},
8002#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008003 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008004#ifdef FEAT_MZSCHEME
8005 {"mzeval", 1, 1, f_mzeval},
8006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008008 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008009 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008010 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008011#ifdef FEAT_FLOAT
8012 {"pow", 2, 2, f_pow},
8013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008015 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008016 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008017#ifdef FEAT_PYTHON3
8018 {"py3eval", 1, 1, f_py3eval},
8019#endif
8020#ifdef FEAT_PYTHON
8021 {"pyeval", 1, 1, f_pyeval},
8022#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008024 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008025 {"reltime", 0, 2, f_reltime},
8026 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 {"remote_expr", 2, 3, f_remote_expr},
8028 {"remote_foreground", 1, 1, f_remote_foreground},
8029 {"remote_peek", 1, 2, f_remote_peek},
8030 {"remote_read", 1, 1, f_remote_read},
8031 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008032 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008034 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008036 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008037#ifdef FEAT_FLOAT
8038 {"round", 1, 1, f_round},
8039#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008040 {"screencol", 0, 0, f_screencol},
8041 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008042 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008043 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008044 {"searchpair", 3, 7, f_searchpair},
8045 {"searchpairpos", 3, 7, f_searchpairpos},
8046 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {"server2client", 2, 2, f_server2client},
8048 {"serverlist", 0, 0, f_serverlist},
8049 {"setbufvar", 3, 3, f_setbufvar},
8050 {"setcmdpos", 1, 1, f_setcmdpos},
8051 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008052 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008053 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008054 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008055 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008057 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008058 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008060#ifdef FEAT_CRYPT
8061 {"sha256", 1, 1, f_sha256},
8062#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008063 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008064 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066#ifdef FEAT_FLOAT
8067 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008068 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008069#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008070 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008071 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008072 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008073 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008074 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"sqrt", 1, 1, f_sqrt},
8077 {"str2float", 1, 1, f_str2float},
8078#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008079 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008080 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008081 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082#ifdef HAVE_STRFTIME
8083 {"strftime", 1, 2, f_strftime},
8084#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008086 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"strlen", 1, 1, f_strlen},
8088 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008089 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008091 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"submatch", 1, 1, f_submatch},
8093 {"substitute", 4, 4, f_substitute},
8094 {"synID", 3, 3, f_synID},
8095 {"synIDattr", 2, 3, f_synIDattr},
8096 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008097 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008098 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008099 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008100 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008101 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008102 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008103 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008104 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105#ifdef FEAT_FLOAT
8106 {"tan", 1, 1, f_tan},
8107 {"tanh", 1, 1, f_tanh},
8108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008110 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"tolower", 1, 1, f_tolower},
8112 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008113 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#ifdef FEAT_FLOAT
8115 {"trunc", 1, 1, f_trunc},
8116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008118 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008119 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008120 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"virtcol", 1, 1, f_virtcol},
8122 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008123 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"winbufnr", 1, 1, f_winbufnr},
8125 {"wincol", 0, 0, f_wincol},
8126 {"winheight", 1, 1, f_winheight},
8127 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008128 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008130 {"winrestview", 1, 1, f_winrestview},
8131 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008133 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008134 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135};
8136
8137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8138
8139/*
8140 * Function given to ExpandGeneric() to obtain the list of internal
8141 * or user defined function names.
8142 */
8143 char_u *
8144get_function_name(xp, idx)
8145 expand_T *xp;
8146 int idx;
8147{
8148 static int intidx = -1;
8149 char_u *name;
8150
8151 if (idx == 0)
8152 intidx = -1;
8153 if (intidx < 0)
8154 {
8155 name = get_user_func_name(xp, idx);
8156 if (name != NULL)
8157 return name;
8158 }
8159 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8160 {
8161 STRCPY(IObuff, functions[intidx].f_name);
8162 STRCAT(IObuff, "(");
8163 if (functions[intidx].f_max_argc == 0)
8164 STRCAT(IObuff, ")");
8165 return IObuff;
8166 }
8167
8168 return NULL;
8169}
8170
8171/*
8172 * Function given to ExpandGeneric() to obtain the list of internal or
8173 * user defined variable or function names.
8174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 char_u *
8176get_expr_name(xp, idx)
8177 expand_T *xp;
8178 int idx;
8179{
8180 static int intidx = -1;
8181 char_u *name;
8182
8183 if (idx == 0)
8184 intidx = -1;
8185 if (intidx < 0)
8186 {
8187 name = get_function_name(xp, idx);
8188 if (name != NULL)
8189 return name;
8190 }
8191 return get_user_var_name(xp, ++intidx);
8192}
8193
8194#endif /* FEAT_CMDL_COMPL */
8195
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008196#if defined(EBCDIC) || defined(PROTO)
8197/*
8198 * Compare struct fst by function name.
8199 */
8200 static int
8201compare_func_name(s1, s2)
8202 const void *s1;
8203 const void *s2;
8204{
8205 struct fst *p1 = (struct fst *)s1;
8206 struct fst *p2 = (struct fst *)s2;
8207
8208 return STRCMP(p1->f_name, p2->f_name);
8209}
8210
8211/*
8212 * Sort the function table by function name.
8213 * The sorting of the table above is ASCII dependant.
8214 * On machines using EBCDIC we have to sort it.
8215 */
8216 static void
8217sortFunctions()
8218{
8219 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8220
8221 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8222}
8223#endif
8224
8225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226/*
8227 * Find internal function in table above.
8228 * Return index, or -1 if not found
8229 */
8230 static int
8231find_internal_func(name)
8232 char_u *name; /* name of the function */
8233{
8234 int first = 0;
8235 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8236 int cmp;
8237 int x;
8238
8239 /*
8240 * Find the function name in the table. Binary search.
8241 */
8242 while (first <= last)
8243 {
8244 x = first + ((unsigned)(last - first) >> 1);
8245 cmp = STRCMP(name, functions[x].f_name);
8246 if (cmp < 0)
8247 last = x - 1;
8248 else if (cmp > 0)
8249 first = x + 1;
8250 else
8251 return x;
8252 }
8253 return -1;
8254}
8255
8256/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008257 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8258 * name it contains, otherwise return "name".
8259 */
8260 static char_u *
8261deref_func_name(name, lenp)
8262 char_u *name;
8263 int *lenp;
8264{
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008266 int cc;
8267
8268 cc = name[*lenp];
8269 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008270 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008273 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 {
8276 *lenp = 0;
8277 return (char_u *)""; /* just in case */
8278 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008279 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008280 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 }
8282
8283 return name;
8284}
8285
8286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 * Allocate a variable for the result of a function.
8288 * Return OK or FAIL.
8289 */
8290 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008291get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8292 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 char_u *name; /* name of the function */
8294 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 char_u **arg; /* argument, pointing to the '(' */
8297 linenr_T firstline; /* first line of range */
8298 linenr_T lastline; /* last line of range */
8299 int *doesrange; /* return: function handled range */
8300 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 char_u *argp;
8304 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008305 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 int argcount = 0; /* number of arguments found */
8307
8308 /*
8309 * Get the arguments.
8310 */
8311 argp = *arg;
8312 while (argcount < MAX_FUNC_ARGS)
8313 {
8314 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8315 if (*argp == ')' || *argp == ',' || *argp == NUL)
8316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8318 {
8319 ret = FAIL;
8320 break;
8321 }
8322 ++argcount;
8323 if (*argp != ',')
8324 break;
8325 }
8326 if (*argp == ')')
8327 ++argp;
8328 else
8329 ret = FAIL;
8330
8331 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008332 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 {
8336 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008337 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008339 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
8345 *arg = skipwhite(argp);
8346 return ret;
8347}
8348
8349
8350/*
8351 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008352 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008353 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 */
8355 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008356call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008357 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008358 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008362 typval_T *argvars; /* vars for arguments, must have "argcount"
8363 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 linenr_T firstline; /* first line of range */
8365 linenr_T lastline; /* last line of range */
8366 int *doesrange; /* return: function handled range */
8367 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371#define ERROR_UNKNOWN 0
8372#define ERROR_TOOMANY 1
8373#define ERROR_TOOFEW 2
8374#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008375#define ERROR_DICT 4
8376#define ERROR_NONE 5
8377#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 int error = ERROR_NONE;
8379 int i;
8380 int llen;
8381 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#define FLEN_FIXED 40
8383 char_u fname_buf[FLEN_FIXED + 1];
8384 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008385 char_u *name;
8386
8387 /* Make a copy of the name, if it comes from a funcref variable it could
8388 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008389 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008390 if (name == NULL)
8391 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392
8393 /*
8394 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8395 * Change <SNR>123_name() to K_SNR 123_name().
8396 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 llen = eval_fname_script(name);
8399 if (llen > 0)
8400 {
8401 fname_buf[0] = K_SPECIAL;
8402 fname_buf[1] = KS_EXTRA;
8403 fname_buf[2] = (int)KE_SNR;
8404 i = 3;
8405 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8406 {
8407 if (current_SID <= 0)
8408 error = ERROR_SCRIPT;
8409 else
8410 {
8411 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8412 i = (int)STRLEN(fname_buf);
8413 }
8414 }
8415 if (i + STRLEN(name + llen) < FLEN_FIXED)
8416 {
8417 STRCPY(fname_buf + i, name + llen);
8418 fname = fname_buf;
8419 }
8420 else
8421 {
8422 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8423 if (fname == NULL)
8424 error = ERROR_OTHER;
8425 else
8426 {
8427 mch_memmove(fname, fname_buf, (size_t)i);
8428 STRCPY(fname + i, name + llen);
8429 }
8430 }
8431 }
8432 else
8433 fname = name;
8434
8435 *doesrange = FALSE;
8436
8437
8438 /* execute the function if no errors detected and executing */
8439 if (evaluate && error == ERROR_NONE)
8440 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008441 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8442 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 error = ERROR_UNKNOWN;
8444
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008445 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
8447 /*
8448 * User defined function.
8449 */
8450 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453 /* Trigger FuncUndefined event, may load the function. */
8454 if (fp == NULL
8455 && apply_autocmds(EVENT_FUNCUNDEFINED,
8456 fname, fname, TRUE, NULL)
8457 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008459 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 fp = find_func(fname);
8461 }
8462#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008464 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 {
8466 /* loaded a package, search for the function again */
8467 fp = find_func(fname);
8468 }
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (fp != NULL)
8471 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008472 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008474 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008478 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 else
8481 {
8482 /*
8483 * Call the user function.
8484 * Save and restore search patterns, script variables and
8485 * redo buffer.
8486 */
8487 save_search_patterns();
8488 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008492 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8493 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8494 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008495 /* Function was unreferenced while being used, free it
8496 * now. */
8497 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 restoreRedobuff();
8499 restore_search_patterns();
8500 error = ERROR_NONE;
8501 }
8502 }
8503 }
8504 else
8505 {
8506 /*
8507 * Find the function name in the table, call its implementation.
8508 */
8509 i = find_internal_func(fname);
8510 if (i >= 0)
8511 {
8512 if (argcount < functions[i].f_min_argc)
8513 error = ERROR_TOOFEW;
8514 else if (argcount > functions[i].f_max_argc)
8515 error = ERROR_TOOMANY;
8516 else
8517 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 error = ERROR_NONE;
8521 }
8522 }
8523 }
8524 /*
8525 * The function call (or "FuncUndefined" autocommand sequence) might
8526 * have been aborted by an error, an interrupt, or an explicitly thrown
8527 * exception that has not been caught so far. This situation can be
8528 * tested for by calling aborting(). For an error in an internal
8529 * function or for the "E132" error in call_user_func(), however, the
8530 * throw point at which the "force_abort" flag (temporarily reset by
8531 * emsg()) is normally updated has not been reached yet. We need to
8532 * update that flag first to make aborting() reliable.
8533 */
8534 update_force_abort();
8535 }
8536 if (error == ERROR_NONE)
8537 ret = OK;
8538
8539 /*
8540 * Report an error unless the argument evaluation or function call has been
8541 * cancelled due to an aborting error, an interrupt, or an exception.
8542 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008543 if (!aborting())
8544 {
8545 switch (error)
8546 {
8547 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008549 break;
8550 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008551 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008552 break;
8553 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008554 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 name);
8556 break;
8557 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008558 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 name);
8560 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008562 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008563 name);
8564 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008565 }
8566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (fname != name && fname != fname_buf)
8569 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008570 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
8572 return ret;
8573}
8574
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008575/*
8576 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008577 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008578 */
8579 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008580emsg_funcname(ermsg, name)
8581 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008582 char_u *name;
8583{
8584 char_u *p;
8585
8586 if (*name == K_SPECIAL)
8587 p = concat_str((char_u *)"<SNR>", name + 3);
8588 else
8589 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008590 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008591 if (p != name)
8592 vim_free(p);
8593}
8594
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008595/*
8596 * Return TRUE for a non-zero Number and a non-empty String.
8597 */
8598 static int
8599non_zero_arg(argvars)
8600 typval_T *argvars;
8601{
8602 return ((argvars[0].v_type == VAR_NUMBER
8603 && argvars[0].vval.v_number != 0)
8604 || (argvars[0].v_type == VAR_STRING
8605 && argvars[0].vval.v_string != NULL
8606 && *argvars[0].vval.v_string != NUL));
8607}
8608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609/*********************************************
8610 * Implementation of the built-in functions
8611 */
8612
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008613#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008614static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8615
8616/*
8617 * Get the float value of "argvars[0]" into "f".
8618 * Returns FAIL when the argument is not a Number or Float.
8619 */
8620 static int
8621get_float_arg(argvars, f)
8622 typval_T *argvars;
8623 float_T *f;
8624{
8625 if (argvars[0].v_type == VAR_FLOAT)
8626 {
8627 *f = argvars[0].vval.v_float;
8628 return OK;
8629 }
8630 if (argvars[0].v_type == VAR_NUMBER)
8631 {
8632 *f = (float_T)argvars[0].vval.v_number;
8633 return OK;
8634 }
8635 EMSG(_("E808: Number or Float required"));
8636 return FAIL;
8637}
8638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008639/*
8640 * "abs(expr)" function
8641 */
8642 static void
8643f_abs(argvars, rettv)
8644 typval_T *argvars;
8645 typval_T *rettv;
8646{
8647 if (argvars[0].v_type == VAR_FLOAT)
8648 {
8649 rettv->v_type = VAR_FLOAT;
8650 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8651 }
8652 else
8653 {
8654 varnumber_T n;
8655 int error = FALSE;
8656
8657 n = get_tv_number_chk(&argvars[0], &error);
8658 if (error)
8659 rettv->vval.v_number = -1;
8660 else if (n > 0)
8661 rettv->vval.v_number = n;
8662 else
8663 rettv->vval.v_number = -n;
8664 }
8665}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008666
8667/*
8668 * "acos()" function
8669 */
8670 static void
8671f_acos(argvars, rettv)
8672 typval_T *argvars;
8673 typval_T *rettv;
8674{
8675 float_T f;
8676
8677 rettv->v_type = VAR_FLOAT;
8678 if (get_float_arg(argvars, &f) == OK)
8679 rettv->vval.v_float = acos(f);
8680 else
8681 rettv->vval.v_float = 0.0;
8682}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683#endif
8684
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008686 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 */
8688 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008689f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 typval_T *argvars;
8691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008696 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008698 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008699 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008700 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008702 }
8703 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008704 EMSG(_(e_listreq));
8705}
8706
8707/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008708 * "and(expr, expr)" function
8709 */
8710 static void
8711f_and(argvars, rettv)
8712 typval_T *argvars;
8713 typval_T *rettv;
8714{
8715 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8716 & get_tv_number_chk(&argvars[1], NULL);
8717}
8718
8719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 * "append(lnum, string/list)" function
8721 */
8722 static void
8723f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008726{
8727 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 list_T *l = NULL;
8730 listitem_T *li = NULL;
8731 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 long added = 0;
8733
Bram Moolenaar0d660222005-01-07 21:51:51 +00008734 lnum = get_tv_lnum(argvars);
8735 if (lnum >= 0
8736 && lnum <= curbuf->b_ml.ml_line_count
8737 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008738 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008739 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008740 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008741 l = argvars[1].vval.v_list;
8742 if (l == NULL)
8743 return;
8744 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008745 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746 for (;;)
8747 {
8748 if (l == NULL)
8749 tv = &argvars[1]; /* append a string */
8750 else if (li == NULL)
8751 break; /* end of list */
8752 else
8753 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008754 line = get_tv_string_chk(tv);
8755 if (line == NULL) /* type error */
8756 {
8757 rettv->vval.v_number = 1; /* Failed */
8758 break;
8759 }
8760 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008761 ++added;
8762 if (l == NULL)
8763 break;
8764 li = li->li_next;
8765 }
8766
8767 appended_lines_mark(lnum, added);
8768 if (curwin->w_cursor.lnum > lnum)
8769 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 else
8772 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773}
8774
8775/*
8776 * "argc()" function
8777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784}
8785
8786/*
8787 * "argidx()" function
8788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795}
8796
8797/*
8798 * "argv(nr)" function
8799 */
8800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 typval_T *argvars;
8803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 int idx;
8806
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008807 if (argvars[0].v_type != VAR_UNKNOWN)
8808 {
8809 idx = get_tv_number_chk(&argvars[0], NULL);
8810 if (idx >= 0 && idx < ARGCOUNT)
8811 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8812 else
8813 rettv->vval.v_string = NULL;
8814 rettv->v_type = VAR_STRING;
8815 }
8816 else if (rettv_list_alloc(rettv) == OK)
8817 for (idx = 0; idx < ARGCOUNT; ++idx)
8818 list_append_string(rettv->vval.v_list,
8819 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820}
8821
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008822#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008823/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008824 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008825 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008826 static void
8827f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008828 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008829 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008830{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008831 float_T f;
8832
8833 rettv->v_type = VAR_FLOAT;
8834 if (get_float_arg(argvars, &f) == OK)
8835 rettv->vval.v_float = asin(f);
8836 else
8837 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008838}
8839
8840/*
8841 * "atan()" function
8842 */
8843 static void
8844f_atan(argvars, rettv)
8845 typval_T *argvars;
8846 typval_T *rettv;
8847{
8848 float_T f;
8849
8850 rettv->v_type = VAR_FLOAT;
8851 if (get_float_arg(argvars, &f) == OK)
8852 rettv->vval.v_float = atan(f);
8853 else
8854 rettv->vval.v_float = 0.0;
8855}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008856
8857/*
8858 * "atan2()" function
8859 */
8860 static void
8861f_atan2(argvars, rettv)
8862 typval_T *argvars;
8863 typval_T *rettv;
8864{
8865 float_T fx, fy;
8866
8867 rettv->v_type = VAR_FLOAT;
8868 if (get_float_arg(argvars, &fx) == OK
8869 && get_float_arg(&argvars[1], &fy) == OK)
8870 rettv->vval.v_float = atan2(fx, fy);
8871 else
8872 rettv->vval.v_float = 0.0;
8873}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008874#endif
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876/*
8877 * "browse(save, title, initdir, default)" function
8878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008881 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884#ifdef FEAT_BROWSE
8885 int save;
8886 char_u *title;
8887 char_u *initdir;
8888 char_u *defname;
8889 char_u buf[NUMBUFLEN];
8890 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 save = get_tv_number_chk(&argvars[0], &error);
8894 title = get_tv_string_chk(&argvars[1]);
8895 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8896 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008898 if (error || title == NULL || initdir == NULL || defname == NULL)
8899 rettv->vval.v_string = NULL;
8900 else
8901 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008902 do_browse(save ? BROWSE_SAVE : 0,
8903 title, defname, NULL, initdir, NULL, curbuf);
8904#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008906#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008908}
8909
8910/*
8911 * "browsedir(title, initdir)" function
8912 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008917{
8918#ifdef FEAT_BROWSE
8919 char_u *title;
8920 char_u *initdir;
8921 char_u buf[NUMBUFLEN];
8922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 title = get_tv_string_chk(&argvars[0]);
8924 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008926 if (title == NULL || initdir == NULL)
8927 rettv->vval.v_string = NULL;
8928 else
8929 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008930 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935}
8936
Bram Moolenaar33570922005-01-25 22:26:29 +00008937static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939/*
8940 * Find a buffer by number or exact name.
8941 */
8942 static buf_T *
8943find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
8946 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008948 if (avar->v_type == VAR_NUMBER)
8949 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008950 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008952 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008953 if (buf == NULL)
8954 {
8955 /* No full path name match, try a match with a URL or a "nofile"
8956 * buffer, these don't use the full path. */
8957 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8958 if (buf->b_fname != NULL
8959 && (path_with_url(buf->b_fname)
8960#ifdef FEAT_QUICKFIX
8961 || bt_nofile(buf)
8962#endif
8963 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008965 break;
8966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
8968 return buf;
8969}
8970
8971/*
8972 * "bufexists(expr)" function
8973 */
8974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *argvars;
8977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
8981
8982/*
8983 * "buflisted(expr)" function
8984 */
8985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 buf_T *buf;
8991
8992 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994}
8995
8996/*
8997 * "bufloaded(expr)" function
8998 */
8999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 buf_T *buf;
9005
9006 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008}
9009
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009010static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012/*
9013 * Get buffer by number or pattern.
9014 */
9015 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009016get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009018 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 int save_magic;
9022 char_u *save_cpo;
9023 buf_T *buf;
9024
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 if (tv->v_type == VAR_NUMBER)
9026 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009027 if (tv->v_type != VAR_STRING)
9028 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 if (name == NULL || *name == NUL)
9030 return curbuf;
9031 if (name[0] == '$' && name[1] == NUL)
9032 return lastbuf;
9033
9034 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9035 save_magic = p_magic;
9036 p_magic = TRUE;
9037 save_cpo = p_cpo;
9038 p_cpo = (char_u *)"";
9039
9040 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009041 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
9043 p_magic = save_magic;
9044 p_cpo = save_cpo;
9045
9046 /* If not found, try expanding the name, like done for bufexists(). */
9047 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049
9050 return buf;
9051}
9052
9053/*
9054 * "bufname(expr)" function
9055 */
9056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009058 typval_T *argvars;
9059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
9061 buf_T *buf;
9062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009063 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009065 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 --emsg_off;
9072}
9073
9074/*
9075 * "bufnr(expr)" function
9076 */
9077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 typval_T *argvars;
9080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009083 int error = FALSE;
9084 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009086 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009088 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009089 --emsg_off;
9090
9091 /* If the buffer isn't found and the second argument is not zero create a
9092 * new buffer. */
9093 if (buf == NULL
9094 && argvars[1].v_type != VAR_UNKNOWN
9095 && get_tv_number_chk(&argvars[1], &error) != 0
9096 && !error
9097 && (name = get_tv_string_chk(&argvars[0])) != NULL
9098 && !error)
9099 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9100
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105}
9106
9107/*
9108 * "bufwinnr(nr)" function
9109 */
9110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 typval_T *argvars;
9113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114{
9115#ifdef FEAT_WINDOWS
9116 win_T *wp;
9117 int winnr = 0;
9118#endif
9119 buf_T *buf;
9120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009123 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124#ifdef FEAT_WINDOWS
9125 for (wp = firstwin; wp; wp = wp->w_next)
9126 {
9127 ++winnr;
9128 if (wp->w_buffer == buf)
9129 break;
9130 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134#endif
9135 --emsg_off;
9136}
9137
9138/*
9139 * "byte2line(byte)" function
9140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009143 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148#else
9149 long boff = 0;
9150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 (linenr_T)0, &boff);
9157#endif
9158}
9159
9160/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009161 * "byteidx()" function
9162 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 typval_T *argvars;
9166 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009167{
9168#ifdef FEAT_MBYTE
9169 char_u *t;
9170#endif
9171 char_u *str;
9172 long idx;
9173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174 str = get_tv_string_chk(&argvars[0]);
9175 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009177 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009178 return;
9179
9180#ifdef FEAT_MBYTE
9181 t = str;
9182 for ( ; idx > 0; idx--)
9183 {
9184 if (*t == NUL) /* EOL reached */
9185 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009186 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009187 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009188 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009189#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009190 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009192#endif
9193}
9194
Bram Moolenaardb913952012-06-29 12:54:53 +02009195 int
9196func_call(name, args, selfdict, rettv)
9197 char_u *name;
9198 typval_T *args;
9199 dict_T *selfdict;
9200 typval_T *rettv;
9201{
9202 listitem_T *item;
9203 typval_T argv[MAX_FUNC_ARGS + 1];
9204 int argc = 0;
9205 int dummy;
9206 int r = 0;
9207
9208 for (item = args->vval.v_list->lv_first; item != NULL;
9209 item = item->li_next)
9210 {
9211 if (argc == MAX_FUNC_ARGS)
9212 {
9213 EMSG(_("E699: Too many arguments"));
9214 break;
9215 }
9216 /* Make a copy of each argument. This is needed to be able to set
9217 * v_lock to VAR_FIXED in the copy without changing the original list.
9218 */
9219 copy_tv(&item->li_tv, &argv[argc++]);
9220 }
9221
9222 if (item == NULL)
9223 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9224 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9225 &dummy, TRUE, selfdict);
9226
9227 /* Free the arguments. */
9228 while (argc > 0)
9229 clear_tv(&argv[--argc]);
9230
9231 return r;
9232}
9233
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009234/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009235 * "call(func, arglist)" function
9236 */
9237 static void
9238f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009239 typval_T *argvars;
9240 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009241{
9242 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009243 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009244
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009245 if (argvars[1].v_type != VAR_LIST)
9246 {
9247 EMSG(_(e_listreq));
9248 return;
9249 }
9250 if (argvars[1].vval.v_list == NULL)
9251 return;
9252
9253 if (argvars[0].v_type == VAR_FUNC)
9254 func = argvars[0].vval.v_string;
9255 else
9256 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 if (*func == NUL)
9258 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009259
Bram Moolenaare9a41262005-01-15 22:18:47 +00009260 if (argvars[2].v_type != VAR_UNKNOWN)
9261 {
9262 if (argvars[2].v_type != VAR_DICT)
9263 {
9264 EMSG(_(e_dictreq));
9265 return;
9266 }
9267 selfdict = argvars[2].vval.v_dict;
9268 }
9269
Bram Moolenaardb913952012-06-29 12:54:53 +02009270 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009271}
9272
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009273#ifdef FEAT_FLOAT
9274/*
9275 * "ceil({float})" function
9276 */
9277 static void
9278f_ceil(argvars, rettv)
9279 typval_T *argvars;
9280 typval_T *rettv;
9281{
9282 float_T f;
9283
9284 rettv->v_type = VAR_FLOAT;
9285 if (get_float_arg(argvars, &f) == OK)
9286 rettv->vval.v_float = ceil(f);
9287 else
9288 rettv->vval.v_float = 0.0;
9289}
9290#endif
9291
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009292/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009293 * "changenr()" function
9294 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009295 static void
9296f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009297 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009298 typval_T *rettv;
9299{
9300 rettv->vval.v_number = curbuf->b_u_seq_cur;
9301}
9302
9303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 * "char2nr(string)" function
9305 */
9306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009308 typval_T *argvars;
9309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311#ifdef FEAT_MBYTE
9312 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009313 {
9314 int utf8 = 0;
9315
9316 if (argvars[1].v_type != VAR_UNKNOWN)
9317 utf8 = get_tv_number_chk(&argvars[1], NULL);
9318
9319 if (utf8)
9320 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9321 else
9322 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 else
9325#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327}
9328
9329/*
9330 * "cindent(lnum)" function
9331 */
9332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009334 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336{
9337#ifdef FEAT_CINDENT
9338 pos_T pos;
9339 linenr_T lnum;
9340
9341 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9344 {
9345 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 curwin->w_cursor = pos;
9348 }
9349 else
9350#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352}
9353
9354/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009355 * "clearmatches()" function
9356 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009357 static void
9358f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009359 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009360 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009361{
9362#ifdef FEAT_SEARCH_EXTRA
9363 clear_matches(curwin);
9364#endif
9365}
9366
9367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 * "col(string)" function
9369 */
9370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 typval_T *argvars;
9373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375 colnr_T col = 0;
9376 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009377 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009379 fp = var2fpos(&argvars[0], FALSE, &fnum);
9380 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 {
9382 if (fp->col == MAXCOL)
9383 {
9384 /* '> can be MAXCOL, get the length of the line then */
9385 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009386 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 else
9388 col = MAXCOL;
9389 }
9390 else
9391 {
9392 col = fp->col + 1;
9393#ifdef FEAT_VIRTUALEDIT
9394 /* col(".") when the cursor is on the NUL at the end of the line
9395 * because of "coladd" can be seen as an extra column. */
9396 if (virtual_active() && fp == &curwin->w_cursor)
9397 {
9398 char_u *p = ml_get_cursor();
9399
9400 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9401 curwin->w_virtcol - curwin->w_cursor.coladd))
9402 {
9403# ifdef FEAT_MBYTE
9404 int l;
9405
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009406 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 col += l;
9408# else
9409 if (*p != NUL && p[1] == NUL)
9410 ++col;
9411# endif
9412 }
9413 }
9414#endif
9415 }
9416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418}
9419
Bram Moolenaar572cb562005-08-05 21:35:02 +00009420#if defined(FEAT_INS_EXPAND)
9421/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009422 * "complete()" function
9423 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009424 static void
9425f_complete(argvars, rettv)
9426 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009427 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009428{
9429 int startcol;
9430
9431 if ((State & INSERT) == 0)
9432 {
9433 EMSG(_("E785: complete() can only be used in Insert mode"));
9434 return;
9435 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009436
9437 /* Check for undo allowed here, because if something was already inserted
9438 * the line was already saved for undo and this check isn't done. */
9439 if (!undo_allowed())
9440 return;
9441
Bram Moolenaarade00832006-03-10 21:46:58 +00009442 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9443 {
9444 EMSG(_(e_invarg));
9445 return;
9446 }
9447
9448 startcol = get_tv_number_chk(&argvars[0], NULL);
9449 if (startcol <= 0)
9450 return;
9451
9452 set_completion(startcol - 1, argvars[1].vval.v_list);
9453}
9454
9455/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009456 * "complete_add()" function
9457 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009458 static void
9459f_complete_add(argvars, rettv)
9460 typval_T *argvars;
9461 typval_T *rettv;
9462{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009463 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009464}
9465
9466/*
9467 * "complete_check()" function
9468 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009469 static void
9470f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009471 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009472 typval_T *rettv;
9473{
9474 int saved = RedrawingDisabled;
9475
9476 RedrawingDisabled = 0;
9477 ins_compl_check_keys(0);
9478 rettv->vval.v_number = compl_interrupted;
9479 RedrawingDisabled = saved;
9480}
9481#endif
9482
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483/*
9484 * "confirm(message, buttons[, default [, type]])" function
9485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009488 typval_T *argvars UNUSED;
9489 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9492 char_u *message;
9493 char_u *buttons = NULL;
9494 char_u buf[NUMBUFLEN];
9495 char_u buf2[NUMBUFLEN];
9496 int def = 1;
9497 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 char_u *typestr;
9499 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009501 message = get_tv_string_chk(&argvars[0]);
9502 if (message == NULL)
9503 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009504 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9507 if (buttons == NULL)
9508 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009509 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009511 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9515 if (typestr == NULL)
9516 error = TRUE;
9517 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 switch (TOUPPER_ASC(*typestr))
9520 {
9521 case 'E': type = VIM_ERROR; break;
9522 case 'Q': type = VIM_QUESTION; break;
9523 case 'I': type = VIM_INFO; break;
9524 case 'W': type = VIM_WARNING; break;
9525 case 'G': type = VIM_GENERIC; break;
9526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 }
9528 }
9529 }
9530 }
9531
9532 if (buttons == NULL || *buttons == NUL)
9533 buttons = (char_u *)_("&Ok");
9534
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009535 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009536 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009537 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538#endif
9539}
9540
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009541/*
9542 * "copy()" function
9543 */
9544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 typval_T *argvars;
9547 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009548{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009549 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009552#ifdef FEAT_FLOAT
9553/*
9554 * "cos()" function
9555 */
9556 static void
9557f_cos(argvars, rettv)
9558 typval_T *argvars;
9559 typval_T *rettv;
9560{
9561 float_T f;
9562
9563 rettv->v_type = VAR_FLOAT;
9564 if (get_float_arg(argvars, &f) == OK)
9565 rettv->vval.v_float = cos(f);
9566 else
9567 rettv->vval.v_float = 0.0;
9568}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009569
9570/*
9571 * "cosh()" function
9572 */
9573 static void
9574f_cosh(argvars, rettv)
9575 typval_T *argvars;
9576 typval_T *rettv;
9577{
9578 float_T f;
9579
9580 rettv->v_type = VAR_FLOAT;
9581 if (get_float_arg(argvars, &f) == OK)
9582 rettv->vval.v_float = cosh(f);
9583 else
9584 rettv->vval.v_float = 0.0;
9585}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009586#endif
9587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009589 * "count()" function
9590 */
9591 static void
9592f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009593 typval_T *argvars;
9594 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009595{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009596 long n = 0;
9597 int ic = FALSE;
9598
Bram Moolenaare9a41262005-01-15 22:18:47 +00009599 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009601 listitem_T *li;
9602 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009603 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009604
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 if ((l = argvars[0].vval.v_list) != NULL)
9606 {
9607 li = l->lv_first;
9608 if (argvars[2].v_type != VAR_UNKNOWN)
9609 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009610 int error = FALSE;
9611
9612 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 if (argvars[3].v_type != VAR_UNKNOWN)
9614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009615 idx = get_tv_number_chk(&argvars[3], &error);
9616 if (!error)
9617 {
9618 li = list_find(l, idx);
9619 if (li == NULL)
9620 EMSGN(_(e_listidx), idx);
9621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 if (error)
9624 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 }
9626
9627 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009628 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 ++n;
9630 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009631 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 else if (argvars[0].v_type == VAR_DICT)
9633 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009634 int todo;
9635 dict_T *d;
9636 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009637
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009638 if ((d = argvars[0].vval.v_dict) != NULL)
9639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 int error = FALSE;
9641
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 if (argvars[2].v_type != VAR_UNKNOWN)
9643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009644 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 if (argvars[3].v_type != VAR_UNKNOWN)
9646 EMSG(_(e_invarg));
9647 }
9648
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009649 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009651 {
9652 if (!HASHITEM_EMPTY(hi))
9653 {
9654 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009655 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009656 ++n;
9657 }
9658 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009659 }
9660 }
9661 else
9662 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009663 rettv->vval.v_number = n;
9664}
9665
9666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9668 *
9669 * Checks the existence of a cscope connection.
9670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009673 typval_T *argvars UNUSED;
9674 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675{
9676#ifdef FEAT_CSCOPE
9677 int num = 0;
9678 char_u *dbpath = NULL;
9679 char_u *prepend = NULL;
9680 char_u buf[NUMBUFLEN];
9681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009682 if (argvars[0].v_type != VAR_UNKNOWN
9683 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 num = (int)get_tv_number(&argvars[0]);
9686 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009687 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 }
9690
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692#endif
9693}
9694
9695/*
9696 * "cursor(lnum, col)" function
9697 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009698 * Moves the cursor to the specified line and column.
9699 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009703 typval_T *argvars;
9704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009707#ifdef FEAT_VIRTUALEDIT
9708 long coladd = 0;
9709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009711 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009712 if (argvars[1].v_type == VAR_UNKNOWN)
9713 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009714 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009715
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009716 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009717 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009718 line = pos.lnum;
9719 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009720#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009721 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009722#endif
9723 }
9724 else
9725 {
9726 line = get_tv_lnum(argvars);
9727 col = get_tv_number_chk(&argvars[1], NULL);
9728#ifdef FEAT_VIRTUALEDIT
9729 if (argvars[2].v_type != VAR_UNKNOWN)
9730 coladd = get_tv_number_chk(&argvars[2], NULL);
9731#endif
9732 }
9733 if (line < 0 || col < 0
9734#ifdef FEAT_VIRTUALEDIT
9735 || coladd < 0
9736#endif
9737 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009738 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (line > 0)
9740 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (col > 0)
9742 curwin->w_cursor.col = col - 1;
9743#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009744 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745#endif
9746
9747 /* Make sure the cursor is in a valid position. */
9748 check_cursor();
9749#ifdef FEAT_MBYTE
9750 /* Correct cursor for multi-byte character. */
9751 if (has_mbyte)
9752 mb_adjust_cursor();
9753#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009754
9755 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009756 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009760 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 */
9762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009767 int noref = 0;
9768
9769 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009770 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009771 if (noref < 0 || noref > 1)
9772 EMSG(_(e_invarg));
9773 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009774 {
9775 current_copyID += COPYID_INC;
9776 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778}
9779
9780/*
9781 * "delete()" function
9782 */
9783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009785 typval_T *argvars;
9786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794/*
9795 * "did_filetype()" function
9796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009799 typval_T *argvars UNUSED;
9800 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804#endif
9805}
9806
9807/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009808 * "diff_filler()" function
9809 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009812 typval_T *argvars UNUSED;
9813 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009814{
9815#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009817#endif
9818}
9819
9820/*
9821 * "diff_hlID()" function
9822 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009824f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009825 typval_T *argvars UNUSED;
9826 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009827{
9828#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009830 static linenr_T prev_lnum = 0;
9831 static int changedtick = 0;
9832 static int fnum = 0;
9833 static int change_start = 0;
9834 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009835 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009836 int filler_lines;
9837 int col;
9838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 if (lnum < 0) /* ignore type error in {lnum} arg */
9840 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009841 if (lnum != prev_lnum
9842 || changedtick != curbuf->b_changedtick
9843 || fnum != curbuf->b_fnum)
9844 {
9845 /* New line, buffer, change: need to get the values. */
9846 filler_lines = diff_check(curwin, lnum);
9847 if (filler_lines < 0)
9848 {
9849 if (filler_lines == -1)
9850 {
9851 change_start = MAXCOL;
9852 change_end = -1;
9853 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9854 hlID = HLF_ADD; /* added line */
9855 else
9856 hlID = HLF_CHD; /* changed line */
9857 }
9858 else
9859 hlID = HLF_ADD; /* added line */
9860 }
9861 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009862 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009863 prev_lnum = lnum;
9864 changedtick = curbuf->b_changedtick;
9865 fnum = curbuf->b_fnum;
9866 }
9867
9868 if (hlID == HLF_CHD || hlID == HLF_TXD)
9869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009870 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009871 if (col >= change_start && col <= change_end)
9872 hlID = HLF_TXD; /* changed text */
9873 else
9874 hlID = HLF_CHD; /* changed line */
9875 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009876 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009877#endif
9878}
9879
9880/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009881 * "empty({expr})" function
9882 */
9883 static void
9884f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009885 typval_T *argvars;
9886 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009887{
9888 int n;
9889
9890 switch (argvars[0].v_type)
9891 {
9892 case VAR_STRING:
9893 case VAR_FUNC:
9894 n = argvars[0].vval.v_string == NULL
9895 || *argvars[0].vval.v_string == NUL;
9896 break;
9897 case VAR_NUMBER:
9898 n = argvars[0].vval.v_number == 0;
9899 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009900#ifdef FEAT_FLOAT
9901 case VAR_FLOAT:
9902 n = argvars[0].vval.v_float == 0.0;
9903 break;
9904#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009905 case VAR_LIST:
9906 n = argvars[0].vval.v_list == NULL
9907 || argvars[0].vval.v_list->lv_first == NULL;
9908 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909 case VAR_DICT:
9910 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009913 default:
9914 EMSG2(_(e_intern2), "f_empty()");
9915 n = 0;
9916 }
9917
9918 rettv->vval.v_number = n;
9919}
9920
9921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 * "escape({string}, {chars})" function
9923 */
9924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009926 typval_T *argvars;
9927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
9929 char_u buf[NUMBUFLEN];
9930
Bram Moolenaar758711c2005-02-02 23:11:38 +00009931 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9932 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934}
9935
9936/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009937 * "eval()" function
9938 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009939 static void
9940f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009941 typval_T *argvars;
9942 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009943{
9944 char_u *s;
9945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 s = get_tv_string_chk(&argvars[0]);
9947 if (s != NULL)
9948 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009950 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9951 {
9952 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009953 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009955 else if (*s != NUL)
9956 EMSG(_(e_trailing));
9957}
9958
9959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 * "eventhandler()" function
9961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009964 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968}
9969
9970/*
9971 * "executable()" function
9972 */
9973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 typval_T *argvars;
9976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
9981/*
9982 * "exists()" function
9983 */
9984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989 char_u *p;
9990 char_u *name;
9991 int n = FALSE;
9992 int len = 0;
9993
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009994 no_autoload = TRUE;
9995
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 if (*p == '$') /* environment variable */
9998 {
9999 /* first try "normal" environment variables (fast) */
10000 if (mch_getenv(p + 1) != NULL)
10001 n = TRUE;
10002 else
10003 {
10004 /* try expanding things like $VIM and ${HOME} */
10005 p = expand_env_save(p);
10006 if (p != NULL && *p != '$')
10007 n = TRUE;
10008 vim_free(p);
10009 }
10010 }
10011 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010014 if (*skipwhite(p) != NUL)
10015 n = FALSE; /* trailing garbage */
10016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 else if (*p == '*') /* internal or user defined function */
10018 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010019 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021 else if (*p == ':')
10022 {
10023 n = cmd_exists(p + 1);
10024 }
10025 else if (*p == '#')
10026 {
10027#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010028 if (p[1] == '#')
10029 n = autocmd_supported(p + 2);
10030 else
10031 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032#endif
10033 }
10034 else /* internal variable */
10035 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010036 char_u *tofree;
10037 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010039 /* get_name_len() takes care of expanding curly braces */
10040 name = p;
10041 len = get_name_len(&p, &tofree, TRUE, FALSE);
10042 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010044 if (tofree != NULL)
10045 name = tofree;
10046 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10047 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010049 /* handle d.key, l[idx], f(expr) */
10050 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10051 if (n)
10052 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010055 if (*p != NUL)
10056 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010058 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
10060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010062
10063 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064}
10065
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010066#ifdef FEAT_FLOAT
10067/*
10068 * "exp()" function
10069 */
10070 static void
10071f_exp(argvars, rettv)
10072 typval_T *argvars;
10073 typval_T *rettv;
10074{
10075 float_T f;
10076
10077 rettv->v_type = VAR_FLOAT;
10078 if (get_float_arg(argvars, &f) == OK)
10079 rettv->vval.v_float = exp(f);
10080 else
10081 rettv->vval.v_float = 0.0;
10082}
10083#endif
10084
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085/*
10086 * "expand()" function
10087 */
10088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010090 typval_T *argvars;
10091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
10093 char_u *s;
10094 int len;
10095 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010096 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010099 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010102 if (argvars[1].v_type != VAR_UNKNOWN
10103 && argvars[2].v_type != VAR_UNKNOWN
10104 && get_tv_number_chk(&argvars[2], &error)
10105 && !error)
10106 {
10107 rettv->v_type = VAR_LIST;
10108 rettv->vval.v_list = NULL;
10109 }
10110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 if (*s == '%' || *s == '#' || *s == '<')
10113 {
10114 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010115 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010117 if (rettv->v_type == VAR_LIST)
10118 {
10119 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10120 list_append_string(rettv->vval.v_list, result, -1);
10121 else
10122 vim_free(result);
10123 }
10124 else
10125 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 }
10127 else
10128 {
10129 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010130 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 if (argvars[1].v_type != VAR_UNKNOWN
10132 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010133 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 if (!error)
10135 {
10136 ExpandInit(&xpc);
10137 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010138 if (p_wic)
10139 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010140 if (rettv->v_type == VAR_STRING)
10141 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10142 options, WILD_ALL);
10143 else if (rettv_list_alloc(rettv) != FAIL)
10144 {
10145 int i;
10146
10147 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10148 for (i = 0; i < xpc.xp_numfiles; i++)
10149 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10150 ExpandCleanup(&xpc);
10151 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152 }
10153 else
10154 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 }
10156}
10157
10158/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010159 * Go over all entries in "d2" and add them to "d1".
10160 * When "action" is "error" then a duplicate key is an error.
10161 * When "action" is "force" then a duplicate key is overwritten.
10162 * Otherwise duplicate keys are ignored ("action" is "keep").
10163 */
10164 void
10165dict_extend(d1, d2, action)
10166 dict_T *d1;
10167 dict_T *d2;
10168 char_u *action;
10169{
10170 dictitem_T *di1;
10171 hashitem_T *hi2;
10172 int todo;
10173
10174 todo = (int)d2->dv_hashtab.ht_used;
10175 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10176 {
10177 if (!HASHITEM_EMPTY(hi2))
10178 {
10179 --todo;
10180 di1 = dict_find(d1, hi2->hi_key, -1);
10181 if (d1->dv_scope != 0)
10182 {
10183 /* Disallow replacing a builtin function in l: and g:.
10184 * Check the key to be valid when adding to any
10185 * scope. */
10186 if (d1->dv_scope == VAR_DEF_SCOPE
10187 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10188 && var_check_func_name(hi2->hi_key,
10189 di1 == NULL))
10190 break;
10191 if (!valid_varname(hi2->hi_key))
10192 break;
10193 }
10194 if (di1 == NULL)
10195 {
10196 di1 = dictitem_copy(HI2DI(hi2));
10197 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10198 dictitem_free(di1);
10199 }
10200 else if (*action == 'e')
10201 {
10202 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10203 break;
10204 }
10205 else if (*action == 'f' && HI2DI(hi2) != di1)
10206 {
10207 clear_tv(&di1->di_tv);
10208 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10209 }
10210 }
10211 }
10212}
10213
10214/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010215 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010217 */
10218 static void
10219f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010220 typval_T *argvars;
10221 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010222{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010223 char *arg_errmsg = N_("extend() argument");
10224
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010227 list_T *l1, *l2;
10228 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010231
Bram Moolenaare9a41262005-01-15 22:18:47 +000010232 l1 = argvars[0].vval.v_list;
10233 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010234 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010235 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 {
10237 if (argvars[2].v_type != VAR_UNKNOWN)
10238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 before = get_tv_number_chk(&argvars[2], &error);
10240 if (error)
10241 return; /* type error; errmsg already given */
10242
Bram Moolenaar758711c2005-02-02 23:11:38 +000010243 if (before == l1->lv_len)
10244 item = NULL;
10245 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010246 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010247 item = list_find(l1, before);
10248 if (item == NULL)
10249 {
10250 EMSGN(_(e_listidx), before);
10251 return;
10252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010253 }
10254 }
10255 else
10256 item = NULL;
10257 list_extend(l1, l2, item);
10258
Bram Moolenaare9a41262005-01-15 22:18:47 +000010259 copy_tv(&argvars[0], rettv);
10260 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010261 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10263 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010264 dict_T *d1, *d2;
10265 char_u *action;
10266 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267
10268 d1 = argvars[0].vval.v_dict;
10269 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010270 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010271 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 {
10273 /* Check the third argument. */
10274 if (argvars[2].v_type != VAR_UNKNOWN)
10275 {
10276 static char *(av[]) = {"keep", "force", "error"};
10277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 action = get_tv_string_chk(&argvars[2]);
10279 if (action == NULL)
10280 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 for (i = 0; i < 3; ++i)
10282 if (STRCMP(action, av[i]) == 0)
10283 break;
10284 if (i == 3)
10285 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010286 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010287 return;
10288 }
10289 }
10290 else
10291 action = (char_u *)"force";
10292
Bram Moolenaara9922d62013-05-30 13:01:18 +020010293 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295 copy_tv(&argvars[0], rettv);
10296 }
10297 }
10298 else
10299 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010300}
10301
10302/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010303 * "feedkeys()" function
10304 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010305 static void
10306f_feedkeys(argvars, rettv)
10307 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010308 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010309{
10310 int remap = TRUE;
10311 char_u *keys, *flags;
10312 char_u nbuf[NUMBUFLEN];
10313 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010314 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010315
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010316 /* This is not allowed in the sandbox. If the commands would still be
10317 * executed in the sandbox it would be OK, but it probably happens later,
10318 * when "sandbox" is no longer set. */
10319 if (check_secure())
10320 return;
10321
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010322 keys = get_tv_string(&argvars[0]);
10323 if (*keys != NUL)
10324 {
10325 if (argvars[1].v_type != VAR_UNKNOWN)
10326 {
10327 flags = get_tv_string_buf(&argvars[1], nbuf);
10328 for ( ; *flags != NUL; ++flags)
10329 {
10330 switch (*flags)
10331 {
10332 case 'n': remap = FALSE; break;
10333 case 'm': remap = TRUE; break;
10334 case 't': typed = TRUE; break;
10335 }
10336 }
10337 }
10338
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010339 /* Need to escape K_SPECIAL and CSI before putting the string in the
10340 * typeahead buffer. */
10341 keys_esc = vim_strsave_escape_csi(keys);
10342 if (keys_esc != NULL)
10343 {
10344 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010345 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010346 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010347 if (vgetc_busy)
10348 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010349 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010350 }
10351}
10352
10353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 * "filereadable()" function
10355 */
10356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 typval_T *argvars;
10359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010361 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 char_u *p;
10363 int n;
10364
Bram Moolenaarc236c162008-07-13 17:41:49 +000010365#ifndef O_NONBLOCK
10366# define O_NONBLOCK 0
10367#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010369 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10370 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 {
10372 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010373 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 }
10375 else
10376 n = FALSE;
10377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379}
10380
10381/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010382 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 * rights to write into.
10384 */
10385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010387 typval_T *argvars;
10388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010390 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391}
10392
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010393static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010394
10395 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010396findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010398 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010399 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010400{
10401#ifdef FEAT_SEARCHPATH
10402 char_u *fname;
10403 char_u *fresult = NULL;
10404 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10405 char_u *p;
10406 char_u pathbuf[NUMBUFLEN];
10407 int count = 1;
10408 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010409 int error = FALSE;
10410#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010411
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010412 rettv->vval.v_string = NULL;
10413 rettv->v_type = VAR_STRING;
10414
10415#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010417
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010418 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10421 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010422 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 else
10424 {
10425 if (*p != NUL)
10426 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010429 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010431 }
10432
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010433 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10434 error = TRUE;
10435
10436 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 do
10439 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010440 if (rettv->v_type == VAR_STRING)
10441 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 fresult = find_file_in_path_option(first ? fname : NULL,
10443 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010444 0, first, path,
10445 find_what,
10446 curbuf->b_ffname,
10447 find_what == FINDFILE_DIR
10448 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010450
10451 if (fresult != NULL && rettv->v_type == VAR_LIST)
10452 list_append_string(rettv->vval.v_list, fresult, -1);
10453
10454 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010456
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010457 if (rettv->v_type == VAR_STRING)
10458 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010459#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010460}
10461
Bram Moolenaar33570922005-01-25 22:26:29 +000010462static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10463static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010464
10465/*
10466 * Implementation of map() and filter().
10467 */
10468 static void
10469filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010470 typval_T *argvars;
10471 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010472 int map;
10473{
10474 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010475 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 listitem_T *li, *nli;
10477 list_T *l = NULL;
10478 dictitem_T *di;
10479 hashtab_T *ht;
10480 hashitem_T *hi;
10481 dict_T *d = NULL;
10482 typval_T save_val;
10483 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010485 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010486 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10487 char *arg_errmsg = (map ? N_("map() argument")
10488 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010489 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010490 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010491
Bram Moolenaare9a41262005-01-15 22:18:47 +000010492 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010493 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010494 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010495 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010496 return;
10497 }
10498 else if (argvars[0].v_type == VAR_DICT)
10499 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010500 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010501 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010502 return;
10503 }
10504 else
10505 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010506 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 return;
10508 }
10509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 expr = get_tv_string_buf_chk(&argvars[1], buf);
10511 /* On type errors, the preceding call has already displayed an error
10512 * message. Avoid a misleading error message for an empty string that
10513 * was not passed as argument. */
10514 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 prepare_vimvar(VV_VAL, &save_val);
10517 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010518
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010519 /* We reset "did_emsg" to be able to detect whether an error
10520 * occurred during evaluation of the expression. */
10521 save_did_emsg = did_emsg;
10522 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010523
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010524 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 vimvars[VV_KEY].vv_type = VAR_STRING;
10528
10529 ht = &d->dv_hashtab;
10530 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010531 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 if (!HASHITEM_EMPTY(hi))
10535 {
10536 --todo;
10537 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010538 if (tv_check_lock(di->di_tv.v_lock,
10539 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 break;
10541 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010542 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010543 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 break;
10545 if (!map && rem)
10546 dictitem_remove(d, di);
10547 clear_tv(&vimvars[VV_KEY].vv_tv);
10548 }
10549 }
10550 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 }
10552 else
10553 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010554 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 for (li = l->lv_first; li != NULL; li = nli)
10557 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010558 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010559 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010561 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010562 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010563 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010564 break;
10565 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010566 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010567 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010568 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010569 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010570
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010571 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010573
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010574 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010575 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576
10577 copy_tv(&argvars[0], rettv);
10578}
10579
10580 static int
10581filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010582 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 char_u *expr;
10584 int map;
10585 int *remp;
10586{
Bram Moolenaar33570922005-01-25 22:26:29 +000010587 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010589 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 s = expr;
10593 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010594 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010595 if (*s != NUL) /* check for trailing chars after expr */
10596 {
10597 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010598 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 }
10600 if (map)
10601 {
10602 /* map(): replace the list item value */
10603 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010604 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 *tv = rettv;
10606 }
10607 else
10608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 int error = FALSE;
10610
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 /* On type error, nothing has been removed; return FAIL to stop the
10615 * loop. The error message was given by get_tv_number_chk(). */
10616 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010617 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010619 retval = OK;
10620theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010621 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010622 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010623}
10624
10625/*
10626 * "filter()" function
10627 */
10628 static void
10629f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *argvars;
10631 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010632{
10633 filter_map(argvars, rettv, FALSE);
10634}
10635
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010637 * "finddir({fname}[, {path}[, {count}]])" function
10638 */
10639 static void
10640f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 typval_T *argvars;
10642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010643{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010644 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010645}
10646
10647/*
10648 * "findfile({fname}[, {path}[, {count}]])" function
10649 */
10650 static void
10651f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 typval_T *argvars;
10653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010654{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010655 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010656}
10657
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010658#ifdef FEAT_FLOAT
10659/*
10660 * "float2nr({float})" function
10661 */
10662 static void
10663f_float2nr(argvars, rettv)
10664 typval_T *argvars;
10665 typval_T *rettv;
10666{
10667 float_T f;
10668
10669 if (get_float_arg(argvars, &f) == OK)
10670 {
10671 if (f < -0x7fffffff)
10672 rettv->vval.v_number = -0x7fffffff;
10673 else if (f > 0x7fffffff)
10674 rettv->vval.v_number = 0x7fffffff;
10675 else
10676 rettv->vval.v_number = (varnumber_T)f;
10677 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010678}
10679
10680/*
10681 * "floor({float})" function
10682 */
10683 static void
10684f_floor(argvars, rettv)
10685 typval_T *argvars;
10686 typval_T *rettv;
10687{
10688 float_T f;
10689
10690 rettv->v_type = VAR_FLOAT;
10691 if (get_float_arg(argvars, &f) == OK)
10692 rettv->vval.v_float = floor(f);
10693 else
10694 rettv->vval.v_float = 0.0;
10695}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010696
10697/*
10698 * "fmod()" function
10699 */
10700 static void
10701f_fmod(argvars, rettv)
10702 typval_T *argvars;
10703 typval_T *rettv;
10704{
10705 float_T fx, fy;
10706
10707 rettv->v_type = VAR_FLOAT;
10708 if (get_float_arg(argvars, &fx) == OK
10709 && get_float_arg(&argvars[1], &fy) == OK)
10710 rettv->vval.v_float = fmod(fx, fy);
10711 else
10712 rettv->vval.v_float = 0.0;
10713}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010714#endif
10715
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010717 * "fnameescape({string})" function
10718 */
10719 static void
10720f_fnameescape(argvars, rettv)
10721 typval_T *argvars;
10722 typval_T *rettv;
10723{
10724 rettv->vval.v_string = vim_strsave_fnameescape(
10725 get_tv_string(&argvars[0]), FALSE);
10726 rettv->v_type = VAR_STRING;
10727}
10728
10729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 * "fnamemodify({fname}, {mods})" function
10731 */
10732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *argvars;
10735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
10737 char_u *fname;
10738 char_u *mods;
10739 int usedlen = 0;
10740 int len;
10741 char_u *fbuf = NULL;
10742 char_u buf[NUMBUFLEN];
10743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 fname = get_tv_string_chk(&argvars[0]);
10745 mods = get_tv_string_buf_chk(&argvars[1], buf);
10746 if (fname == NULL || mods == NULL)
10747 fname = NULL;
10748 else
10749 {
10750 len = (int)STRLEN(fname);
10751 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 vim_free(fbuf);
10760}
10761
Bram Moolenaar33570922005-01-25 22:26:29 +000010762static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763
10764/*
10765 * "foldclosed()" function
10766 */
10767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010769 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010770 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010771 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772{
10773#ifdef FEAT_FOLDING
10774 linenr_T lnum;
10775 linenr_T first, last;
10776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10779 {
10780 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10781 {
10782 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010785 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 return;
10787 }
10788 }
10789#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791}
10792
10793/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010794 * "foldclosed()" function
10795 */
10796 static void
10797f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010798 typval_T *argvars;
10799 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800{
10801 foldclosed_both(argvars, rettv, FALSE);
10802}
10803
10804/*
10805 * "foldclosedend()" function
10806 */
10807 static void
10808f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010809 typval_T *argvars;
10810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811{
10812 foldclosed_both(argvars, rettv, TRUE);
10813}
10814
10815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 * "foldlevel()" function
10817 */
10818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010820 typval_T *argvars UNUSED;
10821 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
10823#ifdef FEAT_FOLDING
10824 linenr_T lnum;
10825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830}
10831
10832/*
10833 * "foldtext()" function
10834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010837 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839{
10840#ifdef FEAT_FOLDING
10841 linenr_T lnum;
10842 char_u *s;
10843 char_u *r;
10844 int len;
10845 char *txt;
10846#endif
10847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848 rettv->v_type = VAR_STRING;
10849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10852 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10853 <= curbuf->b_ml.ml_line_count
10854 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 {
10856 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010857 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10858 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 {
10860 if (!linewhite(lnum))
10861 break;
10862 ++lnum;
10863 }
10864
10865 /* Find interesting text in this line. */
10866 s = skipwhite(ml_get(lnum));
10867 /* skip C comment-start */
10868 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010871 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010872 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010873 {
10874 s = skipwhite(ml_get(lnum + 1));
10875 if (*s == '*')
10876 s = skipwhite(s + 1);
10877 }
10878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 txt = _("+-%s%3ld lines: ");
10880 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010881 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 + 20 /* for %3ld */
10883 + STRLEN(s))); /* concatenated */
10884 if (r != NULL)
10885 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010886 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10887 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10888 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 len = (int)STRLEN(r);
10890 STRCAT(r, s);
10891 /* remove 'foldmarker' and 'commentstring' */
10892 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 }
10895 }
10896#endif
10897}
10898
10899/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010900 * "foldtextresult(lnum)" function
10901 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010906{
10907#ifdef FEAT_FOLDING
10908 linenr_T lnum;
10909 char_u *text;
10910 char_u buf[51];
10911 foldinfo_T foldinfo;
10912 int fold_count;
10913#endif
10914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->v_type = VAR_STRING;
10916 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010917#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 /* treat illegal types and illegal string values for {lnum} the same */
10920 if (lnum < 0)
10921 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010922 fold_count = foldedCount(curwin, lnum, &foldinfo);
10923 if (fold_count > 0)
10924 {
10925 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10926 &foldinfo, buf);
10927 if (text == buf)
10928 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010930 }
10931#endif
10932}
10933
10934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 * "foreground()" function
10936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010939 typval_T *argvars UNUSED;
10940 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942#ifdef FEAT_GUI
10943 if (gui.in_use)
10944 gui_mch_set_foreground();
10945#else
10946# ifdef WIN32
10947 win32_set_foreground();
10948# endif
10949#endif
10950}
10951
10952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010953 * "function()" function
10954 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010956f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 typval_T *argvars;
10958 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010959{
10960 char_u *s;
Bram Moolenaara1544c02013-05-30 12:35:52 +020010961 char_u *name = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010964 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010965 EMSG2(_(e_invarg2), s);
Bram Moolenaara1544c02013-05-30 12:35:52 +020010966 /* Don't check an autoload name for existence here, but still expand it
10967 * checking for validity */
10968 else if ((name = get_expanded_name(s, vim_strchr(s, AUTOLOAD_CHAR) == NULL))
10969 == NULL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010970 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010971 else
10972 {
Bram Moolenaara1544c02013-05-30 12:35:52 +020010973 if (name == NULL)
10974 /* Autoload function, need to copy string */
10975 rettv->vval.v_string = vim_strsave(s);
10976 else
10977 /* Function found by get_expanded_name, string allocated by
10978 * trans_function_name: no need to copy */
10979 rettv->vval.v_string = name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010981 }
10982}
10983
10984/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010985 * "garbagecollect()" function
10986 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010987 static void
10988f_garbagecollect(argvars, rettv)
10989 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010990 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010991{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010992 /* This is postponed until we are back at the toplevel, because we may be
10993 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10994 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010995
10996 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10997 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010998}
10999
11000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001 * "get()" function
11002 */
11003 static void
11004f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 typval_T *argvars;
11006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007{
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 listitem_T *li;
11009 list_T *l;
11010 dictitem_T *di;
11011 dict_T *d;
11012 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011013
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011015 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011018 int error = FALSE;
11019
11020 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11021 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011023 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011024 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011025 else if (argvars[0].v_type == VAR_DICT)
11026 {
11027 if ((d = argvars[0].vval.v_dict) != NULL)
11028 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011029 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011030 if (di != NULL)
11031 tv = &di->di_tv;
11032 }
11033 }
11034 else
11035 EMSG2(_(e_listdictarg), "get()");
11036
11037 if (tv == NULL)
11038 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011039 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011040 copy_tv(&argvars[2], rettv);
11041 }
11042 else
11043 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011044}
11045
Bram Moolenaar342337a2005-07-21 21:11:17 +000011046static 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 +000011047
11048/*
11049 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011050 * Return a range (from start to end) of lines in rettv from the specified
11051 * buffer.
11052 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011053 */
11054 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011055get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011056 buf_T *buf;
11057 linenr_T start;
11058 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011059 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011060 typval_T *rettv;
11061{
11062 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011063
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011064 if (retlist && rettv_list_alloc(rettv) == FAIL)
11065 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011066
11067 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11068 return;
11069
11070 if (!retlist)
11071 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011072 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11073 p = ml_get_buf(buf, start, FALSE);
11074 else
11075 p = (char_u *)"";
11076
11077 rettv->v_type = VAR_STRING;
11078 rettv->vval.v_string = vim_strsave(p);
11079 }
11080 else
11081 {
11082 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011083 return;
11084
11085 if (start < 1)
11086 start = 1;
11087 if (end > buf->b_ml.ml_line_count)
11088 end = buf->b_ml.ml_line_count;
11089 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011090 if (list_append_string(rettv->vval.v_list,
11091 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011092 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011093 }
11094}
11095
11096/*
11097 * "getbufline()" function
11098 */
11099 static void
11100f_getbufline(argvars, rettv)
11101 typval_T *argvars;
11102 typval_T *rettv;
11103{
11104 linenr_T lnum;
11105 linenr_T end;
11106 buf_T *buf;
11107
11108 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11109 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011110 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011111 --emsg_off;
11112
Bram Moolenaar661b1822005-07-28 22:36:45 +000011113 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011114 if (argvars[2].v_type == VAR_UNKNOWN)
11115 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011116 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011117 end = get_tv_lnum_buf(&argvars[2], buf);
11118
Bram Moolenaar342337a2005-07-21 21:11:17 +000011119 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011120}
11121
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122/*
11123 * "getbufvar()" function
11124 */
11125 static void
11126f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011127 typval_T *argvars;
11128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011129{
11130 buf_T *buf;
11131 buf_T *save_curbuf;
11132 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011134 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11137 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011138 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011139 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011140
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011141 rettv->v_type = VAR_STRING;
11142 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143
11144 if (buf != NULL && varname != NULL)
11145 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011146 /* set curbuf to be our buf, temporarily */
11147 save_curbuf = curbuf;
11148 curbuf = buf;
11149
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011151 {
11152 if (get_option_tv(&varname, rettv, TRUE) == OK)
11153 done = TRUE;
11154 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011155 else if (STRCMP(varname, "changedtick") == 0)
11156 {
11157 rettv->v_type = VAR_NUMBER;
11158 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011159 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161 else
11162 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011163 /* Look up the variable. */
11164 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11165 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11166 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011167 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011168 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011169 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011170 done = TRUE;
11171 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011172 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011173
11174 /* restore previous notion of curbuf */
11175 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176 }
11177
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011178 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11179 /* use the default value */
11180 copy_tv(&argvars[2], rettv);
11181
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182 --emsg_off;
11183}
11184
11185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 * "getchar()" function
11187 */
11188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *argvars;
11191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192{
11193 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011196 /* Position the cursor. Needed after a message that ends in a space. */
11197 windgoto(msg_row, msg_col);
11198
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 ++no_mapping;
11200 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011201 for (;;)
11202 {
11203 if (argvars[0].v_type == VAR_UNKNOWN)
11204 /* getchar(): blocking wait. */
11205 n = safe_vgetc();
11206 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11207 /* getchar(1): only check if char avail */
11208 n = vpeekc();
11209 else if (error || vpeekc() == NUL)
11210 /* illegal argument or getchar(0) and no char avail: return zero */
11211 n = 0;
11212 else
11213 /* getchar(0) and char avail: return char */
11214 n = safe_vgetc();
11215 if (n == K_IGNORE)
11216 continue;
11217 break;
11218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 --no_mapping;
11220 --allow_keys;
11221
Bram Moolenaar219b8702006-11-01 14:32:36 +000011222 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11223 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11224 vimvars[VV_MOUSE_COL].vv_nr = 0;
11225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 if (IS_SPECIAL(n) || mod_mask != 0)
11228 {
11229 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11230 int i = 0;
11231
11232 /* Turn a special key into three bytes, plus modifier. */
11233 if (mod_mask != 0)
11234 {
11235 temp[i++] = K_SPECIAL;
11236 temp[i++] = KS_MODIFIER;
11237 temp[i++] = mod_mask;
11238 }
11239 if (IS_SPECIAL(n))
11240 {
11241 temp[i++] = K_SPECIAL;
11242 temp[i++] = K_SECOND(n);
11243 temp[i++] = K_THIRD(n);
11244 }
11245#ifdef FEAT_MBYTE
11246 else if (has_mbyte)
11247 i += (*mb_char2bytes)(n, temp + i);
11248#endif
11249 else
11250 temp[i++] = n;
11251 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->v_type = VAR_STRING;
11253 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011254
11255#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011256 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011257 {
11258 int row = mouse_row;
11259 int col = mouse_col;
11260 win_T *win;
11261 linenr_T lnum;
11262# ifdef FEAT_WINDOWS
11263 win_T *wp;
11264# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011265 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011266
11267 if (row >= 0 && col >= 0)
11268 {
11269 /* Find the window at the mouse coordinates and compute the
11270 * text position. */
11271 win = mouse_find_win(&row, &col);
11272 (void)mouse_comp_pos(win, &row, &col, &lnum);
11273# ifdef FEAT_WINDOWS
11274 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011275 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011276# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011277 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011278 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11279 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11280 }
11281 }
11282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 }
11284}
11285
11286/*
11287 * "getcharmod()" function
11288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011291 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295}
11296
11297/*
11298 * "getcmdline()" function
11299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011302 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->v_type = VAR_STRING;
11306 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307}
11308
11309/*
11310 * "getcmdpos()" function
11311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011314 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318}
11319
11320/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011321 * "getcmdtype()" function
11322 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011323 static void
11324f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011325 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011326 typval_T *rettv;
11327{
11328 rettv->v_type = VAR_STRING;
11329 rettv->vval.v_string = alloc(2);
11330 if (rettv->vval.v_string != NULL)
11331 {
11332 rettv->vval.v_string[0] = get_cmdline_type();
11333 rettv->vval.v_string[1] = NUL;
11334 }
11335}
11336
11337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 * "getcwd()" function
11339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011342 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011345 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011348 rettv->vval.v_string = NULL;
11349 cwd = alloc(MAXPATHL);
11350 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011352 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11353 {
11354 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011356 if (rettv->vval.v_string != NULL)
11357 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011359 }
11360 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 }
11362}
11363
11364/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011365 * "getfontname()" function
11366 */
11367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011369 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 rettv->v_type = VAR_STRING;
11373 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011374#ifdef FEAT_GUI
11375 if (gui.in_use)
11376 {
11377 GuiFont font;
11378 char_u *name = NULL;
11379
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011380 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011381 {
11382 /* Get the "Normal" font. Either the name saved by
11383 * hl_set_font_name() or from the font ID. */
11384 font = gui.norm_font;
11385 name = hl_get_font_name();
11386 }
11387 else
11388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011390 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11391 return;
11392 font = gui_mch_get_font(name, FALSE);
11393 if (font == NOFONT)
11394 return; /* Invalid font name, return empty string. */
11395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011397 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011398 gui_mch_free_font(font);
11399 }
11400#endif
11401}
11402
11403/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011404 * "getfperm({fname})" function
11405 */
11406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011408 typval_T *argvars;
11409 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011410{
11411 char_u *fname;
11412 struct stat st;
11413 char_u *perm = NULL;
11414 char_u flags[] = "rwx";
11415 int i;
11416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011420 if (mch_stat((char *)fname, &st) >= 0)
11421 {
11422 perm = vim_strsave((char_u *)"---------");
11423 if (perm != NULL)
11424 {
11425 for (i = 0; i < 9; i++)
11426 {
11427 if (st.st_mode & (1 << (8 - i)))
11428 perm[i] = flags[i % 3];
11429 }
11430 }
11431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011432 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011433}
11434
11435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 * "getfsize({fname})" function
11437 */
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443 char_u *fname;
11444 struct stat st;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449
11450 if (mch_stat((char *)fname, &st) >= 0)
11451 {
11452 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011457
11458 /* non-perfect check for overflow */
11459 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11460 rettv->vval.v_number = -2;
11461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 }
11463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
11468 * "getftime({fname})" function
11469 */
11470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *argvars;
11473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474{
11475 char_u *fname;
11476 struct stat st;
11477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479
11480 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484}
11485
11486/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011487 * "getftype({fname})" function
11488 */
11489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011491 typval_T *argvars;
11492 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011493{
11494 char_u *fname;
11495 struct stat st;
11496 char_u *type = NULL;
11497 char *t;
11498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011502 if (mch_lstat((char *)fname, &st) >= 0)
11503 {
11504#ifdef S_ISREG
11505 if (S_ISREG(st.st_mode))
11506 t = "file";
11507 else if (S_ISDIR(st.st_mode))
11508 t = "dir";
11509# ifdef S_ISLNK
11510 else if (S_ISLNK(st.st_mode))
11511 t = "link";
11512# endif
11513# ifdef S_ISBLK
11514 else if (S_ISBLK(st.st_mode))
11515 t = "bdev";
11516# endif
11517# ifdef S_ISCHR
11518 else if (S_ISCHR(st.st_mode))
11519 t = "cdev";
11520# endif
11521# ifdef S_ISFIFO
11522 else if (S_ISFIFO(st.st_mode))
11523 t = "fifo";
11524# endif
11525# ifdef S_ISSOCK
11526 else if (S_ISSOCK(st.st_mode))
11527 t = "fifo";
11528# endif
11529 else
11530 t = "other";
11531#else
11532# ifdef S_IFMT
11533 switch (st.st_mode & S_IFMT)
11534 {
11535 case S_IFREG: t = "file"; break;
11536 case S_IFDIR: t = "dir"; break;
11537# ifdef S_IFLNK
11538 case S_IFLNK: t = "link"; break;
11539# endif
11540# ifdef S_IFBLK
11541 case S_IFBLK: t = "bdev"; break;
11542# endif
11543# ifdef S_IFCHR
11544 case S_IFCHR: t = "cdev"; break;
11545# endif
11546# ifdef S_IFIFO
11547 case S_IFIFO: t = "fifo"; break;
11548# endif
11549# ifdef S_IFSOCK
11550 case S_IFSOCK: t = "socket"; break;
11551# endif
11552 default: t = "other";
11553 }
11554# else
11555 if (mch_isdir(fname))
11556 t = "dir";
11557 else
11558 t = "file";
11559# endif
11560#endif
11561 type = vim_strsave((char_u *)t);
11562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011564}
11565
11566/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011567 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011568 */
11569 static void
11570f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011573{
11574 linenr_T lnum;
11575 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011576 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577
11578 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011579 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011581 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011582 retlist = FALSE;
11583 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011584 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011585 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011586 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011587 retlist = TRUE;
11588 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011589
Bram Moolenaar342337a2005-07-21 21:11:17 +000011590 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011591}
11592
11593/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011594 * "getmatches()" function
11595 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011596 static void
11597f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011598 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011599 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011600{
11601#ifdef FEAT_SEARCH_EXTRA
11602 dict_T *dict;
11603 matchitem_T *cur = curwin->w_match_head;
11604
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011605 if (rettv_list_alloc(rettv) == OK)
11606 {
11607 while (cur != NULL)
11608 {
11609 dict = dict_alloc();
11610 if (dict == NULL)
11611 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011612 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11613 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11614 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11615 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11616 list_append_dict(rettv->vval.v_list, dict);
11617 cur = cur->next;
11618 }
11619 }
11620#endif
11621}
11622
11623/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011624 * "getpid()" function
11625 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011626 static void
11627f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011628 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011629 typval_T *rettv;
11630{
11631 rettv->vval.v_number = mch_get_pid();
11632}
11633
11634/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011635 * "getpos(string)" function
11636 */
11637 static void
11638f_getpos(argvars, rettv)
11639 typval_T *argvars;
11640 typval_T *rettv;
11641{
11642 pos_T *fp;
11643 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011644 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011645
11646 if (rettv_list_alloc(rettv) == OK)
11647 {
11648 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011649 fp = var2fpos(&argvars[0], TRUE, &fnum);
11650 if (fnum != -1)
11651 list_append_number(l, (varnumber_T)fnum);
11652 else
11653 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011654 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11655 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011656 list_append_number(l, (fp != NULL)
11657 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011658 : (varnumber_T)0);
11659 list_append_number(l,
11660#ifdef FEAT_VIRTUALEDIT
11661 (fp != NULL) ? (varnumber_T)fp->coladd :
11662#endif
11663 (varnumber_T)0);
11664 }
11665 else
11666 rettv->vval.v_number = FALSE;
11667}
11668
11669/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011670 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011671 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011672 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011673f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011674 typval_T *argvars UNUSED;
11675 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011676{
11677#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011678 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011679#endif
11680
Bram Moolenaar2641f772005-03-25 21:58:17 +000011681#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011682 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011683 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011684 wp = NULL;
11685 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11686 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011687 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011688 if (wp == NULL)
11689 return;
11690 }
11691
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011692 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011693 }
11694#endif
11695}
11696
11697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 * "getreg()" function
11699 */
11700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011702 typval_T *argvars;
11703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704{
11705 char_u *strregname;
11706 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011707 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011708 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011710 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 strregname = get_tv_string_chk(&argvars[0]);
11713 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011714 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011715 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 regname = (strregname == NULL ? '"' : *strregname);
11720 if (regname == 0)
11721 regname = '"';
11722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011723 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 rettv->vval.v_string = error ? NULL :
11725 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726}
11727
11728/*
11729 * "getregtype()" function
11730 */
11731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011732f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011733 typval_T *argvars;
11734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735{
11736 char_u *strregname;
11737 int regname;
11738 char_u buf[NUMBUFLEN + 2];
11739 long reglen = 0;
11740
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011741 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011742 {
11743 strregname = get_tv_string_chk(&argvars[0]);
11744 if (strregname == NULL) /* type error; errmsg already given */
11745 {
11746 rettv->v_type = VAR_STRING;
11747 rettv->vval.v_string = NULL;
11748 return;
11749 }
11750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 else
11752 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011753 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754
11755 regname = (strregname == NULL ? '"' : *strregname);
11756 if (regname == 0)
11757 regname = '"';
11758
11759 buf[0] = NUL;
11760 buf[1] = NUL;
11761 switch (get_reg_type(regname, &reglen))
11762 {
11763 case MLINE: buf[0] = 'V'; break;
11764 case MCHAR: buf[0] = 'v'; break;
11765#ifdef FEAT_VISUAL
11766 case MBLOCK:
11767 buf[0] = Ctrl_V;
11768 sprintf((char *)buf + 1, "%ld", reglen + 1);
11769 break;
11770#endif
11771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772 rettv->v_type = VAR_STRING;
11773 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774}
11775
11776/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011777 * "gettabvar()" function
11778 */
11779 static void
11780f_gettabvar(argvars, rettv)
11781 typval_T *argvars;
11782 typval_T *rettv;
11783{
11784 tabpage_T *tp;
11785 dictitem_T *v;
11786 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011787 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011788
11789 rettv->v_type = VAR_STRING;
11790 rettv->vval.v_string = NULL;
11791
11792 varname = get_tv_string_chk(&argvars[1]);
11793 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11794 if (tp != NULL && varname != NULL)
11795 {
11796 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011797 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011798 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011799 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011800 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011801 done = TRUE;
11802 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011803 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011804
11805 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011806 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011807}
11808
11809/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011810 * "gettabwinvar()" function
11811 */
11812 static void
11813f_gettabwinvar(argvars, rettv)
11814 typval_T *argvars;
11815 typval_T *rettv;
11816{
11817 getwinvar(argvars, rettv, 1);
11818}
11819
11820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 * "getwinposx()" function
11822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829#ifdef FEAT_GUI
11830 if (gui.in_use)
11831 {
11832 int x, y;
11833
11834 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 }
11837#endif
11838}
11839
11840/*
11841 * "getwinposy()" function
11842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011845 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849#ifdef FEAT_GUI
11850 if (gui.in_use)
11851 {
11852 int x, y;
11853
11854 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 }
11857#endif
11858}
11859
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011860/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011861 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011862 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011863 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011864find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011865 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011866 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011867{
11868#ifdef FEAT_WINDOWS
11869 win_T *wp;
11870#endif
11871 int nr;
11872
11873 nr = get_tv_number_chk(vp, NULL);
11874
11875#ifdef FEAT_WINDOWS
11876 if (nr < 0)
11877 return NULL;
11878 if (nr == 0)
11879 return curwin;
11880
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011881 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11882 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011883 if (--nr <= 0)
11884 break;
11885 return wp;
11886#else
11887 if (nr == 0 || nr == 1)
11888 return curwin;
11889 return NULL;
11890#endif
11891}
11892
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893/*
11894 * "getwinvar()" function
11895 */
11896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011898 typval_T *argvars;
11899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011901 getwinvar(argvars, rettv, 0);
11902}
11903
11904/*
11905 * getwinvar() and gettabwinvar()
11906 */
11907 static void
11908getwinvar(argvars, rettv, off)
11909 typval_T *argvars;
11910 typval_T *rettv;
11911 int off; /* 1 for gettabwinvar() */
11912{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 win_T *win, *oldcurwin;
11914 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 dictitem_T *v;
Bram Moolenaar105bc352013-05-17 16:03:57 +020011916 tabpage_T *tp, *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011917 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011919#ifdef FEAT_WINDOWS
11920 if (off == 1)
11921 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11922 else
11923 tp = curtab;
11924#endif
11925 win = find_win_by_nr(&argvars[off], tp);
11926 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011927 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011929 rettv->v_type = VAR_STRING;
11930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931
11932 if (win != NULL && varname != NULL)
11933 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011934 /* Set curwin to be our win, temporarily. Also set the tabpage,
11935 * otherwise the window is not valid. */
11936 switch_win(&oldcurwin, &oldtabpage, win, tp);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011937
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011939 {
11940 if (get_option_tv(&varname, rettv, 1) == OK)
11941 done = TRUE;
11942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 else
11944 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011945 /* Look up the variable. */
11946 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11947 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011949 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011951 done = TRUE;
11952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011954
11955 /* restore previous notion of curwin */
Bram Moolenaar105bc352013-05-17 16:03:57 +020011956 restore_win(oldcurwin, oldtabpage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 }
11958
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011959 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11960 /* use the default return value */
11961 copy_tv(&argvars[off + 2], rettv);
11962
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 --emsg_off;
11964}
11965
11966/*
11967 * "glob()" function
11968 */
11969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011971 typval_T *argvars;
11972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011974 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011978 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011979 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011981 if (argvars[1].v_type != VAR_UNKNOWN)
11982 {
11983 if (get_tv_number_chk(&argvars[1], &error))
11984 options |= WILD_KEEP_ALL;
11985 if (argvars[2].v_type != VAR_UNKNOWN
11986 && get_tv_number_chk(&argvars[2], &error))
11987 {
11988 rettv->v_type = VAR_LIST;
11989 rettv->vval.v_list = NULL;
11990 }
11991 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011992 if (!error)
11993 {
11994 ExpandInit(&xpc);
11995 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011996 if (p_wic)
11997 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011998 if (rettv->v_type == VAR_STRING)
11999 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012000 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012001 else if (rettv_list_alloc(rettv) != FAIL)
12002 {
12003 int i;
12004
12005 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12006 NULL, options, WILD_ALL_KEEP);
12007 for (i = 0; i < xpc.xp_numfiles; i++)
12008 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12009
12010 ExpandCleanup(&xpc);
12011 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012012 }
12013 else
12014 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015}
12016
12017/*
12018 * "globpath()" function
12019 */
12020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 typval_T *argvars;
12023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012025 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012028 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012030 /* When the optional second argument is non-zero, don't remove matches
12031 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12032 if (argvars[2].v_type != VAR_UNKNOWN
12033 && get_tv_number_chk(&argvars[2], &error))
12034 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012036 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012037 rettv->vval.v_string = NULL;
12038 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012039 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12040 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041}
12042
12043/*
12044 * "has()" function
12045 */
12046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012048 typval_T *argvars;
12049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050{
12051 int i;
12052 char_u *name;
12053 int n = FALSE;
12054 static char *(has_list[]) =
12055 {
12056#ifdef AMIGA
12057 "amiga",
12058# ifdef FEAT_ARP
12059 "arp",
12060# endif
12061#endif
12062#ifdef __BEOS__
12063 "beos",
12064#endif
12065#ifdef MSDOS
12066# ifdef DJGPP
12067 "dos32",
12068# else
12069 "dos16",
12070# endif
12071#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012072#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 "mac",
12074#endif
12075#if defined(MACOS_X_UNIX)
12076 "macunix",
12077#endif
12078#ifdef OS2
12079 "os2",
12080#endif
12081#ifdef __QNX__
12082 "qnx",
12083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084#ifdef UNIX
12085 "unix",
12086#endif
12087#ifdef VMS
12088 "vms",
12089#endif
12090#ifdef WIN16
12091 "win16",
12092#endif
12093#ifdef WIN32
12094 "win32",
12095#endif
12096#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12097 "win32unix",
12098#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012099#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 "win64",
12101#endif
12102#ifdef EBCDIC
12103 "ebcdic",
12104#endif
12105#ifndef CASE_INSENSITIVE_FILENAME
12106 "fname_case",
12107#endif
12108#ifdef FEAT_ARABIC
12109 "arabic",
12110#endif
12111#ifdef FEAT_AUTOCMD
12112 "autocmd",
12113#endif
12114#ifdef FEAT_BEVAL
12115 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012116# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12117 "balloon_multiline",
12118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119#endif
12120#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12121 "builtin_terms",
12122# ifdef ALL_BUILTIN_TCAPS
12123 "all_builtin_terms",
12124# endif
12125#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012126#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12127 || defined(FEAT_GUI_W32) \
12128 || defined(FEAT_GUI_MOTIF))
12129 "browsefilter",
12130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131#ifdef FEAT_BYTEOFF
12132 "byte_offset",
12133#endif
12134#ifdef FEAT_CINDENT
12135 "cindent",
12136#endif
12137#ifdef FEAT_CLIENTSERVER
12138 "clientserver",
12139#endif
12140#ifdef FEAT_CLIPBOARD
12141 "clipboard",
12142#endif
12143#ifdef FEAT_CMDL_COMPL
12144 "cmdline_compl",
12145#endif
12146#ifdef FEAT_CMDHIST
12147 "cmdline_hist",
12148#endif
12149#ifdef FEAT_COMMENTS
12150 "comments",
12151#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012152#ifdef FEAT_CONCEAL
12153 "conceal",
12154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155#ifdef FEAT_CRYPT
12156 "cryptv",
12157#endif
12158#ifdef FEAT_CSCOPE
12159 "cscope",
12160#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012161#ifdef FEAT_CURSORBIND
12162 "cursorbind",
12163#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012164#ifdef CURSOR_SHAPE
12165 "cursorshape",
12166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167#ifdef DEBUG
12168 "debug",
12169#endif
12170#ifdef FEAT_CON_DIALOG
12171 "dialog_con",
12172#endif
12173#ifdef FEAT_GUI_DIALOG
12174 "dialog_gui",
12175#endif
12176#ifdef FEAT_DIFF
12177 "diff",
12178#endif
12179#ifdef FEAT_DIGRAPHS
12180 "digraphs",
12181#endif
12182#ifdef FEAT_DND
12183 "dnd",
12184#endif
12185#ifdef FEAT_EMACS_TAGS
12186 "emacs_tags",
12187#endif
12188 "eval", /* always present, of course! */
12189#ifdef FEAT_EX_EXTRA
12190 "ex_extra",
12191#endif
12192#ifdef FEAT_SEARCH_EXTRA
12193 "extra_search",
12194#endif
12195#ifdef FEAT_FKMAP
12196 "farsi",
12197#endif
12198#ifdef FEAT_SEARCHPATH
12199 "file_in_path",
12200#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012201#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012202 "filterpipe",
12203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef FEAT_FIND_ID
12205 "find_in_path",
12206#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012207#ifdef FEAT_FLOAT
12208 "float",
12209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210#ifdef FEAT_FOLDING
12211 "folding",
12212#endif
12213#ifdef FEAT_FOOTER
12214 "footer",
12215#endif
12216#if !defined(USE_SYSTEM) && defined(UNIX)
12217 "fork",
12218#endif
12219#ifdef FEAT_GETTEXT
12220 "gettext",
12221#endif
12222#ifdef FEAT_GUI
12223 "gui",
12224#endif
12225#ifdef FEAT_GUI_ATHENA
12226# ifdef FEAT_GUI_NEXTAW
12227 "gui_neXtaw",
12228# else
12229 "gui_athena",
12230# endif
12231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232#ifdef FEAT_GUI_GTK
12233 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012236#ifdef FEAT_GUI_GNOME
12237 "gui_gnome",
12238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239#ifdef FEAT_GUI_MAC
12240 "gui_mac",
12241#endif
12242#ifdef FEAT_GUI_MOTIF
12243 "gui_motif",
12244#endif
12245#ifdef FEAT_GUI_PHOTON
12246 "gui_photon",
12247#endif
12248#ifdef FEAT_GUI_W16
12249 "gui_win16",
12250#endif
12251#ifdef FEAT_GUI_W32
12252 "gui_win32",
12253#endif
12254#ifdef FEAT_HANGULIN
12255 "hangul_input",
12256#endif
12257#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12258 "iconv",
12259#endif
12260#ifdef FEAT_INS_EXPAND
12261 "insert_expand",
12262#endif
12263#ifdef FEAT_JUMPLIST
12264 "jumplist",
12265#endif
12266#ifdef FEAT_KEYMAP
12267 "keymap",
12268#endif
12269#ifdef FEAT_LANGMAP
12270 "langmap",
12271#endif
12272#ifdef FEAT_LIBCALL
12273 "libcall",
12274#endif
12275#ifdef FEAT_LINEBREAK
12276 "linebreak",
12277#endif
12278#ifdef FEAT_LISP
12279 "lispindent",
12280#endif
12281#ifdef FEAT_LISTCMDS
12282 "listcmds",
12283#endif
12284#ifdef FEAT_LOCALMAP
12285 "localmap",
12286#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012287#ifdef FEAT_LUA
12288# ifndef DYNAMIC_LUA
12289 "lua",
12290# endif
12291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292#ifdef FEAT_MENU
12293 "menu",
12294#endif
12295#ifdef FEAT_SESSION
12296 "mksession",
12297#endif
12298#ifdef FEAT_MODIFY_FNAME
12299 "modify_fname",
12300#endif
12301#ifdef FEAT_MOUSE
12302 "mouse",
12303#endif
12304#ifdef FEAT_MOUSESHAPE
12305 "mouseshape",
12306#endif
12307#if defined(UNIX) || defined(VMS)
12308# ifdef FEAT_MOUSE_DEC
12309 "mouse_dec",
12310# endif
12311# ifdef FEAT_MOUSE_GPM
12312 "mouse_gpm",
12313# endif
12314# ifdef FEAT_MOUSE_JSB
12315 "mouse_jsbterm",
12316# endif
12317# ifdef FEAT_MOUSE_NET
12318 "mouse_netterm",
12319# endif
12320# ifdef FEAT_MOUSE_PTERM
12321 "mouse_pterm",
12322# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012323# ifdef FEAT_MOUSE_SGR
12324 "mouse_sgr",
12325# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012326# ifdef FEAT_SYSMOUSE
12327 "mouse_sysmouse",
12328# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012329# ifdef FEAT_MOUSE_URXVT
12330 "mouse_urxvt",
12331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332# ifdef FEAT_MOUSE_XTERM
12333 "mouse_xterm",
12334# endif
12335#endif
12336#ifdef FEAT_MBYTE
12337 "multi_byte",
12338#endif
12339#ifdef FEAT_MBYTE_IME
12340 "multi_byte_ime",
12341#endif
12342#ifdef FEAT_MULTI_LANG
12343 "multi_lang",
12344#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012345#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012346#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012347 "mzscheme",
12348#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350#ifdef FEAT_OLE
12351 "ole",
12352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353#ifdef FEAT_PATH_EXTRA
12354 "path_extra",
12355#endif
12356#ifdef FEAT_PERL
12357#ifndef DYNAMIC_PERL
12358 "perl",
12359#endif
12360#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012361#ifdef FEAT_PERSISTENT_UNDO
12362 "persistent_undo",
12363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364#ifdef FEAT_PYTHON
12365#ifndef DYNAMIC_PYTHON
12366 "python",
12367#endif
12368#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012369#ifdef FEAT_PYTHON3
12370#ifndef DYNAMIC_PYTHON3
12371 "python3",
12372#endif
12373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374#ifdef FEAT_POSTSCRIPT
12375 "postscript",
12376#endif
12377#ifdef FEAT_PRINTER
12378 "printer",
12379#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012380#ifdef FEAT_PROFILE
12381 "profile",
12382#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012383#ifdef FEAT_RELTIME
12384 "reltime",
12385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386#ifdef FEAT_QUICKFIX
12387 "quickfix",
12388#endif
12389#ifdef FEAT_RIGHTLEFT
12390 "rightleft",
12391#endif
12392#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12393 "ruby",
12394#endif
12395#ifdef FEAT_SCROLLBIND
12396 "scrollbind",
12397#endif
12398#ifdef FEAT_CMDL_INFO
12399 "showcmd",
12400 "cmdline_info",
12401#endif
12402#ifdef FEAT_SIGNS
12403 "signs",
12404#endif
12405#ifdef FEAT_SMARTINDENT
12406 "smartindent",
12407#endif
12408#ifdef FEAT_SNIFF
12409 "sniff",
12410#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012411#ifdef STARTUPTIME
12412 "startuptime",
12413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414#ifdef FEAT_STL_OPT
12415 "statusline",
12416#endif
12417#ifdef FEAT_SUN_WORKSHOP
12418 "sun_workshop",
12419#endif
12420#ifdef FEAT_NETBEANS_INTG
12421 "netbeans_intg",
12422#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012423#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012424 "spell",
12425#endif
12426#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 "syntax",
12428#endif
12429#if defined(USE_SYSTEM) || !defined(UNIX)
12430 "system",
12431#endif
12432#ifdef FEAT_TAG_BINS
12433 "tag_binary",
12434#endif
12435#ifdef FEAT_TAG_OLDSTATIC
12436 "tag_old_static",
12437#endif
12438#ifdef FEAT_TAG_ANYWHITE
12439 "tag_any_white",
12440#endif
12441#ifdef FEAT_TCL
12442# ifndef DYNAMIC_TCL
12443 "tcl",
12444# endif
12445#endif
12446#ifdef TERMINFO
12447 "terminfo",
12448#endif
12449#ifdef FEAT_TERMRESPONSE
12450 "termresponse",
12451#endif
12452#ifdef FEAT_TEXTOBJ
12453 "textobjects",
12454#endif
12455#ifdef HAVE_TGETENT
12456 "tgetent",
12457#endif
12458#ifdef FEAT_TITLE
12459 "title",
12460#endif
12461#ifdef FEAT_TOOLBAR
12462 "toolbar",
12463#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012464#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12465 "unnamedplus",
12466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467#ifdef FEAT_USR_CMDS
12468 "user-commands", /* was accidentally included in 5.4 */
12469 "user_commands",
12470#endif
12471#ifdef FEAT_VIMINFO
12472 "viminfo",
12473#endif
12474#ifdef FEAT_VERTSPLIT
12475 "vertsplit",
12476#endif
12477#ifdef FEAT_VIRTUALEDIT
12478 "virtualedit",
12479#endif
12480#ifdef FEAT_VISUAL
12481 "visual",
12482#endif
12483#ifdef FEAT_VISUALEXTRA
12484 "visualextra",
12485#endif
12486#ifdef FEAT_VREPLACE
12487 "vreplace",
12488#endif
12489#ifdef FEAT_WILDIGN
12490 "wildignore",
12491#endif
12492#ifdef FEAT_WILDMENU
12493 "wildmenu",
12494#endif
12495#ifdef FEAT_WINDOWS
12496 "windows",
12497#endif
12498#ifdef FEAT_WAK
12499 "winaltkeys",
12500#endif
12501#ifdef FEAT_WRITEBACKUP
12502 "writebackup",
12503#endif
12504#ifdef FEAT_XIM
12505 "xim",
12506#endif
12507#ifdef FEAT_XFONTSET
12508 "xfontset",
12509#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012510#ifdef FEAT_XPM_W32
12511 "xpm_w32",
12512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513#ifdef USE_XSMP
12514 "xsmp",
12515#endif
12516#ifdef USE_XSMP_INTERACT
12517 "xsmp_interact",
12518#endif
12519#ifdef FEAT_XCLIPBOARD
12520 "xterm_clipboard",
12521#endif
12522#ifdef FEAT_XTERM_SAVE
12523 "xterm_save",
12524#endif
12525#if defined(UNIX) && defined(FEAT_X11)
12526 "X11",
12527#endif
12528 NULL
12529 };
12530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 for (i = 0; has_list[i] != NULL; ++i)
12533 if (STRICMP(name, has_list[i]) == 0)
12534 {
12535 n = TRUE;
12536 break;
12537 }
12538
12539 if (n == FALSE)
12540 {
12541 if (STRNICMP(name, "patch", 5) == 0)
12542 n = has_patch(atoi((char *)name + 5));
12543 else if (STRICMP(name, "vim_starting") == 0)
12544 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012545#ifdef FEAT_MBYTE
12546 else if (STRICMP(name, "multi_byte_encoding") == 0)
12547 n = has_mbyte;
12548#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012549#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12550 else if (STRICMP(name, "balloon_multiline") == 0)
12551 n = multiline_balloon_available();
12552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553#ifdef DYNAMIC_TCL
12554 else if (STRICMP(name, "tcl") == 0)
12555 n = tcl_enabled(FALSE);
12556#endif
12557#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12558 else if (STRICMP(name, "iconv") == 0)
12559 n = iconv_enabled(FALSE);
12560#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012561#ifdef DYNAMIC_LUA
12562 else if (STRICMP(name, "lua") == 0)
12563 n = lua_enabled(FALSE);
12564#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012565#ifdef DYNAMIC_MZSCHEME
12566 else if (STRICMP(name, "mzscheme") == 0)
12567 n = mzscheme_enabled(FALSE);
12568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569#ifdef DYNAMIC_RUBY
12570 else if (STRICMP(name, "ruby") == 0)
12571 n = ruby_enabled(FALSE);
12572#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012573#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574#ifdef DYNAMIC_PYTHON
12575 else if (STRICMP(name, "python") == 0)
12576 n = python_enabled(FALSE);
12577#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012578#endif
12579#ifdef FEAT_PYTHON3
12580#ifdef DYNAMIC_PYTHON3
12581 else if (STRICMP(name, "python3") == 0)
12582 n = python3_enabled(FALSE);
12583#endif
12584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585#ifdef DYNAMIC_PERL
12586 else if (STRICMP(name, "perl") == 0)
12587 n = perl_enabled(FALSE);
12588#endif
12589#ifdef FEAT_GUI
12590 else if (STRICMP(name, "gui_running") == 0)
12591 n = (gui.in_use || gui.starting);
12592# ifdef FEAT_GUI_W32
12593 else if (STRICMP(name, "gui_win32s") == 0)
12594 n = gui_is_win32s();
12595# endif
12596# ifdef FEAT_BROWSE
12597 else if (STRICMP(name, "browse") == 0)
12598 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12599# endif
12600#endif
12601#ifdef FEAT_SYN_HL
12602 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012603 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604#endif
12605#if defined(WIN3264)
12606 else if (STRICMP(name, "win95") == 0)
12607 n = mch_windows95();
12608#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012609#ifdef FEAT_NETBEANS_INTG
12610 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012611 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 }
12614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012615 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616}
12617
12618/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012619 * "has_key()" function
12620 */
12621 static void
12622f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012623 typval_T *argvars;
12624 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012625{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012626 if (argvars[0].v_type != VAR_DICT)
12627 {
12628 EMSG(_(e_dictreq));
12629 return;
12630 }
12631 if (argvars[0].vval.v_dict == NULL)
12632 return;
12633
12634 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012635 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012636}
12637
12638/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012639 * "haslocaldir()" function
12640 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012641 static void
12642f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012643 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012644 typval_T *rettv;
12645{
12646 rettv->vval.v_number = (curwin->w_localdir != NULL);
12647}
12648
12649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 * "hasmapto()" function
12651 */
12652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012654 typval_T *argvars;
12655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656{
12657 char_u *name;
12658 char_u *mode;
12659 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012660 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012663 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 mode = (char_u *)"nvo";
12665 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012668 if (argvars[2].v_type != VAR_UNKNOWN)
12669 abbr = get_tv_number(&argvars[2]);
12670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671
Bram Moolenaar2c932302006-03-18 21:42:09 +000012672 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676}
12677
12678/*
12679 * "histadd()" function
12680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685{
12686#ifdef FEAT_CMDHIST
12687 int histype;
12688 char_u *str;
12689 char_u buf[NUMBUFLEN];
12690#endif
12691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 if (check_restricted() || check_secure())
12694 return;
12695#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012696 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12697 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 if (histype >= 0)
12699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 if (*str != NUL)
12702 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012703 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 return;
12707 }
12708 }
12709#endif
12710}
12711
12712/*
12713 * "histdel()" function
12714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012717 typval_T *argvars UNUSED;
12718 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719{
12720#ifdef FEAT_CMDHIST
12721 int n;
12722 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12726 if (str == NULL)
12727 n = 0;
12728 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012731 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012733 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 else
12736 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 get_tv_string_buf(&argvars[1], buf));
12739 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740#endif
12741}
12742
12743/*
12744 * "histget()" function
12745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012748 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750{
12751#ifdef FEAT_CMDHIST
12752 int type;
12753 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12757 if (str == NULL)
12758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 {
12761 type = get_histtype(str);
12762 if (argvars[1].v_type == VAR_UNKNOWN)
12763 idx = get_history_idx(type);
12764 else
12765 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12766 /* -1 on type error */
12767 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773}
12774
12775/*
12776 * "histnr()" function
12777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782{
12783 int i;
12784
12785#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786 char_u *history = get_tv_string_chk(&argvars[0]);
12787
12788 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 if (i >= HIST_CMD && i < HIST_COUNT)
12790 i = get_history_idx(i);
12791 else
12792#endif
12793 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795}
12796
12797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 * "highlightID(name)" function
12799 */
12800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012802 typval_T *argvars;
12803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806}
12807
12808/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012809 * "highlight_exists()" function
12810 */
12811 static void
12812f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012813 typval_T *argvars;
12814 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012815{
12816 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12817}
12818
12819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 * "hostname()" function
12821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012824 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
12827 char_u hostname[256];
12828
12829 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832}
12833
12834/*
12835 * iconv() function
12836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841{
12842#ifdef FEAT_MBYTE
12843 char_u buf1[NUMBUFLEN];
12844 char_u buf2[NUMBUFLEN];
12845 char_u *from, *to, *str;
12846 vimconv_T vimconv;
12847#endif
12848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->v_type = VAR_STRING;
12850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851
12852#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 str = get_tv_string(&argvars[0]);
12854 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12855 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 vimconv.vc_type = CONV_NONE;
12857 convert_setup(&vimconv, from, to);
12858
12859 /* If the encodings are equal, no conversion needed. */
12860 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864
12865 convert_setup(&vimconv, NULL, NULL);
12866 vim_free(from);
12867 vim_free(to);
12868#endif
12869}
12870
12871/*
12872 * "indent()" function
12873 */
12874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012876 typval_T *argvars;
12877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878{
12879 linenr_T lnum;
12880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886}
12887
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012888/*
12889 * "index()" function
12890 */
12891 static void
12892f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012893 typval_T *argvars;
12894 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012895{
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 list_T *l;
12897 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012898 long idx = 0;
12899 int ic = FALSE;
12900
12901 rettv->vval.v_number = -1;
12902 if (argvars[0].v_type != VAR_LIST)
12903 {
12904 EMSG(_(e_listreq));
12905 return;
12906 }
12907 l = argvars[0].vval.v_list;
12908 if (l != NULL)
12909 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012910 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012911 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012913 int error = FALSE;
12914
Bram Moolenaar758711c2005-02-02 23:11:38 +000012915 /* Start at specified item. Use the cached index that list_find()
12916 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012917 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012918 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012919 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012920 ic = get_tv_number_chk(&argvars[3], &error);
12921 if (error)
12922 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012923 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012924
Bram Moolenaar758711c2005-02-02 23:11:38 +000012925 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012926 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012927 {
12928 rettv->vval.v_number = idx;
12929 break;
12930 }
12931 }
12932}
12933
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934static int inputsecret_flag = 0;
12935
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012936static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12937
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012939 * This function is used by f_input() and f_inputdialog() functions. The third
12940 * argument to f_input() specifies the type of completion to use at the
12941 * prompt. The third argument to f_inputdialog() specifies the value to return
12942 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 */
12944 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012945get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012946 typval_T *argvars;
12947 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012948 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012950 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 char_u *p = NULL;
12952 int c;
12953 char_u buf[NUMBUFLEN];
12954 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012955 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012956 int xp_type = EXPAND_NOTHING;
12957 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012960 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961
12962#ifdef NO_CONSOLE_INPUT
12963 /* While starting up, there is no place to enter text. */
12964 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966#endif
12967
12968 cmd_silent = FALSE; /* Want to see the prompt. */
12969 if (prompt != NULL)
12970 {
12971 /* Only the part of the message after the last NL is considered as
12972 * prompt for the command line */
12973 p = vim_strrchr(prompt, '\n');
12974 if (p == NULL)
12975 p = prompt;
12976 else
12977 {
12978 ++p;
12979 c = *p;
12980 *p = NUL;
12981 msg_start();
12982 msg_clr_eos();
12983 msg_puts_attr(prompt, echo_attr);
12984 msg_didout = FALSE;
12985 msg_starthere();
12986 *p = c;
12987 }
12988 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012990 if (argvars[1].v_type != VAR_UNKNOWN)
12991 {
12992 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12993 if (defstr != NULL)
12994 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012996 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012997 {
12998 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012999 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013000 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013001
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013002 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013003 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013004
Bram Moolenaar4463f292005-09-25 22:20:24 +000013005 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13006 if (xp_name == NULL)
13007 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013008
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013009 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013010
Bram Moolenaar4463f292005-09-25 22:20:24 +000013011 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13012 &xp_arg) == FAIL)
13013 return;
13014 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013015 }
13016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017 if (defstr != NULL)
13018 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013019 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13020 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013021 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013022 && argvars[1].v_type != VAR_UNKNOWN
13023 && argvars[2].v_type != VAR_UNKNOWN)
13024 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13025 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013026
13027 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 /* since the user typed this, no need to wait for return */
13030 need_wait_return = FALSE;
13031 msg_didout = FALSE;
13032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 cmd_silent = cmd_silent_save;
13034}
13035
13036/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013037 * "input()" function
13038 * Also handles inputsecret() when inputsecret is set.
13039 */
13040 static void
13041f_input(argvars, rettv)
13042 typval_T *argvars;
13043 typval_T *rettv;
13044{
13045 get_user_input(argvars, rettv, FALSE);
13046}
13047
13048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 * "inputdialog()" function
13050 */
13051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013053 typval_T *argvars;
13054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055{
13056#if defined(FEAT_GUI_TEXTDIALOG)
13057 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13058 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13059 {
13060 char_u *message;
13061 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064 message = get_tv_string_chk(&argvars[0]);
13065 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013066 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013067 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 else
13069 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070 if (message != NULL && defstr != NULL
13071 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013072 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 else
13075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013076 if (message != NULL && defstr != NULL
13077 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013078 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->vval.v_string = vim_strsave(
13080 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013084 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 }
13086 else
13087#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013088 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089}
13090
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013091/*
13092 * "inputlist()" function
13093 */
13094 static void
13095f_inputlist(argvars, rettv)
13096 typval_T *argvars;
13097 typval_T *rettv;
13098{
13099 listitem_T *li;
13100 int selected;
13101 int mouse_used;
13102
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013103#ifdef NO_CONSOLE_INPUT
13104 /* While starting up, there is no place to enter text. */
13105 if (no_console_input())
13106 return;
13107#endif
13108 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13109 {
13110 EMSG2(_(e_listarg), "inputlist()");
13111 return;
13112 }
13113
13114 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013115 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013116 lines_left = Rows; /* avoid more prompt */
13117 msg_scroll = TRUE;
13118 msg_clr_eos();
13119
13120 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13121 {
13122 msg_puts(get_tv_string(&li->li_tv));
13123 msg_putchar('\n');
13124 }
13125
13126 /* Ask for choice. */
13127 selected = prompt_for_number(&mouse_used);
13128 if (mouse_used)
13129 selected -= lines_left;
13130
13131 rettv->vval.v_number = selected;
13132}
13133
13134
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13136
13137/*
13138 * "inputrestore()" function
13139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013142 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144{
13145 if (ga_userinput.ga_len > 0)
13146 {
13147 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13149 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013150 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 }
13152 else if (p_verbose > 1)
13153 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013154 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
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/*
13160 * "inputsave()" function
13161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013167 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 if (ga_grow(&ga_userinput, 1) == OK)
13169 {
13170 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13171 + ga_userinput.ga_len);
13172 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013173 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 }
13175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177}
13178
13179/*
13180 * "inputsecret()" function
13181 */
13182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013183f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013184 typval_T *argvars;
13185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186{
13187 ++cmdline_star;
13188 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 --cmdline_star;
13191 --inputsecret_flag;
13192}
13193
13194/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195 * "insert()" function
13196 */
13197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013199 typval_T *argvars;
13200 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013201{
13202 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 listitem_T *item;
13204 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013205 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013206
13207 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013208 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013209 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013210 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013211 {
13212 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013213 before = get_tv_number_chk(&argvars[2], &error);
13214 if (error)
13215 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013216
Bram Moolenaar758711c2005-02-02 23:11:38 +000013217 if (before == l->lv_len)
13218 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013219 else
13220 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013221 item = list_find(l, before);
13222 if (item == NULL)
13223 {
13224 EMSGN(_(e_listidx), before);
13225 l = NULL;
13226 }
13227 }
13228 if (l != NULL)
13229 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013230 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013231 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013232 }
13233 }
13234}
13235
13236/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013237 * "invert(expr)" function
13238 */
13239 static void
13240f_invert(argvars, rettv)
13241 typval_T *argvars;
13242 typval_T *rettv;
13243{
13244 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13245}
13246
13247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 * "isdirectory()" function
13249 */
13250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013252 typval_T *argvars;
13253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256}
13257
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013258/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013259 * "islocked()" function
13260 */
13261 static void
13262f_islocked(argvars, rettv)
13263 typval_T *argvars;
13264 typval_T *rettv;
13265{
13266 lval_T lv;
13267 char_u *end;
13268 dictitem_T *di;
13269
13270 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013271 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13272 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013273 if (end != NULL && lv.ll_name != NULL)
13274 {
13275 if (*end != NUL)
13276 EMSG(_(e_trailing));
13277 else
13278 {
13279 if (lv.ll_tv == NULL)
13280 {
13281 if (check_changedtick(lv.ll_name))
13282 rettv->vval.v_number = 1; /* always locked */
13283 else
13284 {
13285 di = find_var(lv.ll_name, NULL);
13286 if (di != NULL)
13287 {
13288 /* Consider a variable locked when:
13289 * 1. the variable itself is locked
13290 * 2. the value of the variable is locked.
13291 * 3. the List or Dict value is locked.
13292 */
13293 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13294 || tv_islocked(&di->di_tv));
13295 }
13296 }
13297 }
13298 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013299 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013300 else if (lv.ll_newkey != NULL)
13301 EMSG2(_(e_dictkey), lv.ll_newkey);
13302 else if (lv.ll_list != NULL)
13303 /* List item. */
13304 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13305 else
13306 /* Dictionary item. */
13307 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13308 }
13309 }
13310
13311 clear_lval(&lv);
13312}
13313
Bram Moolenaar33570922005-01-25 22:26:29 +000013314static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013315
13316/*
13317 * Turn a dict into a list:
13318 * "what" == 0: list of keys
13319 * "what" == 1: list of values
13320 * "what" == 2: list of items
13321 */
13322 static void
13323dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013324 typval_T *argvars;
13325 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013326 int what;
13327{
Bram Moolenaar33570922005-01-25 22:26:29 +000013328 list_T *l2;
13329 dictitem_T *di;
13330 hashitem_T *hi;
13331 listitem_T *li;
13332 listitem_T *li2;
13333 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013334 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013335
Bram Moolenaar8c711452005-01-14 21:53:12 +000013336 if (argvars[0].v_type != VAR_DICT)
13337 {
13338 EMSG(_(e_dictreq));
13339 return;
13340 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013341 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013342 return;
13343
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013344 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013345 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013346
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013347 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013348 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013349 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013350 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013351 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013352 --todo;
13353 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013354
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013355 li = listitem_alloc();
13356 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013357 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013358 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013359
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013360 if (what == 0)
13361 {
13362 /* keys() */
13363 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013364 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013365 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13366 }
13367 else if (what == 1)
13368 {
13369 /* values() */
13370 copy_tv(&di->di_tv, &li->li_tv);
13371 }
13372 else
13373 {
13374 /* items() */
13375 l2 = list_alloc();
13376 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013377 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013378 li->li_tv.vval.v_list = l2;
13379 if (l2 == NULL)
13380 break;
13381 ++l2->lv_refcount;
13382
13383 li2 = listitem_alloc();
13384 if (li2 == NULL)
13385 break;
13386 list_append(l2, li2);
13387 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013388 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013389 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13390
13391 li2 = listitem_alloc();
13392 if (li2 == NULL)
13393 break;
13394 list_append(l2, li2);
13395 copy_tv(&di->di_tv, &li2->li_tv);
13396 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013397 }
13398 }
13399}
13400
13401/*
13402 * "items(dict)" function
13403 */
13404 static void
13405f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 typval_T *argvars;
13407 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013408{
13409 dict_list(argvars, rettv, 2);
13410}
13411
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013413 * "join()" function
13414 */
13415 static void
13416f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 typval_T *argvars;
13418 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013419{
13420 garray_T ga;
13421 char_u *sep;
13422
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013423 if (argvars[0].v_type != VAR_LIST)
13424 {
13425 EMSG(_(e_listreq));
13426 return;
13427 }
13428 if (argvars[0].vval.v_list == NULL)
13429 return;
13430 if (argvars[1].v_type == VAR_UNKNOWN)
13431 sep = (char_u *)" ";
13432 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013433 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013434
13435 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436
13437 if (sep != NULL)
13438 {
13439 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013440 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013441 ga_append(&ga, NUL);
13442 rettv->vval.v_string = (char_u *)ga.ga_data;
13443 }
13444 else
13445 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013446}
13447
13448/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013449 * "keys()" function
13450 */
13451 static void
13452f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 typval_T *argvars;
13454 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013455{
13456 dict_list(argvars, rettv, 0);
13457}
13458
13459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 * "last_buffer_nr()" function.
13461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466{
13467 int n = 0;
13468 buf_T *buf;
13469
13470 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13471 if (n < buf->b_fnum)
13472 n = buf->b_fnum;
13473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475}
13476
13477/*
13478 * "len()" function
13479 */
13480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 typval_T *argvars;
13483 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013484{
13485 switch (argvars[0].v_type)
13486 {
13487 case VAR_STRING:
13488 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489 rettv->vval.v_number = (varnumber_T)STRLEN(
13490 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013491 break;
13492 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013494 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013495 case VAR_DICT:
13496 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13497 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013498 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013499 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013500 break;
13501 }
13502}
13503
Bram Moolenaar33570922005-01-25 22:26:29 +000013504static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013505
13506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013508 typval_T *argvars;
13509 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013510 int type;
13511{
13512#ifdef FEAT_LIBCALL
13513 char_u *string_in;
13514 char_u **string_result;
13515 int nr_result;
13516#endif
13517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013519 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013521
13522 if (check_restricted() || check_secure())
13523 return;
13524
13525#ifdef FEAT_LIBCALL
13526 /* The first two args must be strings, otherwise its meaningless */
13527 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13528 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013529 string_in = NULL;
13530 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013531 string_in = argvars[2].vval.v_string;
13532 if (type == VAR_NUMBER)
13533 string_result = NULL;
13534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013536 if (mch_libcall(argvars[0].vval.v_string,
13537 argvars[1].vval.v_string,
13538 string_in,
13539 argvars[2].vval.v_number,
13540 string_result,
13541 &nr_result) == OK
13542 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013544 }
13545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546}
13547
13548/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549 * "libcall()" function
13550 */
13551 static void
13552f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013553 typval_T *argvars;
13554 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013555{
13556 libcall_common(argvars, rettv, VAR_STRING);
13557}
13558
13559/*
13560 * "libcallnr()" function
13561 */
13562 static void
13563f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *argvars;
13565 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013566{
13567 libcall_common(argvars, rettv, VAR_NUMBER);
13568}
13569
13570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 * "line(string)" function
13572 */
13573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 typval_T *argvars;
13576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577{
13578 linenr_T lnum = 0;
13579 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013580 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013582 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 if (fp != NULL)
13584 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
13588/*
13589 * "line2byte(lnum)" function
13590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
13596#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598#else
13599 linenr_T lnum;
13600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13606 if (rettv->vval.v_number >= 0)
13607 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608#endif
13609}
13610
13611/*
13612 * "lispindent(lnum)" function
13613 */
13614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013616 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618{
13619#ifdef FEAT_LISP
13620 pos_T pos;
13621 linenr_T lnum;
13622
13623 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13626 {
13627 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 curwin->w_cursor = pos;
13630 }
13631 else
13632#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634}
13635
13636/*
13637 * "localtime()" function
13638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645}
13646
Bram Moolenaar33570922005-01-25 22:26:29 +000013647static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648
13649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013651 typval_T *argvars;
13652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 int exact;
13654{
13655 char_u *keys;
13656 char_u *which;
13657 char_u buf[NUMBUFLEN];
13658 char_u *keys_buf = NULL;
13659 char_u *rhs;
13660 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013661 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013662 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013663 mapblock_T *mp;
13664 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665
13666 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->v_type = VAR_STRING;
13668 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 if (*keys == NUL)
13672 return;
13673
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013674 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013677 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013678 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013679 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013680 if (argvars[3].v_type != VAR_UNKNOWN)
13681 get_dict = get_tv_number(&argvars[3]);
13682 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684 else
13685 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013686 if (which == NULL)
13687 return;
13688
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 mode = get_map_mode(&which, 0);
13690
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013691 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013692 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013694
13695 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013697 /* Return a string. */
13698 if (rhs != NULL)
13699 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700
Bram Moolenaarbd743252010-10-20 21:23:33 +020013701 }
13702 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13703 {
13704 /* Return a dictionary. */
13705 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13706 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13707 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708
Bram Moolenaarbd743252010-10-20 21:23:33 +020013709 dict_add_nr_str(dict, "lhs", 0L, lhs);
13710 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13711 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13712 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13713 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13714 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13715 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13716 dict_add_nr_str(dict, "mode", 0L, mapmode);
13717
13718 vim_free(lhs);
13719 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 }
13721}
13722
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013723#ifdef FEAT_FLOAT
13724/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013725 * "log()" function
13726 */
13727 static void
13728f_log(argvars, rettv)
13729 typval_T *argvars;
13730 typval_T *rettv;
13731{
13732 float_T f;
13733
13734 rettv->v_type = VAR_FLOAT;
13735 if (get_float_arg(argvars, &f) == OK)
13736 rettv->vval.v_float = log(f);
13737 else
13738 rettv->vval.v_float = 0.0;
13739}
13740
13741/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013742 * "log10()" function
13743 */
13744 static void
13745f_log10(argvars, rettv)
13746 typval_T *argvars;
13747 typval_T *rettv;
13748{
13749 float_T f;
13750
13751 rettv->v_type = VAR_FLOAT;
13752 if (get_float_arg(argvars, &f) == OK)
13753 rettv->vval.v_float = log10(f);
13754 else
13755 rettv->vval.v_float = 0.0;
13756}
13757#endif
13758
Bram Moolenaar1dced572012-04-05 16:54:08 +020013759#ifdef FEAT_LUA
13760/*
13761 * "luaeval()" function
13762 */
13763 static void
13764f_luaeval(argvars, rettv)
13765 typval_T *argvars;
13766 typval_T *rettv;
13767{
13768 char_u *str;
13769 char_u buf[NUMBUFLEN];
13770
13771 str = get_tv_string_buf(&argvars[0], buf);
13772 do_luaeval(str, argvars + 1, rettv);
13773}
13774#endif
13775
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013777 * "map()" function
13778 */
13779 static void
13780f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013781 typval_T *argvars;
13782 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013783{
13784 filter_map(argvars, rettv, TRUE);
13785}
13786
13787/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013788 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 */
13790 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013791f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 typval_T *argvars;
13793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013795 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796}
13797
13798/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 */
13801 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013802f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013803 typval_T *argvars;
13804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013806 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807}
13808
Bram Moolenaar33570922005-01-25 22:26:29 +000013809static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810
13811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 typval_T *argvars;
13814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 int type;
13816{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013817 char_u *str = NULL;
13818 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 char_u *pat;
13820 regmatch_T regmatch;
13821 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013822 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 char_u *save_cpo;
13824 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013825 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013826 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013827 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013828 list_T *l = NULL;
13829 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013830 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013831 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832
13833 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13834 save_cpo = p_cpo;
13835 p_cpo = (char_u *)"";
13836
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013837 rettv->vval.v_number = -1;
13838 if (type == 3)
13839 {
13840 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013841 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013842 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013843 }
13844 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013846 rettv->v_type = VAR_STRING;
13847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013850 if (argvars[0].v_type == VAR_LIST)
13851 {
13852 if ((l = argvars[0].vval.v_list) == NULL)
13853 goto theend;
13854 li = l->lv_first;
13855 }
13856 else
13857 expr = str = get_tv_string(&argvars[0]);
13858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13860 if (pat == NULL)
13861 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013862
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013863 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 int error = FALSE;
13866
13867 start = get_tv_number_chk(&argvars[2], &error);
13868 if (error)
13869 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013870 if (l != NULL)
13871 {
13872 li = list_find(l, start);
13873 if (li == NULL)
13874 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013875 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013876 }
13877 else
13878 {
13879 if (start < 0)
13880 start = 0;
13881 if (start > (long)STRLEN(str))
13882 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013883 /* When "count" argument is there ignore matches before "start",
13884 * otherwise skip part of the string. Differs when pattern is "^"
13885 * or "\<". */
13886 if (argvars[3].v_type != VAR_UNKNOWN)
13887 startcol = start;
13888 else
13889 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013890 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013891
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013892 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893 nth = get_tv_number_chk(&argvars[3], &error);
13894 if (error)
13895 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 }
13897
13898 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13899 if (regmatch.regprog != NULL)
13900 {
13901 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013902
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013903 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013904 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 if (l != NULL)
13906 {
13907 if (li == NULL)
13908 {
13909 match = FALSE;
13910 break;
13911 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013912 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013913 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013914 if (str == NULL)
13915 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013916 }
13917
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013918 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013919
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013921 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013922 if (l == NULL && !match)
13923 break;
13924
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013925 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013926 if (l != NULL)
13927 {
13928 li = li->li_next;
13929 ++idx;
13930 }
13931 else
13932 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013933#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013934 startcol = (colnr_T)(regmatch.startp[0]
13935 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013936#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013937 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013938#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013939 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013940 }
13941
13942 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013944 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013945 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013946 int i;
13947
13948 /* return list with matched string and submatches */
13949 for (i = 0; i < NSUBEXP; ++i)
13950 {
13951 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013952 {
13953 if (list_append_string(rettv->vval.v_list,
13954 (char_u *)"", 0) == FAIL)
13955 break;
13956 }
13957 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013958 regmatch.startp[i],
13959 (int)(regmatch.endp[i] - regmatch.startp[i]))
13960 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013961 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013962 }
13963 }
13964 else if (type == 2)
13965 {
13966 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013967 if (l != NULL)
13968 copy_tv(&li->li_tv, rettv);
13969 else
13970 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013972 }
13973 else if (l != NULL)
13974 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 else
13976 {
13977 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 (varnumber_T)(regmatch.startp[0] - str);
13980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013983 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 }
13985 }
Bram Moolenaar473de612013-06-08 18:19:48 +020013986 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 }
13988
13989theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013990 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 p_cpo = save_cpo;
13992}
13993
13994/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013995 * "match()" function
13996 */
13997 static void
13998f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013999 typval_T *argvars;
14000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014001{
14002 find_some_match(argvars, rettv, 1);
14003}
14004
14005/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014006 * "matchadd()" function
14007 */
14008 static void
14009f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014010 typval_T *argvars UNUSED;
14011 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014012{
14013#ifdef FEAT_SEARCH_EXTRA
14014 char_u buf[NUMBUFLEN];
14015 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14016 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14017 int prio = 10; /* default priority */
14018 int id = -1;
14019 int error = FALSE;
14020
14021 rettv->vval.v_number = -1;
14022
14023 if (grp == NULL || pat == NULL)
14024 return;
14025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014026 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014027 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014028 if (argvars[3].v_type != VAR_UNKNOWN)
14029 id = get_tv_number_chk(&argvars[3], &error);
14030 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014031 if (error == TRUE)
14032 return;
14033 if (id >= 1 && id <= 3)
14034 {
14035 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14036 return;
14037 }
14038
14039 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14040#endif
14041}
14042
14043/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014044 * "matcharg()" function
14045 */
14046 static void
14047f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014048 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014049 typval_T *rettv;
14050{
14051 if (rettv_list_alloc(rettv) == OK)
14052 {
14053#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014054 int id = get_tv_number(&argvars[0]);
14055 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014056
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014057 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014058 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014059 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14060 {
14061 list_append_string(rettv->vval.v_list,
14062 syn_id2name(m->hlg_id), -1);
14063 list_append_string(rettv->vval.v_list, m->pattern, -1);
14064 }
14065 else
14066 {
14067 list_append_string(rettv->vval.v_list, NUL, -1);
14068 list_append_string(rettv->vval.v_list, NUL, -1);
14069 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014070 }
14071#endif
14072 }
14073}
14074
14075/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014076 * "matchdelete()" function
14077 */
14078 static void
14079f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014080 typval_T *argvars UNUSED;
14081 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014082{
14083#ifdef FEAT_SEARCH_EXTRA
14084 rettv->vval.v_number = match_delete(curwin,
14085 (int)get_tv_number(&argvars[0]), TRUE);
14086#endif
14087}
14088
14089/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090 * "matchend()" function
14091 */
14092 static void
14093f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014094 typval_T *argvars;
14095 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096{
14097 find_some_match(argvars, rettv, 0);
14098}
14099
14100/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014101 * "matchlist()" function
14102 */
14103 static void
14104f_matchlist(argvars, rettv)
14105 typval_T *argvars;
14106 typval_T *rettv;
14107{
14108 find_some_match(argvars, rettv, 3);
14109}
14110
14111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112 * "matchstr()" function
14113 */
14114 static void
14115f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 typval_T *argvars;
14117 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014118{
14119 find_some_match(argvars, rettv, 2);
14120}
14121
Bram Moolenaar33570922005-01-25 22:26:29 +000014122static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014123
14124 static void
14125max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 typval_T *argvars;
14127 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014128 int domax;
14129{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014130 long n = 0;
14131 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014133
14134 if (argvars[0].v_type == VAR_LIST)
14135 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014136 list_T *l;
14137 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014138
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014139 l = argvars[0].vval.v_list;
14140 if (l != NULL)
14141 {
14142 li = l->lv_first;
14143 if (li != NULL)
14144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014146 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014147 {
14148 li = li->li_next;
14149 if (li == NULL)
14150 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014152 if (domax ? i > n : i < n)
14153 n = i;
14154 }
14155 }
14156 }
14157 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014158 else if (argvars[0].v_type == VAR_DICT)
14159 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014161 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014162 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014163 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014164
14165 d = argvars[0].vval.v_dict;
14166 if (d != NULL)
14167 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014168 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014170 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014171 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014172 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014173 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014175 if (first)
14176 {
14177 n = i;
14178 first = FALSE;
14179 }
14180 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014181 n = i;
14182 }
14183 }
14184 }
14185 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014186 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014187 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014189}
14190
14191/*
14192 * "max()" function
14193 */
14194 static void
14195f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014196 typval_T *argvars;
14197 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014198{
14199 max_min(argvars, rettv, TRUE);
14200}
14201
14202/*
14203 * "min()" function
14204 */
14205 static void
14206f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014207 typval_T *argvars;
14208 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014209{
14210 max_min(argvars, rettv, FALSE);
14211}
14212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014213static int mkdir_recurse __ARGS((char_u *dir, int prot));
14214
14215/*
14216 * Create the directory in which "dir" is located, and higher levels when
14217 * needed.
14218 */
14219 static int
14220mkdir_recurse(dir, prot)
14221 char_u *dir;
14222 int prot;
14223{
14224 char_u *p;
14225 char_u *updir;
14226 int r = FAIL;
14227
14228 /* Get end of directory name in "dir".
14229 * We're done when it's "/" or "c:/". */
14230 p = gettail_sep(dir);
14231 if (p <= get_past_head(dir))
14232 return OK;
14233
14234 /* If the directory exists we're done. Otherwise: create it.*/
14235 updir = vim_strnsave(dir, (int)(p - dir));
14236 if (updir == NULL)
14237 return FAIL;
14238 if (mch_isdir(updir))
14239 r = OK;
14240 else if (mkdir_recurse(updir, prot) == OK)
14241 r = vim_mkdir_emsg(updir, prot);
14242 vim_free(updir);
14243 return r;
14244}
14245
14246#ifdef vim_mkdir
14247/*
14248 * "mkdir()" function
14249 */
14250 static void
14251f_mkdir(argvars, rettv)
14252 typval_T *argvars;
14253 typval_T *rettv;
14254{
14255 char_u *dir;
14256 char_u buf[NUMBUFLEN];
14257 int prot = 0755;
14258
14259 rettv->vval.v_number = FAIL;
14260 if (check_restricted() || check_secure())
14261 return;
14262
14263 dir = get_tv_string_buf(&argvars[0], buf);
14264 if (argvars[1].v_type != VAR_UNKNOWN)
14265 {
14266 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 prot = get_tv_number_chk(&argvars[2], NULL);
14268 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014269 mkdir_recurse(dir, prot);
14270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014272}
14273#endif
14274
Bram Moolenaar0d660222005-01-07 21:51:51 +000014275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 * "mode()" function
14277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014280 typval_T *argvars;
14281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014283 char_u buf[3];
14284
14285 buf[1] = NUL;
14286 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287
14288#ifdef FEAT_VISUAL
14289 if (VIsual_active)
14290 {
14291 if (VIsual_select)
14292 buf[0] = VIsual_mode + 's' - 'v';
14293 else
14294 buf[0] = VIsual_mode;
14295 }
14296 else
14297#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014298 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14299 || State == CONFIRM)
14300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014302 if (State == ASKMORE)
14303 buf[1] = 'm';
14304 else if (State == CONFIRM)
14305 buf[1] = '?';
14306 }
14307 else if (State == EXTERNCMD)
14308 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 else if (State & INSERT)
14310 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014311#ifdef FEAT_VREPLACE
14312 if (State & VREPLACE_FLAG)
14313 {
14314 buf[0] = 'R';
14315 buf[1] = 'v';
14316 }
14317 else
14318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 if (State & REPLACE_FLAG)
14320 buf[0] = 'R';
14321 else
14322 buf[0] = 'i';
14323 }
14324 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014325 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014327 if (exmode_active)
14328 buf[1] = 'v';
14329 }
14330 else if (exmode_active)
14331 {
14332 buf[0] = 'c';
14333 buf[1] = 'e';
14334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014338 if (finish_op)
14339 buf[1] = 'o';
14340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014342 /* Clear out the minor mode when the argument is not a non-zero number or
14343 * non-empty string. */
14344 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014345 buf[1] = NUL;
14346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 rettv->vval.v_string = vim_strsave(buf);
14348 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349}
14350
Bram Moolenaar429fa852013-04-15 12:27:36 +020014351#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014352/*
14353 * "mzeval()" function
14354 */
14355 static void
14356f_mzeval(argvars, rettv)
14357 typval_T *argvars;
14358 typval_T *rettv;
14359{
14360 char_u *str;
14361 char_u buf[NUMBUFLEN];
14362
14363 str = get_tv_string_buf(&argvars[0], buf);
14364 do_mzeval(str, rettv);
14365}
Bram Moolenaar75676462013-01-30 14:55:42 +010014366
14367 void
14368mzscheme_call_vim(name, args, rettv)
14369 char_u *name;
14370 typval_T *args;
14371 typval_T *rettv;
14372{
14373 typval_T argvars[3];
14374
14375 argvars[0].v_type = VAR_STRING;
14376 argvars[0].vval.v_string = name;
14377 copy_tv(args, &argvars[1]);
14378 argvars[2].v_type = VAR_UNKNOWN;
14379 f_call(argvars, rettv);
14380 clear_tv(&argvars[1]);
14381}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014382#endif
14383
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385 * "nextnonblank()" function
14386 */
14387 static void
14388f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014389 typval_T *argvars;
14390 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014391{
14392 linenr_T lnum;
14393
14394 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014397 {
14398 lnum = 0;
14399 break;
14400 }
14401 if (*skipwhite(ml_get(lnum)) != NUL)
14402 break;
14403 }
14404 rettv->vval.v_number = lnum;
14405}
14406
14407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 * "nr2char()" function
14409 */
14410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014412 typval_T *argvars;
14413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414{
14415 char_u buf[NUMBUFLEN];
14416
14417#ifdef FEAT_MBYTE
14418 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014419 {
14420 int utf8 = 0;
14421
14422 if (argvars[1].v_type != VAR_UNKNOWN)
14423 utf8 = get_tv_number_chk(&argvars[1], NULL);
14424 if (utf8)
14425 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14426 else
14427 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 else
14430#endif
14431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 buf[1] = NUL;
14434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 rettv->v_type = VAR_STRING;
14436 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014437}
14438
14439/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014440 * "or(expr, expr)" function
14441 */
14442 static void
14443f_or(argvars, rettv)
14444 typval_T *argvars;
14445 typval_T *rettv;
14446{
14447 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14448 | get_tv_number_chk(&argvars[1], NULL);
14449}
14450
14451/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014452 * "pathshorten()" function
14453 */
14454 static void
14455f_pathshorten(argvars, rettv)
14456 typval_T *argvars;
14457 typval_T *rettv;
14458{
14459 char_u *p;
14460
14461 rettv->v_type = VAR_STRING;
14462 p = get_tv_string_chk(&argvars[0]);
14463 if (p == NULL)
14464 rettv->vval.v_string = NULL;
14465 else
14466 {
14467 p = vim_strsave(p);
14468 rettv->vval.v_string = p;
14469 if (p != NULL)
14470 shorten_dir(p);
14471 }
14472}
14473
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014474#ifdef FEAT_FLOAT
14475/*
14476 * "pow()" function
14477 */
14478 static void
14479f_pow(argvars, rettv)
14480 typval_T *argvars;
14481 typval_T *rettv;
14482{
14483 float_T fx, fy;
14484
14485 rettv->v_type = VAR_FLOAT;
14486 if (get_float_arg(argvars, &fx) == OK
14487 && get_float_arg(&argvars[1], &fy) == OK)
14488 rettv->vval.v_float = pow(fx, fy);
14489 else
14490 rettv->vval.v_float = 0.0;
14491}
14492#endif
14493
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014494/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495 * "prevnonblank()" function
14496 */
14497 static void
14498f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014499 typval_T *argvars;
14500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014501{
14502 linenr_T lnum;
14503
14504 lnum = get_tv_lnum(argvars);
14505 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14506 lnum = 0;
14507 else
14508 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14509 --lnum;
14510 rettv->vval.v_number = lnum;
14511}
14512
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014513#ifdef HAVE_STDARG_H
14514/* This dummy va_list is here because:
14515 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14516 * - locally in the function results in a "used before set" warning
14517 * - using va_start() to initialize it gives "function with fixed args" error */
14518static va_list ap;
14519#endif
14520
Bram Moolenaar8c711452005-01-14 21:53:12 +000014521/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014522 * "printf()" function
14523 */
14524 static void
14525f_printf(argvars, rettv)
14526 typval_T *argvars;
14527 typval_T *rettv;
14528{
14529 rettv->v_type = VAR_STRING;
14530 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014531#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014532 {
14533 char_u buf[NUMBUFLEN];
14534 int len;
14535 char_u *s;
14536 int saved_did_emsg = did_emsg;
14537 char *fmt;
14538
14539 /* Get the required length, allocate the buffer and do it for real. */
14540 did_emsg = FALSE;
14541 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014542 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014543 if (!did_emsg)
14544 {
14545 s = alloc(len + 1);
14546 if (s != NULL)
14547 {
14548 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014549 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014550 }
14551 }
14552 did_emsg |= saved_did_emsg;
14553 }
14554#endif
14555}
14556
14557/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014558 * "pumvisible()" function
14559 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014560 static void
14561f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014562 typval_T *argvars UNUSED;
14563 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014564{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014565#ifdef FEAT_INS_EXPAND
14566 if (pum_visible())
14567 rettv->vval.v_number = 1;
14568#endif
14569}
14570
Bram Moolenaardb913952012-06-29 12:54:53 +020014571#ifdef FEAT_PYTHON3
14572/*
14573 * "py3eval()" function
14574 */
14575 static void
14576f_py3eval(argvars, rettv)
14577 typval_T *argvars;
14578 typval_T *rettv;
14579{
14580 char_u *str;
14581 char_u buf[NUMBUFLEN];
14582
14583 str = get_tv_string_buf(&argvars[0], buf);
14584 do_py3eval(str, rettv);
14585}
14586#endif
14587
14588#ifdef FEAT_PYTHON
14589/*
14590 * "pyeval()" function
14591 */
14592 static void
14593f_pyeval(argvars, rettv)
14594 typval_T *argvars;
14595 typval_T *rettv;
14596{
14597 char_u *str;
14598 char_u buf[NUMBUFLEN];
14599
14600 str = get_tv_string_buf(&argvars[0], buf);
14601 do_pyeval(str, rettv);
14602}
14603#endif
14604
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014606 * "range()" function
14607 */
14608 static void
14609f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014610 typval_T *argvars;
14611 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014612{
14613 long start;
14614 long end;
14615 long stride = 1;
14616 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014617 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014620 if (argvars[1].v_type == VAR_UNKNOWN)
14621 {
14622 end = start - 1;
14623 start = 0;
14624 }
14625 else
14626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014630 }
14631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 if (error)
14633 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014634 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014635 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014636 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014637 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014638 else
14639 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014640 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014641 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014642 if (list_append_number(rettv->vval.v_list,
14643 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014644 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014645 }
14646}
14647
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014648/*
14649 * "readfile()" function
14650 */
14651 static void
14652f_readfile(argvars, rettv)
14653 typval_T *argvars;
14654 typval_T *rettv;
14655{
14656 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014657 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014658 char_u *fname;
14659 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014660 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14661 int io_size = sizeof(buf);
14662 int readlen; /* size of last fread() */
14663 char_u *prev = NULL; /* previously read bytes, if any */
14664 long prevlen = 0; /* length of data in prev */
14665 long prevsize = 0; /* size of prev buffer */
14666 long maxline = MAXLNUM;
14667 long cnt = 0;
14668 char_u *p; /* position in buf */
14669 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014670
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014671 if (argvars[1].v_type != VAR_UNKNOWN)
14672 {
14673 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14674 binary = TRUE;
14675 if (argvars[2].v_type != VAR_UNKNOWN)
14676 maxline = get_tv_number(&argvars[2]);
14677 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014679 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014680 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014681
14682 /* Always open the file in binary mode, library functions have a mind of
14683 * their own about CR-LF conversion. */
14684 fname = get_tv_string(&argvars[0]);
14685 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14686 {
14687 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14688 return;
14689 }
14690
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014691 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014692 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014693 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014694
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014695 /* This for loop processes what was read, but is also entered at end
14696 * of file so that either:
14697 * - an incomplete line gets written
14698 * - a "binary" file gets an empty line at the end if it ends in a
14699 * newline. */
14700 for (p = buf, start = buf;
14701 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14702 ++p)
14703 {
14704 if (*p == '\n' || readlen <= 0)
14705 {
14706 listitem_T *li;
14707 char_u *s = NULL;
14708 long_u len = p - start;
14709
14710 /* Finished a line. Remove CRs before NL. */
14711 if (readlen > 0 && !binary)
14712 {
14713 while (len > 0 && start[len - 1] == '\r')
14714 --len;
14715 /* removal may cross back to the "prev" string */
14716 if (len == 0)
14717 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14718 --prevlen;
14719 }
14720 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014721 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014722 else
14723 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014724 /* Change "prev" buffer to be the right size. This way
14725 * the bytes are only copied once, and very long lines are
14726 * allocated only once. */
14727 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014729 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014730 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014731 prev = NULL; /* the list will own the string */
14732 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014733 }
14734 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014735 if (s == NULL)
14736 {
14737 do_outofmem_msg((long_u) prevlen + len + 1);
14738 failed = TRUE;
14739 break;
14740 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014741
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014742 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014743 {
14744 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014745 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014746 break;
14747 }
14748 li->li_tv.v_type = VAR_STRING;
14749 li->li_tv.v_lock = 0;
14750 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014751 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 start = p + 1; /* step over newline */
14754 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014755 break;
14756 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014757 else if (*p == NUL)
14758 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014759#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014760 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14761 * when finding the BF and check the previous two bytes. */
14762 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014763 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014764 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14765 * + 1, these may be in the "prev" string. */
14766 char_u back1 = p >= buf + 1 ? p[-1]
14767 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14768 char_u back2 = p >= buf + 2 ? p[-2]
14769 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14770 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014771
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014772 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014773 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014774 char_u *dest = p - 2;
14775
14776 /* Usually a BOM is at the beginning of a file, and so at
14777 * the beginning of a line; then we can just step over it.
14778 */
14779 if (start == dest)
14780 start = p + 1;
14781 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014782 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014783 /* have to shuffle buf to close gap */
14784 int adjust_prevlen = 0;
14785
14786 if (dest < buf)
14787 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014788 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014789 dest = buf;
14790 }
14791 if (readlen > p - buf + 1)
14792 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14793 readlen -= 3 - adjust_prevlen;
14794 prevlen -= adjust_prevlen;
14795 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014796 }
14797 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014798 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014799#endif
14800 } /* for */
14801
14802 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14803 break;
14804 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014805 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014806 /* There's part of a line in buf, store it in "prev". */
14807 if (p - start + prevlen >= prevsize)
14808 {
14809 /* need bigger "prev" buffer */
14810 char_u *newprev;
14811
14812 /* A common use case is ordinary text files and "prev" gets a
14813 * fragment of a line, so the first allocation is made
14814 * small, to avoid repeatedly 'allocing' large and
14815 * 'reallocing' small. */
14816 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014817 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014818 else
14819 {
14820 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014821 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822 prevsize = grow50pc > growmin ? grow50pc : growmin;
14823 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014824 newprev = prev == NULL ? alloc(prevsize)
14825 : vim_realloc(prev, prevsize);
14826 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014827 {
14828 do_outofmem_msg((long_u)prevsize);
14829 failed = TRUE;
14830 break;
14831 }
14832 prev = newprev;
14833 }
14834 /* Add the line part to end of "prev". */
14835 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014836 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014837 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014838 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014839
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014840 /*
14841 * For a negative line count use only the lines at the end of the file,
14842 * free the rest.
14843 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014844 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014845 while (cnt > -maxline)
14846 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014847 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014848 --cnt;
14849 }
14850
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014851 if (failed)
14852 {
14853 list_free(rettv->vval.v_list, TRUE);
14854 /* readfile doc says an empty list is returned on error */
14855 rettv->vval.v_list = list_alloc();
14856 }
14857
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014858 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014859 fclose(fd);
14860}
14861
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014862#if defined(FEAT_RELTIME)
14863static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14864
14865/*
14866 * Convert a List to proftime_T.
14867 * Return FAIL when there is something wrong.
14868 */
14869 static int
14870list2proftime(arg, tm)
14871 typval_T *arg;
14872 proftime_T *tm;
14873{
14874 long n1, n2;
14875 int error = FALSE;
14876
14877 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14878 || arg->vval.v_list->lv_len != 2)
14879 return FAIL;
14880 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14881 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14882# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014883 tm->HighPart = n1;
14884 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014885# else
14886 tm->tv_sec = n1;
14887 tm->tv_usec = n2;
14888# endif
14889 return error ? FAIL : OK;
14890}
14891#endif /* FEAT_RELTIME */
14892
14893/*
14894 * "reltime()" function
14895 */
14896 static void
14897f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014898 typval_T *argvars UNUSED;
14899 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014900{
14901#ifdef FEAT_RELTIME
14902 proftime_T res;
14903 proftime_T start;
14904
14905 if (argvars[0].v_type == VAR_UNKNOWN)
14906 {
14907 /* No arguments: get current time. */
14908 profile_start(&res);
14909 }
14910 else if (argvars[1].v_type == VAR_UNKNOWN)
14911 {
14912 if (list2proftime(&argvars[0], &res) == FAIL)
14913 return;
14914 profile_end(&res);
14915 }
14916 else
14917 {
14918 /* Two arguments: compute the difference. */
14919 if (list2proftime(&argvars[0], &start) == FAIL
14920 || list2proftime(&argvars[1], &res) == FAIL)
14921 return;
14922 profile_sub(&res, &start);
14923 }
14924
14925 if (rettv_list_alloc(rettv) == OK)
14926 {
14927 long n1, n2;
14928
14929# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014930 n1 = res.HighPart;
14931 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014932# else
14933 n1 = res.tv_sec;
14934 n2 = res.tv_usec;
14935# endif
14936 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14937 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14938 }
14939#endif
14940}
14941
14942/*
14943 * "reltimestr()" function
14944 */
14945 static void
14946f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014947 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014948 typval_T *rettv;
14949{
14950#ifdef FEAT_RELTIME
14951 proftime_T tm;
14952#endif
14953
14954 rettv->v_type = VAR_STRING;
14955 rettv->vval.v_string = NULL;
14956#ifdef FEAT_RELTIME
14957 if (list2proftime(&argvars[0], &tm) == OK)
14958 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14959#endif
14960}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014961
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14963static void make_connection __ARGS((void));
14964static int check_connection __ARGS((void));
14965
14966 static void
14967make_connection()
14968{
14969 if (X_DISPLAY == NULL
14970# ifdef FEAT_GUI
14971 && !gui.in_use
14972# endif
14973 )
14974 {
14975 x_force_connect = TRUE;
14976 setup_term_clip();
14977 x_force_connect = FALSE;
14978 }
14979}
14980
14981 static int
14982check_connection()
14983{
14984 make_connection();
14985 if (X_DISPLAY == NULL)
14986 {
14987 EMSG(_("E240: No connection to Vim server"));
14988 return FAIL;
14989 }
14990 return OK;
14991}
14992#endif
14993
14994#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014995static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996
14997 static void
14998remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014999 typval_T *argvars;
15000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015001 int expr;
15002{
15003 char_u *server_name;
15004 char_u *keys;
15005 char_u *r = NULL;
15006 char_u buf[NUMBUFLEN];
15007# ifdef WIN32
15008 HWND w;
15009# else
15010 Window w;
15011# endif
15012
15013 if (check_restricted() || check_secure())
15014 return;
15015
15016# ifdef FEAT_X11
15017 if (check_connection() == FAIL)
15018 return;
15019# endif
15020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015021 server_name = get_tv_string_chk(&argvars[0]);
15022 if (server_name == NULL)
15023 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 keys = get_tv_string_buf(&argvars[1], buf);
15025# ifdef WIN32
15026 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15027# else
15028 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15029 < 0)
15030# endif
15031 {
15032 if (r != NULL)
15033 EMSG(r); /* sending worked but evaluation failed */
15034 else
15035 EMSG2(_("E241: Unable to send to %s"), server_name);
15036 return;
15037 }
15038
15039 rettv->vval.v_string = r;
15040
15041 if (argvars[2].v_type != VAR_UNKNOWN)
15042 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015043 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015044 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015047 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015048 v.di_tv.v_type = VAR_STRING;
15049 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 idvar = get_tv_string_chk(&argvars[2]);
15051 if (idvar != NULL)
15052 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015053 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015054 }
15055}
15056#endif
15057
15058/*
15059 * "remote_expr()" function
15060 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061 static void
15062f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065{
15066 rettv->v_type = VAR_STRING;
15067 rettv->vval.v_string = NULL;
15068#ifdef FEAT_CLIENTSERVER
15069 remote_common(argvars, rettv, TRUE);
15070#endif
15071}
15072
15073/*
15074 * "remote_foreground()" function
15075 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076 static void
15077f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015078 typval_T *argvars UNUSED;
15079 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081#ifdef FEAT_CLIENTSERVER
15082# ifdef WIN32
15083 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015084 {
15085 char_u *server_name = get_tv_string_chk(&argvars[0]);
15086
15087 if (server_name != NULL)
15088 serverForeground(server_name);
15089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090# else
15091 /* Send a foreground() expression to the server. */
15092 argvars[1].v_type = VAR_STRING;
15093 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15094 argvars[2].v_type = VAR_UNKNOWN;
15095 remote_common(argvars, rettv, TRUE);
15096 vim_free(argvars[1].vval.v_string);
15097# endif
15098#endif
15099}
15100
Bram Moolenaar0d660222005-01-07 21:51:51 +000015101 static void
15102f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015103 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105{
15106#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015107 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108 char_u *s = NULL;
15109# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015110 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015112 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113
15114 if (check_restricted() || check_secure())
15115 {
15116 rettv->vval.v_number = -1;
15117 return;
15118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015119 serverid = get_tv_string_chk(&argvars[0]);
15120 if (serverid == NULL)
15121 {
15122 rettv->vval.v_number = -1;
15123 return; /* type error; errmsg already given */
15124 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015125# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015126 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 if (n == 0)
15128 rettv->vval.v_number = -1;
15129 else
15130 {
15131 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15132 rettv->vval.v_number = (s != NULL);
15133 }
15134# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135 if (check_connection() == FAIL)
15136 return;
15137
15138 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015139 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015140# endif
15141
15142 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015144 char_u *retvar;
15145
Bram Moolenaar33570922005-01-25 22:26:29 +000015146 v.di_tv.v_type = VAR_STRING;
15147 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 retvar = get_tv_string_chk(&argvars[1]);
15149 if (retvar != NULL)
15150 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015151 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152 }
15153#else
15154 rettv->vval.v_number = -1;
15155#endif
15156}
15157
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 static void
15159f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162{
15163 char_u *r = NULL;
15164
15165#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 char_u *serverid = get_tv_string_chk(&argvars[0]);
15167
15168 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169 {
15170# ifdef WIN32
15171 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015172 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015174 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175 if (n != 0)
15176 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15177 if (r == NULL)
15178# else
15179 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015180 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181# endif
15182 EMSG(_("E277: Unable to read a server reply"));
15183 }
15184#endif
15185 rettv->v_type = VAR_STRING;
15186 rettv->vval.v_string = r;
15187}
15188
15189/*
15190 * "remote_send()" function
15191 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192 static void
15193f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015194 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015195 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015196{
15197 rettv->v_type = VAR_STRING;
15198 rettv->vval.v_string = NULL;
15199#ifdef FEAT_CLIENTSERVER
15200 remote_common(argvars, rettv, FALSE);
15201#endif
15202}
15203
15204/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015205 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015206 */
15207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015209 typval_T *argvars;
15210 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015211{
Bram Moolenaar33570922005-01-25 22:26:29 +000015212 list_T *l;
15213 listitem_T *item, *item2;
15214 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015215 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015216 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015217 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 dict_T *d;
15219 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015220 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015221
Bram Moolenaar8c711452005-01-14 21:53:12 +000015222 if (argvars[0].v_type == VAR_DICT)
15223 {
15224 if (argvars[2].v_type != VAR_UNKNOWN)
15225 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015226 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015227 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 key = get_tv_string_chk(&argvars[1]);
15230 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015232 di = dict_find(d, key, -1);
15233 if (di == NULL)
15234 EMSG2(_(e_dictkey), key);
15235 else
15236 {
15237 *rettv = di->di_tv;
15238 init_tv(&di->di_tv);
15239 dictitem_remove(d, di);
15240 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015242 }
15243 }
15244 else if (argvars[0].v_type != VAR_LIST)
15245 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015246 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015247 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 int error = FALSE;
15250
15251 idx = get_tv_number_chk(&argvars[1], &error);
15252 if (error)
15253 ; /* type error: do nothing, errmsg already given */
15254 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015255 EMSGN(_(e_listidx), idx);
15256 else
15257 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015258 if (argvars[2].v_type == VAR_UNKNOWN)
15259 {
15260 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015261 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015262 *rettv = item->li_tv;
15263 vim_free(item);
15264 }
15265 else
15266 {
15267 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015268 end = get_tv_number_chk(&argvars[2], &error);
15269 if (error)
15270 ; /* type error: do nothing */
15271 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015272 EMSGN(_(e_listidx), end);
15273 else
15274 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015275 int cnt = 0;
15276
15277 for (li = item; li != NULL; li = li->li_next)
15278 {
15279 ++cnt;
15280 if (li == item2)
15281 break;
15282 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015283 if (li == NULL) /* didn't find "item2" after "item" */
15284 EMSG(_(e_invrange));
15285 else
15286 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015287 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015288 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015289 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015290 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015291 l->lv_first = item;
15292 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015293 item->li_prev = NULL;
15294 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015295 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015296 }
15297 }
15298 }
15299 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015300 }
15301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302}
15303
15304/*
15305 * "rename({from}, {to})" function
15306 */
15307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015309 typval_T *argvars;
15310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311{
15312 char_u buf[NUMBUFLEN];
15313
15314 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015315 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015317 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15318 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319}
15320
15321/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015322 * "repeat()" function
15323 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015324 static void
15325f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015326 typval_T *argvars;
15327 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015328{
15329 char_u *p;
15330 int n;
15331 int slen;
15332 int len;
15333 char_u *r;
15334 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015335
15336 n = get_tv_number(&argvars[1]);
15337 if (argvars[0].v_type == VAR_LIST)
15338 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015339 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015340 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015341 if (list_extend(rettv->vval.v_list,
15342 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015343 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015344 }
15345 else
15346 {
15347 p = get_tv_string(&argvars[0]);
15348 rettv->v_type = VAR_STRING;
15349 rettv->vval.v_string = NULL;
15350
15351 slen = (int)STRLEN(p);
15352 len = slen * n;
15353 if (len <= 0)
15354 return;
15355
15356 r = alloc(len + 1);
15357 if (r != NULL)
15358 {
15359 for (i = 0; i < n; i++)
15360 mch_memmove(r + i * slen, p, (size_t)slen);
15361 r[len] = NUL;
15362 }
15363
15364 rettv->vval.v_string = r;
15365 }
15366}
15367
15368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 * "resolve()" function
15370 */
15371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015373 typval_T *argvars;
15374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375{
15376 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015377#ifdef HAVE_READLINK
15378 char_u *buf = NULL;
15379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382#ifdef FEAT_SHORTCUT
15383 {
15384 char_u *v = NULL;
15385
15386 v = mch_resolve_shortcut(p);
15387 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 }
15392#else
15393# ifdef HAVE_READLINK
15394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 char_u *cpy;
15396 int len;
15397 char_u *remain = NULL;
15398 char_u *q;
15399 int is_relative_to_current = FALSE;
15400 int has_trailing_pathsep = FALSE;
15401 int limit = 100;
15402
15403 p = vim_strsave(p);
15404
15405 if (p[0] == '.' && (vim_ispathsep(p[1])
15406 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15407 is_relative_to_current = TRUE;
15408
15409 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015410 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015413 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415
15416 q = getnextcomp(p);
15417 if (*q != NUL)
15418 {
15419 /* Separate the first path component in "p", and keep the
15420 * remainder (beginning with the path separator). */
15421 remain = vim_strsave(q - 1);
15422 q[-1] = NUL;
15423 }
15424
Bram Moolenaard9462e32011-04-11 21:35:11 +020015425 buf = alloc(MAXPATHL + 1);
15426 if (buf == NULL)
15427 goto fail;
15428
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 for (;;)
15430 {
15431 for (;;)
15432 {
15433 len = readlink((char *)p, (char *)buf, MAXPATHL);
15434 if (len <= 0)
15435 break;
15436 buf[len] = NUL;
15437
15438 if (limit-- == 0)
15439 {
15440 vim_free(p);
15441 vim_free(remain);
15442 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 goto fail;
15445 }
15446
15447 /* Ensure that the result will have a trailing path separator
15448 * if the argument has one. */
15449 if (remain == NULL && has_trailing_pathsep)
15450 add_pathsep(buf);
15451
15452 /* Separate the first path component in the link value and
15453 * concatenate the remainders. */
15454 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15455 if (*q != NUL)
15456 {
15457 if (remain == NULL)
15458 remain = vim_strsave(q - 1);
15459 else
15460 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015461 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 if (cpy != NULL)
15463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 vim_free(remain);
15465 remain = cpy;
15466 }
15467 }
15468 q[-1] = NUL;
15469 }
15470
15471 q = gettail(p);
15472 if (q > p && *q == NUL)
15473 {
15474 /* Ignore trailing path separator. */
15475 q[-1] = NUL;
15476 q = gettail(p);
15477 }
15478 if (q > p && !mch_isFullName(buf))
15479 {
15480 /* symlink is relative to directory of argument */
15481 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15482 if (cpy != NULL)
15483 {
15484 STRCPY(cpy, p);
15485 STRCPY(gettail(cpy), buf);
15486 vim_free(p);
15487 p = cpy;
15488 }
15489 }
15490 else
15491 {
15492 vim_free(p);
15493 p = vim_strsave(buf);
15494 }
15495 }
15496
15497 if (remain == NULL)
15498 break;
15499
15500 /* Append the first path component of "remain" to "p". */
15501 q = getnextcomp(remain + 1);
15502 len = q - remain - (*q != NUL);
15503 cpy = vim_strnsave(p, STRLEN(p) + len);
15504 if (cpy != NULL)
15505 {
15506 STRNCAT(cpy, remain, len);
15507 vim_free(p);
15508 p = cpy;
15509 }
15510 /* Shorten "remain". */
15511 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015512 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 else
15514 {
15515 vim_free(remain);
15516 remain = NULL;
15517 }
15518 }
15519
15520 /* If the result is a relative path name, make it explicitly relative to
15521 * the current directory if and only if the argument had this form. */
15522 if (!vim_ispathsep(*p))
15523 {
15524 if (is_relative_to_current
15525 && *p != NUL
15526 && !(p[0] == '.'
15527 && (p[1] == NUL
15528 || vim_ispathsep(p[1])
15529 || (p[1] == '.'
15530 && (p[2] == NUL
15531 || vim_ispathsep(p[2]))))))
15532 {
15533 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015534 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 if (cpy != NULL)
15536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 vim_free(p);
15538 p = cpy;
15539 }
15540 }
15541 else if (!is_relative_to_current)
15542 {
15543 /* Strip leading "./". */
15544 q = p;
15545 while (q[0] == '.' && vim_ispathsep(q[1]))
15546 q += 2;
15547 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015548 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 }
15550 }
15551
15552 /* Ensure that the result will have no trailing path separator
15553 * if the argument had none. But keep "/" or "//". */
15554 if (!has_trailing_pathsep)
15555 {
15556 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015557 if (after_pathsep(p, q))
15558 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 }
15560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015561 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 }
15563# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565# endif
15566#endif
15567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015568 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569
15570#ifdef HAVE_READLINK
15571fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015572 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575}
15576
15577/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015578 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 */
15580 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015582 typval_T *argvars;
15583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584{
Bram Moolenaar33570922005-01-25 22:26:29 +000015585 list_T *l;
15586 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588 if (argvars[0].v_type != VAR_LIST)
15589 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015590 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015591 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 {
15593 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015594 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015595 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 while (li != NULL)
15597 {
15598 ni = li->li_prev;
15599 list_append(l, li);
15600 li = ni;
15601 }
15602 rettv->vval.v_list = l;
15603 rettv->v_type = VAR_LIST;
15604 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015605 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015609#define SP_NOMOVE 0x01 /* don't move cursor */
15610#define SP_REPEAT 0x02 /* repeat to find outer pair */
15611#define SP_RETCOUNT 0x04 /* return matchcount */
15612#define SP_SETPCMARK 0x08 /* set previous context mark */
15613#define SP_START 0x10 /* accept match at start position */
15614#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15615#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015616
Bram Moolenaar33570922005-01-25 22:26:29 +000015617static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015618
15619/*
15620 * Get flags for a search function.
15621 * Possibly sets "p_ws".
15622 * Returns BACKWARD, FORWARD or zero (for an error).
15623 */
15624 static int
15625get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015626 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015627 int *flagsp;
15628{
15629 int dir = FORWARD;
15630 char_u *flags;
15631 char_u nbuf[NUMBUFLEN];
15632 int mask;
15633
15634 if (varp->v_type != VAR_UNKNOWN)
15635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636 flags = get_tv_string_buf_chk(varp, nbuf);
15637 if (flags == NULL)
15638 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015639 while (*flags != NUL)
15640 {
15641 switch (*flags)
15642 {
15643 case 'b': dir = BACKWARD; break;
15644 case 'w': p_ws = TRUE; break;
15645 case 'W': p_ws = FALSE; break;
15646 default: mask = 0;
15647 if (flagsp != NULL)
15648 switch (*flags)
15649 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015650 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015651 case 'e': mask = SP_END; break;
15652 case 'm': mask = SP_RETCOUNT; break;
15653 case 'n': mask = SP_NOMOVE; break;
15654 case 'p': mask = SP_SUBPAT; break;
15655 case 'r': mask = SP_REPEAT; break;
15656 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657 }
15658 if (mask == 0)
15659 {
15660 EMSG2(_(e_invarg2), flags);
15661 dir = 0;
15662 }
15663 else
15664 *flagsp |= mask;
15665 }
15666 if (dir == 0)
15667 break;
15668 ++flags;
15669 }
15670 }
15671 return dir;
15672}
15673
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015675 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015677 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015678search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015679 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015680 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015681 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015683 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 char_u *pat;
15685 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015686 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 int save_p_ws = p_ws;
15688 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015689 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015690 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015691 proftime_T tm;
15692#ifdef FEAT_RELTIME
15693 long time_limit = 0;
15694#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015695 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015696 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015698 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015699 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015700 if (dir == 0)
15701 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015702 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015703 if (flags & SP_START)
15704 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015705 if (flags & SP_END)
15706 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015707
Bram Moolenaar76929292008-01-06 19:07:36 +000015708 /* Optional arguments: line number to stop searching and timeout. */
15709 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015710 {
15711 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15712 if (lnum_stop < 0)
15713 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015714#ifdef FEAT_RELTIME
15715 if (argvars[3].v_type != VAR_UNKNOWN)
15716 {
15717 time_limit = get_tv_number_chk(&argvars[3], NULL);
15718 if (time_limit < 0)
15719 goto theend;
15720 }
15721#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015722 }
15723
Bram Moolenaar76929292008-01-06 19:07:36 +000015724#ifdef FEAT_RELTIME
15725 /* Set the time limit, if there is one. */
15726 profile_setlimit(time_limit, &tm);
15727#endif
15728
Bram Moolenaar231334e2005-07-25 20:46:57 +000015729 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015730 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015731 * Check to make sure only those flags are set.
15732 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15733 * flags cannot be set. Check for that condition also.
15734 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015735 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015736 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015739 goto theend;
15740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015742 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015743 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015744 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015745 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015747 if (flags & SP_SUBPAT)
15748 retval = subpatnum;
15749 else
15750 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015751 if (flags & SP_SETPCMARK)
15752 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015754 if (match_pos != NULL)
15755 {
15756 /* Store the match cursor position */
15757 match_pos->lnum = pos.lnum;
15758 match_pos->col = pos.col + 1;
15759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 /* "/$" will put the cursor after the end of the line, may need to
15761 * correct that here */
15762 check_cursor();
15763 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015764
15765 /* If 'n' flag is used: restore cursor position. */
15766 if (flags & SP_NOMOVE)
15767 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015768 else
15769 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015770theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015772
15773 return retval;
15774}
15775
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015776#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015777
15778/*
15779 * round() is not in C90, use ceil() or floor() instead.
15780 */
15781 float_T
15782vim_round(f)
15783 float_T f;
15784{
15785 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15786}
15787
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015788/*
15789 * "round({float})" function
15790 */
15791 static void
15792f_round(argvars, rettv)
15793 typval_T *argvars;
15794 typval_T *rettv;
15795{
15796 float_T f;
15797
15798 rettv->v_type = VAR_FLOAT;
15799 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015800 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015801 else
15802 rettv->vval.v_float = 0.0;
15803}
15804#endif
15805
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015806/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015807 * "screencol()" function
15808 *
15809 * First column is 1 to be consistent with virtcol().
15810 */
15811 static void
15812f_screencol(argvars, rettv)
15813 typval_T *argvars UNUSED;
15814 typval_T *rettv;
15815{
15816 rettv->vval.v_number = screen_screencol() + 1;
15817}
15818
15819/*
15820 * "screenrow()" function
15821 */
15822 static void
15823f_screenrow(argvars, rettv)
15824 typval_T *argvars UNUSED;
15825 typval_T *rettv;
15826{
15827 rettv->vval.v_number = screen_screenrow() + 1;
15828}
15829
15830/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015831 * "search()" function
15832 */
15833 static void
15834f_search(argvars, rettv)
15835 typval_T *argvars;
15836 typval_T *rettv;
15837{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015838 int flags = 0;
15839
15840 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841}
15842
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015844 * "searchdecl()" function
15845 */
15846 static void
15847f_searchdecl(argvars, rettv)
15848 typval_T *argvars;
15849 typval_T *rettv;
15850{
15851 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015852 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015853 int error = FALSE;
15854 char_u *name;
15855
15856 rettv->vval.v_number = 1; /* default: FAIL */
15857
15858 name = get_tv_string_chk(&argvars[0]);
15859 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015860 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015861 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015862 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15863 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15864 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015865 if (!error && name != NULL)
15866 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015867 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015868}
15869
15870/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015871 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015873 static int
15874searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015875 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015876 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877{
15878 char_u *spat, *mpat, *epat;
15879 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 int dir;
15882 int flags = 0;
15883 char_u nbuf1[NUMBUFLEN];
15884 char_u nbuf2[NUMBUFLEN];
15885 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015886 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015887 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015888 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015891 spat = get_tv_string_chk(&argvars[0]);
15892 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15893 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15894 if (spat == NULL || mpat == NULL || epat == NULL)
15895 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 /* Handle the optional fourth argument: flags */
15898 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015899 if (dir == 0)
15900 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015901
15902 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015903 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15904 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015905 if ((flags & (SP_END | SP_SUBPAT)) != 0
15906 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015907 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015908 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015909 goto theend;
15910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015912 /* Using 'r' implies 'W', otherwise it doesn't work. */
15913 if (flags & SP_REPEAT)
15914 p_ws = FALSE;
15915
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015916 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015917 if (argvars[3].v_type == VAR_UNKNOWN
15918 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919 skip = (char_u *)"";
15920 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015922 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015923 if (argvars[5].v_type != VAR_UNKNOWN)
15924 {
15925 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15926 if (lnum_stop < 0)
15927 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015928#ifdef FEAT_RELTIME
15929 if (argvars[6].v_type != VAR_UNKNOWN)
15930 {
15931 time_limit = get_tv_number_chk(&argvars[6], NULL);
15932 if (time_limit < 0)
15933 goto theend;
15934 }
15935#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015936 }
15937 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015938 if (skip == NULL)
15939 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015941 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015942 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015943
15944theend:
15945 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015946
15947 return retval;
15948}
15949
15950/*
15951 * "searchpair()" function
15952 */
15953 static void
15954f_searchpair(argvars, rettv)
15955 typval_T *argvars;
15956 typval_T *rettv;
15957{
15958 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15959}
15960
15961/*
15962 * "searchpairpos()" function
15963 */
15964 static void
15965f_searchpairpos(argvars, rettv)
15966 typval_T *argvars;
15967 typval_T *rettv;
15968{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015969 pos_T match_pos;
15970 int lnum = 0;
15971 int col = 0;
15972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015973 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015974 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015975
15976 if (searchpair_cmn(argvars, &match_pos) > 0)
15977 {
15978 lnum = match_pos.lnum;
15979 col = match_pos.col;
15980 }
15981
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015982 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15983 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015984}
15985
15986/*
15987 * Search for a start/middle/end thing.
15988 * Used by searchpair(), see its documentation for the details.
15989 * Returns 0 or -1 for no match,
15990 */
15991 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015992do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15993 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015994 char_u *spat; /* start pattern */
15995 char_u *mpat; /* middle pattern */
15996 char_u *epat; /* end pattern */
15997 int dir; /* BACKWARD or FORWARD */
15998 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015999 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016000 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016001 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016002 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016003{
16004 char_u *save_cpo;
16005 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16006 long retval = 0;
16007 pos_T pos;
16008 pos_T firstpos;
16009 pos_T foundpos;
16010 pos_T save_cursor;
16011 pos_T save_pos;
16012 int n;
16013 int r;
16014 int nest = 1;
16015 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016016 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016017 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016018
16019 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16020 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016021 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016022
Bram Moolenaar76929292008-01-06 19:07:36 +000016023#ifdef FEAT_RELTIME
16024 /* Set the time limit, if there is one. */
16025 profile_setlimit(time_limit, &tm);
16026#endif
16027
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016028 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16029 * start/middle/end (pat3, for the top pair). */
16030 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16031 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16032 if (pat2 == NULL || pat3 == NULL)
16033 goto theend;
16034 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16035 if (*mpat == NUL)
16036 STRCPY(pat3, pat2);
16037 else
16038 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16039 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016040 if (flags & SP_START)
16041 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016042
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 save_cursor = curwin->w_cursor;
16044 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016045 clearpos(&firstpos);
16046 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 pat = pat3;
16048 for (;;)
16049 {
16050 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016051 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16053 /* didn't find it or found the first match again: FAIL */
16054 break;
16055
16056 if (firstpos.lnum == 0)
16057 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016058 if (equalpos(pos, foundpos))
16059 {
16060 /* Found the same position again. Can happen with a pattern that
16061 * has "\zs" at the end and searching backwards. Advance one
16062 * character and try again. */
16063 if (dir == BACKWARD)
16064 decl(&pos);
16065 else
16066 incl(&pos);
16067 }
16068 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016070 /* clear the start flag to avoid getting stuck here */
16071 options &= ~SEARCH_START;
16072
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 /* If the skip pattern matches, ignore this match. */
16074 if (*skip != NUL)
16075 {
16076 save_pos = curwin->w_cursor;
16077 curwin->w_cursor = pos;
16078 r = eval_to_bool(skip, &err, NULL, FALSE);
16079 curwin->w_cursor = save_pos;
16080 if (err)
16081 {
16082 /* Evaluating {skip} caused an error, break here. */
16083 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016084 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 break;
16086 }
16087 if (r)
16088 continue;
16089 }
16090
16091 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16092 {
16093 /* Found end when searching backwards or start when searching
16094 * forward: nested pair. */
16095 ++nest;
16096 pat = pat2; /* nested, don't search for middle */
16097 }
16098 else
16099 {
16100 /* Found end when searching forward or start when searching
16101 * backward: end of (nested) pair; or found middle in outer pair. */
16102 if (--nest == 1)
16103 pat = pat3; /* outer level, search for middle */
16104 }
16105
16106 if (nest == 0)
16107 {
16108 /* Found the match: return matchcount or line number. */
16109 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016110 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016112 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016113 if (flags & SP_SETPCMARK)
16114 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 curwin->w_cursor = pos;
16116 if (!(flags & SP_REPEAT))
16117 break;
16118 nest = 1; /* search for next unmatched */
16119 }
16120 }
16121
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016122 if (match_pos != NULL)
16123 {
16124 /* Store the match cursor position */
16125 match_pos->lnum = curwin->w_cursor.lnum;
16126 match_pos->col = curwin->w_cursor.col + 1;
16127 }
16128
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016130 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 curwin->w_cursor = save_cursor;
16132
16133theend:
16134 vim_free(pat2);
16135 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016136 if (p_cpo == empty_option)
16137 p_cpo = save_cpo;
16138 else
16139 /* Darn, evaluating the {skip} expression changed the value. */
16140 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016141
16142 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143}
16144
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016145/*
16146 * "searchpos()" function
16147 */
16148 static void
16149f_searchpos(argvars, rettv)
16150 typval_T *argvars;
16151 typval_T *rettv;
16152{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016153 pos_T match_pos;
16154 int lnum = 0;
16155 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016156 int n;
16157 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016158
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016159 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016160 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016161
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016162 n = search_cmn(argvars, &match_pos, &flags);
16163 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016164 {
16165 lnum = match_pos.lnum;
16166 col = match_pos.col;
16167 }
16168
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016169 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16170 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016171 if (flags & SP_SUBPAT)
16172 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016173}
16174
16175
Bram Moolenaar0d660222005-01-07 21:51:51 +000016176 static void
16177f_server2client(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 Moolenaar071d4272004-06-13 20:20:40 +000016180{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016181#ifdef FEAT_CLIENTSERVER
16182 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016183 char_u *server = get_tv_string_chk(&argvars[0]);
16184 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016187 if (server == NULL || reply == NULL)
16188 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 if (check_restricted() || check_secure())
16190 return;
16191# ifdef FEAT_X11
16192 if (check_connection() == FAIL)
16193 return;
16194# endif
16195
16196 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198 EMSG(_("E258: Unable to send to client"));
16199 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201 rettv->vval.v_number = 0;
16202#else
16203 rettv->vval.v_number = -1;
16204#endif
16205}
16206
Bram Moolenaar0d660222005-01-07 21:51:51 +000016207 static void
16208f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016209 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016210 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211{
16212 char_u *r = NULL;
16213
16214#ifdef FEAT_CLIENTSERVER
16215# ifdef WIN32
16216 r = serverGetVimNames();
16217# else
16218 make_connection();
16219 if (X_DISPLAY != NULL)
16220 r = serverGetVimNames(X_DISPLAY);
16221# endif
16222#endif
16223 rettv->v_type = VAR_STRING;
16224 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225}
16226
16227/*
16228 * "setbufvar()" function
16229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016231f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016232 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016233 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234{
16235 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016238 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 char_u nbuf[NUMBUFLEN];
16240
16241 if (check_restricted() || check_secure())
16242 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016243 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16244 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016245 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 varp = &argvars[2];
16247
16248 if (buf != NULL && varname != NULL && varp != NULL)
16249 {
16250 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252
16253 if (*varname == '&')
16254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016255 long numval;
16256 char_u *strval;
16257 int error = FALSE;
16258
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016260 numval = get_tv_number_chk(varp, &error);
16261 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 if (!error && strval != NULL)
16263 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 }
16265 else
16266 {
16267 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16268 if (bufvarname != NULL)
16269 {
16270 STRCPY(bufvarname, "b:");
16271 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016272 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 vim_free(bufvarname);
16274 }
16275 }
16276
16277 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280}
16281
16282/*
16283 * "setcmdpos()" function
16284 */
16285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016286f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016287 typval_T *argvars;
16288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016290 int pos = (int)get_tv_number(&argvars[0]) - 1;
16291
16292 if (pos >= 0)
16293 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294}
16295
16296/*
16297 * "setline()" function
16298 */
16299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 typval_T *argvars;
16302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303{
16304 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016305 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016306 list_T *l = NULL;
16307 listitem_T *li = NULL;
16308 long added = 0;
16309 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016311 lnum = get_tv_lnum(&argvars[0]);
16312 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016314 l = argvars[1].vval.v_list;
16315 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016317 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016318 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016319
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016320 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016321 for (;;)
16322 {
16323 if (l != NULL)
16324 {
16325 /* list argument, get next string */
16326 if (li == NULL)
16327 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016328 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016329 li = li->li_next;
16330 }
16331
16332 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016334 break;
16335 if (lnum <= curbuf->b_ml.ml_line_count)
16336 {
16337 /* existing line, replace it */
16338 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16339 {
16340 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016341 if (lnum == curwin->w_cursor.lnum)
16342 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016343 rettv->vval.v_number = 0; /* OK */
16344 }
16345 }
16346 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16347 {
16348 /* lnum is one past the last line, append the line */
16349 ++added;
16350 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16351 rettv->vval.v_number = 0; /* OK */
16352 }
16353
16354 if (l == NULL) /* only one string argument */
16355 break;
16356 ++lnum;
16357 }
16358
16359 if (added > 0)
16360 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361}
16362
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016363static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16364
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016366 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016367 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016368 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016369set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016370 win_T *wp UNUSED;
16371 typval_T *list_arg UNUSED;
16372 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016373 typval_T *rettv;
16374{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016375#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016376 char_u *act;
16377 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016378#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016379
Bram Moolenaar2641f772005-03-25 21:58:17 +000016380 rettv->vval.v_number = -1;
16381
16382#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016383 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016384 EMSG(_(e_listreq));
16385 else
16386 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016387 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016388
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016389 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016390 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016391 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016392 if (act == NULL)
16393 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016394 if (*act == 'a' || *act == 'r')
16395 action = *act;
16396 }
16397
Bram Moolenaar81484f42012-12-05 15:16:47 +010016398 if (l != NULL && set_errorlist(wp, l, action,
16399 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016400 rettv->vval.v_number = 0;
16401 }
16402#endif
16403}
16404
16405/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016406 * "setloclist()" function
16407 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016408 static void
16409f_setloclist(argvars, rettv)
16410 typval_T *argvars;
16411 typval_T *rettv;
16412{
16413 win_T *win;
16414
16415 rettv->vval.v_number = -1;
16416
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016417 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016418 if (win != NULL)
16419 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16420}
16421
16422/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016423 * "setmatches()" function
16424 */
16425 static void
16426f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016427 typval_T *argvars UNUSED;
16428 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016429{
16430#ifdef FEAT_SEARCH_EXTRA
16431 list_T *l;
16432 listitem_T *li;
16433 dict_T *d;
16434
16435 rettv->vval.v_number = -1;
16436 if (argvars[0].v_type != VAR_LIST)
16437 {
16438 EMSG(_(e_listreq));
16439 return;
16440 }
16441 if ((l = argvars[0].vval.v_list) != NULL)
16442 {
16443
16444 /* To some extent make sure that we are dealing with a list from
16445 * "getmatches()". */
16446 li = l->lv_first;
16447 while (li != NULL)
16448 {
16449 if (li->li_tv.v_type != VAR_DICT
16450 || (d = li->li_tv.vval.v_dict) == NULL)
16451 {
16452 EMSG(_(e_invarg));
16453 return;
16454 }
16455 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16456 && dict_find(d, (char_u *)"pattern", -1) != NULL
16457 && dict_find(d, (char_u *)"priority", -1) != NULL
16458 && dict_find(d, (char_u *)"id", -1) != NULL))
16459 {
16460 EMSG(_(e_invarg));
16461 return;
16462 }
16463 li = li->li_next;
16464 }
16465
16466 clear_matches(curwin);
16467 li = l->lv_first;
16468 while (li != NULL)
16469 {
16470 d = li->li_tv.vval.v_dict;
16471 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16472 get_dict_string(d, (char_u *)"pattern", FALSE),
16473 (int)get_dict_number(d, (char_u *)"priority"),
16474 (int)get_dict_number(d, (char_u *)"id"));
16475 li = li->li_next;
16476 }
16477 rettv->vval.v_number = 0;
16478 }
16479#endif
16480}
16481
16482/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016483 * "setpos()" function
16484 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016485 static void
16486f_setpos(argvars, rettv)
16487 typval_T *argvars;
16488 typval_T *rettv;
16489{
16490 pos_T pos;
16491 int fnum;
16492 char_u *name;
16493
Bram Moolenaar08250432008-02-13 11:42:46 +000016494 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016495 name = get_tv_string_chk(argvars);
16496 if (name != NULL)
16497 {
16498 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16499 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016500 if (--pos.col < 0)
16501 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016502 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016503 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016504 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016505 if (fnum == curbuf->b_fnum)
16506 {
16507 curwin->w_cursor = pos;
16508 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016509 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016510 }
16511 else
16512 EMSG(_(e_invarg));
16513 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016514 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16515 {
16516 /* set mark */
16517 if (setmark_pos(name[1], &pos, fnum) == OK)
16518 rettv->vval.v_number = 0;
16519 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016520 else
16521 EMSG(_(e_invarg));
16522 }
16523 }
16524}
16525
16526/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016527 * "setqflist()" function
16528 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016529 static void
16530f_setqflist(argvars, rettv)
16531 typval_T *argvars;
16532 typval_T *rettv;
16533{
16534 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16535}
16536
16537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 * "setreg()" function
16539 */
16540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016541f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016542 typval_T *argvars;
16543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544{
16545 int regname;
16546 char_u *strregname;
16547 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016548 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 int append;
16550 char_u yank_type;
16551 long block_len;
16552
16553 block_len = -1;
16554 yank_type = MAUTO;
16555 append = FALSE;
16556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016557 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016558 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016560 if (strregname == NULL)
16561 return; /* type error; errmsg already given */
16562 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563 if (regname == 0 || regname == '@')
16564 regname = '"';
16565 else if (regname == '=')
16566 return;
16567
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016568 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016570 stropt = get_tv_string_chk(&argvars[2]);
16571 if (stropt == NULL)
16572 return; /* type error */
16573 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 switch (*stropt)
16575 {
16576 case 'a': case 'A': /* append */
16577 append = TRUE;
16578 break;
16579 case 'v': case 'c': /* character-wise selection */
16580 yank_type = MCHAR;
16581 break;
16582 case 'V': case 'l': /* line-wise selection */
16583 yank_type = MLINE;
16584 break;
16585#ifdef FEAT_VISUAL
16586 case 'b': case Ctrl_V: /* block-wise selection */
16587 yank_type = MBLOCK;
16588 if (VIM_ISDIGIT(stropt[1]))
16589 {
16590 ++stropt;
16591 block_len = getdigits(&stropt) - 1;
16592 --stropt;
16593 }
16594 break;
16595#endif
16596 }
16597 }
16598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016599 strval = get_tv_string_chk(&argvars[1]);
16600 if (strval != NULL)
16601 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016603 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604}
16605
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016606/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016607 * "settabvar()" function
16608 */
16609 static void
16610f_settabvar(argvars, rettv)
16611 typval_T *argvars;
16612 typval_T *rettv;
16613{
16614 tabpage_T *save_curtab;
16615 char_u *varname, *tabvarname;
16616 typval_T *varp;
16617 tabpage_T *tp;
16618
16619 rettv->vval.v_number = 0;
16620
16621 if (check_restricted() || check_secure())
16622 return;
16623
16624 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16625 varname = get_tv_string_chk(&argvars[1]);
16626 varp = &argvars[2];
16627
16628 if (tp != NULL && varname != NULL && varp != NULL)
16629 {
16630 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016631 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016632
16633 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16634 if (tabvarname != NULL)
16635 {
16636 STRCPY(tabvarname, "t:");
16637 STRCPY(tabvarname + 2, varname);
16638 set_var(tabvarname, varp, TRUE);
16639 vim_free(tabvarname);
16640 }
16641
16642 /* Restore current tabpage */
16643 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016644 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016645 }
16646}
16647
16648/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016649 * "settabwinvar()" function
16650 */
16651 static void
16652f_settabwinvar(argvars, rettv)
16653 typval_T *argvars;
16654 typval_T *rettv;
16655{
16656 setwinvar(argvars, rettv, 1);
16657}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658
16659/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016660 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016663f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016664 typval_T *argvars;
16665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016667 setwinvar(argvars, rettv, 0);
16668}
16669
16670/*
16671 * "setwinvar()" and "settabwinvar()" functions
16672 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016673
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016674 static void
16675setwinvar(argvars, rettv, off)
16676 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016677 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016678 int off;
16679{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 win_T *win;
16681#ifdef FEAT_WINDOWS
16682 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016683 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684#endif
16685 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016686 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016688 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689
16690 if (check_restricted() || check_secure())
16691 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016692
16693#ifdef FEAT_WINDOWS
16694 if (off == 1)
16695 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16696 else
16697 tp = curtab;
16698#endif
16699 win = find_win_by_nr(&argvars[off], tp);
16700 varname = get_tv_string_chk(&argvars[off + 1]);
16701 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702
16703 if (win != NULL && varname != NULL && varp != NULL)
16704 {
16705#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016706 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016707 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708#endif
16709
16710 if (*varname == '&')
16711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016712 long numval;
16713 char_u *strval;
16714 int error = FALSE;
16715
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016717 numval = get_tv_number_chk(varp, &error);
16718 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016719 if (!error && strval != NULL)
16720 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 }
16722 else
16723 {
16724 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16725 if (winvarname != NULL)
16726 {
16727 STRCPY(winvarname, "w:");
16728 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016729 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 vim_free(winvarname);
16731 }
16732 }
16733
16734#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016735 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736#endif
16737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738}
16739
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016740#ifdef FEAT_CRYPT
16741/*
16742 * "sha256({string})" function
16743 */
16744 static void
16745f_sha256(argvars, rettv)
16746 typval_T *argvars;
16747 typval_T *rettv;
16748{
16749 char_u *p;
16750
16751 p = get_tv_string(&argvars[0]);
16752 rettv->vval.v_string = vim_strsave(
16753 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16754 rettv->v_type = VAR_STRING;
16755}
16756#endif /* FEAT_CRYPT */
16757
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016759 * "shellescape({string})" function
16760 */
16761 static void
16762f_shellescape(argvars, rettv)
16763 typval_T *argvars;
16764 typval_T *rettv;
16765{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016766 rettv->vval.v_string = vim_strsave_shellescape(
16767 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016768 rettv->v_type = VAR_STRING;
16769}
16770
16771/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016772 * shiftwidth() function
16773 */
16774 static void
16775f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016776 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016777 typval_T *rettv;
16778{
16779 rettv->vval.v_number = get_sw_value();
16780}
16781
16782/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 */
16785 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016786f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016787 typval_T *argvars;
16788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016790 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791
Bram Moolenaar0d660222005-01-07 21:51:51 +000016792 p = get_tv_string(&argvars[0]);
16793 rettv->vval.v_string = vim_strsave(p);
16794 simplify_filename(rettv->vval.v_string); /* simplify in place */
16795 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796}
16797
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016798#ifdef FEAT_FLOAT
16799/*
16800 * "sin()" function
16801 */
16802 static void
16803f_sin(argvars, rettv)
16804 typval_T *argvars;
16805 typval_T *rettv;
16806{
16807 float_T f;
16808
16809 rettv->v_type = VAR_FLOAT;
16810 if (get_float_arg(argvars, &f) == OK)
16811 rettv->vval.v_float = sin(f);
16812 else
16813 rettv->vval.v_float = 0.0;
16814}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016815
16816/*
16817 * "sinh()" function
16818 */
16819 static void
16820f_sinh(argvars, rettv)
16821 typval_T *argvars;
16822 typval_T *rettv;
16823{
16824 float_T f;
16825
16826 rettv->v_type = VAR_FLOAT;
16827 if (get_float_arg(argvars, &f) == OK)
16828 rettv->vval.v_float = sinh(f);
16829 else
16830 rettv->vval.v_float = 0.0;
16831}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016832#endif
16833
Bram Moolenaar0d660222005-01-07 21:51:51 +000016834static int
16835#ifdef __BORLANDC__
16836 _RTLENTRYF
16837#endif
16838 item_compare __ARGS((const void *s1, const void *s2));
16839static int
16840#ifdef __BORLANDC__
16841 _RTLENTRYF
16842#endif
16843 item_compare2 __ARGS((const void *s1, const void *s2));
16844
16845static int item_compare_ic;
16846static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016847static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016848static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016849#define ITEM_COMPARE_FAIL 999
16850
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016852 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016854 static int
16855#ifdef __BORLANDC__
16856_RTLENTRYF
16857#endif
16858item_compare(s1, s2)
16859 const void *s1;
16860 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862 char_u *p1, *p2;
16863 char_u *tofree1, *tofree2;
16864 int res;
16865 char_u numbuf1[NUMBUFLEN];
16866 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016868 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16869 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016870 if (p1 == NULL)
16871 p1 = (char_u *)"";
16872 if (p2 == NULL)
16873 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874 if (item_compare_ic)
16875 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877 res = STRCMP(p1, p2);
16878 vim_free(tofree1);
16879 vim_free(tofree2);
16880 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881}
16882
16883 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016884#ifdef __BORLANDC__
16885_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887item_compare2(s1, s2)
16888 const void *s1;
16889 const void *s2;
16890{
16891 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016892 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016893 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016894 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016896 /* shortcut after failure in previous call; compare all items equal */
16897 if (item_compare_func_err)
16898 return 0;
16899
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016900 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16901 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016902 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16903 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016904
16905 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016906 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016907 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16908 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909 clear_tv(&argv[0]);
16910 clear_tv(&argv[1]);
16911
16912 if (res == FAIL)
16913 res = ITEM_COMPARE_FAIL;
16914 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016915 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16916 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016917 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016918 clear_tv(&rettv);
16919 return res;
16920}
16921
16922/*
16923 * "sort({list})" function
16924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 typval_T *argvars;
16928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929{
Bram Moolenaar33570922005-01-25 22:26:29 +000016930 list_T *l;
16931 listitem_T *li;
16932 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 long len;
16934 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 if (argvars[0].v_type != VAR_LIST)
16937 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 else
16939 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016941 if (l == NULL || tv_check_lock(l->lv_lock,
16942 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943 return;
16944 rettv->vval.v_list = l;
16945 rettv->v_type = VAR_LIST;
16946 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 len = list_len(l);
16949 if (len <= 1)
16950 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 item_compare_ic = FALSE;
16953 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016954 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 if (argvars[1].v_type != VAR_UNKNOWN)
16956 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016957 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 else
16961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016962 int error = FALSE;
16963
16964 i = get_tv_number_chk(&argvars[1], &error);
16965 if (error)
16966 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 if (i == 1)
16968 item_compare_ic = TRUE;
16969 else
16970 item_compare_func = get_tv_string(&argvars[1]);
16971 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016972
16973 if (argvars[2].v_type != VAR_UNKNOWN)
16974 {
16975 /* optional third argument: {dict} */
16976 if (argvars[2].v_type != VAR_DICT)
16977 {
16978 EMSG(_(e_dictreq));
16979 return;
16980 }
16981 item_compare_selfdict = argvars[2].vval.v_dict;
16982 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 if (ptrs == NULL)
16988 return;
16989 i = 0;
16990 for (li = l->lv_first; li != NULL; li = li->li_next)
16991 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016993 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994 /* test the compare function */
16995 if (item_compare_func != NULL
16996 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16997 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016998 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 {
17001 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017003 item_compare_func == NULL ? item_compare : item_compare2);
17004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 if (!item_compare_func_err)
17006 {
17007 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017008 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 l->lv_len = 0;
17010 for (i = 0; i < len; ++i)
17011 list_append(l, ptrs[i]);
17012 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013 }
17014
17015 vim_free(ptrs);
17016 }
17017}
17018
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017019/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017020 * "soundfold({word})" function
17021 */
17022 static void
17023f_soundfold(argvars, rettv)
17024 typval_T *argvars;
17025 typval_T *rettv;
17026{
17027 char_u *s;
17028
17029 rettv->v_type = VAR_STRING;
17030 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017031#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017032 rettv->vval.v_string = eval_soundfold(s);
17033#else
17034 rettv->vval.v_string = vim_strsave(s);
17035#endif
17036}
17037
17038/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017039 * "spellbadword()" function
17040 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017041 static void
17042f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017043 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017044 typval_T *rettv;
17045{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017046 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017047 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017048 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017049
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017050 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017051 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017052
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017053#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017054 if (argvars[0].v_type == VAR_UNKNOWN)
17055 {
17056 /* Find the start and length of the badly spelled word. */
17057 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17058 if (len != 0)
17059 word = ml_get_cursor();
17060 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017061 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017062 {
17063 char_u *str = get_tv_string_chk(&argvars[0]);
17064 int capcol = -1;
17065
17066 if (str != NULL)
17067 {
17068 /* Check the argument for spelling. */
17069 while (*str != NUL)
17070 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017071 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017072 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017073 {
17074 word = str;
17075 break;
17076 }
17077 str += len;
17078 }
17079 }
17080 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017081#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017083 list_append_string(rettv->vval.v_list, word, len);
17084 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017085 attr == HLF_SPB ? "bad" :
17086 attr == HLF_SPR ? "rare" :
17087 attr == HLF_SPL ? "local" :
17088 attr == HLF_SPC ? "caps" :
17089 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017090}
17091
17092/*
17093 * "spellsuggest()" function
17094 */
17095 static void
17096f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017097 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017098 typval_T *rettv;
17099{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017100#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017101 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017102 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017103 int maxcount;
17104 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017105 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017106 listitem_T *li;
17107 int need_capital = FALSE;
17108#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017109
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017110 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017111 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017112
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017113#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017114 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017115 {
17116 str = get_tv_string(&argvars[0]);
17117 if (argvars[1].v_type != VAR_UNKNOWN)
17118 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017119 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017120 if (maxcount <= 0)
17121 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017122 if (argvars[2].v_type != VAR_UNKNOWN)
17123 {
17124 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17125 if (typeerr)
17126 return;
17127 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017128 }
17129 else
17130 maxcount = 25;
17131
Bram Moolenaar4770d092006-01-12 23:22:24 +000017132 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017133
17134 for (i = 0; i < ga.ga_len; ++i)
17135 {
17136 str = ((char_u **)ga.ga_data)[i];
17137
17138 li = listitem_alloc();
17139 if (li == NULL)
17140 vim_free(str);
17141 else
17142 {
17143 li->li_tv.v_type = VAR_STRING;
17144 li->li_tv.v_lock = 0;
17145 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017146 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017147 }
17148 }
17149 ga_clear(&ga);
17150 }
17151#endif
17152}
17153
Bram Moolenaar0d660222005-01-07 21:51:51 +000017154 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017155f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 typval_T *argvars;
17157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017158{
17159 char_u *str;
17160 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017161 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 regmatch_T regmatch;
17163 char_u patbuf[NUMBUFLEN];
17164 char_u *save_cpo;
17165 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017167 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017168 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017169
17170 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17171 save_cpo = p_cpo;
17172 p_cpo = (char_u *)"";
17173
17174 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017175 if (argvars[1].v_type != VAR_UNKNOWN)
17176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17178 if (pat == NULL)
17179 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017180 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017181 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017182 }
17183 if (pat == NULL || *pat == NUL)
17184 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017186 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017188 if (typeerr)
17189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17192 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017194 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017195 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017196 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017197 if (*str == NUL)
17198 match = FALSE; /* empty item at the end */
17199 else
17200 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201 if (match)
17202 end = regmatch.startp[0];
17203 else
17204 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017205 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17206 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017207 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017208 if (list_append_string(rettv->vval.v_list, str,
17209 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017210 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017211 }
17212 if (!match)
17213 break;
17214 /* Advance to just after the match. */
17215 if (regmatch.endp[0] > str)
17216 col = 0;
17217 else
17218 {
17219 /* Don't get stuck at the same match. */
17220#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017221 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222#else
17223 col = 1;
17224#endif
17225 }
17226 str = regmatch.endp[0];
17227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228
Bram Moolenaar473de612013-06-08 18:19:48 +020017229 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231
Bram Moolenaar0d660222005-01-07 21:51:51 +000017232 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233}
17234
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017235#ifdef FEAT_FLOAT
17236/*
17237 * "sqrt()" function
17238 */
17239 static void
17240f_sqrt(argvars, rettv)
17241 typval_T *argvars;
17242 typval_T *rettv;
17243{
17244 float_T f;
17245
17246 rettv->v_type = VAR_FLOAT;
17247 if (get_float_arg(argvars, &f) == OK)
17248 rettv->vval.v_float = sqrt(f);
17249 else
17250 rettv->vval.v_float = 0.0;
17251}
17252
17253/*
17254 * "str2float()" function
17255 */
17256 static void
17257f_str2float(argvars, rettv)
17258 typval_T *argvars;
17259 typval_T *rettv;
17260{
17261 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17262
17263 if (*p == '+')
17264 p = skipwhite(p + 1);
17265 (void)string2float(p, &rettv->vval.v_float);
17266 rettv->v_type = VAR_FLOAT;
17267}
17268#endif
17269
Bram Moolenaar2c932302006-03-18 21:42:09 +000017270/*
17271 * "str2nr()" function
17272 */
17273 static void
17274f_str2nr(argvars, rettv)
17275 typval_T *argvars;
17276 typval_T *rettv;
17277{
17278 int base = 10;
17279 char_u *p;
17280 long n;
17281
17282 if (argvars[1].v_type != VAR_UNKNOWN)
17283 {
17284 base = get_tv_number(&argvars[1]);
17285 if (base != 8 && base != 10 && base != 16)
17286 {
17287 EMSG(_(e_invarg));
17288 return;
17289 }
17290 }
17291
17292 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017293 if (*p == '+')
17294 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017295 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17296 rettv->vval.v_number = n;
17297}
17298
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299#ifdef HAVE_STRFTIME
17300/*
17301 * "strftime({format}[, {time}])" function
17302 */
17303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017304f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 typval_T *argvars;
17306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307{
17308 char_u result_buf[256];
17309 struct tm *curtime;
17310 time_t seconds;
17311 char_u *p;
17312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017313 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017315 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017316 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 seconds = time(NULL);
17318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 curtime = localtime(&seconds);
17321 /* MSVC returns NULL for an invalid value of seconds. */
17322 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017323 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 else
17325 {
17326# ifdef FEAT_MBYTE
17327 vimconv_T conv;
17328 char_u *enc;
17329
17330 conv.vc_type = CONV_NONE;
17331 enc = enc_locale();
17332 convert_setup(&conv, p_enc, enc);
17333 if (conv.vc_type != CONV_NONE)
17334 p = string_convert(&conv, p, NULL);
17335# endif
17336 if (p != NULL)
17337 (void)strftime((char *)result_buf, sizeof(result_buf),
17338 (char *)p, curtime);
17339 else
17340 result_buf[0] = NUL;
17341
17342# ifdef FEAT_MBYTE
17343 if (conv.vc_type != CONV_NONE)
17344 vim_free(p);
17345 convert_setup(&conv, enc, p_enc);
17346 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017347 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 else
17349# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017350 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351
17352# ifdef FEAT_MBYTE
17353 /* Release conversion descriptors */
17354 convert_setup(&conv, NULL, NULL);
17355 vim_free(enc);
17356# endif
17357 }
17358}
17359#endif
17360
17361/*
17362 * "stridx()" function
17363 */
17364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017365f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017366 typval_T *argvars;
17367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368{
17369 char_u buf[NUMBUFLEN];
17370 char_u *needle;
17371 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017374 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 needle = get_tv_string_chk(&argvars[1]);
17377 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017379 if (needle == NULL || haystack == NULL)
17380 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 if (argvars[2].v_type != VAR_UNKNOWN)
17383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017384 int error = FALSE;
17385
17386 start_idx = get_tv_number_chk(&argvars[2], &error);
17387 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017389 if (start_idx >= 0)
17390 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 }
17392
17393 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17394 if (pos != NULL)
17395 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396}
17397
17398/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017399 * "string()" function
17400 */
17401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017402f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017403 typval_T *argvars;
17404 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017405{
17406 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017407 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017409 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017410 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017411 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017412 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414}
17415
17416/*
17417 * "strlen()" function
17418 */
17419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017420f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017421 typval_T *argvars;
17422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 rettv->vval.v_number = (varnumber_T)(STRLEN(
17425 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426}
17427
17428/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017429 * "strchars()" function
17430 */
17431 static void
17432f_strchars(argvars, rettv)
17433 typval_T *argvars;
17434 typval_T *rettv;
17435{
17436 char_u *s = get_tv_string(&argvars[0]);
17437#ifdef FEAT_MBYTE
17438 varnumber_T len = 0;
17439
17440 while (*s != NUL)
17441 {
17442 mb_cptr2char_adv(&s);
17443 ++len;
17444 }
17445 rettv->vval.v_number = len;
17446#else
17447 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17448#endif
17449}
17450
17451/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017452 * "strdisplaywidth()" function
17453 */
17454 static void
17455f_strdisplaywidth(argvars, rettv)
17456 typval_T *argvars;
17457 typval_T *rettv;
17458{
17459 char_u *s = get_tv_string(&argvars[0]);
17460 int col = 0;
17461
17462 if (argvars[1].v_type != VAR_UNKNOWN)
17463 col = get_tv_number(&argvars[1]);
17464
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017465 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017466}
17467
17468/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017469 * "strwidth()" function
17470 */
17471 static void
17472f_strwidth(argvars, rettv)
17473 typval_T *argvars;
17474 typval_T *rettv;
17475{
17476 char_u *s = get_tv_string(&argvars[0]);
17477
17478 rettv->vval.v_number = (varnumber_T)(
17479#ifdef FEAT_MBYTE
17480 mb_string2cells(s, -1)
17481#else
17482 STRLEN(s)
17483#endif
17484 );
17485}
17486
17487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 * "strpart()" function
17489 */
17490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017491f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017492 typval_T *argvars;
17493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494{
17495 char_u *p;
17496 int n;
17497 int len;
17498 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017499 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017501 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 slen = (int)STRLEN(p);
17503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017504 n = get_tv_number_chk(&argvars[1], &error);
17505 if (error)
17506 len = 0;
17507 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 else
17510 len = slen - n; /* default len: all bytes that are available. */
17511
17512 /*
17513 * Only return the overlap between the specified part and the actual
17514 * string.
17515 */
17516 if (n < 0)
17517 {
17518 len += n;
17519 n = 0;
17520 }
17521 else if (n > slen)
17522 n = slen;
17523 if (len < 0)
17524 len = 0;
17525 else if (n + len > slen)
17526 len = slen - n;
17527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017528 rettv->v_type = VAR_STRING;
17529 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530}
17531
17532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533 * "strridx()" function
17534 */
17535 static void
17536f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 typval_T *argvars;
17538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017539{
17540 char_u buf[NUMBUFLEN];
17541 char_u *needle;
17542 char_u *haystack;
17543 char_u *rest;
17544 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017545 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017547 needle = get_tv_string_chk(&argvars[1]);
17548 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017549
17550 rettv->vval.v_number = -1;
17551 if (needle == NULL || haystack == NULL)
17552 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017553
17554 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017555 if (argvars[2].v_type != VAR_UNKNOWN)
17556 {
17557 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017558 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017559 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017560 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017561 }
17562 else
17563 end_idx = haystack_len;
17564
Bram Moolenaar0d660222005-01-07 21:51:51 +000017565 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017568 lastmatch = haystack + end_idx;
17569 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017570 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017571 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017572 for (rest = haystack; *rest != '\0'; ++rest)
17573 {
17574 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017575 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576 break;
17577 lastmatch = rest;
17578 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017579 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017580
17581 if (lastmatch == NULL)
17582 rettv->vval.v_number = -1;
17583 else
17584 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17585}
17586
17587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588 * "strtrans()" function
17589 */
17590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017591f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017592 typval_T *argvars;
17593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017595 rettv->v_type = VAR_STRING;
17596 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597}
17598
17599/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017600 * "submatch()" function
17601 */
17602 static void
17603f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017604 typval_T *argvars;
17605 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017606{
17607 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017608 rettv->vval.v_string =
17609 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017610}
17611
17612/*
17613 * "substitute()" function
17614 */
17615 static void
17616f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017617 typval_T *argvars;
17618 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017619{
17620 char_u patbuf[NUMBUFLEN];
17621 char_u subbuf[NUMBUFLEN];
17622 char_u flagsbuf[NUMBUFLEN];
17623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017624 char_u *str = get_tv_string_chk(&argvars[0]);
17625 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17626 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17627 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17628
Bram Moolenaar0d660222005-01-07 21:51:51 +000017629 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017630 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17631 rettv->vval.v_string = NULL;
17632 else
17633 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634}
17635
17636/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017637 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017640f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643{
17644 int id = 0;
17645#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017646 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 long col;
17648 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017649 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017651 lnum = get_tv_lnum(argvars); /* -1 on type error */
17652 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17653 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017655 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017656 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017657 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658#endif
17659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017660 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661}
17662
17663/*
17664 * "synIDattr(id, what [, mode])" function
17665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017667f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017668 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670{
17671 char_u *p = NULL;
17672#ifdef FEAT_SYN_HL
17673 int id;
17674 char_u *what;
17675 char_u *mode;
17676 char_u modebuf[NUMBUFLEN];
17677 int modec;
17678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679 id = get_tv_number(&argvars[0]);
17680 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017681 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017683 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017685 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 modec = 0; /* replace invalid with current */
17687 }
17688 else
17689 {
17690#ifdef FEAT_GUI
17691 if (gui.in_use)
17692 modec = 'g';
17693 else
17694#endif
17695 if (t_colors > 1)
17696 modec = 'c';
17697 else
17698 modec = 't';
17699 }
17700
17701
17702 switch (TOLOWER_ASC(what[0]))
17703 {
17704 case 'b':
17705 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17706 p = highlight_color(id, what, modec);
17707 else /* bold */
17708 p = highlight_has_attr(id, HL_BOLD, modec);
17709 break;
17710
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017711 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 p = highlight_color(id, what, modec);
17713 break;
17714
17715 case 'i':
17716 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17717 p = highlight_has_attr(id, HL_INVERSE, modec);
17718 else /* italic */
17719 p = highlight_has_attr(id, HL_ITALIC, modec);
17720 break;
17721
17722 case 'n': /* name */
17723 p = get_highlight_name(NULL, id - 1);
17724 break;
17725
17726 case 'r': /* reverse */
17727 p = highlight_has_attr(id, HL_INVERSE, modec);
17728 break;
17729
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017730 case 's':
17731 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17732 p = highlight_color(id, what, modec);
17733 else /* standout */
17734 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 break;
17736
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017737 case 'u':
17738 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17739 /* underline */
17740 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17741 else
17742 /* undercurl */
17743 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 break;
17745 }
17746
17747 if (p != NULL)
17748 p = vim_strsave(p);
17749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017750 rettv->v_type = VAR_STRING;
17751 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752}
17753
17754/*
17755 * "synIDtrans(id)" function
17756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761{
17762 int id;
17763
17764#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017765 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766
17767 if (id > 0)
17768 id = syn_get_final_id(id);
17769 else
17770#endif
17771 id = 0;
17772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017773 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774}
17775
17776/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017777 * "synconcealed(lnum, col)" function
17778 */
17779 static void
17780f_synconcealed(argvars, rettv)
17781 typval_T *argvars UNUSED;
17782 typval_T *rettv;
17783{
17784#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17785 long lnum;
17786 long col;
17787 int syntax_flags = 0;
17788 int cchar;
17789 int matchid = 0;
17790 char_u str[NUMBUFLEN];
17791#endif
17792
17793 rettv->v_type = VAR_LIST;
17794 rettv->vval.v_list = NULL;
17795
17796#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17797 lnum = get_tv_lnum(argvars); /* -1 on type error */
17798 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17799
17800 vim_memset(str, NUL, sizeof(str));
17801
17802 if (rettv_list_alloc(rettv) != FAIL)
17803 {
17804 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17805 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17806 && curwin->w_p_cole > 0)
17807 {
17808 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17809 syntax_flags = get_syntax_info(&matchid);
17810
17811 /* get the conceal character */
17812 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17813 {
17814 cchar = syn_get_sub_char();
17815 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17816 cchar = lcs_conceal;
17817 if (cchar != NUL)
17818 {
17819# ifdef FEAT_MBYTE
17820 if (has_mbyte)
17821 (*mb_char2bytes)(cchar, str);
17822 else
17823# endif
17824 str[0] = cchar;
17825 }
17826 }
17827 }
17828
17829 list_append_number(rettv->vval.v_list,
17830 (syntax_flags & HL_CONCEAL) != 0);
17831 /* -1 to auto-determine strlen */
17832 list_append_string(rettv->vval.v_list, str, -1);
17833 list_append_number(rettv->vval.v_list, matchid);
17834 }
17835#endif
17836}
17837
17838/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017839 * "synstack(lnum, col)" function
17840 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017841 static void
17842f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017843 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017844 typval_T *rettv;
17845{
17846#ifdef FEAT_SYN_HL
17847 long lnum;
17848 long col;
17849 int i;
17850 int id;
17851#endif
17852
17853 rettv->v_type = VAR_LIST;
17854 rettv->vval.v_list = NULL;
17855
17856#ifdef FEAT_SYN_HL
17857 lnum = get_tv_lnum(argvars); /* -1 on type error */
17858 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17859
17860 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017861 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017862 && rettv_list_alloc(rettv) != FAIL)
17863 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017864 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017865 for (i = 0; ; ++i)
17866 {
17867 id = syn_get_stack_item(i);
17868 if (id < 0)
17869 break;
17870 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17871 break;
17872 }
17873 }
17874#endif
17875}
17876
17877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 * "system()" function
17879 */
17880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017881f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017882 typval_T *argvars;
17883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017885 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017887 char_u *infile = NULL;
17888 char_u buf[NUMBUFLEN];
17889 int err = FALSE;
17890 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017892 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017893 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017894
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017895 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017896 {
17897 /*
17898 * Write the string to a temp file, to be used for input of the shell
17899 * command.
17900 */
17901 if ((infile = vim_tempname('i')) == NULL)
17902 {
17903 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017904 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017905 }
17906
17907 fd = mch_fopen((char *)infile, WRITEBIN);
17908 if (fd == NULL)
17909 {
17910 EMSG2(_(e_notopen), infile);
17911 goto done;
17912 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017913 p = get_tv_string_buf_chk(&argvars[1], buf);
17914 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017915 {
17916 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017917 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017918 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017919 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17920 err = TRUE;
17921 if (fclose(fd) != 0)
17922 err = TRUE;
17923 if (err)
17924 {
17925 EMSG(_("E677: Error writing temp file"));
17926 goto done;
17927 }
17928 }
17929
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017930 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17931 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017932
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933#ifdef USE_CR
17934 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017935 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017936 {
17937 char_u *s;
17938
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017939 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 {
17941 if (*s == CAR)
17942 *s = NL;
17943 }
17944 }
17945#else
17946# ifdef USE_CRNL
17947 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017948 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 {
17950 char_u *s, *d;
17951
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017952 d = res;
17953 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 {
17955 if (s[0] == CAR && s[1] == NL)
17956 ++s;
17957 *d++ = *s;
17958 }
17959 *d = NUL;
17960 }
17961# endif
17962#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017963
17964done:
17965 if (infile != NULL)
17966 {
17967 mch_remove(infile);
17968 vim_free(infile);
17969 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017970 rettv->v_type = VAR_STRING;
17971 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972}
17973
17974/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017975 * "tabpagebuflist()" function
17976 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017977 static void
17978f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017979 typval_T *argvars UNUSED;
17980 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017981{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017982#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017983 tabpage_T *tp;
17984 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017985
17986 if (argvars[0].v_type == VAR_UNKNOWN)
17987 wp = firstwin;
17988 else
17989 {
17990 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17991 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017992 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017993 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017994 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017995 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017996 for (; wp != NULL; wp = wp->w_next)
17997 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017998 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017999 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018000 }
18001#endif
18002}
18003
18004
18005/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018006 * "tabpagenr()" function
18007 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018008 static void
18009f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018010 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018011 typval_T *rettv;
18012{
18013 int nr = 1;
18014#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018015 char_u *arg;
18016
18017 if (argvars[0].v_type != VAR_UNKNOWN)
18018 {
18019 arg = get_tv_string_chk(&argvars[0]);
18020 nr = 0;
18021 if (arg != NULL)
18022 {
18023 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018024 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018025 else
18026 EMSG2(_(e_invexpr2), arg);
18027 }
18028 }
18029 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018030 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018031#endif
18032 rettv->vval.v_number = nr;
18033}
18034
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018035
18036#ifdef FEAT_WINDOWS
18037static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18038
18039/*
18040 * Common code for tabpagewinnr() and winnr().
18041 */
18042 static int
18043get_winnr(tp, argvar)
18044 tabpage_T *tp;
18045 typval_T *argvar;
18046{
18047 win_T *twin;
18048 int nr = 1;
18049 win_T *wp;
18050 char_u *arg;
18051
18052 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18053 if (argvar->v_type != VAR_UNKNOWN)
18054 {
18055 arg = get_tv_string_chk(argvar);
18056 if (arg == NULL)
18057 nr = 0; /* type error; errmsg already given */
18058 else if (STRCMP(arg, "$") == 0)
18059 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18060 else if (STRCMP(arg, "#") == 0)
18061 {
18062 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18063 if (twin == NULL)
18064 nr = 0;
18065 }
18066 else
18067 {
18068 EMSG2(_(e_invexpr2), arg);
18069 nr = 0;
18070 }
18071 }
18072
18073 if (nr > 0)
18074 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18075 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018076 {
18077 if (wp == NULL)
18078 {
18079 /* didn't find it in this tabpage */
18080 nr = 0;
18081 break;
18082 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018083 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018084 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018085 return nr;
18086}
18087#endif
18088
18089/*
18090 * "tabpagewinnr()" function
18091 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018092 static void
18093f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018094 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018095 typval_T *rettv;
18096{
18097 int nr = 1;
18098#ifdef FEAT_WINDOWS
18099 tabpage_T *tp;
18100
18101 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18102 if (tp == NULL)
18103 nr = 0;
18104 else
18105 nr = get_winnr(tp, &argvars[1]);
18106#endif
18107 rettv->vval.v_number = nr;
18108}
18109
18110
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018111/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018112 * "tagfiles()" function
18113 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018114 static void
18115f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018116 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018117 typval_T *rettv;
18118{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018119 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018120 tagname_T tn;
18121 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018122
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018123 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018124 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018125 fname = alloc(MAXPATHL);
18126 if (fname == NULL)
18127 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018128
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018129 for (first = TRUE; ; first = FALSE)
18130 if (get_tagfname(&tn, first, fname) == FAIL
18131 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018132 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018133 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018134 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018135}
18136
18137/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018138 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018139 */
18140 static void
18141f_taglist(argvars, rettv)
18142 typval_T *argvars;
18143 typval_T *rettv;
18144{
18145 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018146
18147 tag_pattern = get_tv_string(&argvars[0]);
18148
18149 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018150 if (*tag_pattern == NUL)
18151 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018152
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018153 if (rettv_list_alloc(rettv) == OK)
18154 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018155}
18156
18157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 * "tempname()" function
18159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018161f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164{
18165 static int x = 'A';
18166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018167 rettv->v_type = VAR_STRING;
18168 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
18170 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18171 * names. Skip 'I' and 'O', they are used for shell redirection. */
18172 do
18173 {
18174 if (x == 'Z')
18175 x = '0';
18176 else if (x == '9')
18177 x = 'A';
18178 else
18179 {
18180#ifdef EBCDIC
18181 if (x == 'I')
18182 x = 'J';
18183 else if (x == 'R')
18184 x = 'S';
18185 else
18186#endif
18187 ++x;
18188 }
18189 } while (x == 'I' || x == 'O');
18190}
18191
18192/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018193 * "test(list)" function: Just checking the walls...
18194 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018195 static void
18196f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018197 typval_T *argvars UNUSED;
18198 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018199{
18200 /* Used for unit testing. Change the code below to your liking. */
18201#if 0
18202 listitem_T *li;
18203 list_T *l;
18204 char_u *bad, *good;
18205
18206 if (argvars[0].v_type != VAR_LIST)
18207 return;
18208 l = argvars[0].vval.v_list;
18209 if (l == NULL)
18210 return;
18211 li = l->lv_first;
18212 if (li == NULL)
18213 return;
18214 bad = get_tv_string(&li->li_tv);
18215 li = li->li_next;
18216 if (li == NULL)
18217 return;
18218 good = get_tv_string(&li->li_tv);
18219 rettv->vval.v_number = test_edit_score(bad, good);
18220#endif
18221}
18222
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018223#ifdef FEAT_FLOAT
18224/*
18225 * "tan()" function
18226 */
18227 static void
18228f_tan(argvars, rettv)
18229 typval_T *argvars;
18230 typval_T *rettv;
18231{
18232 float_T f;
18233
18234 rettv->v_type = VAR_FLOAT;
18235 if (get_float_arg(argvars, &f) == OK)
18236 rettv->vval.v_float = tan(f);
18237 else
18238 rettv->vval.v_float = 0.0;
18239}
18240
18241/*
18242 * "tanh()" function
18243 */
18244 static void
18245f_tanh(argvars, rettv)
18246 typval_T *argvars;
18247 typval_T *rettv;
18248{
18249 float_T f;
18250
18251 rettv->v_type = VAR_FLOAT;
18252 if (get_float_arg(argvars, &f) == OK)
18253 rettv->vval.v_float = tanh(f);
18254 else
18255 rettv->vval.v_float = 0.0;
18256}
18257#endif
18258
Bram Moolenaard52d9742005-08-21 22:20:28 +000018259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 * "tolower(string)" function
18261 */
18262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018263f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018264 typval_T *argvars;
18265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266{
18267 char_u *p;
18268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018269 p = vim_strsave(get_tv_string(&argvars[0]));
18270 rettv->v_type = VAR_STRING;
18271 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272
18273 if (p != NULL)
18274 while (*p != NUL)
18275 {
18276#ifdef FEAT_MBYTE
18277 int l;
18278
18279 if (enc_utf8)
18280 {
18281 int c, lc;
18282
18283 c = utf_ptr2char(p);
18284 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018285 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 /* TODO: reallocate string when byte count changes. */
18287 if (utf_char2len(lc) == l)
18288 utf_char2bytes(lc, p);
18289 p += l;
18290 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018291 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 p += l; /* skip multi-byte character */
18293 else
18294#endif
18295 {
18296 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18297 ++p;
18298 }
18299 }
18300}
18301
18302/*
18303 * "toupper(string)" function
18304 */
18305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 typval_T *argvars;
18308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018310 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018311 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312}
18313
18314/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018315 * "tr(string, fromstr, tostr)" function
18316 */
18317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018318f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018319 typval_T *argvars;
18320 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018321{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018322 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018323 char_u *fromstr;
18324 char_u *tostr;
18325 char_u *p;
18326#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018327 int inlen;
18328 int fromlen;
18329 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018330 int idx;
18331 char_u *cpstr;
18332 int cplen;
18333 int first = TRUE;
18334#endif
18335 char_u buf[NUMBUFLEN];
18336 char_u buf2[NUMBUFLEN];
18337 garray_T ga;
18338
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018339 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018340 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18341 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018342
18343 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018344 rettv->v_type = VAR_STRING;
18345 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018346 if (fromstr == NULL || tostr == NULL)
18347 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018348 ga_init2(&ga, (int)sizeof(char), 80);
18349
18350#ifdef FEAT_MBYTE
18351 if (!has_mbyte)
18352#endif
18353 /* not multi-byte: fromstr and tostr must be the same length */
18354 if (STRLEN(fromstr) != STRLEN(tostr))
18355 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018356#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018357error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018358#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018359 EMSG2(_(e_invarg2), fromstr);
18360 ga_clear(&ga);
18361 return;
18362 }
18363
18364 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018365 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018366 {
18367#ifdef FEAT_MBYTE
18368 if (has_mbyte)
18369 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018370 inlen = (*mb_ptr2len)(in_str);
18371 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018372 cplen = inlen;
18373 idx = 0;
18374 for (p = fromstr; *p != NUL; p += fromlen)
18375 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018376 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018377 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018378 {
18379 for (p = tostr; *p != NUL; p += tolen)
18380 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018381 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018382 if (idx-- == 0)
18383 {
18384 cplen = tolen;
18385 cpstr = p;
18386 break;
18387 }
18388 }
18389 if (*p == NUL) /* tostr is shorter than fromstr */
18390 goto error;
18391 break;
18392 }
18393 ++idx;
18394 }
18395
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018396 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018397 {
18398 /* Check that fromstr and tostr have the same number of
18399 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018400 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018401 first = FALSE;
18402 for (p = tostr; *p != NUL; p += tolen)
18403 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018404 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018405 --idx;
18406 }
18407 if (idx != 0)
18408 goto error;
18409 }
18410
18411 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018412 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018413 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018414
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018415 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018416 }
18417 else
18418#endif
18419 {
18420 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018421 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018422 if (p != NULL)
18423 ga_append(&ga, tostr[p - fromstr]);
18424 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018425 ga_append(&ga, *in_str);
18426 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018427 }
18428 }
18429
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018430 /* add a terminating NUL */
18431 ga_grow(&ga, 1);
18432 ga_append(&ga, NUL);
18433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018434 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018435}
18436
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018437#ifdef FEAT_FLOAT
18438/*
18439 * "trunc({float})" function
18440 */
18441 static void
18442f_trunc(argvars, rettv)
18443 typval_T *argvars;
18444 typval_T *rettv;
18445{
18446 float_T f;
18447
18448 rettv->v_type = VAR_FLOAT;
18449 if (get_float_arg(argvars, &f) == OK)
18450 /* trunc() is not in C90, use floor() or ceil() instead. */
18451 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18452 else
18453 rettv->vval.v_float = 0.0;
18454}
18455#endif
18456
Bram Moolenaar8299df92004-07-10 09:47:34 +000018457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 * "type(expr)" function
18459 */
18460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018461f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018462 typval_T *argvars;
18463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018465 int n;
18466
18467 switch (argvars[0].v_type)
18468 {
18469 case VAR_NUMBER: n = 0; break;
18470 case VAR_STRING: n = 1; break;
18471 case VAR_FUNC: n = 2; break;
18472 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018473 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018474#ifdef FEAT_FLOAT
18475 case VAR_FLOAT: n = 5; break;
18476#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018477 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18478 }
18479 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480}
18481
18482/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018483 * "undofile(name)" function
18484 */
18485 static void
18486f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018487 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018488 typval_T *rettv;
18489{
18490 rettv->v_type = VAR_STRING;
18491#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018492 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018493 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018494
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018495 if (*fname == NUL)
18496 {
18497 /* If there is no file name there will be no undo file. */
18498 rettv->vval.v_string = NULL;
18499 }
18500 else
18501 {
18502 char_u *ffname = FullName_save(fname, FALSE);
18503
18504 if (ffname != NULL)
18505 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18506 vim_free(ffname);
18507 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018508 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018509#else
18510 rettv->vval.v_string = NULL;
18511#endif
18512}
18513
18514/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018515 * "undotree()" function
18516 */
18517 static void
18518f_undotree(argvars, rettv)
18519 typval_T *argvars UNUSED;
18520 typval_T *rettv;
18521{
18522 if (rettv_dict_alloc(rettv) == OK)
18523 {
18524 dict_T *dict = rettv->vval.v_dict;
18525 list_T *list;
18526
Bram Moolenaar730cde92010-06-27 05:18:54 +020018527 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018528 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018529 dict_add_nr_str(dict, "save_last",
18530 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018531 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18532 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018533 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018534
18535 list = list_alloc();
18536 if (list != NULL)
18537 {
18538 u_eval_tree(curbuf->b_u_oldhead, list);
18539 dict_add_list(dict, "entries", list);
18540 }
18541 }
18542}
18543
18544/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018545 * "values(dict)" function
18546 */
18547 static void
18548f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018549 typval_T *argvars;
18550 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018551{
18552 dict_list(argvars, rettv, 1);
18553}
18554
18555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 * "virtcol(string)" function
18557 */
18558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018559f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018560 typval_T *argvars;
18561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562{
18563 colnr_T vcol = 0;
18564 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018565 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018567 fp = var2fpos(&argvars[0], FALSE, &fnum);
18568 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18569 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 {
18571 getvvcol(curwin, fp, NULL, NULL, &vcol);
18572 ++vcol;
18573 }
18574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576}
18577
18578/*
18579 * "visualmode()" function
18580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018583 typval_T *argvars UNUSED;
18584 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585{
18586#ifdef FEAT_VISUAL
18587 char_u str[2];
18588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 str[0] = curbuf->b_visual_mode_eval;
18591 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593
18594 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018595 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597#endif
18598}
18599
18600/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018601 * "wildmenumode()" function
18602 */
18603 static void
18604f_wildmenumode(argvars, rettv)
18605 typval_T *argvars UNUSED;
18606 typval_T *rettv UNUSED;
18607{
18608#ifdef FEAT_WILDMENU
18609 if (wild_menu_showing)
18610 rettv->vval.v_number = 1;
18611#endif
18612}
18613
18614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 * "winbufnr(nr)" function
18616 */
18617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018618f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018619 typval_T *argvars;
18620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621{
18622 win_T *wp;
18623
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018624 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018628 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629}
18630
18631/*
18632 * "wincol()" function
18633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638{
18639 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641}
18642
18643/*
18644 * "winheight(nr)" function
18645 */
18646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018647f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018648 typval_T *argvars;
18649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650{
18651 win_T *wp;
18652
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018653 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658}
18659
18660/*
18661 * "winline()" function
18662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667{
18668 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670}
18671
18672/*
18673 * "winnr()" function
18674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018677 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679{
18680 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018681
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018683 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018685 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686}
18687
18688/*
18689 * "winrestcmd()" function
18690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018692f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018693 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695{
18696#ifdef FEAT_WINDOWS
18697 win_T *wp;
18698 int winnr = 1;
18699 garray_T ga;
18700 char_u buf[50];
18701
18702 ga_init2(&ga, (int)sizeof(char), 70);
18703 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18704 {
18705 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18706 ga_concat(&ga, buf);
18707# ifdef FEAT_VERTSPLIT
18708 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18709 ga_concat(&ga, buf);
18710# endif
18711 ++winnr;
18712 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018713 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018715 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018719 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720}
18721
18722/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018723 * "winrestview()" function
18724 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018725 static void
18726f_winrestview(argvars, rettv)
18727 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018728 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018729{
18730 dict_T *dict;
18731
18732 if (argvars[0].v_type != VAR_DICT
18733 || (dict = argvars[0].vval.v_dict) == NULL)
18734 EMSG(_(e_invarg));
18735 else
18736 {
18737 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18738 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18739#ifdef FEAT_VIRTUALEDIT
18740 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18741#endif
18742 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018743 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018744
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018745 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018746#ifdef FEAT_DIFF
18747 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18748#endif
18749 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18750 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18751
18752 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018753 win_new_height(curwin, curwin->w_height);
18754# ifdef FEAT_VERTSPLIT
18755 win_new_width(curwin, W_WIDTH(curwin));
18756# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018757 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018758
18759 if (curwin->w_topline == 0)
18760 curwin->w_topline = 1;
18761 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18762 curwin->w_topline = curbuf->b_ml.ml_line_count;
18763#ifdef FEAT_DIFF
18764 check_topfill(curwin, TRUE);
18765#endif
18766 }
18767}
18768
18769/*
18770 * "winsaveview()" function
18771 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018772 static void
18773f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018774 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018775 typval_T *rettv;
18776{
18777 dict_T *dict;
18778
Bram Moolenaara800b422010-06-27 01:15:55 +020018779 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018780 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018781 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018782
18783 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18784 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18785#ifdef FEAT_VIRTUALEDIT
18786 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18787#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018788 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018789 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18790
18791 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18792#ifdef FEAT_DIFF
18793 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18794#endif
18795 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18796 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18797}
18798
18799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 * "winwidth(nr)" function
18801 */
18802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018804 typval_T *argvars;
18805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806{
18807 win_T *wp;
18808
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018809 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 else
18813#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018814 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018816 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817#endif
18818}
18819
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018821 * "writefile()" function
18822 */
18823 static void
18824f_writefile(argvars, rettv)
18825 typval_T *argvars;
18826 typval_T *rettv;
18827{
18828 int binary = FALSE;
18829 char_u *fname;
18830 FILE *fd;
18831 listitem_T *li;
18832 char_u *s;
18833 int ret = 0;
18834 int c;
18835
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018836 if (check_restricted() || check_secure())
18837 return;
18838
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018839 if (argvars[0].v_type != VAR_LIST)
18840 {
18841 EMSG2(_(e_listarg), "writefile()");
18842 return;
18843 }
18844 if (argvars[0].vval.v_list == NULL)
18845 return;
18846
18847 if (argvars[2].v_type != VAR_UNKNOWN
18848 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18849 binary = TRUE;
18850
18851 /* Always open the file in binary mode, library functions have a mind of
18852 * their own about CR-LF conversion. */
18853 fname = get_tv_string(&argvars[1]);
18854 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18855 {
18856 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18857 ret = -1;
18858 }
18859 else
18860 {
18861 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18862 li = li->li_next)
18863 {
18864 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18865 {
18866 if (*s == '\n')
18867 c = putc(NUL, fd);
18868 else
18869 c = putc(*s, fd);
18870 if (c == EOF)
18871 {
18872 ret = -1;
18873 break;
18874 }
18875 }
18876 if (!binary || li->li_next != NULL)
18877 if (putc('\n', fd) == EOF)
18878 {
18879 ret = -1;
18880 break;
18881 }
18882 if (ret < 0)
18883 {
18884 EMSG(_(e_write));
18885 break;
18886 }
18887 }
18888 fclose(fd);
18889 }
18890
18891 rettv->vval.v_number = ret;
18892}
18893
18894/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018895 * "xor(expr, expr)" function
18896 */
18897 static void
18898f_xor(argvars, rettv)
18899 typval_T *argvars;
18900 typval_T *rettv;
18901{
18902 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18903 ^ get_tv_number_chk(&argvars[1], NULL);
18904}
18905
18906
18907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018909 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 */
18911 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018912var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018913 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018914 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018915 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018917 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018919 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920
Bram Moolenaara5525202006-03-02 22:52:09 +000018921 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018922 if (varp->v_type == VAR_LIST)
18923 {
18924 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018925 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018926 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018927 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018928
18929 l = varp->vval.v_list;
18930 if (l == NULL)
18931 return NULL;
18932
18933 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018934 pos.lnum = list_find_nr(l, 0L, &error);
18935 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018936 return NULL; /* invalid line number */
18937
18938 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018939 pos.col = list_find_nr(l, 1L, &error);
18940 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018941 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018942 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018943
18944 /* We accept "$" for the column number: last column. */
18945 li = list_find(l, 1L);
18946 if (li != NULL && li->li_tv.v_type == VAR_STRING
18947 && li->li_tv.vval.v_string != NULL
18948 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18949 pos.col = len + 1;
18950
Bram Moolenaara5525202006-03-02 22:52:09 +000018951 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018952 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018953 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018954 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018955
Bram Moolenaara5525202006-03-02 22:52:09 +000018956#ifdef FEAT_VIRTUALEDIT
18957 /* Get the virtual offset. Defaults to zero. */
18958 pos.coladd = list_find_nr(l, 2L, &error);
18959 if (error)
18960 pos.coladd = 0;
18961#endif
18962
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018963 return &pos;
18964 }
18965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018966 name = get_tv_string_chk(varp);
18967 if (name == NULL)
18968 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018969 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018971#ifdef FEAT_VISUAL
18972 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18973 {
18974 if (VIsual_active)
18975 return &VIsual;
18976 return &curwin->w_cursor;
18977 }
18978#endif
18979 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018981 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18983 return NULL;
18984 return pp;
18985 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018986
18987#ifdef FEAT_VIRTUALEDIT
18988 pos.coladd = 0;
18989#endif
18990
Bram Moolenaar477933c2007-07-17 14:32:23 +000018991 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018992 {
18993 pos.col = 0;
18994 if (name[1] == '0') /* "w0": first visible line */
18995 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018996 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018997 pos.lnum = curwin->w_topline;
18998 return &pos;
18999 }
19000 else if (name[1] == '$') /* "w$": last visible line */
19001 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019002 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019003 pos.lnum = curwin->w_botline - 1;
19004 return &pos;
19005 }
19006 }
19007 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019009 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 {
19011 pos.lnum = curbuf->b_ml.ml_line_count;
19012 pos.col = 0;
19013 }
19014 else
19015 {
19016 pos.lnum = curwin->w_cursor.lnum;
19017 pos.col = (colnr_T)STRLEN(ml_get_curline());
19018 }
19019 return &pos;
19020 }
19021 return NULL;
19022}
19023
19024/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019025 * Convert list in "arg" into a position and optional file number.
19026 * When "fnump" is NULL there is no file number, only 3 items.
19027 * Note that the column is passed on as-is, the caller may want to decrement
19028 * it to use 1 for the first column.
19029 * Return FAIL when conversion is not possible, doesn't check the position for
19030 * validity.
19031 */
19032 static int
19033list2fpos(arg, posp, fnump)
19034 typval_T *arg;
19035 pos_T *posp;
19036 int *fnump;
19037{
19038 list_T *l = arg->vval.v_list;
19039 long i = 0;
19040 long n;
19041
Bram Moolenaarbde35262006-07-23 20:12:24 +000019042 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19043 * when "fnump" isn't NULL and "coladd" is optional. */
19044 if (arg->v_type != VAR_LIST
19045 || l == NULL
19046 || l->lv_len < (fnump == NULL ? 2 : 3)
19047 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019048 return FAIL;
19049
19050 if (fnump != NULL)
19051 {
19052 n = list_find_nr(l, i++, NULL); /* fnum */
19053 if (n < 0)
19054 return FAIL;
19055 if (n == 0)
19056 n = curbuf->b_fnum; /* current buffer */
19057 *fnump = n;
19058 }
19059
19060 n = list_find_nr(l, i++, NULL); /* lnum */
19061 if (n < 0)
19062 return FAIL;
19063 posp->lnum = n;
19064
19065 n = list_find_nr(l, i++, NULL); /* col */
19066 if (n < 0)
19067 return FAIL;
19068 posp->col = n;
19069
19070#ifdef FEAT_VIRTUALEDIT
19071 n = list_find_nr(l, i, NULL);
19072 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019073 posp->coladd = 0;
19074 else
19075 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019076#endif
19077
19078 return OK;
19079}
19080
19081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 * Get the length of an environment variable name.
19083 * Advance "arg" to the first character after the name.
19084 * Return 0 for error.
19085 */
19086 static int
19087get_env_len(arg)
19088 char_u **arg;
19089{
19090 char_u *p;
19091 int len;
19092
19093 for (p = *arg; vim_isIDc(*p); ++p)
19094 ;
19095 if (p == *arg) /* no name found */
19096 return 0;
19097
19098 len = (int)(p - *arg);
19099 *arg = p;
19100 return len;
19101}
19102
19103/*
19104 * Get the length of the name of a function or internal variable.
19105 * "arg" is advanced to the first non-white character after the name.
19106 * Return 0 if something is wrong.
19107 */
19108 static int
19109get_id_len(arg)
19110 char_u **arg;
19111{
19112 char_u *p;
19113 int len;
19114
19115 /* Find the end of the name. */
19116 for (p = *arg; eval_isnamec(*p); ++p)
19117 ;
19118 if (p == *arg) /* no name found */
19119 return 0;
19120
19121 len = (int)(p - *arg);
19122 *arg = skipwhite(p);
19123
19124 return len;
19125}
19126
19127/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019128 * Get the length of the name of a variable or function.
19129 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019131 * Return -1 if curly braces expansion failed.
19132 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 * If the name contains 'magic' {}'s, expand them and return the
19134 * expanded name in an allocated string via 'alias' - caller must free.
19135 */
19136 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019137get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 char_u **arg;
19139 char_u **alias;
19140 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019141 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142{
19143 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 char_u *p;
19145 char_u *expr_start;
19146 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147
19148 *alias = NULL; /* default to no alias */
19149
19150 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19151 && (*arg)[2] == (int)KE_SNR)
19152 {
19153 /* hard coded <SNR>, already translated */
19154 *arg += 3;
19155 return get_id_len(arg) + 3;
19156 }
19157 len = eval_fname_script(*arg);
19158 if (len > 0)
19159 {
19160 /* literal "<SID>", "s:" or "<SNR>" */
19161 *arg += len;
19162 }
19163
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019167 p = find_name_end(*arg, &expr_start, &expr_end,
19168 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 if (expr_start != NULL)
19170 {
19171 char_u *temp_string;
19172
19173 if (!evaluate)
19174 {
19175 len += (int)(p - *arg);
19176 *arg = skipwhite(p);
19177 return len;
19178 }
19179
19180 /*
19181 * Include any <SID> etc in the expanded string:
19182 * Thus the -len here.
19183 */
19184 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19185 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019186 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 *alias = temp_string;
19188 *arg = skipwhite(p);
19189 return (int)STRLEN(temp_string);
19190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191
19192 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019193 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194 EMSG2(_(e_invexpr2), *arg);
19195
19196 return len;
19197}
19198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199/*
19200 * Find the end of a variable or function name, taking care of magic braces.
19201 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19202 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019203 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 * Return a pointer to just after the name. Equal to "arg" if there is no
19205 * valid name.
19206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019208find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 char_u *arg;
19210 char_u **expr_start;
19211 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019212 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019214 int mb_nest = 0;
19215 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216 char_u *p;
19217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019218 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019220 *expr_start = NULL;
19221 *expr_end = NULL;
19222 }
19223
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019224 /* Quick check for valid starting character. */
19225 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19226 return arg;
19227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019228 for (p = arg; *p != NUL
19229 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019230 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019231 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019233 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019234 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019235 if (*p == '\'')
19236 {
19237 /* skip over 'string' to avoid counting [ and ] inside it. */
19238 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19239 ;
19240 if (*p == NUL)
19241 break;
19242 }
19243 else if (*p == '"')
19244 {
19245 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19246 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19247 if (*p == '\\' && p[1] != NUL)
19248 ++p;
19249 if (*p == NUL)
19250 break;
19251 }
19252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019253 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019255 if (*p == '[')
19256 ++br_nest;
19257 else if (*p == ']')
19258 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019261 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019263 if (*p == '{')
19264 {
19265 mb_nest++;
19266 if (expr_start != NULL && *expr_start == NULL)
19267 *expr_start = p;
19268 }
19269 else if (*p == '}')
19270 {
19271 mb_nest--;
19272 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19273 *expr_end = p;
19274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 }
19277
19278 return p;
19279}
19280
19281/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019282 * Expands out the 'magic' {}'s in a variable/function name.
19283 * Note that this can call itself recursively, to deal with
19284 * constructs like foo{bar}{baz}{bam}
19285 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19286 * "in_start" ^
19287 * "expr_start" ^
19288 * "expr_end" ^
19289 * "in_end" ^
19290 *
19291 * Returns a new allocated string, which the caller must free.
19292 * Returns NULL for failure.
19293 */
19294 static char_u *
19295make_expanded_name(in_start, expr_start, expr_end, in_end)
19296 char_u *in_start;
19297 char_u *expr_start;
19298 char_u *expr_end;
19299 char_u *in_end;
19300{
19301 char_u c1;
19302 char_u *retval = NULL;
19303 char_u *temp_result;
19304 char_u *nextcmd = NULL;
19305
19306 if (expr_end == NULL || in_end == NULL)
19307 return NULL;
19308 *expr_start = NUL;
19309 *expr_end = NUL;
19310 c1 = *in_end;
19311 *in_end = NUL;
19312
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019313 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019314 if (temp_result != NULL && nextcmd == NULL)
19315 {
19316 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19317 + (in_end - expr_end) + 1));
19318 if (retval != NULL)
19319 {
19320 STRCPY(retval, in_start);
19321 STRCAT(retval, temp_result);
19322 STRCAT(retval, expr_end + 1);
19323 }
19324 }
19325 vim_free(temp_result);
19326
19327 *in_end = c1; /* put char back for error messages */
19328 *expr_start = '{';
19329 *expr_end = '}';
19330
19331 if (retval != NULL)
19332 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019333 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019334 if (expr_start != NULL)
19335 {
19336 /* Further expansion! */
19337 temp_result = make_expanded_name(retval, expr_start,
19338 expr_end, temp_result);
19339 vim_free(retval);
19340 retval = temp_result;
19341 }
19342 }
19343
19344 return retval;
19345}
19346
19347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019349 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 */
19351 static int
19352eval_isnamec(c)
19353 int c;
19354{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019355 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19356}
19357
19358/*
19359 * Return TRUE if character "c" can be used as the first character in a
19360 * variable or function name (excluding '{' and '}').
19361 */
19362 static int
19363eval_isnamec1(c)
19364 int c;
19365{
19366 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367}
19368
19369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370 * Set number v: variable to "val".
19371 */
19372 void
19373set_vim_var_nr(idx, val)
19374 int idx;
19375 long val;
19376{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019377 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378}
19379
19380/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019381 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 */
19383 long
19384get_vim_var_nr(idx)
19385 int idx;
19386{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019387 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388}
19389
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019390/*
19391 * Get string v: variable value. Uses a static buffer, can only be used once.
19392 */
19393 char_u *
19394get_vim_var_str(idx)
19395 int idx;
19396{
19397 return get_tv_string(&vimvars[idx].vv_tv);
19398}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019399
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019401 * Get List v: variable value. Caller must take care of reference count when
19402 * needed.
19403 */
19404 list_T *
19405get_vim_var_list(idx)
19406 int idx;
19407{
19408 return vimvars[idx].vv_list;
19409}
19410
19411/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019412 * Set v:char to character "c".
19413 */
19414 void
19415set_vim_var_char(c)
19416 int c;
19417{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019418 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019419
19420#ifdef FEAT_MBYTE
19421 if (has_mbyte)
19422 buf[(*mb_char2bytes)(c, buf)] = NUL;
19423 else
19424#endif
19425 {
19426 buf[0] = c;
19427 buf[1] = NUL;
19428 }
19429 set_vim_var_string(VV_CHAR, buf, -1);
19430}
19431
19432/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019433 * Set v:count to "count" and v:count1 to "count1".
19434 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 */
19436 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019437set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 long count;
19439 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019440 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019442 if (set_prevcount)
19443 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019444 vimvars[VV_COUNT].vv_nr = count;
19445 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446}
19447
19448/*
19449 * Set string v: variable to a copy of "val".
19450 */
19451 void
19452set_vim_var_string(idx, val, len)
19453 int idx;
19454 char_u *val;
19455 int len; /* length of "val" to use or -1 (whole string) */
19456{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019457 /* Need to do this (at least) once, since we can't initialize a union.
19458 * Will always be invoked when "v:progname" is set. */
19459 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19460
Bram Moolenaare9a41262005-01-15 22:18:47 +000019461 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019463 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019465 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019467 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468}
19469
19470/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019471 * Set List v: variable to "val".
19472 */
19473 void
19474set_vim_var_list(idx, val)
19475 int idx;
19476 list_T *val;
19477{
19478 list_unref(vimvars[idx].vv_list);
19479 vimvars[idx].vv_list = val;
19480 if (val != NULL)
19481 ++val->lv_refcount;
19482}
19483
19484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 * Set v:register if needed.
19486 */
19487 void
19488set_reg_var(c)
19489 int c;
19490{
19491 char_u regname;
19492
19493 if (c == 0 || c == ' ')
19494 regname = '"';
19495 else
19496 regname = c;
19497 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019498 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 set_vim_var_string(VV_REG, &regname, 1);
19500}
19501
19502/*
19503 * Get or set v:exception. If "oldval" == NULL, return the current value.
19504 * Otherwise, restore the value to "oldval" and return NULL.
19505 * Must always be called in pairs to save and restore v:exception! Does not
19506 * take care of memory allocations.
19507 */
19508 char_u *
19509v_exception(oldval)
19510 char_u *oldval;
19511{
19512 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019513 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514
Bram Moolenaare9a41262005-01-15 22:18:47 +000019515 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 return NULL;
19517}
19518
19519/*
19520 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19521 * Otherwise, restore the value to "oldval" and return NULL.
19522 * Must always be called in pairs to save and restore v:throwpoint! Does not
19523 * take care of memory allocations.
19524 */
19525 char_u *
19526v_throwpoint(oldval)
19527 char_u *oldval;
19528{
19529 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019530 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531
Bram Moolenaare9a41262005-01-15 22:18:47 +000019532 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 return NULL;
19534}
19535
19536#if defined(FEAT_AUTOCMD) || defined(PROTO)
19537/*
19538 * Set v:cmdarg.
19539 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19540 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19541 * Must always be called in pairs!
19542 */
19543 char_u *
19544set_cmdarg(eap, oldarg)
19545 exarg_T *eap;
19546 char_u *oldarg;
19547{
19548 char_u *oldval;
19549 char_u *newval;
19550 unsigned len;
19551
Bram Moolenaare9a41262005-01-15 22:18:47 +000019552 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019553 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019555 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019556 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019557 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 }
19559
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019560 if (eap->force_bin == FORCE_BIN)
19561 len = 6;
19562 else if (eap->force_bin == FORCE_NOBIN)
19563 len = 8;
19564 else
19565 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019566
19567 if (eap->read_edit)
19568 len += 7;
19569
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019570 if (eap->force_ff != 0)
19571 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19572# ifdef FEAT_MBYTE
19573 if (eap->force_enc != 0)
19574 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019575 if (eap->bad_char != 0)
19576 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019577# endif
19578
19579 newval = alloc(len + 1);
19580 if (newval == NULL)
19581 return NULL;
19582
19583 if (eap->force_bin == FORCE_BIN)
19584 sprintf((char *)newval, " ++bin");
19585 else if (eap->force_bin == FORCE_NOBIN)
19586 sprintf((char *)newval, " ++nobin");
19587 else
19588 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019589
19590 if (eap->read_edit)
19591 STRCAT(newval, " ++edit");
19592
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019593 if (eap->force_ff != 0)
19594 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19595 eap->cmd + eap->force_ff);
19596# ifdef FEAT_MBYTE
19597 if (eap->force_enc != 0)
19598 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19599 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019600 if (eap->bad_char == BAD_KEEP)
19601 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19602 else if (eap->bad_char == BAD_DROP)
19603 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19604 else if (eap->bad_char != 0)
19605 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019606# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019607 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019608 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609}
19610#endif
19611
19612/*
19613 * Get the value of internal variable "name".
19614 * Return OK or FAIL.
19615 */
19616 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019617get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 char_u *name;
19619 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019620 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019621 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622{
19623 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019624 typval_T *tv = NULL;
19625 typval_T atv;
19626 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628
19629 /* truncate the name, so that we can use strcmp() */
19630 cc = name[len];
19631 name[len] = NUL;
19632
19633 /*
19634 * Check for "b:changedtick".
19635 */
19636 if (STRCMP(name, "b:changedtick") == 0)
19637 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019638 atv.v_type = VAR_NUMBER;
19639 atv.vval.v_number = curbuf->b_changedtick;
19640 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 }
19642
19643 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 * Check for user-defined variables.
19645 */
19646 else
19647 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019648 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 }
19652
Bram Moolenaare9a41262005-01-15 22:18:47 +000019653 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019655 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 ret = FAIL;
19658 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019659 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019660 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661
19662 name[len] = cc;
19663
19664 return ret;
19665}
19666
19667/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019668 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19669 * Also handle function call with Funcref variable: func(expr)
19670 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19671 */
19672 static int
19673handle_subscript(arg, rettv, evaluate, verbose)
19674 char_u **arg;
19675 typval_T *rettv;
19676 int evaluate; /* do more than finding the end */
19677 int verbose; /* give error messages */
19678{
19679 int ret = OK;
19680 dict_T *selfdict = NULL;
19681 char_u *s;
19682 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019683 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019684
19685 while (ret == OK
19686 && (**arg == '['
19687 || (**arg == '.' && rettv->v_type == VAR_DICT)
19688 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19689 && !vim_iswhite(*(*arg - 1)))
19690 {
19691 if (**arg == '(')
19692 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019693 /* need to copy the funcref so that we can clear rettv */
19694 functv = *rettv;
19695 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019696
19697 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019698 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019699 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019700 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19701 &len, evaluate, selfdict);
19702
19703 /* Clear the funcref afterwards, so that deleting it while
19704 * evaluating the arguments is possible (see test55). */
19705 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019706
19707 /* Stop the expression evaluation when immediately aborting on
19708 * error, or when an interrupt occurred or an exception was thrown
19709 * but not caught. */
19710 if (aborting())
19711 {
19712 if (ret == OK)
19713 clear_tv(rettv);
19714 ret = FAIL;
19715 }
19716 dict_unref(selfdict);
19717 selfdict = NULL;
19718 }
19719 else /* **arg == '[' || **arg == '.' */
19720 {
19721 dict_unref(selfdict);
19722 if (rettv->v_type == VAR_DICT)
19723 {
19724 selfdict = rettv->vval.v_dict;
19725 if (selfdict != NULL)
19726 ++selfdict->dv_refcount;
19727 }
19728 else
19729 selfdict = NULL;
19730 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19731 {
19732 clear_tv(rettv);
19733 ret = FAIL;
19734 }
19735 }
19736 }
19737 dict_unref(selfdict);
19738 return ret;
19739}
19740
19741/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019742 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019743 * value).
19744 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019745 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019747{
Bram Moolenaar33570922005-01-25 22:26:29 +000019748 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019749}
19750
19751/*
19752 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 * The string "s" must have been allocated, it is consumed.
19754 * Return NULL for out of memory, the variable otherwise.
19755 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019756 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 char_u *s;
19759{
Bram Moolenaar33570922005-01-25 22:26:29 +000019760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019762 rettv = alloc_tv();
19763 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 rettv->v_type = VAR_STRING;
19766 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 }
19768 else
19769 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019770 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771}
19772
19773/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019774 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019776 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019778 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779{
19780 if (varp != NULL)
19781 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019782 switch (varp->v_type)
19783 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019784 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019785 func_unref(varp->vval.v_string);
19786 /*FALLTHROUGH*/
19787 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019788 vim_free(varp->vval.v_string);
19789 break;
19790 case VAR_LIST:
19791 list_unref(varp->vval.v_list);
19792 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019793 case VAR_DICT:
19794 dict_unref(varp->vval.v_dict);
19795 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019796 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019797#ifdef FEAT_FLOAT
19798 case VAR_FLOAT:
19799#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019800 case VAR_UNKNOWN:
19801 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019802 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019803 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019804 break;
19805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 vim_free(varp);
19807 }
19808}
19809
19810/*
19811 * Free the memory for a variable value and set the value to NULL or 0.
19812 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019813 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019814clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019815 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816{
19817 if (varp != NULL)
19818 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019819 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019821 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019822 func_unref(varp->vval.v_string);
19823 /*FALLTHROUGH*/
19824 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019825 vim_free(varp->vval.v_string);
19826 varp->vval.v_string = NULL;
19827 break;
19828 case VAR_LIST:
19829 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019830 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019831 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019832 case VAR_DICT:
19833 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019834 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019835 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019837 varp->vval.v_number = 0;
19838 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019839#ifdef FEAT_FLOAT
19840 case VAR_FLOAT:
19841 varp->vval.v_float = 0.0;
19842 break;
19843#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019844 case VAR_UNKNOWN:
19845 break;
19846 default:
19847 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019849 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 }
19851}
19852
19853/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019854 * Set the value of a variable to NULL without freeing items.
19855 */
19856 static void
19857init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019858 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019859{
19860 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019862}
19863
19864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 * Get the number value of a variable.
19866 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019867 * For incompatible types, return 0.
19868 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19869 * caller of incompatible types: it sets *denote to TRUE if "denote"
19870 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 */
19872 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019873get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019874 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019876 int error = FALSE;
19877
19878 return get_tv_number_chk(varp, &error); /* return 0L on error */
19879}
19880
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019881 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019882get_tv_number_chk(varp, denote)
19883 typval_T *varp;
19884 int *denote;
19885{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019886 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019888 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019890 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019891 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019892#ifdef FEAT_FLOAT
19893 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019894 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019895 break;
19896#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019897 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019898 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019899 break;
19900 case VAR_STRING:
19901 if (varp->vval.v_string != NULL)
19902 vim_str2nr(varp->vval.v_string, NULL, NULL,
19903 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019904 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019905 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019906 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019907 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019908 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019909 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019910 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019911 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019912 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019915 if (denote == NULL) /* useful for values that must be unsigned */
19916 n = -1;
19917 else
19918 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019919 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920}
19921
19922/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019923 * Get the lnum from the first argument.
19924 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019925 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 */
19927 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019928get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019929 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930{
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 linenr_T lnum;
19933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019934 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 if (lnum == 0) /* no valid number, try using line() */
19936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937 rettv.v_type = VAR_NUMBER;
19938 f_line(argvars, &rettv);
19939 lnum = rettv.vval.v_number;
19940 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 }
19942 return lnum;
19943}
19944
19945/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019946 * Get the lnum from the first argument.
19947 * Also accepts "$", then "buf" is used.
19948 * Returns 0 on error.
19949 */
19950 static linenr_T
19951get_tv_lnum_buf(argvars, buf)
19952 typval_T *argvars;
19953 buf_T *buf;
19954{
19955 if (argvars[0].v_type == VAR_STRING
19956 && argvars[0].vval.v_string != NULL
19957 && argvars[0].vval.v_string[0] == '$'
19958 && buf != NULL)
19959 return buf->b_ml.ml_line_count;
19960 return get_tv_number_chk(&argvars[0], NULL);
19961}
19962
19963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 * Get the string value of a variable.
19965 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019966 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19967 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 * If the String variable has never been set, return an empty string.
19969 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019970 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19971 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 */
19973 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019975 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976{
19977 static char_u mybuf[NUMBUFLEN];
19978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019979 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980}
19981
19982 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019983get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019984 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019985 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019987 char_u *res = get_tv_string_buf_chk(varp, buf);
19988
19989 return res != NULL ? res : (char_u *)"";
19990}
19991
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019992 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019993get_tv_string_chk(varp)
19994 typval_T *varp;
19995{
19996 static char_u mybuf[NUMBUFLEN];
19997
19998 return get_tv_string_buf_chk(varp, mybuf);
19999}
20000
20001 static char_u *
20002get_tv_string_buf_chk(varp, buf)
20003 typval_T *varp;
20004 char_u *buf;
20005{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020006 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020008 case VAR_NUMBER:
20009 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20010 return buf;
20011 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020012 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020013 break;
20014 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020015 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020016 break;
20017 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020018 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020019 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020020#ifdef FEAT_FLOAT
20021 case VAR_FLOAT:
20022 EMSG(_("E806: using Float as a String"));
20023 break;
20024#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020025 case VAR_STRING:
20026 if (varp->vval.v_string != NULL)
20027 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020028 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020029 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020030 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020033 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034}
20035
20036/*
20037 * Find variable "name" in the list of variables.
20038 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020039 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020040 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020043 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020044find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020046 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020049 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050
Bram Moolenaara7043832005-01-21 11:56:39 +000020051 ht = find_var_ht(name, &varname);
20052 if (htp != NULL)
20053 *htp = ht;
20054 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020056 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057}
20058
20059/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020060 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020061 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020064find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020066 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020067 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020068 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020069{
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 hashitem_T *hi;
20071
20072 if (*varname == NUL)
20073 {
20074 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020075 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020076 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020077 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020078 case 'g': return &globvars_var;
20079 case 'v': return &vimvars_var;
20080 case 'b': return &curbuf->b_bufvar;
20081 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020082#ifdef FEAT_WINDOWS
20083 case 't': return &curtab->tp_winvar;
20084#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020085 case 'l': return current_funccal == NULL
20086 ? NULL : &current_funccal->l_vars_var;
20087 case 'a': return current_funccal == NULL
20088 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020089 }
20090 return NULL;
20091 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020092
20093 hi = hash_find(ht, varname);
20094 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020095 {
20096 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020097 * worked find the variable again. Don't auto-load a script if it was
20098 * loaded already, otherwise it would be loaded every time when
20099 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020100 if (ht == &globvarht && !writing)
20101 {
20102 /* Note: script_autoload() may make "hi" invalid. It must either
20103 * be obtained again or not used. */
20104 if (!script_autoload(varname, FALSE) || aborting())
20105 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020106 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020107 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020108 if (HASHITEM_EMPTY(hi))
20109 return NULL;
20110 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020112}
20113
20114/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020115 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020116 * Set "varname" to the start of name without ':'.
20117 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020118 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020119find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 char_u *name;
20121 char_u **varname;
20122{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020123 hashitem_T *hi;
20124
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 if (name[1] != ':')
20126 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020127 /* The name must not start with a colon or #. */
20128 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 return NULL;
20130 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020131
20132 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020133 hi = hash_find(&compat_hashtab, name);
20134 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020135 return &compat_hashtab;
20136
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020138 return &globvarht; /* global variable */
20139 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 }
20141 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020142 if (*name == 'g') /* global variable */
20143 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020144 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20145 */
20146 if (vim_strchr(name + 2, ':') != NULL
20147 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020148 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020150 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020152 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020153#ifdef FEAT_WINDOWS
20154 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020155 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020156#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020157 if (*name == 'v') /* v: variable */
20158 return &vimvarht;
20159 if (*name == 'a' && current_funccal != NULL) /* function argument */
20160 return &current_funccal->l_avars.dv_hashtab;
20161 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20162 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 if (*name == 's' /* script variable */
20164 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20165 return &SCRIPT_VARS(current_SID);
20166 return NULL;
20167}
20168
20169/*
20170 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020171 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 * Returns NULL when it doesn't exist.
20173 */
20174 char_u *
20175get_var_value(name)
20176 char_u *name;
20177{
Bram Moolenaar33570922005-01-25 22:26:29 +000020178 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179
Bram Moolenaara7043832005-01-21 11:56:39 +000020180 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 if (v == NULL)
20182 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184}
20185
20186/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020187 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 * sourcing this script and when executing functions defined in the script.
20189 */
20190 void
20191new_script_vars(id)
20192 scid_T id;
20193{
Bram Moolenaara7043832005-01-21 11:56:39 +000020194 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020195 hashtab_T *ht;
20196 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020197
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20199 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020200 /* Re-allocating ga_data means that an ht_array pointing to
20201 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020203 for (i = 1; i <= ga_scripts.ga_len; ++i)
20204 {
20205 ht = &SCRIPT_VARS(i);
20206 if (ht->ht_mask == HT_INIT_SIZE - 1)
20207 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020208 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020210 }
20211
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 while (ga_scripts.ga_len < id)
20213 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020214 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020215 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020216 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 }
20219 }
20220}
20221
20222/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20224 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 */
20226 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020227init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 dict_T *dict;
20229 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020230 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231{
Bram Moolenaar33570922005-01-25 22:26:29 +000020232 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020233 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020234 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020235 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020236 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020237 dict_var->di_tv.vval.v_dict = dict;
20238 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020239 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20241 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242}
20243
20244/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020245 * Unreference a dictionary initialized by init_var_dict().
20246 */
20247 void
20248unref_var_dict(dict)
20249 dict_T *dict;
20250{
20251 /* Now the dict needs to be freed if no one else is using it, go back to
20252 * normal reference counting. */
20253 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20254 dict_unref(dict);
20255}
20256
20257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 * Frees all allocated variables and the value they contain.
20260 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 */
20262 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020263vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020264 hashtab_T *ht;
20265{
20266 vars_clear_ext(ht, TRUE);
20267}
20268
20269/*
20270 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20271 */
20272 static void
20273vars_clear_ext(ht, free_val)
20274 hashtab_T *ht;
20275 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276{
Bram Moolenaara7043832005-01-21 11:56:39 +000020277 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020278 hashitem_T *hi;
20279 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280
Bram Moolenaar33570922005-01-25 22:26:29 +000020281 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020282 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020283 for (hi = ht->ht_array; todo > 0; ++hi)
20284 {
20285 if (!HASHITEM_EMPTY(hi))
20286 {
20287 --todo;
20288
Bram Moolenaar33570922005-01-25 22:26:29 +000020289 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020290 * ht_array might change then. hash_clear() takes care of it
20291 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 v = HI2DI(hi);
20293 if (free_val)
20294 clear_tv(&v->di_tv);
20295 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20296 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020297 }
20298 }
20299 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020300 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301}
20302
Bram Moolenaara7043832005-01-21 11:56:39 +000020303/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020304 * Delete a variable from hashtab "ht" at item "hi".
20305 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020308delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020309 hashtab_T *ht;
20310 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311{
Bram Moolenaar33570922005-01-25 22:26:29 +000020312 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020313
20314 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 clear_tv(&di->di_tv);
20316 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317}
20318
20319/*
20320 * List the value of one internal variable.
20321 */
20322 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020323list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020326 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020328 char_u *tofree;
20329 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020330 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020331
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020332 current_copyID += COPYID_INC;
20333 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020334 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020335 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020336 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337}
20338
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020340list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 char_u *prefix;
20342 char_u *name;
20343 int type;
20344 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020345 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346{
Bram Moolenaar31859182007-08-14 20:41:13 +000020347 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20348 msg_start();
20349 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 if (name != NULL) /* "a:" vars don't have a name stored */
20351 msg_puts(name);
20352 msg_putchar(' ');
20353 msg_advance(22);
20354 if (type == VAR_NUMBER)
20355 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020356 else if (type == VAR_FUNC)
20357 msg_putchar('*');
20358 else if (type == VAR_LIST)
20359 {
20360 msg_putchar('[');
20361 if (*string == '[')
20362 ++string;
20363 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020364 else if (type == VAR_DICT)
20365 {
20366 msg_putchar('{');
20367 if (*string == '{')
20368 ++string;
20369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 else
20371 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020372
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020374
20375 if (type == VAR_FUNC)
20376 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020377 if (*first)
20378 {
20379 msg_clr_eos();
20380 *first = FALSE;
20381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382}
20383
20384/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020385 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 * If the variable already exists, the value is updated.
20387 * Otherwise the variable is created.
20388 */
20389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020390set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020392 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020393 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394{
Bram Moolenaar33570922005-01-25 22:26:29 +000020395 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020397 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020399 ht = find_var_ht(name, &varname);
20400 if (ht == NULL || *varname == NUL)
20401 {
20402 EMSG2(_(e_illvar), name);
20403 return;
20404 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020405 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020406
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020407 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20408 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020409
Bram Moolenaar33570922005-01-25 22:26:29 +000020410 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020412 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020413 if (var_check_ro(v->di_flags, name)
20414 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020415 return;
20416 if (v->di_tv.v_type != tv->v_type
20417 && !((v->di_tv.v_type == VAR_STRING
20418 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020419 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020420 || tv->v_type == VAR_NUMBER))
20421#ifdef FEAT_FLOAT
20422 && !((v->di_tv.v_type == VAR_NUMBER
20423 || v->di_tv.v_type == VAR_FLOAT)
20424 && (tv->v_type == VAR_NUMBER
20425 || tv->v_type == VAR_FLOAT))
20426#endif
20427 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020428 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020429 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020430 return;
20431 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020432
20433 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020434 * Handle setting internal v: variables separately: we don't change
20435 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020436 */
20437 if (ht == &vimvarht)
20438 {
20439 if (v->di_tv.v_type == VAR_STRING)
20440 {
20441 vim_free(v->di_tv.vval.v_string);
20442 if (copy || tv->v_type != VAR_STRING)
20443 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20444 else
20445 {
20446 /* Take over the string to avoid an extra alloc/free. */
20447 v->di_tv.vval.v_string = tv->vval.v_string;
20448 tv->vval.v_string = NULL;
20449 }
20450 }
20451 else if (v->di_tv.v_type != VAR_NUMBER)
20452 EMSG2(_(e_intern2), "set_var()");
20453 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020454 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020455 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020456 if (STRCMP(varname, "searchforward") == 0)
20457 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20458 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020459 return;
20460 }
20461
20462 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 }
20464 else /* add a new variable */
20465 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020466 /* Can't add "v:" variable. */
20467 if (ht == &vimvarht)
20468 {
20469 EMSG2(_(e_illvar), name);
20470 return;
20471 }
20472
Bram Moolenaar92124a32005-06-17 22:03:40 +000020473 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020474 if (!valid_varname(varname))
20475 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020476
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020477 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20478 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020479 if (v == NULL)
20480 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020481 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020484 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 return;
20486 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020487 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020489
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020490 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020491 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020492 else
20493 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020494 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020495 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020496 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498}
20499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020500/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020501 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020502 * Also give an error message.
20503 */
20504 static int
20505var_check_ro(flags, name)
20506 int flags;
20507 char_u *name;
20508{
20509 if (flags & DI_FLAGS_RO)
20510 {
20511 EMSG2(_(e_readonlyvar), name);
20512 return TRUE;
20513 }
20514 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20515 {
20516 EMSG2(_(e_readonlysbx), name);
20517 return TRUE;
20518 }
20519 return FALSE;
20520}
20521
20522/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020523 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20524 * Also give an error message.
20525 */
20526 static int
20527var_check_fixed(flags, name)
20528 int flags;
20529 char_u *name;
20530{
20531 if (flags & DI_FLAGS_FIX)
20532 {
20533 EMSG2(_("E795: Cannot delete variable %s"), name);
20534 return TRUE;
20535 }
20536 return FALSE;
20537}
20538
20539/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020540 * Check if a funcref is assigned to a valid variable name.
20541 * Return TRUE and give an error if not.
20542 */
20543 static int
20544var_check_func_name(name, new_var)
20545 char_u *name; /* points to start of variable name */
20546 int new_var; /* TRUE when creating the variable */
20547{
20548 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20549 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20550 ? name[2] : name[0]))
20551 {
20552 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20553 name);
20554 return TRUE;
20555 }
20556 /* Don't allow hiding a function. When "v" is not NULL we might be
20557 * assigning another function to the same var, the type is checked
20558 * below. */
20559 if (new_var && function_exists(name))
20560 {
20561 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20562 name);
20563 return TRUE;
20564 }
20565 return FALSE;
20566}
20567
20568/*
20569 * Check if a variable name is valid.
20570 * Return FALSE and give an error if not.
20571 */
20572 static int
20573valid_varname(varname)
20574 char_u *varname;
20575{
20576 char_u *p;
20577
20578 for (p = varname; *p != NUL; ++p)
20579 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20580 && *p != AUTOLOAD_CHAR)
20581 {
20582 EMSG2(_(e_illvar), varname);
20583 return FALSE;
20584 }
20585 return TRUE;
20586}
20587
20588/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020589 * Return TRUE if typeval "tv" is set to be locked (immutable).
20590 * Also give an error message, using "name".
20591 */
20592 static int
20593tv_check_lock(lock, name)
20594 int lock;
20595 char_u *name;
20596{
20597 if (lock & VAR_LOCKED)
20598 {
20599 EMSG2(_("E741: Value is locked: %s"),
20600 name == NULL ? (char_u *)_("Unknown") : name);
20601 return TRUE;
20602 }
20603 if (lock & VAR_FIXED)
20604 {
20605 EMSG2(_("E742: Cannot change value of %s"),
20606 name == NULL ? (char_u *)_("Unknown") : name);
20607 return TRUE;
20608 }
20609 return FALSE;
20610}
20611
20612/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020613 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020614 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020615 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020616 * It is OK for "from" and "to" to point to the same item. This is used to
20617 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020618 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020619 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020620copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020621 typval_T *from;
20622 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020624 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020625 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020626 switch (from->v_type)
20627 {
20628 case VAR_NUMBER:
20629 to->vval.v_number = from->vval.v_number;
20630 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020631#ifdef FEAT_FLOAT
20632 case VAR_FLOAT:
20633 to->vval.v_float = from->vval.v_float;
20634 break;
20635#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020636 case VAR_STRING:
20637 case VAR_FUNC:
20638 if (from->vval.v_string == NULL)
20639 to->vval.v_string = NULL;
20640 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020642 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020643 if (from->v_type == VAR_FUNC)
20644 func_ref(to->vval.v_string);
20645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020646 break;
20647 case VAR_LIST:
20648 if (from->vval.v_list == NULL)
20649 to->vval.v_list = NULL;
20650 else
20651 {
20652 to->vval.v_list = from->vval.v_list;
20653 ++to->vval.v_list->lv_refcount;
20654 }
20655 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020656 case VAR_DICT:
20657 if (from->vval.v_dict == NULL)
20658 to->vval.v_dict = NULL;
20659 else
20660 {
20661 to->vval.v_dict = from->vval.v_dict;
20662 ++to->vval.v_dict->dv_refcount;
20663 }
20664 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020665 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020666 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020667 break;
20668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669}
20670
20671/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020672 * Make a copy of an item.
20673 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020674 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20675 * reference to an already copied list/dict can be used.
20676 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020677 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020678 static int
20679item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020680 typval_T *from;
20681 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020682 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020683 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020684{
20685 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020686 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020687
Bram Moolenaar33570922005-01-25 22:26:29 +000020688 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020689 {
20690 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020691 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020692 }
20693 ++recurse;
20694
20695 switch (from->v_type)
20696 {
20697 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020698#ifdef FEAT_FLOAT
20699 case VAR_FLOAT:
20700#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020701 case VAR_STRING:
20702 case VAR_FUNC:
20703 copy_tv(from, to);
20704 break;
20705 case VAR_LIST:
20706 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020707 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020708 if (from->vval.v_list == NULL)
20709 to->vval.v_list = NULL;
20710 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20711 {
20712 /* use the copy made earlier */
20713 to->vval.v_list = from->vval.v_list->lv_copylist;
20714 ++to->vval.v_list->lv_refcount;
20715 }
20716 else
20717 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20718 if (to->vval.v_list == NULL)
20719 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020720 break;
20721 case VAR_DICT:
20722 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020723 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020724 if (from->vval.v_dict == NULL)
20725 to->vval.v_dict = NULL;
20726 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20727 {
20728 /* use the copy made earlier */
20729 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20730 ++to->vval.v_dict->dv_refcount;
20731 }
20732 else
20733 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20734 if (to->vval.v_dict == NULL)
20735 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020736 break;
20737 default:
20738 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020739 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020740 }
20741 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020742 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020743}
20744
20745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 * ":echo expr1 ..." print each argument separated with a space, add a
20747 * newline at the end.
20748 * ":echon expr1 ..." print each argument plain.
20749 */
20750 void
20751ex_echo(eap)
20752 exarg_T *eap;
20753{
20754 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020755 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020756 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 char_u *p;
20758 int needclr = TRUE;
20759 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020760 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761
20762 if (eap->skip)
20763 ++emsg_skip;
20764 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20765 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020766 /* If eval1() causes an error message the text from the command may
20767 * still need to be cleared. E.g., "echo 22,44". */
20768 need_clr_eos = needclr;
20769
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020771 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 {
20773 /*
20774 * Report the invalid expression unless the expression evaluation
20775 * has been cancelled due to an aborting error, an interrupt, or an
20776 * exception.
20777 */
20778 if (!aborting())
20779 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020780 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 break;
20782 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020783 need_clr_eos = FALSE;
20784
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 if (!eap->skip)
20786 {
20787 if (atstart)
20788 {
20789 atstart = FALSE;
20790 /* Call msg_start() after eval1(), evaluating the expression
20791 * may cause a message to appear. */
20792 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020793 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020794 /* Mark the saved text as finishing the line, so that what
20795 * follows is displayed on a new line when scrolling back
20796 * at the more prompt. */
20797 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 }
20801 else if (eap->cmdidx == CMD_echo)
20802 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020803 current_copyID += COPYID_INC;
20804 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020805 if (p != NULL)
20806 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020808 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020810 if (*p != TAB && needclr)
20811 {
20812 /* remove any text still there from the command */
20813 msg_clr_eos();
20814 needclr = FALSE;
20815 }
20816 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 }
20818 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020819 {
20820#ifdef FEAT_MBYTE
20821 if (has_mbyte)
20822 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020823 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020824
20825 (void)msg_outtrans_len_attr(p, i, echo_attr);
20826 p += i - 1;
20827 }
20828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020830 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020833 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020835 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 arg = skipwhite(arg);
20837 }
20838 eap->nextcmd = check_nextcmd(arg);
20839
20840 if (eap->skip)
20841 --emsg_skip;
20842 else
20843 {
20844 /* remove text that may still be there from the command */
20845 if (needclr)
20846 msg_clr_eos();
20847 if (eap->cmdidx == CMD_echo)
20848 msg_end();
20849 }
20850}
20851
20852/*
20853 * ":echohl {name}".
20854 */
20855 void
20856ex_echohl(eap)
20857 exarg_T *eap;
20858{
20859 int id;
20860
20861 id = syn_name2id(eap->arg);
20862 if (id == 0)
20863 echo_attr = 0;
20864 else
20865 echo_attr = syn_id2attr(id);
20866}
20867
20868/*
20869 * ":execute expr1 ..." execute the result of an expression.
20870 * ":echomsg expr1 ..." Print a message
20871 * ":echoerr expr1 ..." Print an error
20872 * Each gets spaces around each argument and a newline at the end for
20873 * echo commands
20874 */
20875 void
20876ex_execute(eap)
20877 exarg_T *eap;
20878{
20879 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020880 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 int ret = OK;
20882 char_u *p;
20883 garray_T ga;
20884 int len;
20885 int save_did_emsg;
20886
20887 ga_init2(&ga, 1, 80);
20888
20889 if (eap->skip)
20890 ++emsg_skip;
20891 while (*arg != NUL && *arg != '|' && *arg != '\n')
20892 {
20893 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 {
20896 /*
20897 * Report the invalid expression unless the expression evaluation
20898 * has been cancelled due to an aborting error, an interrupt, or an
20899 * exception.
20900 */
20901 if (!aborting())
20902 EMSG2(_(e_invexpr2), p);
20903 ret = FAIL;
20904 break;
20905 }
20906
20907 if (!eap->skip)
20908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020909 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 len = (int)STRLEN(p);
20911 if (ga_grow(&ga, len + 2) == FAIL)
20912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 ret = FAIL;
20915 break;
20916 }
20917 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 ga.ga_len += len;
20921 }
20922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 arg = skipwhite(arg);
20925 }
20926
20927 if (ret != FAIL && ga.ga_data != NULL)
20928 {
20929 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020932 out_flush();
20933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 else if (eap->cmdidx == CMD_echoerr)
20935 {
20936 /* We don't want to abort following commands, restore did_emsg. */
20937 save_did_emsg = did_emsg;
20938 EMSG((char_u *)ga.ga_data);
20939 if (!force_abort)
20940 did_emsg = save_did_emsg;
20941 }
20942 else if (eap->cmdidx == CMD_execute)
20943 do_cmdline((char_u *)ga.ga_data,
20944 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20945 }
20946
20947 ga_clear(&ga);
20948
20949 if (eap->skip)
20950 --emsg_skip;
20951
20952 eap->nextcmd = check_nextcmd(arg);
20953}
20954
20955/*
20956 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20957 * "arg" points to the "&" or '+' when called, to "option" when returning.
20958 * Returns NULL when no option name found. Otherwise pointer to the char
20959 * after the option name.
20960 */
20961 static char_u *
20962find_option_end(arg, opt_flags)
20963 char_u **arg;
20964 int *opt_flags;
20965{
20966 char_u *p = *arg;
20967
20968 ++p;
20969 if (*p == 'g' && p[1] == ':')
20970 {
20971 *opt_flags = OPT_GLOBAL;
20972 p += 2;
20973 }
20974 else if (*p == 'l' && p[1] == ':')
20975 {
20976 *opt_flags = OPT_LOCAL;
20977 p += 2;
20978 }
20979 else
20980 *opt_flags = 0;
20981
20982 if (!ASCII_ISALPHA(*p))
20983 return NULL;
20984 *arg = p;
20985
20986 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20987 p += 4; /* termcap option */
20988 else
20989 while (ASCII_ISALPHA(*p))
20990 ++p;
20991 return p;
20992}
20993
20994/*
20995 * ":function"
20996 */
20997 void
20998ex_function(eap)
20999 exarg_T *eap;
21000{
21001 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021002 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 int j;
21004 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 char_u *name = NULL;
21007 char_u *p;
21008 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021009 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 garray_T newargs;
21011 garray_T newlines;
21012 int varargs = FALSE;
21013 int mustend = FALSE;
21014 int flags = 0;
21015 ufunc_T *fp;
21016 int indent;
21017 int nesting;
21018 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021019 dictitem_T *v;
21020 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021021 static int func_nr = 0; /* number for nameless function */
21022 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021023 hashtab_T *ht;
21024 int todo;
21025 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021026 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027
21028 /*
21029 * ":function" without argument: list functions.
21030 */
21031 if (ends_excmd(*eap->arg))
21032 {
21033 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021034 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021035 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021036 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021037 {
21038 if (!HASHITEM_EMPTY(hi))
21039 {
21040 --todo;
21041 fp = HI2UF(hi);
21042 if (!isdigit(*fp->uf_name))
21043 list_func_head(fp, FALSE);
21044 }
21045 }
21046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047 eap->nextcmd = check_nextcmd(eap->arg);
21048 return;
21049 }
21050
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021051 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021052 * ":function /pat": list functions matching pattern.
21053 */
21054 if (*eap->arg == '/')
21055 {
21056 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21057 if (!eap->skip)
21058 {
21059 regmatch_T regmatch;
21060
21061 c = *p;
21062 *p = NUL;
21063 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21064 *p = c;
21065 if (regmatch.regprog != NULL)
21066 {
21067 regmatch.rm_ic = p_ic;
21068
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021069 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021070 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21071 {
21072 if (!HASHITEM_EMPTY(hi))
21073 {
21074 --todo;
21075 fp = HI2UF(hi);
21076 if (!isdigit(*fp->uf_name)
21077 && vim_regexec(&regmatch, fp->uf_name, 0))
21078 list_func_head(fp, FALSE);
21079 }
21080 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021081 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021082 }
21083 }
21084 if (*p == '/')
21085 ++p;
21086 eap->nextcmd = check_nextcmd(p);
21087 return;
21088 }
21089
21090 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021091 * Get the function name. There are these situations:
21092 * func normal function name
21093 * "name" == func, "fudi.fd_dict" == NULL
21094 * dict.func new dictionary entry
21095 * "name" == NULL, "fudi.fd_dict" set,
21096 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21097 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021098 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021099 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21100 * dict.func existing dict entry that's not a Funcref
21101 * "name" == NULL, "fudi.fd_dict" set,
21102 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021105 name = trans_function_name(&p, eap->skip, 0, &fudi);
21106 paren = (vim_strchr(p, '(') != NULL);
21107 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 {
21109 /*
21110 * Return on an invalid expression in braces, unless the expression
21111 * evaluation has been cancelled due to an aborting error, an
21112 * interrupt, or an exception.
21113 */
21114 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021116 if (!eap->skip && fudi.fd_newkey != NULL)
21117 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021118 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 else
21122 eap->skip = TRUE;
21123 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021124
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 /* An error in a function call during evaluation of an expression in magic
21126 * braces should not cause the function not to be defined. */
21127 saved_did_emsg = did_emsg;
21128 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129
21130 /*
21131 * ":function func" with only function name: list function.
21132 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021133 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 {
21135 if (!ends_excmd(*skipwhite(p)))
21136 {
21137 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021138 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 }
21140 eap->nextcmd = check_nextcmd(p);
21141 if (eap->nextcmd != NULL)
21142 *p = NUL;
21143 if (!eap->skip && !got_int)
21144 {
21145 fp = find_func(name);
21146 if (fp != NULL)
21147 {
21148 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021149 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021151 if (FUNCLINE(fp, j) == NULL)
21152 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 msg_putchar('\n');
21154 msg_outnum((long)(j + 1));
21155 if (j < 9)
21156 msg_putchar(' ');
21157 if (j < 99)
21158 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021159 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 out_flush(); /* show a line at a time */
21161 ui_breakcheck();
21162 }
21163 if (!got_int)
21164 {
21165 msg_putchar('\n');
21166 msg_puts((char_u *)" endfunction");
21167 }
21168 }
21169 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021170 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021172 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 }
21174
21175 /*
21176 * ":function name(arg1, arg2)" Define function.
21177 */
21178 p = skipwhite(p);
21179 if (*p != '(')
21180 {
21181 if (!eap->skip)
21182 {
21183 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021184 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 }
21186 /* attempt to continue by skipping some text */
21187 if (vim_strchr(p, '(') != NULL)
21188 p = vim_strchr(p, '(');
21189 }
21190 p = skipwhite(p + 1);
21191
21192 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21193 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21194
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021195 if (!eap->skip)
21196 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021197 /* Check the name of the function. Unless it's a dictionary function
21198 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021199 if (name != NULL)
21200 arg = name;
21201 else
21202 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021203 if (arg != NULL && (fudi.fd_di == NULL
21204 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021205 {
21206 if (*arg == K_SPECIAL)
21207 j = 3;
21208 else
21209 j = 0;
21210 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21211 : eval_isnamec(arg[j])))
21212 ++j;
21213 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021214 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021215 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021216 /* Disallow using the g: dict. */
21217 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21218 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021219 }
21220
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 /*
21222 * Isolate the arguments: "arg1, arg2, ...)"
21223 */
21224 while (*p != ')')
21225 {
21226 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21227 {
21228 varargs = TRUE;
21229 p += 3;
21230 mustend = TRUE;
21231 }
21232 else
21233 {
21234 arg = p;
21235 while (ASCII_ISALNUM(*p) || *p == '_')
21236 ++p;
21237 if (arg == p || isdigit(*arg)
21238 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21239 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21240 {
21241 if (!eap->skip)
21242 EMSG2(_("E125: Illegal argument: %s"), arg);
21243 break;
21244 }
21245 if (ga_grow(&newargs, 1) == FAIL)
21246 goto erret;
21247 c = *p;
21248 *p = NUL;
21249 arg = vim_strsave(arg);
21250 if (arg == NULL)
21251 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021252
21253 /* Check for duplicate argument name. */
21254 for (i = 0; i < newargs.ga_len; ++i)
21255 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21256 {
21257 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21258 goto erret;
21259 }
21260
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21262 *p = c;
21263 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 if (*p == ',')
21265 ++p;
21266 else
21267 mustend = TRUE;
21268 }
21269 p = skipwhite(p);
21270 if (mustend && *p != ')')
21271 {
21272 if (!eap->skip)
21273 EMSG2(_(e_invarg2), eap->arg);
21274 break;
21275 }
21276 }
21277 ++p; /* skip the ')' */
21278
Bram Moolenaare9a41262005-01-15 22:18:47 +000021279 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 for (;;)
21281 {
21282 p = skipwhite(p);
21283 if (STRNCMP(p, "range", 5) == 0)
21284 {
21285 flags |= FC_RANGE;
21286 p += 5;
21287 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021288 else if (STRNCMP(p, "dict", 4) == 0)
21289 {
21290 flags |= FC_DICT;
21291 p += 4;
21292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 else if (STRNCMP(p, "abort", 5) == 0)
21294 {
21295 flags |= FC_ABORT;
21296 p += 5;
21297 }
21298 else
21299 break;
21300 }
21301
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021302 /* When there is a line break use what follows for the function body.
21303 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21304 if (*p == '\n')
21305 line_arg = p + 1;
21306 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 EMSG(_(e_trailing));
21308
21309 /*
21310 * Read the body of the function, until ":endfunction" is found.
21311 */
21312 if (KeyTyped)
21313 {
21314 /* Check if the function already exists, don't let the user type the
21315 * whole function before telling him it doesn't work! For a script we
21316 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021317 if (!eap->skip && !eap->forceit)
21318 {
21319 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21320 EMSG(_(e_funcdict));
21321 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021322 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021325 if (!eap->skip && did_emsg)
21326 goto erret;
21327
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 msg_putchar('\n'); /* don't overwrite the function name */
21329 cmdline_row = msg_row;
21330 }
21331
21332 indent = 2;
21333 nesting = 0;
21334 for (;;)
21335 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021336 if (KeyTyped)
21337 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021339 sourcing_lnum_off = sourcing_lnum;
21340
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021341 if (line_arg != NULL)
21342 {
21343 /* Use eap->arg, split up in parts by line breaks. */
21344 theline = line_arg;
21345 p = vim_strchr(theline, '\n');
21346 if (p == NULL)
21347 line_arg += STRLEN(line_arg);
21348 else
21349 {
21350 *p = NUL;
21351 line_arg = p + 1;
21352 }
21353 }
21354 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 theline = getcmdline(':', 0L, indent);
21356 else
21357 theline = eap->getline(':', eap->cookie, indent);
21358 if (KeyTyped)
21359 lines_left = Rows - 1;
21360 if (theline == NULL)
21361 {
21362 EMSG(_("E126: Missing :endfunction"));
21363 goto erret;
21364 }
21365
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021366 /* Detect line continuation: sourcing_lnum increased more than one. */
21367 if (sourcing_lnum > sourcing_lnum_off + 1)
21368 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21369 else
21370 sourcing_lnum_off = 0;
21371
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 if (skip_until != NULL)
21373 {
21374 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21375 * don't check for ":endfunc". */
21376 if (STRCMP(theline, skip_until) == 0)
21377 {
21378 vim_free(skip_until);
21379 skip_until = NULL;
21380 }
21381 }
21382 else
21383 {
21384 /* skip ':' and blanks*/
21385 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21386 ;
21387
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021388 /* Check for "endfunction". */
21389 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021391 if (line_arg == NULL)
21392 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 break;
21394 }
21395
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021396 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 * at "end". */
21398 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21399 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021400 else if (STRNCMP(p, "if", 2) == 0
21401 || STRNCMP(p, "wh", 2) == 0
21402 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 || STRNCMP(p, "try", 3) == 0)
21404 indent += 2;
21405
21406 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021407 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021409 if (*p == '!')
21410 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 p += eval_fname_script(p);
21412 if (ASCII_ISALPHA(*p))
21413 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021414 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 if (*skipwhite(p) == '(')
21416 {
21417 ++nesting;
21418 indent += 2;
21419 }
21420 }
21421 }
21422
21423 /* Check for ":append" or ":insert". */
21424 p = skip_range(p, NULL);
21425 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21426 || (p[0] == 'i'
21427 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21428 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21429 skip_until = vim_strsave((char_u *)".");
21430
21431 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21432 arg = skipwhite(skiptowhite(p));
21433 if (arg[0] == '<' && arg[1] =='<'
21434 && ((p[0] == 'p' && p[1] == 'y'
21435 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21436 || (p[0] == 'p' && p[1] == 'e'
21437 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21438 || (p[0] == 't' && p[1] == 'c'
21439 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021440 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21441 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21443 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021444 || (p[0] == 'm' && p[1] == 'z'
21445 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 ))
21447 {
21448 /* ":python <<" continues until a dot, like ":append" */
21449 p = skipwhite(arg + 2);
21450 if (*p == NUL)
21451 skip_until = vim_strsave((char_u *)".");
21452 else
21453 skip_until = vim_strsave(p);
21454 }
21455 }
21456
21457 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021458 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021459 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021460 if (line_arg == NULL)
21461 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021463 }
21464
21465 /* Copy the line to newly allocated memory. get_one_sourceline()
21466 * allocates 250 bytes per line, this saves 80% on average. The cost
21467 * is an extra alloc/free. */
21468 p = vim_strsave(theline);
21469 if (p != NULL)
21470 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021471 if (line_arg == NULL)
21472 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021473 theline = p;
21474 }
21475
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021476 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21477
21478 /* Add NULL lines for continuation lines, so that the line count is
21479 * equal to the index in the growarray. */
21480 while (sourcing_lnum_off-- > 0)
21481 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021482
21483 /* Check for end of eap->arg. */
21484 if (line_arg != NULL && *line_arg == NUL)
21485 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 }
21487
21488 /* Don't define the function when skipping commands or when an error was
21489 * detected. */
21490 if (eap->skip || did_emsg)
21491 goto erret;
21492
21493 /*
21494 * If there are no errors, add the function
21495 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021496 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021497 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021498 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021499 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021500 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021501 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021502 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021503 goto erret;
21504 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021505
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021506 fp = find_func(name);
21507 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021509 if (!eap->forceit)
21510 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021511 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021512 goto erret;
21513 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021514 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021515 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021516 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021517 name);
21518 goto erret;
21519 }
21520 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021521 ga_clear_strings(&(fp->uf_args));
21522 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021523 vim_free(name);
21524 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 }
21527 else
21528 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021529 char numbuf[20];
21530
21531 fp = NULL;
21532 if (fudi.fd_newkey == NULL && !eap->forceit)
21533 {
21534 EMSG(_(e_funcdict));
21535 goto erret;
21536 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021537 if (fudi.fd_di == NULL)
21538 {
21539 /* Can't add a function to a locked dictionary */
21540 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21541 goto erret;
21542 }
21543 /* Can't change an existing function if it is locked */
21544 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21545 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021546
21547 /* Give the function a sequential number. Can only be used with a
21548 * Funcref! */
21549 vim_free(name);
21550 sprintf(numbuf, "%d", ++func_nr);
21551 name = vim_strsave((char_u *)numbuf);
21552 if (name == NULL)
21553 goto erret;
21554 }
21555
21556 if (fp == NULL)
21557 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021558 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021559 {
21560 int slen, plen;
21561 char_u *scriptname;
21562
21563 /* Check that the autoload name matches the script name. */
21564 j = FAIL;
21565 if (sourcing_name != NULL)
21566 {
21567 scriptname = autoload_name(name);
21568 if (scriptname != NULL)
21569 {
21570 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021571 plen = (int)STRLEN(p);
21572 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021573 if (slen > plen && fnamecmp(p,
21574 sourcing_name + slen - plen) == 0)
21575 j = OK;
21576 vim_free(scriptname);
21577 }
21578 }
21579 if (j == FAIL)
21580 {
21581 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21582 goto erret;
21583 }
21584 }
21585
21586 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 if (fp == NULL)
21588 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021589
21590 if (fudi.fd_dict != NULL)
21591 {
21592 if (fudi.fd_di == NULL)
21593 {
21594 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021595 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021596 if (fudi.fd_di == NULL)
21597 {
21598 vim_free(fp);
21599 goto erret;
21600 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021601 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21602 {
21603 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021604 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021605 goto erret;
21606 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021607 }
21608 else
21609 /* overwrite existing dict entry */
21610 clear_tv(&fudi.fd_di->di_tv);
21611 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021612 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021613 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021614 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021615
21616 /* behave like "dict" was used */
21617 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021618 }
21619
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021621 STRCPY(fp->uf_name, name);
21622 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021624 fp->uf_args = newargs;
21625 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021626#ifdef FEAT_PROFILE
21627 fp->uf_tml_count = NULL;
21628 fp->uf_tml_total = NULL;
21629 fp->uf_tml_self = NULL;
21630 fp->uf_profiling = FALSE;
21631 if (prof_def_func())
21632 func_do_profile(fp);
21633#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021634 fp->uf_varargs = varargs;
21635 fp->uf_flags = flags;
21636 fp->uf_calls = 0;
21637 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021638 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639
21640erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 ga_clear_strings(&newargs);
21642 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021643ret_free:
21644 vim_free(skip_until);
21645 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648}
21649
21650/*
21651 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021652 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021654 * flags:
21655 * TFN_INT: internal function name OK
21656 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 * Advances "pp" to just after the function name (if no error).
21658 */
21659 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021660trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 char_u **pp;
21662 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021663 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021666 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 char_u *start;
21668 char_u *end;
21669 int lead;
21670 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021672 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021673
21674 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021677
21678 /* Check for hard coded <SNR>: already translated function ID (from a user
21679 * command). */
21680 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21681 && (*pp)[2] == (int)KE_SNR)
21682 {
21683 *pp += 3;
21684 len = get_id_len(pp) + 3;
21685 return vim_strnsave(start, len);
21686 }
21687
21688 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21689 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021691 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021692 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021693
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021694 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21695 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021696 if (end == start)
21697 {
21698 if (!skip)
21699 EMSG(_("E129: Function name required"));
21700 goto theend;
21701 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021702 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021703 {
21704 /*
21705 * Report an invalid expression in braces, unless the expression
21706 * evaluation has been cancelled due to an aborting error, an
21707 * interrupt, or an exception.
21708 */
21709 if (!aborting())
21710 {
21711 if (end != NULL)
21712 EMSG2(_(e_invarg2), start);
21713 }
21714 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021715 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021716 goto theend;
21717 }
21718
21719 if (lv.ll_tv != NULL)
21720 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021721 if (fdp != NULL)
21722 {
21723 fdp->fd_dict = lv.ll_dict;
21724 fdp->fd_newkey = lv.ll_newkey;
21725 lv.ll_newkey = NULL;
21726 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021727 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021728 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21729 {
21730 name = vim_strsave(lv.ll_tv->vval.v_string);
21731 *pp = end;
21732 }
21733 else
21734 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021735 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21736 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021737 EMSG(_(e_funcref));
21738 else
21739 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021740 name = NULL;
21741 }
21742 goto theend;
21743 }
21744
21745 if (lv.ll_name == NULL)
21746 {
21747 /* Error found, but continue after the function name. */
21748 *pp = end;
21749 goto theend;
21750 }
21751
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021752 /* Check if the name is a Funcref. If so, use the value. */
21753 if (lv.ll_exp_name != NULL)
21754 {
21755 len = (int)STRLEN(lv.ll_exp_name);
21756 name = deref_func_name(lv.ll_exp_name, &len);
21757 if (name == lv.ll_exp_name)
21758 name = NULL;
21759 }
21760 else
21761 {
21762 len = (int)(end - *pp);
21763 name = deref_func_name(*pp, &len);
21764 if (name == *pp)
21765 name = NULL;
21766 }
21767 if (name != NULL)
21768 {
21769 name = vim_strsave(name);
21770 *pp = end;
21771 goto theend;
21772 }
21773
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021774 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021775 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021776 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021777 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21778 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21779 {
21780 /* When there was "s:" already or the name expanded to get a
21781 * leading "s:" then remove it. */
21782 lv.ll_name += 2;
21783 len -= 2;
21784 lead = 2;
21785 }
21786 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021787 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021788 {
21789 if (lead == 2) /* skip over "s:" */
21790 lv.ll_name += 2;
21791 len = (int)(end - lv.ll_name);
21792 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021793
21794 /*
21795 * Copy the function name to allocated memory.
21796 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21797 * Accept <SNR>123_name() outside a script.
21798 */
21799 if (skip)
21800 lead = 0; /* do nothing */
21801 else if (lead > 0)
21802 {
21803 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021804 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21805 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021806 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021807 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021808 if (current_SID <= 0)
21809 {
21810 EMSG(_(e_usingsid));
21811 goto theend;
21812 }
21813 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21814 lead += (int)STRLEN(sid_buf);
21815 }
21816 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021817 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021818 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021819 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021820 goto theend;
21821 }
21822 name = alloc((unsigned)(len + lead + 1));
21823 if (name != NULL)
21824 {
21825 if (lead > 0)
21826 {
21827 name[0] = K_SPECIAL;
21828 name[1] = KS_EXTRA;
21829 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021830 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021831 STRCPY(name + 3, sid_buf);
21832 }
21833 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21834 name[len + lead] = NUL;
21835 }
21836 *pp = end;
21837
21838theend:
21839 clear_lval(&lv);
21840 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841}
21842
21843/*
21844 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21845 * Return 2 if "p" starts with "s:".
21846 * Return 0 otherwise.
21847 */
21848 static int
21849eval_fname_script(p)
21850 char_u *p;
21851{
21852 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21853 || STRNICMP(p + 1, "SNR>", 4) == 0))
21854 return 5;
21855 if (p[0] == 's' && p[1] == ':')
21856 return 2;
21857 return 0;
21858}
21859
21860/*
21861 * Return TRUE if "p" starts with "<SID>" or "s:".
21862 * Only works if eval_fname_script() returned non-zero for "p"!
21863 */
21864 static int
21865eval_fname_sid(p)
21866 char_u *p;
21867{
21868 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21869}
21870
21871/*
21872 * List the head of the function: "name(arg1, arg2)".
21873 */
21874 static void
21875list_func_head(fp, indent)
21876 ufunc_T *fp;
21877 int indent;
21878{
21879 int j;
21880
21881 msg_start();
21882 if (indent)
21883 MSG_PUTS(" ");
21884 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021885 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 {
21887 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021888 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 }
21890 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021891 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021893 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 {
21895 if (j)
21896 MSG_PUTS(", ");
21897 msg_puts(FUNCARG(fp, j));
21898 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021899 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 {
21901 if (j)
21902 MSG_PUTS(", ");
21903 MSG_PUTS("...");
21904 }
21905 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020021906 if (fp->uf_flags & FC_ABORT)
21907 MSG_PUTS(" abort");
21908 if (fp->uf_flags & FC_RANGE)
21909 MSG_PUTS(" range");
21910 if (fp->uf_flags & FC_DICT)
21911 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021912 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021913 if (p_verbose > 0)
21914 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915}
21916
21917/*
21918 * Find a function by name, return pointer to it in ufuncs.
21919 * Return NULL for unknown function.
21920 */
21921 static ufunc_T *
21922find_func(name)
21923 char_u *name;
21924{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021925 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021927 hi = hash_find(&func_hashtab, name);
21928 if (!HASHITEM_EMPTY(hi))
21929 return HI2UF(hi);
21930 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931}
21932
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021933#if defined(EXITFREE) || defined(PROTO)
21934 void
21935free_all_functions()
21936{
21937 hashitem_T *hi;
21938
21939 /* Need to start all over every time, because func_free() may change the
21940 * hash table. */
21941 while (func_hashtab.ht_used > 0)
21942 for (hi = func_hashtab.ht_array; ; ++hi)
21943 if (!HASHITEM_EMPTY(hi))
21944 {
21945 func_free(HI2UF(hi));
21946 break;
21947 }
21948}
21949#endif
21950
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021951 int
21952translated_function_exists(name)
21953 char_u *name;
21954{
21955 if (builtin_function(name))
21956 return find_internal_func(name) >= 0;
21957 return find_func(name) != NULL;
21958}
21959
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021960/*
21961 * Return TRUE if a function "name" exists.
21962 */
21963 static int
21964function_exists(name)
21965 char_u *name;
21966{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021967 char_u *nm = name;
21968 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021969 int n = FALSE;
21970
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021971 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021972 nm = skipwhite(nm);
21973
21974 /* Only accept "funcname", "funcname ", "funcname (..." and
21975 * "funcname(...", not "funcname!...". */
21976 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021977 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000021978 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021979 return n;
21980}
21981
Bram Moolenaara1544c02013-05-30 12:35:52 +020021982 char_u *
21983get_expanded_name(name, check)
21984 char_u *name;
21985 int check;
21986{
21987 char_u *nm = name;
21988 char_u *p;
21989
21990 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
21991
21992 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021993 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020021994 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021995
Bram Moolenaara1544c02013-05-30 12:35:52 +020021996 vim_free(p);
21997 return NULL;
21998}
21999
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022000/*
22001 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022002 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022003 */
22004 static int
22005builtin_function(name)
22006 char_u *name;
22007{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022008 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22009 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022010}
22011
Bram Moolenaar05159a02005-02-26 23:04:13 +000022012#if defined(FEAT_PROFILE) || defined(PROTO)
22013/*
22014 * Start profiling function "fp".
22015 */
22016 static void
22017func_do_profile(fp)
22018 ufunc_T *fp;
22019{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022020 int len = fp->uf_lines.ga_len;
22021
22022 if (len == 0)
22023 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022024 fp->uf_tm_count = 0;
22025 profile_zero(&fp->uf_tm_self);
22026 profile_zero(&fp->uf_tm_total);
22027 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022028 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022029 if (fp->uf_tml_total == NULL)
22030 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022031 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022032 if (fp->uf_tml_self == NULL)
22033 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022034 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022035 fp->uf_tml_idx = -1;
22036 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22037 || fp->uf_tml_self == NULL)
22038 return; /* out of memory */
22039
22040 fp->uf_profiling = TRUE;
22041}
22042
22043/*
22044 * Dump the profiling results for all functions in file "fd".
22045 */
22046 void
22047func_dump_profile(fd)
22048 FILE *fd;
22049{
22050 hashitem_T *hi;
22051 int todo;
22052 ufunc_T *fp;
22053 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022054 ufunc_T **sorttab;
22055 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022057 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022058 if (todo == 0)
22059 return; /* nothing to dump */
22060
Bram Moolenaar73830342005-02-28 22:48:19 +000022061 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22062
Bram Moolenaar05159a02005-02-26 23:04:13 +000022063 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22064 {
22065 if (!HASHITEM_EMPTY(hi))
22066 {
22067 --todo;
22068 fp = HI2UF(hi);
22069 if (fp->uf_profiling)
22070 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022071 if (sorttab != NULL)
22072 sorttab[st_len++] = fp;
22073
Bram Moolenaar05159a02005-02-26 23:04:13 +000022074 if (fp->uf_name[0] == K_SPECIAL)
22075 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22076 else
22077 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22078 if (fp->uf_tm_count == 1)
22079 fprintf(fd, "Called 1 time\n");
22080 else
22081 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22082 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22083 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22084 fprintf(fd, "\n");
22085 fprintf(fd, "count total (s) self (s)\n");
22086
22087 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22088 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022089 if (FUNCLINE(fp, i) == NULL)
22090 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022091 prof_func_line(fd, fp->uf_tml_count[i],
22092 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022093 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22094 }
22095 fprintf(fd, "\n");
22096 }
22097 }
22098 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022099
22100 if (sorttab != NULL && st_len > 0)
22101 {
22102 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22103 prof_total_cmp);
22104 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22105 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22106 prof_self_cmp);
22107 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22108 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022109
22110 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022111}
Bram Moolenaar73830342005-02-28 22:48:19 +000022112
22113 static void
22114prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22115 FILE *fd;
22116 ufunc_T **sorttab;
22117 int st_len;
22118 char *title;
22119 int prefer_self; /* when equal print only self time */
22120{
22121 int i;
22122 ufunc_T *fp;
22123
22124 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22125 fprintf(fd, "count total (s) self (s) function\n");
22126 for (i = 0; i < 20 && i < st_len; ++i)
22127 {
22128 fp = sorttab[i];
22129 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22130 prefer_self);
22131 if (fp->uf_name[0] == K_SPECIAL)
22132 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22133 else
22134 fprintf(fd, " %s()\n", fp->uf_name);
22135 }
22136 fprintf(fd, "\n");
22137}
22138
22139/*
22140 * Print the count and times for one function or function line.
22141 */
22142 static void
22143prof_func_line(fd, count, total, self, prefer_self)
22144 FILE *fd;
22145 int count;
22146 proftime_T *total;
22147 proftime_T *self;
22148 int prefer_self; /* when equal print only self time */
22149{
22150 if (count > 0)
22151 {
22152 fprintf(fd, "%5d ", count);
22153 if (prefer_self && profile_equal(total, self))
22154 fprintf(fd, " ");
22155 else
22156 fprintf(fd, "%s ", profile_msg(total));
22157 if (!prefer_self && profile_equal(total, self))
22158 fprintf(fd, " ");
22159 else
22160 fprintf(fd, "%s ", profile_msg(self));
22161 }
22162 else
22163 fprintf(fd, " ");
22164}
22165
22166/*
22167 * Compare function for total time sorting.
22168 */
22169 static int
22170#ifdef __BORLANDC__
22171_RTLENTRYF
22172#endif
22173prof_total_cmp(s1, s2)
22174 const void *s1;
22175 const void *s2;
22176{
22177 ufunc_T *p1, *p2;
22178
22179 p1 = *(ufunc_T **)s1;
22180 p2 = *(ufunc_T **)s2;
22181 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22182}
22183
22184/*
22185 * Compare function for self time sorting.
22186 */
22187 static int
22188#ifdef __BORLANDC__
22189_RTLENTRYF
22190#endif
22191prof_self_cmp(s1, s2)
22192 const void *s1;
22193 const void *s2;
22194{
22195 ufunc_T *p1, *p2;
22196
22197 p1 = *(ufunc_T **)s1;
22198 p2 = *(ufunc_T **)s2;
22199 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22200}
22201
Bram Moolenaar05159a02005-02-26 23:04:13 +000022202#endif
22203
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022204/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022205 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022206 * Return TRUE if a package was loaded.
22207 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022208 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022209script_autoload(name, reload)
22210 char_u *name;
22211 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022212{
22213 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022214 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022215 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022216 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022217
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022218 /* Return quickly when autoload disabled. */
22219 if (no_autoload)
22220 return FALSE;
22221
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022222 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022223 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022224 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022225 return FALSE;
22226
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022227 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022228
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022229 /* Find the name in the list of previously loaded package names. Skip
22230 * "autoload/", it's always the same. */
22231 for (i = 0; i < ga_loaded.ga_len; ++i)
22232 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22233 break;
22234 if (!reload && i < ga_loaded.ga_len)
22235 ret = FALSE; /* was loaded already */
22236 else
22237 {
22238 /* Remember the name if it wasn't loaded already. */
22239 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22240 {
22241 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22242 tofree = NULL;
22243 }
22244
22245 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022246 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022247 ret = TRUE;
22248 }
22249
22250 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022251 return ret;
22252}
22253
22254/*
22255 * Return the autoload script name for a function or variable name.
22256 * Returns NULL when out of memory.
22257 */
22258 static char_u *
22259autoload_name(name)
22260 char_u *name;
22261{
22262 char_u *p;
22263 char_u *scriptname;
22264
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022265 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022266 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22267 if (scriptname == NULL)
22268 return FALSE;
22269 STRCPY(scriptname, "autoload/");
22270 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022271 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022272 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022273 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022274 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022275 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022276}
22277
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22279
22280/*
22281 * Function given to ExpandGeneric() to obtain the list of user defined
22282 * function names.
22283 */
22284 char_u *
22285get_user_func_name(xp, idx)
22286 expand_T *xp;
22287 int idx;
22288{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022289 static long_u done;
22290 static hashitem_T *hi;
22291 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292
22293 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022295 done = 0;
22296 hi = func_hashtab.ht_array;
22297 }
22298 if (done < func_hashtab.ht_used)
22299 {
22300 if (done++ > 0)
22301 ++hi;
22302 while (HASHITEM_EMPTY(hi))
22303 ++hi;
22304 fp = HI2UF(hi);
22305
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022306 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022307 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022308
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022309 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22310 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311
22312 cat_func_name(IObuff, fp);
22313 if (xp->xp_context != EXPAND_USER_FUNC)
22314 {
22315 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022316 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 STRCAT(IObuff, ")");
22318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 return IObuff;
22320 }
22321 return NULL;
22322}
22323
22324#endif /* FEAT_CMDL_COMPL */
22325
22326/*
22327 * Copy the function name of "fp" to buffer "buf".
22328 * "buf" must be able to hold the function name plus three bytes.
22329 * Takes care of script-local function names.
22330 */
22331 static void
22332cat_func_name(buf, fp)
22333 char_u *buf;
22334 ufunc_T *fp;
22335{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022336 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 {
22338 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022339 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 }
22341 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022342 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343}
22344
22345/*
22346 * ":delfunction {name}"
22347 */
22348 void
22349ex_delfunction(eap)
22350 exarg_T *eap;
22351{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022352 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 char_u *p;
22354 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022355 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356
22357 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022358 name = trans_function_name(&p, eap->skip, 0, &fudi);
22359 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022361 {
22362 if (fudi.fd_dict != NULL && !eap->skip)
22363 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 if (!ends_excmd(*skipwhite(p)))
22367 {
22368 vim_free(name);
22369 EMSG(_(e_trailing));
22370 return;
22371 }
22372 eap->nextcmd = check_nextcmd(p);
22373 if (eap->nextcmd != NULL)
22374 *p = NUL;
22375
22376 if (!eap->skip)
22377 fp = find_func(name);
22378 vim_free(name);
22379
22380 if (!eap->skip)
22381 {
22382 if (fp == NULL)
22383 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022384 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 return;
22386 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022387 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 {
22389 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22390 return;
22391 }
22392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022393 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022395 /* Delete the dict item that refers to the function, it will
22396 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022397 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022399 else
22400 func_free(fp);
22401 }
22402}
22403
22404/*
22405 * Free a function and remove it from the list of functions.
22406 */
22407 static void
22408func_free(fp)
22409 ufunc_T *fp;
22410{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022411 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022412
22413 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022414 ga_clear_strings(&(fp->uf_args));
22415 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022416#ifdef FEAT_PROFILE
22417 vim_free(fp->uf_tml_count);
22418 vim_free(fp->uf_tml_total);
22419 vim_free(fp->uf_tml_self);
22420#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022421
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022422 /* remove the function from the function hashtable */
22423 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22424 if (HASHITEM_EMPTY(hi))
22425 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022426 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022427 hash_remove(&func_hashtab, hi);
22428
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022429 vim_free(fp);
22430}
22431
22432/*
22433 * Unreference a Function: decrement the reference count and free it when it
22434 * becomes zero. Only for numbered functions.
22435 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022436 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022437func_unref(name)
22438 char_u *name;
22439{
22440 ufunc_T *fp;
22441
22442 if (name != NULL && isdigit(*name))
22443 {
22444 fp = find_func(name);
22445 if (fp == NULL)
22446 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022447 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022448 {
22449 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022450 * when "uf_calls" becomes zero. */
22451 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022452 func_free(fp);
22453 }
22454 }
22455}
22456
22457/*
22458 * Count a reference to a Function.
22459 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022460 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022461func_ref(name)
22462 char_u *name;
22463{
22464 ufunc_T *fp;
22465
22466 if (name != NULL && isdigit(*name))
22467 {
22468 fp = find_func(name);
22469 if (fp == NULL)
22470 EMSG2(_(e_intern2), "func_ref()");
22471 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022472 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 }
22474}
22475
22476/*
22477 * Call a user function.
22478 */
22479 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022480call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 ufunc_T *fp; /* pointer to function */
22482 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022483 typval_T *argvars; /* arguments */
22484 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 linenr_T firstline; /* first line of range */
22486 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022487 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488{
Bram Moolenaar33570922005-01-25 22:26:29 +000022489 char_u *save_sourcing_name;
22490 linenr_T save_sourcing_lnum;
22491 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022492 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022493 int save_did_emsg;
22494 static int depth = 0;
22495 dictitem_T *v;
22496 int fixvar_idx = 0; /* index in fixvar[] */
22497 int i;
22498 int ai;
22499 char_u numbuf[NUMBUFLEN];
22500 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022501#ifdef FEAT_PROFILE
22502 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022503 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505
22506 /* If depth of calling is getting too high, don't execute the function */
22507 if (depth >= p_mfd)
22508 {
22509 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022510 rettv->v_type = VAR_NUMBER;
22511 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 return;
22513 }
22514 ++depth;
22515
22516 line_breakcheck(); /* check for CTRL-C hit */
22517
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022518 fc = (funccall_T *)alloc(sizeof(funccall_T));
22519 fc->caller = current_funccal;
22520 current_funccal = fc;
22521 fc->func = fp;
22522 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022523 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022524 fc->linenr = 0;
22525 fc->returned = FALSE;
22526 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022528 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22529 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530
Bram Moolenaar33570922005-01-25 22:26:29 +000022531 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022532 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022533 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22534 * each argument variable and saves a lot of time.
22535 */
22536 /*
22537 * Init l: variables.
22538 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022539 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022540 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022541 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022542 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22543 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022544 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022545 name = v->di_key;
22546 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022547 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022548 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022549 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022550 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022551 v->di_tv.vval.v_dict = selfdict;
22552 ++selfdict->dv_refcount;
22553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022554
Bram Moolenaar33570922005-01-25 22:26:29 +000022555 /*
22556 * Init a: variables.
22557 * Set a:0 to "argcount".
22558 * Set a:000 to a list with room for the "..." arguments.
22559 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022560 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022561 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022562 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022563 /* Use "name" to avoid a warning from some compiler that checks the
22564 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022565 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022566 name = v->di_key;
22567 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022568 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022569 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022571 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022572 v->di_tv.vval.v_list = &fc->l_varlist;
22573 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22574 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22575 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022576
22577 /*
22578 * Set a:firstline to "firstline" and a:lastline to "lastline".
22579 * Set a:name to named arguments.
22580 * Set a:N to the "..." arguments.
22581 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022582 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022583 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022584 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022585 (varnumber_T)lastline);
22586 for (i = 0; i < argcount; ++i)
22587 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022588 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 if (ai < 0)
22590 /* named argument a:name */
22591 name = FUNCARG(fp, i);
22592 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022593 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022594 /* "..." argument a:1, a:2, etc. */
22595 sprintf((char *)numbuf, "%d", ai + 1);
22596 name = numbuf;
22597 }
22598 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22599 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022600 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022601 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22602 }
22603 else
22604 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022605 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22606 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022607 if (v == NULL)
22608 break;
22609 v->di_flags = DI_FLAGS_RO;
22610 }
22611 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022612 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022613
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022614 /* Note: the values are copied directly to avoid alloc/free.
22615 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022616 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022617 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022618
22619 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22620 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022621 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22622 fc->l_listitems[ai].li_tv = argvars[i];
22623 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022624 }
22625 }
22626
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 /* Don't redraw while executing the function. */
22628 ++RedrawingDisabled;
22629 save_sourcing_name = sourcing_name;
22630 save_sourcing_lnum = sourcing_lnum;
22631 sourcing_lnum = 1;
22632 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022633 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 if (sourcing_name != NULL)
22635 {
22636 if (save_sourcing_name != NULL
22637 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22638 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22639 else
22640 STRCPY(sourcing_name, "function ");
22641 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22642
22643 if (p_verbose >= 12)
22644 {
22645 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022646 verbose_enter_scroll();
22647
Bram Moolenaar555b2802005-05-19 21:08:39 +000022648 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 if (p_verbose >= 14)
22650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022652 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022653 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022654 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655
22656 msg_puts((char_u *)"(");
22657 for (i = 0; i < argcount; ++i)
22658 {
22659 if (i > 0)
22660 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022661 if (argvars[i].v_type == VAR_NUMBER)
22662 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 else
22664 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022665 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22666 if (s != NULL)
22667 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022668 if (vim_strsize(s) > MSG_BUF_CLEN)
22669 {
22670 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22671 s = buf;
22672 }
22673 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022674 vim_free(tofree);
22675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 }
22677 }
22678 msg_puts((char_u *)")");
22679 }
22680 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022681
22682 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 --no_wait_return;
22684 }
22685 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022686#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022687 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022688 {
22689 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22690 func_do_profile(fp);
22691 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022692 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022693 {
22694 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022695 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022696 profile_zero(&fp->uf_tm_children);
22697 }
22698 script_prof_save(&wait_start);
22699 }
22700#endif
22701
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022703 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 save_did_emsg = did_emsg;
22705 did_emsg = FALSE;
22706
22707 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022708 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22710
22711 --RedrawingDisabled;
22712
22713 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022714 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022716 clear_tv(rettv);
22717 rettv->v_type = VAR_NUMBER;
22718 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 }
22720
Bram Moolenaar05159a02005-02-26 23:04:13 +000022721#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022722 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022723 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022724 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022725 profile_end(&call_start);
22726 profile_sub_wait(&wait_start, &call_start);
22727 profile_add(&fp->uf_tm_total, &call_start);
22728 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022729 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022730 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022731 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22732 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022733 }
22734 }
22735#endif
22736
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 /* when being verbose, mention the return value */
22738 if (p_verbose >= 12)
22739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022741 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022744 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022745 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022746 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022747 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022750 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022751 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022752 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022753 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022754
Bram Moolenaar555b2802005-05-19 21:08:39 +000022755 /* The value may be very long. Skip the middle part, so that we
22756 * have some idea how it starts and ends. smsg() would always
22757 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022758 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022759 if (s != NULL)
22760 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022761 if (vim_strsize(s) > MSG_BUF_CLEN)
22762 {
22763 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22764 s = buf;
22765 }
22766 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022767 vim_free(tofree);
22768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 }
22770 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022771
22772 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 --no_wait_return;
22774 }
22775
22776 vim_free(sourcing_name);
22777 sourcing_name = save_sourcing_name;
22778 sourcing_lnum = save_sourcing_lnum;
22779 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022780#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022781 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022782 script_prof_restore(&wait_start);
22783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784
22785 if (p_verbose >= 12 && sourcing_name != NULL)
22786 {
22787 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022788 verbose_enter_scroll();
22789
Bram Moolenaar555b2802005-05-19 21:08:39 +000022790 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022792
22793 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 --no_wait_return;
22795 }
22796
22797 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022798 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022800
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022801 /* If the a:000 list and the l: and a: dicts are not referenced we can
22802 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022803 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22804 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22805 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22806 {
22807 free_funccal(fc, FALSE);
22808 }
22809 else
22810 {
22811 hashitem_T *hi;
22812 listitem_T *li;
22813 int todo;
22814
22815 /* "fc" is still in use. This can happen when returning "a:000" or
22816 * assigning "l:" to a global variable.
22817 * Link "fc" in the list for garbage collection later. */
22818 fc->caller = previous_funccal;
22819 previous_funccal = fc;
22820
22821 /* Make a copy of the a: variables, since we didn't do that above. */
22822 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22823 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22824 {
22825 if (!HASHITEM_EMPTY(hi))
22826 {
22827 --todo;
22828 v = HI2DI(hi);
22829 copy_tv(&v->di_tv, &v->di_tv);
22830 }
22831 }
22832
22833 /* Make a copy of the a:000 items, since we didn't do that above. */
22834 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22835 copy_tv(&li->li_tv, &li->li_tv);
22836 }
22837}
22838
22839/*
22840 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022841 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022842 */
22843 static int
22844can_free_funccal(fc, copyID)
22845 funccall_T *fc;
22846 int copyID;
22847{
22848 return (fc->l_varlist.lv_copyID != copyID
22849 && fc->l_vars.dv_copyID != copyID
22850 && fc->l_avars.dv_copyID != copyID);
22851}
22852
22853/*
22854 * Free "fc" and what it contains.
22855 */
22856 static void
22857free_funccal(fc, free_val)
22858 funccall_T *fc;
22859 int free_val; /* a: vars were allocated */
22860{
22861 listitem_T *li;
22862
22863 /* The a: variables typevals may not have been allocated, only free the
22864 * allocated variables. */
22865 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22866
22867 /* free all l: variables */
22868 vars_clear(&fc->l_vars.dv_hashtab);
22869
22870 /* Free the a:000 variables if they were allocated. */
22871 if (free_val)
22872 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22873 clear_tv(&li->li_tv);
22874
22875 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876}
22877
22878/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022879 * Add a number variable "name" to dict "dp" with value "nr".
22880 */
22881 static void
22882add_nr_var(dp, v, name, nr)
22883 dict_T *dp;
22884 dictitem_T *v;
22885 char *name;
22886 varnumber_T nr;
22887{
22888 STRCPY(v->di_key, name);
22889 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22890 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22891 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022892 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 v->di_tv.vval.v_number = nr;
22894}
22895
22896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 * ":return [expr]"
22898 */
22899 void
22900ex_return(eap)
22901 exarg_T *eap;
22902{
22903 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022904 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 int returning = FALSE;
22906
22907 if (current_funccal == NULL)
22908 {
22909 EMSG(_("E133: :return not inside a function"));
22910 return;
22911 }
22912
22913 if (eap->skip)
22914 ++emsg_skip;
22915
22916 eap->nextcmd = NULL;
22917 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022918 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 {
22920 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022921 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924 }
22925 /* It's safer to return also on error. */
22926 else if (!eap->skip)
22927 {
22928 /*
22929 * Return unless the expression evaluation has been cancelled due to an
22930 * aborting error, an interrupt, or an exception.
22931 */
22932 if (!aborting())
22933 returning = do_return(eap, FALSE, TRUE, NULL);
22934 }
22935
22936 /* When skipping or the return gets pending, advance to the next command
22937 * in this line (!returning). Otherwise, ignore the rest of the line.
22938 * Following lines will be ignored by get_func_line(). */
22939 if (returning)
22940 eap->nextcmd = NULL;
22941 else if (eap->nextcmd == NULL) /* no argument */
22942 eap->nextcmd = check_nextcmd(arg);
22943
22944 if (eap->skip)
22945 --emsg_skip;
22946}
22947
22948/*
22949 * Return from a function. Possibly makes the return pending. Also called
22950 * for a pending return at the ":endtry" or after returning from an extra
22951 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022952 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022953 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 * FALSE when the return gets pending.
22955 */
22956 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022957do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 exarg_T *eap;
22959 int reanimate;
22960 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022961 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962{
22963 int idx;
22964 struct condstack *cstack = eap->cstack;
22965
22966 if (reanimate)
22967 /* Undo the return. */
22968 current_funccal->returned = FALSE;
22969
22970 /*
22971 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22972 * not in its finally clause (which then is to be executed next) is found.
22973 * In this case, make the ":return" pending for execution at the ":endtry".
22974 * Otherwise, return normally.
22975 */
22976 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22977 if (idx >= 0)
22978 {
22979 cstack->cs_pending[idx] = CSTP_RETURN;
22980
22981 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022982 /* A pending return again gets pending. "rettv" points to an
22983 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022985 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 else
22987 {
22988 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022989 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022991 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022993 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 {
22995 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022996 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022997 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 else
22999 EMSG(_(e_outofmem));
23000 }
23001 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023002 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003
23004 if (reanimate)
23005 {
23006 /* The pending return value could be overwritten by a ":return"
23007 * without argument in a finally clause; reset the default
23008 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023009 current_funccal->rettv->v_type = VAR_NUMBER;
23010 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011 }
23012 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023013 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 }
23015 else
23016 {
23017 current_funccal->returned = TRUE;
23018
23019 /* If the return is carried out now, store the return value. For
23020 * a return immediately after reanimation, the value is already
23021 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023022 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023024 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023027 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 }
23029 }
23030
23031 return idx < 0;
23032}
23033
23034/*
23035 * Free the variable with a pending return value.
23036 */
23037 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023038discard_pending_return(rettv)
23039 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040{
Bram Moolenaar33570922005-01-25 22:26:29 +000023041 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042}
23043
23044/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023045 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 * is an allocated string. Used by report_pending() for verbose messages.
23047 */
23048 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023049get_return_cmd(rettv)
23050 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023052 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023053 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023054 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023056 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023057 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023058 if (s == NULL)
23059 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023060
23061 STRCPY(IObuff, ":return ");
23062 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23063 if (STRLEN(s) + 8 >= IOSIZE)
23064 STRCPY(IObuff + IOSIZE - 4, "...");
23065 vim_free(tofree);
23066 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067}
23068
23069/*
23070 * Get next function line.
23071 * Called by do_cmdline() to get the next line.
23072 * Returns allocated string, or NULL for end of function.
23073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 char_u *
23075get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023076 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023078 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079{
Bram Moolenaar33570922005-01-25 22:26:29 +000023080 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023081 ufunc_T *fp = fcp->func;
23082 char_u *retval;
23083 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084
23085 /* If breakpoints have been added/deleted need to check for it. */
23086 if (fcp->dbg_tick != debug_tick)
23087 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023088 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 sourcing_lnum);
23090 fcp->dbg_tick = debug_tick;
23091 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023092#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023093 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023094 func_line_end(cookie);
23095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096
Bram Moolenaar05159a02005-02-26 23:04:13 +000023097 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023098 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23099 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 retval = NULL;
23101 else
23102 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023103 /* Skip NULL lines (continuation lines). */
23104 while (fcp->linenr < gap->ga_len
23105 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23106 ++fcp->linenr;
23107 if (fcp->linenr >= gap->ga_len)
23108 retval = NULL;
23109 else
23110 {
23111 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23112 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023113#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023114 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023115 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023116#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 }
23119
23120 /* Did we encounter a breakpoint? */
23121 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23122 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023123 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023125 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 sourcing_lnum);
23127 fcp->dbg_tick = debug_tick;
23128 }
23129
23130 return retval;
23131}
23132
Bram Moolenaar05159a02005-02-26 23:04:13 +000023133#if defined(FEAT_PROFILE) || defined(PROTO)
23134/*
23135 * Called when starting to read a function line.
23136 * "sourcing_lnum" must be correct!
23137 * When skipping lines it may not actually be executed, but we won't find out
23138 * until later and we need to store the time now.
23139 */
23140 void
23141func_line_start(cookie)
23142 void *cookie;
23143{
23144 funccall_T *fcp = (funccall_T *)cookie;
23145 ufunc_T *fp = fcp->func;
23146
23147 if (fp->uf_profiling && sourcing_lnum >= 1
23148 && sourcing_lnum <= fp->uf_lines.ga_len)
23149 {
23150 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023151 /* Skip continuation lines. */
23152 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23153 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023154 fp->uf_tml_execed = FALSE;
23155 profile_start(&fp->uf_tml_start);
23156 profile_zero(&fp->uf_tml_children);
23157 profile_get_wait(&fp->uf_tml_wait);
23158 }
23159}
23160
23161/*
23162 * Called when actually executing a function line.
23163 */
23164 void
23165func_line_exec(cookie)
23166 void *cookie;
23167{
23168 funccall_T *fcp = (funccall_T *)cookie;
23169 ufunc_T *fp = fcp->func;
23170
23171 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23172 fp->uf_tml_execed = TRUE;
23173}
23174
23175/*
23176 * Called when done with a function line.
23177 */
23178 void
23179func_line_end(cookie)
23180 void *cookie;
23181{
23182 funccall_T *fcp = (funccall_T *)cookie;
23183 ufunc_T *fp = fcp->func;
23184
23185 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23186 {
23187 if (fp->uf_tml_execed)
23188 {
23189 ++fp->uf_tml_count[fp->uf_tml_idx];
23190 profile_end(&fp->uf_tml_start);
23191 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023192 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023193 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23194 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023195 }
23196 fp->uf_tml_idx = -1;
23197 }
23198}
23199#endif
23200
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201/*
23202 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023203 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 */
23205 int
23206func_has_ended(cookie)
23207 void *cookie;
23208{
Bram Moolenaar33570922005-01-25 22:26:29 +000023209 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210
23211 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23212 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023213 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 || fcp->returned);
23215}
23216
23217/*
23218 * return TRUE if cookie indicates a function which "abort"s on errors.
23219 */
23220 int
23221func_has_abort(cookie)
23222 void *cookie;
23223{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023224 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225}
23226
23227#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23228typedef enum
23229{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023230 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23231 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23232 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233} var_flavour_T;
23234
23235static var_flavour_T var_flavour __ARGS((char_u *varname));
23236
23237 static var_flavour_T
23238var_flavour(varname)
23239 char_u *varname;
23240{
23241 char_u *p = varname;
23242
23243 if (ASCII_ISUPPER(*p))
23244 {
23245 while (*(++p))
23246 if (ASCII_ISLOWER(*p))
23247 return VAR_FLAVOUR_SESSION;
23248 return VAR_FLAVOUR_VIMINFO;
23249 }
23250 else
23251 return VAR_FLAVOUR_DEFAULT;
23252}
23253#endif
23254
23255#if defined(FEAT_VIMINFO) || defined(PROTO)
23256/*
23257 * Restore global vars that start with a capital from the viminfo file
23258 */
23259 int
23260read_viminfo_varlist(virp, writing)
23261 vir_T *virp;
23262 int writing;
23263{
23264 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023265 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023266 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267
23268 if (!writing && (find_viminfo_parameter('!') != NULL))
23269 {
23270 tab = vim_strchr(virp->vir_line + 1, '\t');
23271 if (tab != NULL)
23272 {
23273 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023274 switch (*tab)
23275 {
23276 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023277#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023278 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023279#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023280 case 'D': type = VAR_DICT; break;
23281 case 'L': type = VAR_LIST; break;
23282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283
23284 tab = vim_strchr(tab, '\t');
23285 if (tab != NULL)
23286 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023287 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023288 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023289 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023291#ifdef FEAT_FLOAT
23292 else if (type == VAR_FLOAT)
23293 (void)string2float(tab + 1, &tv.vval.v_float);
23294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023296 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023297 if (type == VAR_DICT || type == VAR_LIST)
23298 {
23299 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23300
23301 if (etv == NULL)
23302 /* Failed to parse back the dict or list, use it as a
23303 * string. */
23304 tv.v_type = VAR_STRING;
23305 else
23306 {
23307 vim_free(tv.vval.v_string);
23308 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023309 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023310 }
23311 }
23312
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023313 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023314
23315 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023316 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023317 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23318 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 }
23320 }
23321 }
23322
23323 return viminfo_readline(virp);
23324}
23325
23326/*
23327 * Write global vars that start with a capital to the viminfo file
23328 */
23329 void
23330write_viminfo_varlist(fp)
23331 FILE *fp;
23332{
Bram Moolenaar33570922005-01-25 22:26:29 +000023333 hashitem_T *hi;
23334 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023335 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023336 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023337 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023338 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023339 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340
23341 if (find_viminfo_parameter('!') == NULL)
23342 return;
23343
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023344 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023345
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023346 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023347 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023349 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023351 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023352 this_var = HI2DI(hi);
23353 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023354 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023355 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023356 {
23357 case VAR_STRING: s = "STR"; break;
23358 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023359#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023360 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023361#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023362 case VAR_DICT: s = "DIC"; break;
23363 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023364 default: continue;
23365 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023366 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023367 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023368 if (p != NULL)
23369 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023370 vim_free(tofree);
23371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 }
23373 }
23374}
23375#endif
23376
23377#if defined(FEAT_SESSION) || defined(PROTO)
23378 int
23379store_session_globals(fd)
23380 FILE *fd;
23381{
Bram Moolenaar33570922005-01-25 22:26:29 +000023382 hashitem_T *hi;
23383 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023384 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385 char_u *p, *t;
23386
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023387 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023388 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023390 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023392 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023393 this_var = HI2DI(hi);
23394 if ((this_var->di_tv.v_type == VAR_NUMBER
23395 || this_var->di_tv.v_type == VAR_STRING)
23396 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023397 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023398 /* Escape special characters with a backslash. Turn a LF and
23399 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023400 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023401 (char_u *)"\\\"\n\r");
23402 if (p == NULL) /* out of memory */
23403 break;
23404 for (t = p; *t != NUL; ++t)
23405 if (*t == '\n')
23406 *t = 'n';
23407 else if (*t == '\r')
23408 *t = 'r';
23409 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023410 this_var->di_key,
23411 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23412 : ' ',
23413 p,
23414 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23415 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023416 || put_eol(fd) == FAIL)
23417 {
23418 vim_free(p);
23419 return FAIL;
23420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 vim_free(p);
23422 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023423#ifdef FEAT_FLOAT
23424 else if (this_var->di_tv.v_type == VAR_FLOAT
23425 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23426 {
23427 float_T f = this_var->di_tv.vval.v_float;
23428 int sign = ' ';
23429
23430 if (f < 0)
23431 {
23432 f = -f;
23433 sign = '-';
23434 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023435 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023436 this_var->di_key, sign, f) < 0)
23437 || put_eol(fd) == FAIL)
23438 return FAIL;
23439 }
23440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 }
23442 }
23443 return OK;
23444}
23445#endif
23446
Bram Moolenaar661b1822005-07-28 22:36:45 +000023447/*
23448 * Display script name where an item was last set.
23449 * Should only be invoked when 'verbose' is non-zero.
23450 */
23451 void
23452last_set_msg(scriptID)
23453 scid_T scriptID;
23454{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023455 char_u *p;
23456
Bram Moolenaar661b1822005-07-28 22:36:45 +000023457 if (scriptID != 0)
23458 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023459 p = home_replace_save(NULL, get_scriptname(scriptID));
23460 if (p != NULL)
23461 {
23462 verbose_enter();
23463 MSG_PUTS(_("\n\tLast set from "));
23464 MSG_PUTS(p);
23465 vim_free(p);
23466 verbose_leave();
23467 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023468 }
23469}
23470
Bram Moolenaard812df62008-11-09 12:46:09 +000023471/*
23472 * List v:oldfiles in a nice way.
23473 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023474 void
23475ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023476 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023477{
23478 list_T *l = vimvars[VV_OLDFILES].vv_list;
23479 listitem_T *li;
23480 int nr = 0;
23481
23482 if (l == NULL)
23483 msg((char_u *)_("No old files"));
23484 else
23485 {
23486 msg_start();
23487 msg_scroll = TRUE;
23488 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23489 {
23490 msg_outnum((long)++nr);
23491 MSG_PUTS(": ");
23492 msg_outtrans(get_tv_string(&li->li_tv));
23493 msg_putchar('\n');
23494 out_flush(); /* output one line at a time */
23495 ui_breakcheck();
23496 }
23497 /* Assume "got_int" was set to truncate the listing. */
23498 got_int = FALSE;
23499
23500#ifdef FEAT_BROWSE_CMD
23501 if (cmdmod.browse)
23502 {
23503 quit_more = FALSE;
23504 nr = prompt_for_number(FALSE);
23505 msg_starthere();
23506 if (nr > 0)
23507 {
23508 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23509 (long)nr);
23510
23511 if (p != NULL)
23512 {
23513 p = expand_env_save(p);
23514 eap->arg = p;
23515 eap->cmdidx = CMD_edit;
23516 cmdmod.browse = FALSE;
23517 do_exedit(eap, NULL);
23518 vim_free(p);
23519 }
23520 }
23521 }
23522#endif
23523 }
23524}
23525
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526#endif /* FEAT_EVAL */
23527
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023529#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530
23531#ifdef WIN3264
23532/*
23533 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23534 */
23535static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23536static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23537static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23538
23539/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023540 * Get the short path (8.3) for the filename in "fnamep".
23541 * Only works for a valid file name.
23542 * When the path gets longer "fnamep" is changed and the allocated buffer
23543 * is put in "bufp".
23544 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23545 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 */
23547 static int
23548get_short_pathname(fnamep, bufp, fnamelen)
23549 char_u **fnamep;
23550 char_u **bufp;
23551 int *fnamelen;
23552{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023553 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 char_u *newbuf;
23555
23556 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 l = GetShortPathName(*fnamep, *fnamep, len);
23558 if (l > len - 1)
23559 {
23560 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023561 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 newbuf = vim_strnsave(*fnamep, l);
23563 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023564 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565
23566 vim_free(*bufp);
23567 *fnamep = *bufp = newbuf;
23568
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023569 /* Really should always succeed, as the buffer is big enough. */
23570 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 }
23572
23573 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023574 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575}
23576
23577/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023578 * Get the short path (8.3) for the filename in "fname". The converted
23579 * path is returned in "bufp".
23580 *
23581 * Some of the directories specified in "fname" may not exist. This function
23582 * will shorten the existing directories at the beginning of the path and then
23583 * append the remaining non-existing path.
23584 *
23585 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023586 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023587 * bufp - Pointer to an allocated buffer for the filename.
23588 * fnamelen - Length of the filename pointed to by fname
23589 *
23590 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591 */
23592 static int
23593shortpath_for_invalid_fname(fname, bufp, fnamelen)
23594 char_u **fname;
23595 char_u **bufp;
23596 int *fnamelen;
23597{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023598 char_u *short_fname, *save_fname, *pbuf_unused;
23599 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023601 int old_len, len;
23602 int new_len, sfx_len;
23603 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604
23605 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023606 old_len = *fnamelen;
23607 save_fname = vim_strnsave(*fname, old_len);
23608 pbuf_unused = NULL;
23609 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023611 endp = save_fname + old_len - 1; /* Find the end of the copy */
23612 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023614 /*
23615 * Try shortening the supplied path till it succeeds by removing one
23616 * directory at a time from the tail of the path.
23617 */
23618 len = 0;
23619 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023621 /* go back one path-separator */
23622 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23623 --endp;
23624 if (endp <= save_fname)
23625 break; /* processed the complete path */
23626
23627 /*
23628 * Replace the path separator with a NUL and try to shorten the
23629 * resulting path.
23630 */
23631 ch = *endp;
23632 *endp = 0;
23633 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023634 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023635 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23636 {
23637 retval = FAIL;
23638 goto theend;
23639 }
23640 *endp = ch; /* preserve the string */
23641
23642 if (len > 0)
23643 break; /* successfully shortened the path */
23644
23645 /* failed to shorten the path. Skip the path separator */
23646 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 }
23648
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023649 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023651 /*
23652 * Succeeded in shortening the path. Now concatenate the shortened
23653 * path with the remaining path at the tail.
23654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023656 /* Compute the length of the new path. */
23657 sfx_len = (int)(save_endp - endp) + 1;
23658 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023660 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023662 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023664 /* There is not enough space in the currently allocated string,
23665 * copy it to a buffer big enough. */
23666 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023668 {
23669 retval = FAIL;
23670 goto theend;
23671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 }
23673 else
23674 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023675 /* Transfer short_fname to the main buffer (it's big enough),
23676 * unless get_short_pathname() did its work in-place. */
23677 *fname = *bufp = save_fname;
23678 if (short_fname != save_fname)
23679 vim_strncpy(save_fname, short_fname, len);
23680 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023682
23683 /* concat the not-shortened part of the path */
23684 vim_strncpy(*fname + len, endp, sfx_len);
23685 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023687
23688theend:
23689 vim_free(pbuf_unused);
23690 vim_free(save_fname);
23691
23692 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693}
23694
23695/*
23696 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023697 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 */
23699 static int
23700shortpath_for_partial(fnamep, bufp, fnamelen)
23701 char_u **fnamep;
23702 char_u **bufp;
23703 int *fnamelen;
23704{
23705 int sepcount, len, tflen;
23706 char_u *p;
23707 char_u *pbuf, *tfname;
23708 int hasTilde;
23709
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023710 /* Count up the path separators from the RHS.. so we know which part
23711 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023713 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 if (vim_ispathsep(*p))
23715 ++sepcount;
23716
23717 /* Need full path first (use expand_env() to remove a "~/") */
23718 hasTilde = (**fnamep == '~');
23719 if (hasTilde)
23720 pbuf = tfname = expand_env_save(*fnamep);
23721 else
23722 pbuf = tfname = FullName_save(*fnamep, FALSE);
23723
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023724 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023726 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23727 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728
23729 if (len == 0)
23730 {
23731 /* Don't have a valid filename, so shorten the rest of the
23732 * path if we can. This CAN give us invalid 8.3 filenames, but
23733 * there's not a lot of point in guessing what it might be.
23734 */
23735 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023736 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23737 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 }
23739
23740 /* Count the paths backward to find the beginning of the desired string. */
23741 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023742 {
23743#ifdef FEAT_MBYTE
23744 if (has_mbyte)
23745 p -= mb_head_off(tfname, p);
23746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 if (vim_ispathsep(*p))
23748 {
23749 if (sepcount == 0 || (hasTilde && sepcount == 1))
23750 break;
23751 else
23752 sepcount --;
23753 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 if (hasTilde)
23756 {
23757 --p;
23758 if (p >= tfname)
23759 *p = '~';
23760 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023761 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 }
23763 else
23764 ++p;
23765
23766 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23767 vim_free(*bufp);
23768 *fnamelen = (int)STRLEN(p);
23769 *bufp = pbuf;
23770 *fnamep = p;
23771
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023772 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773}
23774#endif /* WIN3264 */
23775
23776/*
23777 * Adjust a filename, according to a string of modifiers.
23778 * *fnamep must be NUL terminated when called. When returning, the length is
23779 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023780 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781 * When there is an error, *fnamep is set to NULL.
23782 */
23783 int
23784modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23785 char_u *src; /* string with modifiers */
23786 int *usedlen; /* characters after src that are used */
23787 char_u **fnamep; /* file name so far */
23788 char_u **bufp; /* buffer for allocated file name or NULL */
23789 int *fnamelen; /* length of fnamep */
23790{
23791 int valid = 0;
23792 char_u *tail;
23793 char_u *s, *p, *pbuf;
23794 char_u dirname[MAXPATHL];
23795 int c;
23796 int has_fullname = 0;
23797#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023798 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 int has_shortname = 0;
23800#endif
23801
23802repeat:
23803 /* ":p" - full path/file_name */
23804 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23805 {
23806 has_fullname = 1;
23807
23808 valid |= VALID_PATH;
23809 *usedlen += 2;
23810
23811 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23812 if ((*fnamep)[0] == '~'
23813#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23814 && ((*fnamep)[1] == '/'
23815# ifdef BACKSLASH_IN_FILENAME
23816 || (*fnamep)[1] == '\\'
23817# endif
23818 || (*fnamep)[1] == NUL)
23819
23820#endif
23821 )
23822 {
23823 *fnamep = expand_env_save(*fnamep);
23824 vim_free(*bufp); /* free any allocated file name */
23825 *bufp = *fnamep;
23826 if (*fnamep == NULL)
23827 return -1;
23828 }
23829
23830 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023831 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 {
23833 if (vim_ispathsep(*p)
23834 && p[1] == '.'
23835 && (p[2] == NUL
23836 || vim_ispathsep(p[2])
23837 || (p[2] == '.'
23838 && (p[3] == NUL || vim_ispathsep(p[3])))))
23839 break;
23840 }
23841
23842 /* FullName_save() is slow, don't use it when not needed. */
23843 if (*p != NUL || !vim_isAbsName(*fnamep))
23844 {
23845 *fnamep = FullName_save(*fnamep, *p != NUL);
23846 vim_free(*bufp); /* free any allocated file name */
23847 *bufp = *fnamep;
23848 if (*fnamep == NULL)
23849 return -1;
23850 }
23851
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023852#ifdef WIN3264
23853# if _WIN32_WINNT >= 0x0500
23854 if (vim_strchr(*fnamep, '~') != NULL)
23855 {
23856 /* Expand 8.3 filename to full path. Needed to make sure the same
23857 * file does not have two different names.
23858 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23859 p = alloc(_MAX_PATH + 1);
23860 if (p != NULL)
23861 {
23862 if (GetLongPathName(*fnamep, p, MAXPATHL))
23863 {
23864 vim_free(*bufp);
23865 *bufp = *fnamep = p;
23866 }
23867 else
23868 vim_free(p);
23869 }
23870 }
23871# endif
23872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 /* Append a path separator to a directory. */
23874 if (mch_isdir(*fnamep))
23875 {
23876 /* Make room for one or two extra characters. */
23877 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23878 vim_free(*bufp); /* free any allocated file name */
23879 *bufp = *fnamep;
23880 if (*fnamep == NULL)
23881 return -1;
23882 add_pathsep(*fnamep);
23883 }
23884 }
23885
23886 /* ":." - path relative to the current directory */
23887 /* ":~" - path relative to the home directory */
23888 /* ":8" - shortname path - postponed till after */
23889 while (src[*usedlen] == ':'
23890 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23891 {
23892 *usedlen += 2;
23893 if (c == '8')
23894 {
23895#ifdef WIN3264
23896 has_shortname = 1; /* Postpone this. */
23897#endif
23898 continue;
23899 }
23900 pbuf = NULL;
23901 /* Need full path first (use expand_env() to remove a "~/") */
23902 if (!has_fullname)
23903 {
23904 if (c == '.' && **fnamep == '~')
23905 p = pbuf = expand_env_save(*fnamep);
23906 else
23907 p = pbuf = FullName_save(*fnamep, FALSE);
23908 }
23909 else
23910 p = *fnamep;
23911
23912 has_fullname = 0;
23913
23914 if (p != NULL)
23915 {
23916 if (c == '.')
23917 {
23918 mch_dirname(dirname, MAXPATHL);
23919 s = shorten_fname(p, dirname);
23920 if (s != NULL)
23921 {
23922 *fnamep = s;
23923 if (pbuf != NULL)
23924 {
23925 vim_free(*bufp); /* free any allocated file name */
23926 *bufp = pbuf;
23927 pbuf = NULL;
23928 }
23929 }
23930 }
23931 else
23932 {
23933 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23934 /* Only replace it when it starts with '~' */
23935 if (*dirname == '~')
23936 {
23937 s = vim_strsave(dirname);
23938 if (s != NULL)
23939 {
23940 *fnamep = s;
23941 vim_free(*bufp);
23942 *bufp = s;
23943 }
23944 }
23945 }
23946 vim_free(pbuf);
23947 }
23948 }
23949
23950 tail = gettail(*fnamep);
23951 *fnamelen = (int)STRLEN(*fnamep);
23952
23953 /* ":h" - head, remove "/file_name", can be repeated */
23954 /* Don't remove the first "/" or "c:\" */
23955 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23956 {
23957 valid |= VALID_HEAD;
23958 *usedlen += 2;
23959 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023960 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023961 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 *fnamelen = (int)(tail - *fnamep);
23963#ifdef VMS
23964 if (*fnamelen > 0)
23965 *fnamelen += 1; /* the path separator is part of the path */
23966#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023967 if (*fnamelen == 0)
23968 {
23969 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23970 p = vim_strsave((char_u *)".");
23971 if (p == NULL)
23972 return -1;
23973 vim_free(*bufp);
23974 *bufp = *fnamep = tail = p;
23975 *fnamelen = 1;
23976 }
23977 else
23978 {
23979 while (tail > s && !after_pathsep(s, tail))
23980 mb_ptr_back(*fnamep, tail);
23981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982 }
23983
23984 /* ":8" - shortname */
23985 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23986 {
23987 *usedlen += 2;
23988#ifdef WIN3264
23989 has_shortname = 1;
23990#endif
23991 }
23992
23993#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023994 /*
23995 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 */
23997 if (has_shortname)
23998 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023999 /* Copy the string if it is shortened by :h and when it wasn't copied
24000 * yet, because we are going to change it in place. Avoids changing
24001 * the buffer name for "%:8". */
24002 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 {
24004 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024005 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 return -1;
24007 vim_free(*bufp);
24008 *bufp = *fnamep = p;
24009 }
24010
24011 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024012 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 if (!has_fullname && !vim_isAbsName(*fnamep))
24014 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024015 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 return -1;
24017 }
24018 else
24019 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024020 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024021
Bram Moolenaardc935552011-08-17 15:23:23 +020024022 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024024 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 return -1;
24026
24027 if (l == 0)
24028 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024029 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024031 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 return -1;
24033 }
24034 *fnamelen = l;
24035 }
24036 }
24037#endif /* WIN3264 */
24038
24039 /* ":t" - tail, just the basename */
24040 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24041 {
24042 *usedlen += 2;
24043 *fnamelen -= (int)(tail - *fnamep);
24044 *fnamep = tail;
24045 }
24046
24047 /* ":e" - extension, can be repeated */
24048 /* ":r" - root, without extension, can be repeated */
24049 while (src[*usedlen] == ':'
24050 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24051 {
24052 /* find a '.' in the tail:
24053 * - for second :e: before the current fname
24054 * - otherwise: The last '.'
24055 */
24056 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24057 s = *fnamep - 2;
24058 else
24059 s = *fnamep + *fnamelen - 1;
24060 for ( ; s > tail; --s)
24061 if (s[0] == '.')
24062 break;
24063 if (src[*usedlen + 1] == 'e') /* :e */
24064 {
24065 if (s > tail)
24066 {
24067 *fnamelen += (int)(*fnamep - (s + 1));
24068 *fnamep = s + 1;
24069#ifdef VMS
24070 /* cut version from the extension */
24071 s = *fnamep + *fnamelen - 1;
24072 for ( ; s > *fnamep; --s)
24073 if (s[0] == ';')
24074 break;
24075 if (s > *fnamep)
24076 *fnamelen = s - *fnamep;
24077#endif
24078 }
24079 else if (*fnamep <= tail)
24080 *fnamelen = 0;
24081 }
24082 else /* :r */
24083 {
24084 if (s > tail) /* remove one extension */
24085 *fnamelen = (int)(s - *fnamep);
24086 }
24087 *usedlen += 2;
24088 }
24089
24090 /* ":s?pat?foo?" - substitute */
24091 /* ":gs?pat?foo?" - global substitute */
24092 if (src[*usedlen] == ':'
24093 && (src[*usedlen + 1] == 's'
24094 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24095 {
24096 char_u *str;
24097 char_u *pat;
24098 char_u *sub;
24099 int sep;
24100 char_u *flags;
24101 int didit = FALSE;
24102
24103 flags = (char_u *)"";
24104 s = src + *usedlen + 2;
24105 if (src[*usedlen + 1] == 'g')
24106 {
24107 flags = (char_u *)"g";
24108 ++s;
24109 }
24110
24111 sep = *s++;
24112 if (sep)
24113 {
24114 /* find end of pattern */
24115 p = vim_strchr(s, sep);
24116 if (p != NULL)
24117 {
24118 pat = vim_strnsave(s, (int)(p - s));
24119 if (pat != NULL)
24120 {
24121 s = p + 1;
24122 /* find end of substitution */
24123 p = vim_strchr(s, sep);
24124 if (p != NULL)
24125 {
24126 sub = vim_strnsave(s, (int)(p - s));
24127 str = vim_strnsave(*fnamep, *fnamelen);
24128 if (sub != NULL && str != NULL)
24129 {
24130 *usedlen = (int)(p + 1 - src);
24131 s = do_string_sub(str, pat, sub, flags);
24132 if (s != NULL)
24133 {
24134 *fnamep = s;
24135 *fnamelen = (int)STRLEN(s);
24136 vim_free(*bufp);
24137 *bufp = s;
24138 didit = TRUE;
24139 }
24140 }
24141 vim_free(sub);
24142 vim_free(str);
24143 }
24144 vim_free(pat);
24145 }
24146 }
24147 /* after using ":s", repeat all the modifiers */
24148 if (didit)
24149 goto repeat;
24150 }
24151 }
24152
24153 return valid;
24154}
24155
24156/*
24157 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24158 * "flags" can be "g" to do a global substitute.
24159 * Returns an allocated string, NULL for error.
24160 */
24161 char_u *
24162do_string_sub(str, pat, sub, flags)
24163 char_u *str;
24164 char_u *pat;
24165 char_u *sub;
24166 char_u *flags;
24167{
24168 int sublen;
24169 regmatch_T regmatch;
24170 int i;
24171 int do_all;
24172 char_u *tail;
24173 garray_T ga;
24174 char_u *ret;
24175 char_u *save_cpo;
24176
24177 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24178 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024179 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180
24181 ga_init2(&ga, 1, 200);
24182
24183 do_all = (flags[0] == 'g');
24184
24185 regmatch.rm_ic = p_ic;
24186 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24187 if (regmatch.regprog != NULL)
24188 {
24189 tail = str;
24190 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24191 {
24192 /*
24193 * Get some space for a temporary buffer to do the substitution
24194 * into. It will contain:
24195 * - The text up to where the match is.
24196 * - The substituted text.
24197 * - The text after the match.
24198 */
24199 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24200 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24201 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24202 {
24203 ga_clear(&ga);
24204 break;
24205 }
24206
24207 /* copy the text up to where the match is */
24208 i = (int)(regmatch.startp[0] - tail);
24209 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24210 /* add the substituted text */
24211 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24212 + ga.ga_len + i, TRUE, TRUE, FALSE);
24213 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 /* avoid getting stuck on a match with an empty string */
24215 if (tail == regmatch.endp[0])
24216 {
24217 if (*tail == NUL)
24218 break;
24219 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24220 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 }
24222 else
24223 {
24224 tail = regmatch.endp[0];
24225 if (*tail == NUL)
24226 break;
24227 }
24228 if (!do_all)
24229 break;
24230 }
24231
24232 if (ga.ga_data != NULL)
24233 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24234
Bram Moolenaar473de612013-06-08 18:19:48 +020024235 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 }
24237
24238 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24239 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024240 if (p_cpo == empty_option)
24241 p_cpo = save_cpo;
24242 else
24243 /* Darn, evaluating {sub} expression changed the value. */
24244 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245
24246 return ret;
24247}
24248
24249#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */