blob: 25785b777669eb3217cb8dcf8780c832e800c4ff [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 Moolenaar5313dcb2005-02-22 08:56:13 +0000813static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000814static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000815static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static 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 +0000817static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
818static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000819static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000820static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
821static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000822static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000823static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000824static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200826
827#ifdef EBCDIC
828static int compare_func_name __ARGS((const void *s1, const void *s2));
829static void sortFunctions __ARGS(());
830#endif
831
832
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000833/* Character used as separated in autoload function/variable names. */
834#define AUTOLOAD_CHAR '#'
835
Bram Moolenaar33570922005-01-25 22:26:29 +0000836/*
837 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000838 */
839 void
840eval_init()
841{
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 int i;
843 struct vimvar *p;
844
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200845 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
846 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200847 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000848 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000849 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000850
851 for (i = 0; i < VV_LEN; ++i)
852 {
853 p = &vimvars[i];
854 STRCPY(p->vv_di.di_key, p->vv_name);
855 if (p->vv_flags & VV_RO)
856 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
857 else if (p->vv_flags & VV_RO_SBX)
858 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
859 else
860 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000861
862 /* add to v: scope dict, unless the value is not always available */
863 if (p->vv_type != VAR_UNKNOWN)
864 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000866 /* add to compat scope dict */
867 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000869 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200870 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200871
872#ifdef EBCDIC
873 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100874 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200875 */
876 sortFunctions();
877#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000878}
879
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000880#if defined(EXITFREE) || defined(PROTO)
881 void
882eval_clear()
883{
884 int i;
885 struct vimvar *p;
886
887 for (i = 0; i < VV_LEN; ++i)
888 {
889 p = &vimvars[i];
890 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000891 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000892 vim_free(p->vv_str);
893 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000894 }
895 else if (p->vv_di.di_tv.v_type == VAR_LIST)
896 {
897 list_unref(p->vv_list);
898 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000900 }
901 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000902 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000903 hash_clear(&compat_hashtab);
904
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100906# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200907 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100908# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909
910 /* global variables */
911 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000913 /* autoloaded script names */
914 ga_clear_strings(&ga_loaded);
915
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200916 /* script-local variables */
917 for (i = 1; i <= ga_scripts.ga_len; ++i)
918 {
919 vars_clear(&SCRIPT_VARS(i));
920 vim_free(SCRIPT_SV(i));
921 }
922 ga_clear(&ga_scripts);
923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000924 /* unreferenced lists and dicts */
925 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000926
927 /* functions */
928 free_all_functions();
929 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930}
931#endif
932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 * Return the name of the executed function.
935 */
936 char_u *
937func_name(cookie)
938 void *cookie;
939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941}
942
943/*
944 * Return the address holding the next breakpoint line for a funccall cookie.
945 */
946 linenr_T *
947func_breakpoint(cookie)
948 void *cookie;
949{
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951}
952
953/*
954 * Return the address holding the debug tick for a funccall cookie.
955 */
956 int *
957func_dbg_tick(cookie)
958 void *cookie;
959{
Bram Moolenaar33570922005-01-25 22:26:29 +0000960 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961}
962
963/*
964 * Return the nesting level for a funccall cookie.
965 */
966 int
967func_level(cookie)
968 void *cookie;
969{
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000974funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000976/* pointer to list of previously used funccal, still around because some
977 * item in it is still being used. */
978funccall_T *previous_funccal = NULL;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980/*
981 * Return TRUE when a function was ended by a ":return" command.
982 */
983 int
984current_func_returned()
985{
986 return current_funccal->returned;
987}
988
989
990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * Set an internal variable to a string value. Creates the variable if it does
992 * not already exist.
993 */
994 void
995set_internal_string_var(name, value)
996 char_u *name;
997 char_u *value;
998{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000999 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 val = vim_strsave(value);
1003 if (val != NULL)
1004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 }
1012}
1013
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001014static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001015static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static char_u *redir_endp = NULL;
1017static char_u *redir_varname = NULL;
1018
1019/*
1020 * Start recording command output to a variable
1021 * Returns OK if successfully completed the setup. FAIL otherwise.
1022 */
1023 int
1024var_redir_start(name, append)
1025 char_u *name;
1026 int append; /* append to an existing variable */
1027{
1028 int save_emsg;
1029 int err;
1030 typval_T tv;
1031
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001032 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001033 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 {
1035 EMSG(_(e_invarg));
1036 return FAIL;
1037 }
1038
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 redir_varname = vim_strsave(name);
1041 if (redir_varname == NULL)
1042 return FAIL;
1043
1044 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1045 if (redir_lval == NULL)
1046 {
1047 var_redir_stop();
1048 return FAIL;
1049 }
1050
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051 /* The output is stored in growarray "redir_ga" until redirection ends. */
1052 ga_init2(&redir_ga, (int)sizeof(char), 500);
1053
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1056 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1058 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001059 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 if (redir_endp != NULL && *redir_endp != NUL)
1061 /* Trailing characters are present after the variable name */
1062 EMSG(_(e_trailing));
1063 else
1064 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001065 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 var_redir_stop();
1067 return FAIL;
1068 }
1069
1070 /* check if we can write to the variable: set it to or append an empty
1071 * string */
1072 save_emsg = did_emsg;
1073 did_emsg = FALSE;
1074 tv.v_type = VAR_STRING;
1075 tv.vval.v_string = (char_u *)"";
1076 if (append)
1077 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1078 else
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001080 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001082 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 if (err)
1084 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001085 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 var_redir_stop();
1087 return FAIL;
1088 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089
1090 return OK;
1091}
1092
1093/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094 * Append "value[value_len]" to the variable set by var_redir_start().
1095 * The actual appending is postponed until redirection ends, because the value
1096 * appended may in fact be the string we write to, changing it may cause freed
1097 * memory to be used:
1098 * :redir => foo
1099 * :let foo
1100 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 */
1102 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001107 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108
1109 if (redir_lval == NULL)
1110 return;
1111
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 {
1119 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 }
1122 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124}
1125
1126/*
1127 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 */
1130 void
1131var_redir_stop()
1132{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 typval_T tv;
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_lval != NULL)
1136 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 /* If there was no error: assign the text to the variable. */
1138 if (redir_endp != NULL)
1139 {
1140 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1141 tv.v_type = VAR_STRING;
1142 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001143 /* Call get_lval() again, if it's inside a Dict or List it may
1144 * have changed. */
1145 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1146 FALSE, FALSE, FALSE, FNE_CHECK_START);
1147 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1148 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1149 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* free the collected output */
1153 vim_free(redir_ga.ga_data);
1154 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 vim_free(redir_lval);
1157 redir_lval = NULL;
1158 }
1159 vim_free(redir_varname);
1160 redir_varname = NULL;
1161}
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163# if defined(FEAT_MBYTE) || defined(PROTO)
1164 int
1165eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1166 char_u *enc_from;
1167 char_u *enc_to;
1168 char_u *fname_from;
1169 char_u *fname_to;
1170{
1171 int err = FALSE;
1172
1173 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1174 set_vim_var_string(VV_CC_TO, enc_to, -1);
1175 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1176 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1177 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1178 err = TRUE;
1179 set_vim_var_string(VV_CC_FROM, NULL, -1);
1180 set_vim_var_string(VV_CC_TO, NULL, -1);
1181 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1182 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1183
1184 if (err)
1185 return FAIL;
1186 return OK;
1187}
1188# endif
1189
1190# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1191 int
1192eval_printexpr(fname, args)
1193 char_u *fname;
1194 char_u *args;
1195{
1196 int err = FALSE;
1197
1198 set_vim_var_string(VV_FNAME_IN, fname, -1);
1199 set_vim_var_string(VV_CMDARG, args, -1);
1200 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1201 err = TRUE;
1202 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1203 set_vim_var_string(VV_CMDARG, NULL, -1);
1204
1205 if (err)
1206 {
1207 mch_remove(fname);
1208 return FAIL;
1209 }
1210 return OK;
1211}
1212# endif
1213
1214# if defined(FEAT_DIFF) || defined(PROTO)
1215 void
1216eval_diff(origfile, newfile, outfile)
1217 char_u *origfile;
1218 char_u *newfile;
1219 char_u *outfile;
1220{
1221 int err = FALSE;
1222
1223 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1224 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1225 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1226 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1227 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1228 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1229 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1230}
1231
1232 void
1233eval_patch(origfile, difffile, outfile)
1234 char_u *origfile;
1235 char_u *difffile;
1236 char_u *outfile;
1237{
1238 int err;
1239
1240 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1241 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1242 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1243 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1244 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1245 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1246 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1247}
1248# endif
1249
1250/*
1251 * Top level evaluation function, returning a boolean.
1252 * Sets "error" to TRUE if there was an error.
1253 * Return TRUE or FALSE.
1254 */
1255 int
1256eval_to_bool(arg, error, nextcmd, skip)
1257 char_u *arg;
1258 int *error;
1259 char_u **nextcmd;
1260 int skip; /* only parse, don't execute */
1261{
Bram Moolenaar33570922005-01-25 22:26:29 +00001262 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 int retval = FALSE;
1264
1265 if (skip)
1266 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 else
1270 {
1271 *error = FALSE;
1272 if (!skip)
1273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001274 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001275 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277 }
1278 if (skip)
1279 --emsg_skip;
1280
1281 return retval;
1282}
1283
1284/*
1285 * Top level evaluation function, returning a string. If "skip" is TRUE,
1286 * only parsing to "nextcmd" is done, without reporting errors. Return
1287 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1288 */
1289 char_u *
1290eval_to_string_skip(arg, nextcmd, skip)
1291 char_u *arg;
1292 char_u **nextcmd;
1293 int skip; /* only parse, don't execute */
1294{
Bram Moolenaar33570922005-01-25 22:26:29 +00001295 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 char_u *retval;
1297
1298 if (skip)
1299 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 retval = NULL;
1302 else
1303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 retval = vim_strsave(get_tv_string(&tv));
1305 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 if (skip)
1308 --emsg_skip;
1309
1310 return retval;
1311}
1312
1313/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001314 * Skip over an expression at "*pp".
1315 * Return FAIL for an error, OK otherwise.
1316 */
1317 int
1318skip_expr(pp)
1319 char_u **pp;
1320{
Bram Moolenaar33570922005-01-25 22:26:29 +00001321 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001322
1323 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001324 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325}
1326
1327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001329 * When "convert" is TRUE convert a List into a sequence of lines and convert
1330 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * Return pointer to allocated memory, or NULL for failure.
1332 */
1333 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 char_u *arg;
1336 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001337 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
Bram Moolenaar33570922005-01-25 22:26:29 +00001339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001341 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001342#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 retval = NULL;
1348 else
1349 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001351 {
1352 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001353 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001354 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001356 if (tv.vval.v_list->lv_len > 0)
1357 ga_append(&ga, NL);
1358 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001359 ga_append(&ga, NUL);
1360 retval = (char_u *)ga.ga_data;
1361 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362#ifdef FEAT_FLOAT
1363 else if (convert && tv.v_type == VAR_FLOAT)
1364 {
1365 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1366 retval = vim_strsave(numbuf);
1367 }
1368#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 else
1370 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373
1374 return retval;
1375}
1376
1377/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378 * Call eval_to_string() without using current local variables and using
1379 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 */
1381 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 char_u *arg;
1384 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 char_u *retval;
1388 void *save_funccalp;
1389
1390 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 if (use_sandbox)
1392 ++sandbox;
1393 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001395 if (use_sandbox)
1396 --sandbox;
1397 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 restore_funccal(save_funccalp);
1399 return retval;
1400}
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402/*
1403 * Top level evaluation function, returning a number.
1404 * Evaluates "expr" silently.
1405 * Returns -1 for an error.
1406 */
1407 int
1408eval_to_number(expr)
1409 char_u *expr;
1410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001413 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
1415 ++emsg_off;
1416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 retval = -1;
1419 else
1420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001421 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 --emsg_off;
1425
1426 return retval;
1427}
1428
Bram Moolenaara40058a2005-07-11 22:42:07 +00001429/*
1430 * Prepare v: variable "idx" to be used.
1431 * Save the current typeval in "save_tv".
1432 * When not used yet add the variable to the v: hashtable.
1433 */
1434 static void
1435prepare_vimvar(idx, save_tv)
1436 int idx;
1437 typval_T *save_tv;
1438{
1439 *save_tv = vimvars[idx].vv_tv;
1440 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1441 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1442}
1443
1444/*
1445 * Restore v: variable "idx" to typeval "save_tv".
1446 * When no longer defined, remove the variable from the v: hashtable.
1447 */
1448 static void
1449restore_vimvar(idx, save_tv)
1450 int idx;
1451 typval_T *save_tv;
1452{
1453 hashitem_T *hi;
1454
Bram Moolenaara40058a2005-07-11 22:42:07 +00001455 vimvars[idx].vv_tv = *save_tv;
1456 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1457 {
1458 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1459 if (HASHITEM_EMPTY(hi))
1460 EMSG2(_(e_intern2), "restore_vimvar()");
1461 else
1462 hash_remove(&vimvarht, hi);
1463 }
1464}
1465
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001466#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001467/*
1468 * Evaluate an expression to a list with suggestions.
1469 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001470 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001471 */
1472 list_T *
1473eval_spell_expr(badword, expr)
1474 char_u *badword;
1475 char_u *expr;
1476{
1477 typval_T save_val;
1478 typval_T rettv;
1479 list_T *list = NULL;
1480 char_u *p = skipwhite(expr);
1481
1482 /* Set "v:val" to the bad word. */
1483 prepare_vimvar(VV_VAL, &save_val);
1484 vimvars[VV_VAL].vv_type = VAR_STRING;
1485 vimvars[VV_VAL].vv_str = badword;
1486 if (p_verbose == 0)
1487 ++emsg_off;
1488
1489 if (eval1(&p, &rettv, TRUE) == OK)
1490 {
1491 if (rettv.v_type != VAR_LIST)
1492 clear_tv(&rettv);
1493 else
1494 list = rettv.vval.v_list;
1495 }
1496
1497 if (p_verbose == 0)
1498 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001499 restore_vimvar(VV_VAL, &save_val);
1500
1501 return list;
1502}
1503
1504/*
1505 * "list" is supposed to contain two items: a word and a number. Return the
1506 * word in "pp" and the number as the return value.
1507 * Return -1 if anything isn't right.
1508 * Used to get the good word and score from the eval_spell_expr() result.
1509 */
1510 int
1511get_spellword(list, pp)
1512 list_T *list;
1513 char_u **pp;
1514{
1515 listitem_T *li;
1516
1517 li = list->lv_first;
1518 if (li == NULL)
1519 return -1;
1520 *pp = get_tv_string(&li->li_tv);
1521
1522 li = li->li_next;
1523 if (li == NULL)
1524 return -1;
1525 return get_tv_number(&li->li_tv);
1526}
1527#endif
1528
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001529/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001530 * Top level evaluation function.
1531 * Returns an allocated typval_T with the result.
1532 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001533 */
1534 typval_T *
1535eval_expr(arg, nextcmd)
1536 char_u *arg;
1537 char_u **nextcmd;
1538{
1539 typval_T *tv;
1540
1541 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 {
1544 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 }
1547
1548 return tv;
1549}
1550
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554 * Uses argv[argc] for the function arguments. Only Number and String
1555 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001558 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001559call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 char_u *func;
1561 int argc;
1562 char_u **argv;
1563 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001564 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566{
Bram Moolenaar33570922005-01-25 22:26:29 +00001567 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 long n;
1569 int len;
1570 int i;
1571 int doesrange;
1572 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001575 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579 for (i = 0; i < argc; i++)
1580 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001581 /* Pass a NULL or empty argument as an empty string */
1582 if (argv[i] == NULL || *argv[i] == NUL)
1583 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001584 argvars[i].v_type = VAR_STRING;
1585 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001586 continue;
1587 }
1588
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001589 if (str_arg_only)
1590 len = 0;
1591 else
1592 /* Recognize a number argument, the others must be strings. */
1593 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (len != 0 && len == (int)STRLEN(argv[i]))
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_NUMBER;
1597 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_STRING;
1602 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 }
1605
1606 if (safe)
1607 {
1608 save_funccalp = save_funccal();
1609 ++sandbox;
1610 }
1611
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1613 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (safe)
1617 {
1618 --sandbox;
1619 restore_funccal(save_funccalp);
1620 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 vim_free(argvars);
1622
1623 if (ret == FAIL)
1624 clear_tv(rettv);
1625
1626 return ret;
1627}
1628
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001629/*
1630 * Call vimL function "func" and return the result as a number.
1631 * Returns -1 when calling the function fails.
1632 * Uses argv[argc] for the function arguments.
1633 */
1634 long
1635call_func_retnr(func, argc, argv, safe)
1636 char_u *func;
1637 int argc;
1638 char_u **argv;
1639 int safe; /* use the sandbox */
1640{
1641 typval_T rettv;
1642 long retval;
1643
1644 /* All arguments are passed as strings, no conversion to number. */
1645 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1646 return -1;
1647
1648 retval = get_tv_number_chk(&rettv, NULL);
1649 clear_tv(&rettv);
1650 return retval;
1651}
1652
1653#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1654 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1655
Bram Moolenaar4f688582007-07-24 12:34:30 +00001656# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001658 * Call vimL function "func" and return the result as a string.
1659 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660 * Uses argv[argc] for the function arguments.
1661 */
1662 void *
1663call_func_retstr(func, argc, argv, safe)
1664 char_u *func;
1665 int argc;
1666 char_u **argv;
1667 int safe; /* use the sandbox */
1668{
1669 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001670 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001672 /* All arguments are passed as strings, no conversion to number. */
1673 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 return NULL;
1675
1676 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 return retval;
1679}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001680# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001683 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001685 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 */
1687 void *
1688call_func_retlist(func, argc, argv, safe)
1689 char_u *func;
1690 int argc;
1691 char_u **argv;
1692 int safe; /* use the sandbox */
1693{
1694 typval_T rettv;
1695
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001696 /* All arguments are passed as strings, no conversion to number. */
1697 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 return NULL;
1699
1700 if (rettv.v_type != VAR_LIST)
1701 {
1702 clear_tv(&rettv);
1703 return NULL;
1704 }
1705
1706 return rettv.vval.v_list;
1707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709
1710/*
1711 * Save the current function call pointer, and set it to NULL.
1712 * Used when executing autocommands and for ":source".
1713 */
1714 void *
1715save_funccal()
1716{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001717 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 current_funccal = NULL;
1720 return (void *)fc;
1721}
1722
1723 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724restore_funccal(vfc)
1725 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001727 funccall_T *fc = (funccall_T *)vfc;
1728
1729 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730}
1731
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732#if defined(FEAT_PROFILE) || defined(PROTO)
1733/*
1734 * Prepare profiling for entering a child or something else that is not
1735 * counted for the script/function itself.
1736 * Should always be called in pair with prof_child_exit().
1737 */
1738 void
1739prof_child_enter(tm)
1740 proftime_T *tm; /* place to store waittime */
1741{
1742 funccall_T *fc = current_funccal;
1743
1744 if (fc != NULL && fc->func->uf_profiling)
1745 profile_start(&fc->prof_child);
1746 script_prof_save(tm);
1747}
1748
1749/*
1750 * Take care of time spent in a child.
1751 * Should always be called after prof_child_enter().
1752 */
1753 void
1754prof_child_exit(tm)
1755 proftime_T *tm; /* where waittime was stored */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 {
1761 profile_end(&fc->prof_child);
1762 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1763 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1764 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1765 }
1766 script_prof_restore(tm);
1767}
1768#endif
1769
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#ifdef FEAT_FOLDING
1772/*
1773 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1774 * it in "*cp". Doesn't give error messages.
1775 */
1776 int
1777eval_foldexpr(arg, cp)
1778 char_u *arg;
1779 int *cp;
1780{
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 int retval;
1783 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001784 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1785 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001788 if (use_sandbox)
1789 ++sandbox;
1790 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 retval = 0;
1794 else
1795 {
1796 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 if (tv.v_type == VAR_NUMBER)
1798 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001799 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 retval = 0;
1801 else
1802 {
1803 /* If the result is a string, check if there is a non-digit before
1804 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (!VIM_ISDIGIT(*s) && *s != '-')
1807 *cp = *s++;
1808 retval = atol((char *)s);
1809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001813 if (use_sandbox)
1814 --sandbox;
1815 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 return retval;
1818}
1819#endif
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 * ":let" list all variable values
1823 * ":let var1 var2" list variable values
1824 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001825 * ":let var += expr" assignment command.
1826 * ":let var -= expr" assignment command.
1827 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 */
1830 void
1831ex_let(eap)
1832 exarg_T *eap;
1833{
1834 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001836 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 int var_count = 0;
1839 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001842 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 argend = skip_var_list(arg, &var_count, &semicolon);
1845 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001847 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1848 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001849 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001852 /*
1853 * ":let" without "=": list variables
1854 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 if (*arg == '[')
1856 EMSG(_(e_invarg));
1857 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 list_glob_vars(&first);
1864 list_buf_vars(&first);
1865 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001866#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001867 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001868#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_script_vars(&first);
1870 list_func_vars(&first);
1871 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 eap->nextcmd = check_nextcmd(arg);
1874 }
1875 else
1876 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 op[0] = '=';
1878 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 {
1881 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1882 op[0] = expr[-1]; /* +=, -= or .= */
1883 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (eap->skip)
1887 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (eap->skip)
1890 {
1891 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 --emsg_skip;
1894 }
1895 else if (i != FAIL)
1896 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901 }
1902}
1903
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904/*
1905 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1906 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 * When "nextchars" is not NULL it points to a string with characters that
1908 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1909 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910 * Returns OK or FAIL;
1911 */
1912 static int
1913ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1914 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 int copy; /* copy values from "tv", don't move */
1917 int semicolon; /* from skip_var_list() */
1918 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920{
1921 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 listitem_T *item;
1925 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926
1927 if (*arg != '[')
1928 {
1929 /*
1930 * ":let var = expr" or ":for var in list"
1931 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 return FAIL;
1934 return OK;
1935 }
1936
1937 /*
1938 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1939 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001940 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 {
1942 EMSG(_(e_listreq));
1943 return FAIL;
1944 }
1945
1946 i = list_len(l);
1947 if (semicolon == 0 && var_count < i)
1948 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001949 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 return FAIL;
1951 }
1952 if (var_count - semicolon > i)
1953 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001954 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 return FAIL;
1956 }
1957
1958 item = l->lv_first;
1959 while (*arg != ']')
1960 {
1961 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 item = item->li_next;
1964 if (arg == NULL)
1965 return FAIL;
1966
1967 arg = skipwhite(arg);
1968 if (*arg == ';')
1969 {
1970 /* Put the rest of the list (may be empty) in the var after ';'.
1971 * Create a new list for this. */
1972 l = list_alloc();
1973 if (l == NULL)
1974 return FAIL;
1975 while (item != NULL)
1976 {
1977 list_append_tv(l, &item->li_tv);
1978 item = item->li_next;
1979 }
1980
1981 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001982 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 ltv.vval.v_list = l;
1984 l->lv_refcount = 1;
1985
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1987 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 clear_tv(&ltv);
1989 if (arg == NULL)
1990 return FAIL;
1991 break;
1992 }
1993 else if (*arg != ',' && *arg != ']')
1994 {
1995 EMSG2(_(e_intern2), "ex_let_vars()");
1996 return FAIL;
1997 }
1998 }
1999
2000 return OK;
2001}
2002
2003/*
2004 * Skip over assignable variable "var" or list of variables "[var, var]".
2005 * Used for ":let varvar = expr" and ":for varvar in expr".
2006 * For "[var, var]" increment "*var_count" for each variable.
2007 * for "[var, var; var]" set "semicolon".
2008 * Return NULL for an error.
2009 */
2010 static char_u *
2011skip_var_list(arg, var_count, semicolon)
2012 char_u *arg;
2013 int *var_count;
2014 int *semicolon;
2015{
2016 char_u *p, *s;
2017
2018 if (*arg == '[')
2019 {
2020 /* "[var, var]": find the matching ']'. */
2021 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002022 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 {
2024 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2025 s = skip_var_one(p);
2026 if (s == p)
2027 {
2028 EMSG2(_(e_invarg2), p);
2029 return NULL;
2030 }
2031 ++*var_count;
2032
2033 p = skipwhite(s);
2034 if (*p == ']')
2035 break;
2036 else if (*p == ';')
2037 {
2038 if (*semicolon == 1)
2039 {
2040 EMSG(_("Double ; in list of variables"));
2041 return NULL;
2042 }
2043 *semicolon = 1;
2044 }
2045 else if (*p != ',')
2046 {
2047 EMSG2(_(e_invarg2), p);
2048 return NULL;
2049 }
2050 }
2051 return p + 1;
2052 }
2053 else
2054 return skip_var_one(arg);
2055}
2056
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002058 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002059 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002060 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 static char_u *
2062skip_var_one(arg)
2063 char_u *arg;
2064{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002065 if (*arg == '@' && arg[1] != NUL)
2066 return arg + 2;
2067 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2068 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002069}
2070
Bram Moolenaara7043832005-01-21 11:56:39 +00002071/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 * List variables for hashtab "ht" with prefix "prefix".
2073 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002076list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002077 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002080 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081{
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 hashitem_T *hi;
2083 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 int todo;
2085
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002086 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2088 {
2089 if (!HASHITEM_EMPTY(hi))
2090 {
2091 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 di = HI2DI(hi);
2093 if (empty || di->di_tv.v_type != VAR_STRING
2094 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 }
2097 }
2098}
2099
2100/*
2101 * List global variables.
2102 */
2103 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104list_glob_vars(first)
2105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002108}
2109
2110/*
2111 * List buffer variables.
2112 */
2113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_buf_vars(first)
2115 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002116{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117 char_u numbuf[NUMBUFLEN];
2118
Bram Moolenaar429fa852013-04-15 12:27:36 +02002119 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121
2122 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2124 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002125}
2126
2127/*
2128 * List window variables.
2129 */
2130 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131list_win_vars(first)
2132 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002133{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002134 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002136}
2137
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002138#ifdef FEAT_WINDOWS
2139/*
2140 * List tab page variables.
2141 */
2142 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002143list_tab_vars(first)
2144 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002146 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002148}
2149#endif
2150
Bram Moolenaara7043832005-01-21 11:56:39 +00002151/*
2152 * List Vim variables.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_vim_vars(first)
2156 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159}
2160
2161/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002162 * List script-local variables, if there is a script.
2163 */
2164 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165list_script_vars(first)
2166 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167{
2168 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2170 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002171}
2172
2173/*
2174 * List function variables, if there is a function.
2175 */
2176 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177list_func_vars(first)
2178 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002179{
2180 if (current_funccal != NULL)
2181 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002183}
2184
2185/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 * List variables in "arg".
2187 */
2188 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 exarg_T *eap;
2191 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193{
2194 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002195 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 char_u *name_start;
2198 char_u *arg_subsc;
2199 char_u *tofree;
2200 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201
2202 while (!ends_excmd(*arg) && !got_int)
2203 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002206 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2208 {
2209 emsg_severe = TRUE;
2210 EMSG(_(e_trailing));
2211 break;
2212 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 /* get_name_len() takes care of expanding curly braces */
2217 name_start = name = arg;
2218 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2219 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 /* This is mainly to keep test 49 working: when expanding
2222 * curly braces fails overrule the exception error message. */
2223 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 emsg_severe = TRUE;
2226 EMSG2(_(e_invarg2), arg);
2227 break;
2228 }
2229 error = TRUE;
2230 }
2231 else
2232 {
2233 if (tofree != NULL)
2234 name = tofree;
2235 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 else
2238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 /* handle d.key, l[idx], f(expr) */
2240 arg_subsc = arg;
2241 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002249 case 'g': list_glob_vars(first); break;
2250 case 'b': list_buf_vars(first); break;
2251 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002252#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002253 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 'v': list_vim_vars(first); break;
2256 case 's': list_script_vars(first); break;
2257 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 default:
2259 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 }
2262 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 {
2264 char_u numbuf[NUMBUFLEN];
2265 char_u *tf;
2266 int c;
2267 char_u *s;
2268
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002269 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 c = *arg;
2271 *arg = NUL;
2272 list_one_var_a((char_u *)"",
2273 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 tv.v_type,
2275 s == NULL ? (char_u *)"" : s,
2276 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 *arg = c;
2278 vim_free(tf);
2279 }
2280 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
2283 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284
2285 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287
2288 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 }
2290
2291 return arg;
2292}
2293
2294/*
2295 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2296 * Returns a pointer to the char just after the var name.
2297 * Returns NULL if there is an error.
2298 */
2299 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002302 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002304 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306{
2307 int c1;
2308 char_u *name;
2309 char_u *p;
2310 char_u *arg_end = NULL;
2311 int len;
2312 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002313 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314
2315 /*
2316 * ":let $VAR = expr": Set environment variable.
2317 */
2318 if (*arg == '$')
2319 {
2320 /* Find the end of the name. */
2321 ++arg;
2322 name = arg;
2323 len = get_env_len(&arg);
2324 if (len == 0)
2325 EMSG2(_(e_invarg2), name - 1);
2326 else
2327 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 if (op != NULL && (*op == '+' || *op == '-'))
2329 EMSG2(_(e_letwrong), op);
2330 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002331 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002333 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 {
2335 c1 = name[len];
2336 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337 p = get_tv_string_chk(tv);
2338 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339 {
2340 int mustfree = FALSE;
2341 char_u *s = vim_getenv(name, &mustfree);
2342
2343 if (s != NULL)
2344 {
2345 p = tofree = concat_str(s, p);
2346 if (mustfree)
2347 vim_free(s);
2348 }
2349 }
2350 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002351 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002352 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 if (STRICMP(name, "HOME") == 0)
2354 init_homedir();
2355 else if (didset_vim && STRICMP(name, "VIM") == 0)
2356 didset_vim = FALSE;
2357 else if (didset_vimruntime
2358 && STRICMP(name, "VIMRUNTIME") == 0)
2359 didset_vimruntime = FALSE;
2360 arg_end = arg;
2361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
2365 }
2366 }
2367
2368 /*
2369 * ":let &option = expr": Set option value.
2370 * ":let &l:option = expr": Set local option value.
2371 * ":let &g:option = expr": Set global option value.
2372 */
2373 else if (*arg == '&')
2374 {
2375 /* Find the end of the name. */
2376 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002377 if (p == NULL || (endchars != NULL
2378 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 EMSG(_(e_letunexp));
2380 else
2381 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 long n;
2383 int opt_type;
2384 long numval;
2385 char_u *stringval = NULL;
2386 char_u *s;
2387
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 c1 = *p;
2389 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390
2391 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 s = get_tv_string_chk(tv); /* != NULL if number or string */
2393 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 {
2395 opt_type = get_option_value(arg, &numval,
2396 &stringval, opt_flags);
2397 if ((opt_type == 1 && *op == '.')
2398 || (opt_type == 0 && *op != '.'))
2399 EMSG2(_(e_letwrong), op);
2400 else
2401 {
2402 if (opt_type == 1) /* number */
2403 {
2404 if (*op == '+')
2405 n = numval + n;
2406 else
2407 n = numval - n;
2408 }
2409 else if (opt_type == 0 && stringval != NULL) /* string */
2410 {
2411 s = concat_str(stringval, s);
2412 vim_free(stringval);
2413 stringval = s;
2414 }
2415 }
2416 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 if (s != NULL)
2418 {
2419 set_option_value(arg, n, s, opt_flags);
2420 arg_end = p;
2421 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 }
2425 }
2426
2427 /*
2428 * ":let @r = expr": Set register contents.
2429 */
2430 else if (*arg == '@')
2431 {
2432 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 if (op != NULL && (*op == '+' || *op == '-'))
2434 EMSG2(_(e_letwrong), op);
2435 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002436 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 EMSG(_(e_letunexp));
2438 else
2439 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002440 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 char_u *s;
2442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 p = get_tv_string_chk(tv);
2444 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002446 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 if (s != NULL)
2448 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 vim_free(s);
2451 }
2452 }
2453 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 arg_end = arg + 1;
2457 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459 }
2460 }
2461
2462 /*
2463 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002466 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002468 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002470 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2474 EMSG(_(e_letunexp));
2475 else
2476 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 arg_end = p;
2479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
2483
2484 else
2485 EMSG2(_(e_invarg2), arg);
2486
2487 return arg_end;
2488}
2489
2490/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2492 */
2493 static int
2494check_changedtick(arg)
2495 char_u *arg;
2496{
2497 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2498 {
2499 EMSG2(_(e_readonlyvar), arg);
2500 return TRUE;
2501 }
2502 return FALSE;
2503}
2504
2505/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 * Get an lval: variable, Dict item or List item that can be assigned a value
2507 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2508 * "name.key", "name.key[expr]" etc.
2509 * Indexing only works if "name" is an existing List or Dictionary.
2510 * "name" points to the start of the name.
2511 * If "rettv" is not NULL it points to the value to be assigned.
2512 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2513 * wrong; must end in space or cmd separator.
2514 *
2515 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002516 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 */
2519 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002520get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 typval_T *rettv;
2523 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 int unlet;
2525 int skip;
2526 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002527 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 char_u *expr_start, *expr_end;
2531 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 dictitem_T *v;
2533 typval_T var1;
2534 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002537 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543
2544 if (skip)
2545 {
2546 /* When skipping just find the end of the name. */
2547 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002548 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 }
2550
2551 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002552 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 if (expr_start != NULL)
2554 {
2555 /* Don't expand the name when we already know there is an error. */
2556 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2557 && *p != '[' && *p != '.')
2558 {
2559 EMSG(_(e_trailing));
2560 return NULL;
2561 }
2562
2563 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2564 if (lp->ll_exp_name == NULL)
2565 {
2566 /* Report an invalid expression in braces, unless the
2567 * expression evaluation has been cancelled due to an
2568 * aborting error, an interrupt, or an exception. */
2569 if (!aborting() && !quiet)
2570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002571 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 EMSG2(_(e_invarg2), name);
2573 return NULL;
2574 }
2575 }
2576 lp->ll_name = lp->ll_exp_name;
2577 }
2578 else
2579 lp->ll_name = name;
2580
2581 /* Without [idx] or .key we are done. */
2582 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2583 return p;
2584
2585 cc = *p;
2586 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002587 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (v == NULL && !quiet)
2589 EMSG2(_(e_undefvar), lp->ll_name);
2590 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 if (v == NULL)
2592 return NULL;
2593
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 /*
2595 * Loop until no more [idx] or .key is following.
2596 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2601 && !(lp->ll_tv->v_type == VAR_DICT
2602 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (!quiet)
2605 EMSG(_("E689: Can only index a List or Dictionary"));
2606 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002607 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (!quiet)
2611 EMSG(_("E708: [:] must come last"));
2612 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614
Bram Moolenaar8c711452005-01-14 21:53:12 +00002615 len = -1;
2616 if (*p == '.')
2617 {
2618 key = p + 1;
2619 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2620 ;
2621 if (len == 0)
2622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!quiet)
2624 EMSG(_(e_emptykey));
2625 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 }
2627 p = key + len;
2628 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 else
2630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 if (*p == ':')
2634 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635 else
2636 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 empty1 = FALSE;
2638 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002640 if (get_tv_string_chk(&var1) == NULL)
2641 {
2642 /* not a number or string */
2643 clear_tv(&var1);
2644 return NULL;
2645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 }
2647
2648 /* Optionally get the second index [ :expr]. */
2649 if (*p == ':')
2650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002654 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 if (!empty1)
2656 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (rettv != NULL && (rettv->v_type != VAR_LIST
2660 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!quiet)
2663 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 if (!empty1)
2665 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 }
2668 p = skipwhite(p + 1);
2669 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 else
2672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (!empty1)
2677 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002680 if (get_tv_string_chk(&var2) == NULL)
2681 {
2682 /* not a number or string */
2683 if (!empty1)
2684 clear_tv(&var1);
2685 clear_tv(&var2);
2686 return NULL;
2687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
2697 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 if (!empty1)
2699 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704
2705 /* Skip to past ']'. */
2706 ++p;
2707 }
2708
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 {
2711 if (len == -1)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002714 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (*key == NUL)
2716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
2718 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
2722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 lp->ll_list = NULL;
2724 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002725 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002726
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002727 /* When assigning to a scope dictionary check that a function and
2728 * variable name is valid (only variable name unless it is l: or
2729 * g: dictionary). Disallow overwriting a builtin function. */
2730 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002731 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002732 int prevval;
2733 int wrong;
2734
2735 if (len != -1)
2736 {
2737 prevval = key[len];
2738 key[len] = NUL;
2739 }
2740 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2741 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002742 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002743 || !valid_varname(key);
2744 if (len != -1)
2745 key[len] = prevval;
2746 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002747 return NULL;
2748 }
2749
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002752 /* Can't add "v:" variable. */
2753 if (lp->ll_dict == &vimvardict)
2754 {
2755 EMSG2(_(e_illvar), name);
2756 return NULL;
2757 }
2758
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002759 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002763 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 if (len == -1)
2765 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 }
2768 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (len == -1)
2773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 p = NULL;
2776 break;
2777 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 /* existing variable, need to check if it can be changed */
2779 else if (var_check_ro(lp->ll_di->di_flags, name))
2780 return NULL;
2781
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 if (len == -1)
2783 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 }
2786 else
2787 {
2788 /*
2789 * Get the number and item for the only or first index of the List.
2790 */
2791 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 else
2794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002795 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 clear_tv(&var1);
2797 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002798 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_list = lp->ll_tv->vval.v_list;
2800 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2801 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002803 if (lp->ll_n1 < 0)
2804 {
2805 lp->ll_n1 = 0;
2806 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2807 }
2808 }
2809 if (lp->ll_li == NULL)
2810 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002813 if (!quiet)
2814 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 }
2817
2818 /*
2819 * May need to find the item or absolute index for the second
2820 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 * When no index given: "lp->ll_empty2" is TRUE.
2822 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002826 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002832 {
2833 if (!quiet)
2834 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002836 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2841 if (lp->ll_n1 < 0)
2842 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2843 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002844 {
2845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002849 }
2850
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 return p;
2856}
2857
2858/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002859 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 */
2861 static void
2862clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864{
2865 vim_free(lp->ll_exp_name);
2866 vim_free(lp->ll_newkey);
2867}
2868
2869/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002870 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002872 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 */
2874 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002875set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881{
2882 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 listitem_T *ri;
2884 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885
2886 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 cc = *endp;
2891 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002892 if (op != NULL && *op != '=')
2893 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002896 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002897 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002898 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002899 {
2900 if (tv_op(&tv, rettv, op) == OK)
2901 set_var(lp->ll_name, &tv, FALSE);
2902 clear_tv(&tv);
2903 }
2904 }
2905 else
2906 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002908 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002910 else if (tv_check_lock(lp->ll_newkey == NULL
2911 ? lp->ll_tv->v_lock
2912 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2913 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 else if (lp->ll_range)
2915 {
2916 /*
2917 * Assign the List values to the list items.
2918 */
2919 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002920 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921 if (op != NULL && *op != '=')
2922 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2923 else
2924 {
2925 clear_tv(&lp->ll_li->li_tv);
2926 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2927 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 ri = ri->li_next;
2929 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2930 break;
2931 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002934 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 ri = NULL;
2937 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002938 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 lp->ll_li = lp->ll_li->li_next;
2941 ++lp->ll_n1;
2942 }
2943 if (ri != NULL)
2944 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 else if (lp->ll_empty2
2946 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 : lp->ll_n1 != lp->ll_n2)
2948 EMSG(_("E711: List value has not enough items"));
2949 }
2950 else
2951 {
2952 /*
2953 * Assign to a List or Dictionary item.
2954 */
2955 if (lp->ll_newkey != NULL)
2956 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 if (op != NULL && *op != '=')
2958 {
2959 EMSG2(_(e_letwrong), op);
2960 return;
2961 }
2962
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002964 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 if (di == NULL)
2966 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002967 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2968 {
2969 vim_free(di);
2970 return;
2971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002973 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002974 else if (op != NULL && *op != '=')
2975 {
2976 tv_op(lp->ll_tv, rettv, op);
2977 return;
2978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 /*
2983 * Assign the value to the variable or list item.
2984 */
2985 if (copy)
2986 copy_tv(rettv, lp->ll_tv);
2987 else
2988 {
2989 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002990 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002992 }
2993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994}
2995
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002996/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2998 * Returns OK or FAIL.
2999 */
3000 static int
3001tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 typval_T *tv1;
3003 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004 char_u *op;
3005{
3006 long n;
3007 char_u numbuf[NUMBUFLEN];
3008 char_u *s;
3009
3010 /* Can't do anything with a Funcref or a Dict on the right. */
3011 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3012 {
3013 switch (tv1->v_type)
3014 {
3015 case VAR_DICT:
3016 case VAR_FUNC:
3017 break;
3018
3019 case VAR_LIST:
3020 if (*op != '+' || tv2->v_type != VAR_LIST)
3021 break;
3022 /* List += List */
3023 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3024 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3025 return OK;
3026
3027 case VAR_NUMBER:
3028 case VAR_STRING:
3029 if (tv2->v_type == VAR_LIST)
3030 break;
3031 if (*op == '+' || *op == '-')
3032 {
3033 /* nr += nr or nr -= nr*/
3034 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003035#ifdef FEAT_FLOAT
3036 if (tv2->v_type == VAR_FLOAT)
3037 {
3038 float_T f = n;
3039
3040 if (*op == '+')
3041 f += tv2->vval.v_float;
3042 else
3043 f -= tv2->vval.v_float;
3044 clear_tv(tv1);
3045 tv1->v_type = VAR_FLOAT;
3046 tv1->vval.v_float = f;
3047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003049#endif
3050 {
3051 if (*op == '+')
3052 n += get_tv_number(tv2);
3053 else
3054 n -= get_tv_number(tv2);
3055 clear_tv(tv1);
3056 tv1->v_type = VAR_NUMBER;
3057 tv1->vval.v_number = n;
3058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 }
3060 else
3061 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003062 if (tv2->v_type == VAR_FLOAT)
3063 break;
3064
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003065 /* str .= str */
3066 s = get_tv_string(tv1);
3067 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3068 clear_tv(tv1);
3069 tv1->v_type = VAR_STRING;
3070 tv1->vval.v_string = s;
3071 }
3072 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073
3074#ifdef FEAT_FLOAT
3075 case VAR_FLOAT:
3076 {
3077 float_T f;
3078
3079 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3080 && tv2->v_type != VAR_NUMBER
3081 && tv2->v_type != VAR_STRING))
3082 break;
3083 if (tv2->v_type == VAR_FLOAT)
3084 f = tv2->vval.v_float;
3085 else
3086 f = get_tv_number(tv2);
3087 if (*op == '+')
3088 tv1->vval.v_float += f;
3089 else
3090 tv1->vval.v_float -= f;
3091 }
3092 return OK;
3093#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094 }
3095 }
3096
3097 EMSG2(_(e_letwrong), op);
3098 return FAIL;
3099}
3100
3101/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003102 * Add a watcher to a list.
3103 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003104 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003105list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003106 list_T *l;
3107 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108{
3109 lw->lw_next = l->lv_watch;
3110 l->lv_watch = lw;
3111}
3112
3113/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003114 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115 * No warning when it isn't found...
3116 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003117 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 list_T *l;
3120 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121{
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123
3124 lwp = &l->lv_watch;
3125 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3126 {
3127 if (lw == lwrem)
3128 {
3129 *lwp = lw->lw_next;
3130 break;
3131 }
3132 lwp = &lw->lw_next;
3133 }
3134}
3135
3136/*
3137 * Just before removing an item from a list: advance watchers to the next
3138 * item.
3139 */
3140 static void
3141list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 list_T *l;
3143 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003144{
Bram Moolenaar33570922005-01-25 22:26:29 +00003145 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146
3147 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3148 if (lw->lw_item == item)
3149 lw->lw_item = item->li_next;
3150}
3151
3152/*
3153 * Evaluate the expression used in a ":for var in expr" command.
3154 * "arg" points to "var".
3155 * Set "*errp" to TRUE for an error, FALSE otherwise;
3156 * Return a pointer that holds the info. Null when there is an error.
3157 */
3158 void *
3159eval_for_line(arg, errp, nextcmdp, skip)
3160 char_u *arg;
3161 int *errp;
3162 char_u **nextcmdp;
3163 int skip;
3164{
Bram Moolenaar33570922005-01-25 22:26:29 +00003165 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 typval_T tv;
3168 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169
3170 *errp = TRUE; /* default: there is an error */
3171
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 if (fi == NULL)
3174 return NULL;
3175
3176 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3177 if (expr == NULL)
3178 return fi;
3179
3180 expr = skipwhite(expr);
3181 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3182 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003183 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 return fi;
3185 }
3186
3187 if (skip)
3188 ++emsg_skip;
3189 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3190 {
3191 *errp = FALSE;
3192 if (!skip)
3193 {
3194 l = tv.vval.v_list;
3195 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003196 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003198 clear_tv(&tv);
3199 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 else
3201 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003202 /* No need to increment the refcount, it's already set for the
3203 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 fi->fi_list = l;
3205 list_add_watch(l, &fi->fi_lw);
3206 fi->fi_lw.lw_item = l->lv_first;
3207 }
3208 }
3209 }
3210 if (skip)
3211 --emsg_skip;
3212
3213 return fi;
3214}
3215
3216/*
3217 * Use the first item in a ":for" list. Advance to the next.
3218 * Assign the values to the variable (list). "arg" points to the first one.
3219 * Return TRUE when a valid item was found, FALSE when at end of list or
3220 * something wrong.
3221 */
3222 int
3223next_for_item(fi_void, arg)
3224 void *fi_void;
3225 char_u *arg;
3226{
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003229 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230
3231 item = fi->fi_lw.lw_item;
3232 if (item == NULL)
3233 result = FALSE;
3234 else
3235 {
3236 fi->fi_lw.lw_item = item->li_next;
3237 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3238 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3239 }
3240 return result;
3241}
3242
3243/*
3244 * Free the structure used to store info used by ":for".
3245 */
3246 void
3247free_for_info(fi_void)
3248 void *fi_void;
3249{
Bram Moolenaar33570922005-01-25 22:26:29 +00003250 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003252 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003253 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003255 list_unref(fi->fi_list);
3256 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 vim_free(fi);
3258}
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3261
3262 void
3263set_context_for_expression(xp, arg, cmdidx)
3264 expand_T *xp;
3265 char_u *arg;
3266 cmdidx_T cmdidx;
3267{
3268 int got_eq = FALSE;
3269 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 if (cmdidx == CMD_let)
3273 {
3274 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003275 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 {
3277 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003278 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 {
3280 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 if (vim_iswhite(*p))
3283 break;
3284 }
3285 return;
3286 }
3287 }
3288 else
3289 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3290 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 while ((xp->xp_pattern = vim_strpbrk(arg,
3292 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3293 {
3294 c = *xp->xp_pattern;
3295 if (c == '&')
3296 {
3297 c = xp->xp_pattern[1];
3298 if (c == '&')
3299 {
3300 ++xp->xp_pattern;
3301 xp->xp_context = cmdidx != CMD_let || got_eq
3302 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3303 }
3304 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003307 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3308 xp->xp_pattern += 2;
3309
3310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312 else if (c == '$')
3313 {
3314 /* environment variable */
3315 xp->xp_context = EXPAND_ENV_VARS;
3316 }
3317 else if (c == '=')
3318 {
3319 got_eq = TRUE;
3320 xp->xp_context = EXPAND_EXPRESSION;
3321 }
3322 else if (c == '<'
3323 && xp->xp_context == EXPAND_FUNCTIONS
3324 && vim_strchr(xp->xp_pattern, '(') == NULL)
3325 {
3326 /* Function name can start with "<SNR>" */
3327 break;
3328 }
3329 else if (cmdidx != CMD_let || got_eq)
3330 {
3331 if (c == '"') /* string */
3332 {
3333 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3334 if (c == '\\' && xp->xp_pattern[1] != NUL)
3335 ++xp->xp_pattern;
3336 xp->xp_context = EXPAND_NOTHING;
3337 }
3338 else if (c == '\'') /* literal string */
3339 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003340 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3342 /* skip */ ;
3343 xp->xp_context = EXPAND_NOTHING;
3344 }
3345 else if (c == '|')
3346 {
3347 if (xp->xp_pattern[1] == '|')
3348 {
3349 ++xp->xp_pattern;
3350 xp->xp_context = EXPAND_EXPRESSION;
3351 }
3352 else
3353 xp->xp_context = EXPAND_COMMANDS;
3354 }
3355 else
3356 xp->xp_context = EXPAND_EXPRESSION;
3357 }
3358 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 /* Doesn't look like something valid, expand as an expression
3360 * anyway. */
3361 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 arg = xp->xp_pattern;
3363 if (*arg != NUL)
3364 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3365 /* skip */ ;
3366 }
3367 xp->xp_pattern = arg;
3368}
3369
3370#endif /* FEAT_CMDL_COMPL */
3371
3372/*
3373 * ":1,25call func(arg1, arg2)" function call.
3374 */
3375 void
3376ex_call(eap)
3377 exarg_T *eap;
3378{
3379 char_u *arg = eap->arg;
3380 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003382 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003384 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 linenr_T lnum;
3386 int doesrange;
3387 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003390 if (eap->skip)
3391 {
3392 /* trans_function_name() doesn't work well when skipping, use eval0()
3393 * instead to skip to any following command, e.g. for:
3394 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003395 ++emsg_skip;
3396 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3397 clear_tv(&rettv);
3398 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003399 return;
3400 }
3401
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003402 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003403 if (fudi.fd_newkey != NULL)
3404 {
3405 /* Still need to give an error message for missing key. */
3406 EMSG2(_(e_dictkey), fudi.fd_newkey);
3407 vim_free(fudi.fd_newkey);
3408 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003409 if (tofree == NULL)
3410 return;
3411
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003412 /* Increase refcount on dictionary, it could get deleted when evaluating
3413 * the arguments. */
3414 if (fudi.fd_dict != NULL)
3415 ++fudi.fd_dict->dv_refcount;
3416
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003417 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003418 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003419 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar532c7802005-01-27 14:44:31 +00003421 /* Skip white space to allow ":call func ()". Not good, but required for
3422 * backward compatibility. */
3423 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003424 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 if (*startarg != '(')
3427 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003428 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 goto end;
3430 }
3431
3432 /*
3433 * When skipping, evaluate the function once, to find the end of the
3434 * arguments.
3435 * When the function takes a range, this is discovered after the first
3436 * call, and the loop is broken.
3437 */
3438 if (eap->skip)
3439 {
3440 ++emsg_skip;
3441 lnum = eap->line2; /* do it once, also with an invalid range */
3442 }
3443 else
3444 lnum = eap->line1;
3445 for ( ; lnum <= eap->line2; ++lnum)
3446 {
3447 if (!eap->skip && eap->addr_count > 0)
3448 {
3449 curwin->w_cursor.lnum = lnum;
3450 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003451#ifdef FEAT_VIRTUALEDIT
3452 curwin->w_cursor.coladd = 0;
3453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
3455 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003456 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003457 eap->line1, eap->line2, &doesrange,
3458 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
3460 failed = TRUE;
3461 break;
3462 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003463
3464 /* Handle a function returning a Funcref, Dictionary or List. */
3465 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3466 {
3467 failed = TRUE;
3468 break;
3469 }
3470
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (doesrange || eap->skip)
3473 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003476 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003477 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003478 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (aborting())
3480 break;
3481 }
3482 if (eap->skip)
3483 --emsg_skip;
3484
3485 if (!failed)
3486 {
3487 /* Check for trailing illegal characters and a following command. */
3488 if (!ends_excmd(*arg))
3489 {
3490 emsg_severe = TRUE;
3491 EMSG(_(e_trailing));
3492 }
3493 else
3494 eap->nextcmd = check_nextcmd(arg);
3495 }
3496
3497end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003498 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003499 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500}
3501
3502/*
3503 * ":unlet[!] var1 ... " command.
3504 */
3505 void
3506ex_unlet(eap)
3507 exarg_T *eap;
3508{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003509 ex_unletlock(eap, eap->arg, 0);
3510}
3511
3512/*
3513 * ":lockvar" and ":unlockvar" commands
3514 */
3515 void
3516ex_lockvar(eap)
3517 exarg_T *eap;
3518{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003520 int deep = 2;
3521
3522 if (eap->forceit)
3523 deep = -1;
3524 else if (vim_isdigit(*arg))
3525 {
3526 deep = getdigits(&arg);
3527 arg = skipwhite(arg);
3528 }
3529
3530 ex_unletlock(eap, arg, deep);
3531}
3532
3533/*
3534 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3535 */
3536 static void
3537ex_unletlock(eap, argstart, deep)
3538 exarg_T *eap;
3539 char_u *argstart;
3540 int deep;
3541{
3542 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003545 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 do
3548 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003549 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003550 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3551 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552 if (lv.ll_name == NULL)
3553 error = TRUE; /* error but continue parsing */
3554 if (name_end == NULL || (!vim_iswhite(*name_end)
3555 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003557 if (name_end != NULL)
3558 {
3559 emsg_severe = TRUE;
3560 EMSG(_(e_trailing));
3561 }
3562 if (!(eap->skip || error))
3563 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 break;
3565 }
3566
3567 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568 {
3569 if (eap->cmdidx == CMD_unlet)
3570 {
3571 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3572 error = TRUE;
3573 }
3574 else
3575 {
3576 if (do_lock_var(&lv, name_end, deep,
3577 eap->cmdidx == CMD_lockvar) == FAIL)
3578 error = TRUE;
3579 }
3580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 if (!eap->skip)
3583 clear_lval(&lv);
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 arg = skipwhite(name_end);
3586 } while (!ends_excmd(*arg));
3587
3588 eap->nextcmd = check_nextcmd(arg);
3589}
3590
Bram Moolenaar8c711452005-01-14 21:53:12 +00003591 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003592do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003594 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 int forceit;
3596{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 int ret = OK;
3598 int cc;
3599
3600 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003602 cc = *name_end;
3603 *name_end = NUL;
3604
3605 /* Normal name or expanded name. */
3606 if (check_changedtick(lp->ll_name))
3607 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003608 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3613 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 else if (lp->ll_range)
3615 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003616 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617
3618 /* Delete a range of List items. */
3619 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3620 {
3621 li = lp->ll_li->li_next;
3622 listitem_remove(lp->ll_list, lp->ll_li);
3623 lp->ll_li = li;
3624 ++lp->ll_n1;
3625 }
3626 }
3627 else
3628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 /* unlet a List item. */
3631 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003634 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 }
3636
3637 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003638}
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640/*
3641 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 */
3644 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648{
Bram Moolenaar33570922005-01-25 22:26:29 +00003649 hashtab_T *ht;
3650 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003651 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003652 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
Bram Moolenaar33570922005-01-25 22:26:29 +00003654 ht = find_var_ht(name, &varname);
3655 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003657 hi = hash_find(ht, varname);
3658 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003659 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003660 di = HI2DI(hi);
3661 if (var_check_fixed(di->di_flags, name)
3662 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 return FAIL;
3664 delete_var(ht, hi);
3665 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 if (forceit)
3669 return OK;
3670 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 return FAIL;
3672}
3673
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674/*
3675 * Lock or unlock variable indicated by "lp".
3676 * "deep" is the levels to go (-1 for unlimited);
3677 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3678 */
3679 static int
3680do_lock_var(lp, name_end, deep, lock)
3681 lval_T *lp;
3682 char_u *name_end;
3683 int deep;
3684 int lock;
3685{
3686 int ret = OK;
3687 int cc;
3688 dictitem_T *di;
3689
3690 if (deep == 0) /* nothing to do */
3691 return OK;
3692
3693 if (lp->ll_tv == NULL)
3694 {
3695 cc = *name_end;
3696 *name_end = NUL;
3697
3698 /* Normal name or expanded name. */
3699 if (check_changedtick(lp->ll_name))
3700 ret = FAIL;
3701 else
3702 {
3703 di = find_var(lp->ll_name, NULL);
3704 if (di == NULL)
3705 ret = FAIL;
3706 else
3707 {
3708 if (lock)
3709 di->di_flags |= DI_FLAGS_LOCK;
3710 else
3711 di->di_flags &= ~DI_FLAGS_LOCK;
3712 item_lock(&di->di_tv, deep, lock);
3713 }
3714 }
3715 *name_end = cc;
3716 }
3717 else if (lp->ll_range)
3718 {
3719 listitem_T *li = lp->ll_li;
3720
3721 /* (un)lock a range of List items. */
3722 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3723 {
3724 item_lock(&li->li_tv, deep, lock);
3725 li = li->li_next;
3726 ++lp->ll_n1;
3727 }
3728 }
3729 else if (lp->ll_list != NULL)
3730 /* (un)lock a List item. */
3731 item_lock(&lp->ll_li->li_tv, deep, lock);
3732 else
3733 /* un(lock) a Dictionary item. */
3734 item_lock(&lp->ll_di->di_tv, deep, lock);
3735
3736 return ret;
3737}
3738
3739/*
3740 * Lock or unlock an item. "deep" is nr of levels to go.
3741 */
3742 static void
3743item_lock(tv, deep, lock)
3744 typval_T *tv;
3745 int deep;
3746 int lock;
3747{
3748 static int recurse = 0;
3749 list_T *l;
3750 listitem_T *li;
3751 dict_T *d;
3752 hashitem_T *hi;
3753 int todo;
3754
3755 if (recurse >= DICT_MAXNEST)
3756 {
3757 EMSG(_("E743: variable nested too deep for (un)lock"));
3758 return;
3759 }
3760 if (deep == 0)
3761 return;
3762 ++recurse;
3763
3764 /* lock/unlock the item itself */
3765 if (lock)
3766 tv->v_lock |= VAR_LOCKED;
3767 else
3768 tv->v_lock &= ~VAR_LOCKED;
3769
3770 switch (tv->v_type)
3771 {
3772 case VAR_LIST:
3773 if ((l = tv->vval.v_list) != NULL)
3774 {
3775 if (lock)
3776 l->lv_lock |= VAR_LOCKED;
3777 else
3778 l->lv_lock &= ~VAR_LOCKED;
3779 if (deep < 0 || deep > 1)
3780 /* recursive: lock/unlock the items the List contains */
3781 for (li = l->lv_first; li != NULL; li = li->li_next)
3782 item_lock(&li->li_tv, deep - 1, lock);
3783 }
3784 break;
3785 case VAR_DICT:
3786 if ((d = tv->vval.v_dict) != NULL)
3787 {
3788 if (lock)
3789 d->dv_lock |= VAR_LOCKED;
3790 else
3791 d->dv_lock &= ~VAR_LOCKED;
3792 if (deep < 0 || deep > 1)
3793 {
3794 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003795 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003796 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3797 {
3798 if (!HASHITEM_EMPTY(hi))
3799 {
3800 --todo;
3801 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3802 }
3803 }
3804 }
3805 }
3806 }
3807 --recurse;
3808}
3809
Bram Moolenaara40058a2005-07-11 22:42:07 +00003810/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003811 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3812 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003813 */
3814 static int
3815tv_islocked(tv)
3816 typval_T *tv;
3817{
3818 return (tv->v_lock & VAR_LOCKED)
3819 || (tv->v_type == VAR_LIST
3820 && tv->vval.v_list != NULL
3821 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3822 || (tv->v_type == VAR_DICT
3823 && tv->vval.v_dict != NULL
3824 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3825}
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3828/*
3829 * Delete all "menutrans_" variables.
3830 */
3831 void
3832del_menutrans_vars()
3833{
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003835 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003838 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003840 {
3841 if (!HASHITEM_EMPTY(hi))
3842 {
3843 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003844 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3845 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003846 }
3847 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849}
3850#endif
3851
3852#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3853
3854/*
3855 * Local string buffer for the next two functions to store a variable name
3856 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3857 * get_user_var_name().
3858 */
3859
3860static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3861
3862static char_u *varnamebuf = NULL;
3863static int varnamebuflen = 0;
3864
3865/*
3866 * Function to concatenate a prefix and a variable name.
3867 */
3868 static char_u *
3869cat_prefix_varname(prefix, name)
3870 int prefix;
3871 char_u *name;
3872{
3873 int len;
3874
3875 len = (int)STRLEN(name) + 3;
3876 if (len > varnamebuflen)
3877 {
3878 vim_free(varnamebuf);
3879 len += 10; /* some additional space */
3880 varnamebuf = alloc(len);
3881 if (varnamebuf == NULL)
3882 {
3883 varnamebuflen = 0;
3884 return NULL;
3885 }
3886 varnamebuflen = len;
3887 }
3888 *varnamebuf = prefix;
3889 varnamebuf[1] = ':';
3890 STRCPY(varnamebuf + 2, name);
3891 return varnamebuf;
3892}
3893
3894/*
3895 * Function given to ExpandGeneric() to obtain the list of user defined
3896 * (global/buffer/window/built-in) variable names.
3897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 char_u *
3899get_user_var_name(xp, idx)
3900 expand_T *xp;
3901 int idx;
3902{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003903 static long_u gdone;
3904 static long_u bdone;
3905 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003906#ifdef FEAT_WINDOWS
3907 static long_u tdone;
3908#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003909 static int vidx;
3910 static hashitem_T *hi;
3911 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
3913 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003914 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003915 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003916#ifdef FEAT_WINDOWS
3917 tdone = 0;
3918#endif
3919 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003920
3921 /* Global variables */
3922 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003924 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003925 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003926 else
3927 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 while (HASHITEM_EMPTY(hi))
3929 ++hi;
3930 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3931 return cat_prefix_varname('g', hi->hi_key);
3932 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003934
3935 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003936 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003941 else
3942 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 while (HASHITEM_EMPTY(hi))
3944 ++hi;
3945 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 return (char_u *)"b:changedtick";
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952
3953 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003954 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003957 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003959 else
3960 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003961 while (HASHITEM_EMPTY(hi))
3962 ++hi;
3963 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003965
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003966#ifdef FEAT_WINDOWS
3967 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003968 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003969 if (tdone < ht->ht_used)
3970 {
3971 if (tdone++ == 0)
3972 hi = ht->ht_array;
3973 else
3974 ++hi;
3975 while (HASHITEM_EMPTY(hi))
3976 ++hi;
3977 return cat_prefix_varname('t', hi->hi_key);
3978 }
3979#endif
3980
Bram Moolenaar33570922005-01-25 22:26:29 +00003981 /* v: variables */
3982 if (vidx < VV_LEN)
3983 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
3985 vim_free(varnamebuf);
3986 varnamebuf = NULL;
3987 varnamebuflen = 0;
3988 return NULL;
3989}
3990
3991#endif /* FEAT_CMDL_COMPL */
3992
3993/*
3994 * types for expressions.
3995 */
3996typedef enum
3997{
3998 TYPE_UNKNOWN = 0
3999 , TYPE_EQUAL /* == */
4000 , TYPE_NEQUAL /* != */
4001 , TYPE_GREATER /* > */
4002 , TYPE_GEQUAL /* >= */
4003 , TYPE_SMALLER /* < */
4004 , TYPE_SEQUAL /* <= */
4005 , TYPE_MATCH /* =~ */
4006 , TYPE_NOMATCH /* !~ */
4007} exptype_T;
4008
4009/*
4010 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4013 */
4014
4015/*
4016 * Handle zero level expression.
4017 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004019 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 * Return OK or FAIL.
4021 */
4022 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 char_u **nextcmd;
4027 int evaluate;
4028{
4029 int ret;
4030 char_u *p;
4031
4032 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if (ret == FAIL || !ends_excmd(*p))
4035 {
4036 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 /*
4039 * Report the invalid expression unless the expression evaluation has
4040 * been cancelled due to an aborting error, an interrupt, or an
4041 * exception.
4042 */
4043 if (!aborting())
4044 EMSG2(_(e_invexpr2), arg);
4045 ret = FAIL;
4046 }
4047 if (nextcmd != NULL)
4048 *nextcmd = check_nextcmd(p);
4049
4050 return ret;
4051}
4052
4053/*
4054 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004055 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 *
4057 * "arg" must point to the first non-white of the expression.
4058 * "arg" is advanced to the next non-white after the recognized expression.
4059 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004060 * Note: "rettv.v_lock" is not set.
4061 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 * Return OK or FAIL.
4063 */
4064 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004065eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 int evaluate;
4069{
4070 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004071 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 /*
4074 * Get the first variable.
4075 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004076 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 return FAIL;
4078
4079 if ((*arg)[0] == '?')
4080 {
4081 result = FALSE;
4082 if (evaluate)
4083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004084 int error = FALSE;
4085
4086 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 if (error)
4090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092
4093 /*
4094 * Get the second variable.
4095 */
4096 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return FAIL;
4099
4100 /*
4101 * Check for the ":".
4102 */
4103 if ((*arg)[0] != ':')
4104 {
4105 EMSG(_("E109: Missing ':' after '?'"));
4106 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 return FAIL;
4109 }
4110
4111 /*
4112 * Get the third variable.
4113 */
4114 *arg = skipwhite(*arg + 1);
4115 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4116 {
4117 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 return FAIL;
4120 }
4121 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124
4125 return OK;
4126}
4127
4128/*
4129 * Handle first level expression:
4130 * expr2 || expr2 || expr2 logical OR
4131 *
4132 * "arg" must point to the first non-white of the expression.
4133 * "arg" is advanced to the next non-white after the recognized expression.
4134 *
4135 * Return OK or FAIL.
4136 */
4137 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 int evaluate;
4142{
Bram Moolenaar33570922005-01-25 22:26:29 +00004143 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 long result;
4145 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004146 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 /*
4149 * Get the first variable.
4150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 return FAIL;
4153
4154 /*
4155 * Repeat until there is no following "||".
4156 */
4157 first = TRUE;
4158 result = FALSE;
4159 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4160 {
4161 if (evaluate && first)
4162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 if (error)
4167 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 first = FALSE;
4169 }
4170
4171 /*
4172 * Get the second variable.
4173 */
4174 *arg = skipwhite(*arg + 2);
4175 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4176 return FAIL;
4177
4178 /*
4179 * Compute the result.
4180 */
4181 if (evaluate && !result)
4182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (error)
4187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 if (evaluate)
4190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 rettv->v_type = VAR_NUMBER;
4192 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 }
4195
4196 return OK;
4197}
4198
4199/*
4200 * Handle second level expression:
4201 * expr3 && expr3 && expr3 logical AND
4202 *
4203 * "arg" must point to the first non-white of the expression.
4204 * "arg" is advanced to the next non-white after the recognized expression.
4205 *
4206 * Return OK or FAIL.
4207 */
4208 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 int evaluate;
4213{
Bram Moolenaar33570922005-01-25 22:26:29 +00004214 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 long result;
4216 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004217 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
4219 /*
4220 * Get the first variable.
4221 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return FAIL;
4224
4225 /*
4226 * Repeat until there is no following "&&".
4227 */
4228 first = TRUE;
4229 result = TRUE;
4230 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4231 {
4232 if (evaluate && first)
4233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004237 if (error)
4238 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 first = FALSE;
4240 }
4241
4242 /*
4243 * Get the second variable.
4244 */
4245 *arg = skipwhite(*arg + 2);
4246 if (eval4(arg, &var2, evaluate && result) == FAIL)
4247 return FAIL;
4248
4249 /*
4250 * Compute the result.
4251 */
4252 if (evaluate && result)
4253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (error)
4258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 if (evaluate)
4261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 rettv->v_type = VAR_NUMBER;
4263 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 }
4266
4267 return OK;
4268}
4269
4270/*
4271 * Handle third level expression:
4272 * var1 == var2
4273 * var1 =~ var2
4274 * var1 != var2
4275 * var1 !~ var2
4276 * var1 > var2
4277 * var1 >= var2
4278 * var1 < var2
4279 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004280 * var1 is var2
4281 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 *
4283 * "arg" must point to the first non-white of the expression.
4284 * "arg" is advanced to the next non-white after the recognized expression.
4285 *
4286 * Return OK or FAIL.
4287 */
4288 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 int evaluate;
4293{
Bram Moolenaar33570922005-01-25 22:26:29 +00004294 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 char_u *p;
4296 int i;
4297 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004298 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 int len = 2;
4300 long n1, n2;
4301 char_u *s1, *s2;
4302 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4303 regmatch_T regmatch;
4304 int ic;
4305 char_u *save_cpo;
4306
4307 /*
4308 * Get the first variable.
4309 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004310 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 return FAIL;
4312
4313 p = *arg;
4314 switch (p[0])
4315 {
4316 case '=': if (p[1] == '=')
4317 type = TYPE_EQUAL;
4318 else if (p[1] == '~')
4319 type = TYPE_MATCH;
4320 break;
4321 case '!': if (p[1] == '=')
4322 type = TYPE_NEQUAL;
4323 else if (p[1] == '~')
4324 type = TYPE_NOMATCH;
4325 break;
4326 case '>': if (p[1] != '=')
4327 {
4328 type = TYPE_GREATER;
4329 len = 1;
4330 }
4331 else
4332 type = TYPE_GEQUAL;
4333 break;
4334 case '<': if (p[1] != '=')
4335 {
4336 type = TYPE_SMALLER;
4337 len = 1;
4338 }
4339 else
4340 type = TYPE_SEQUAL;
4341 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004342 case 'i': if (p[1] == 's')
4343 {
4344 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4345 len = 5;
4346 if (!vim_isIDc(p[len]))
4347 {
4348 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4349 type_is = TRUE;
4350 }
4351 }
4352 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354
4355 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004356 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 */
4358 if (type != TYPE_UNKNOWN)
4359 {
4360 /* extra question mark appended: ignore case */
4361 if (p[len] == '?')
4362 {
4363 ic = TRUE;
4364 ++len;
4365 }
4366 /* extra '#' appended: match case */
4367 else if (p[len] == '#')
4368 {
4369 ic = FALSE;
4370 ++len;
4371 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 else
4374 ic = p_ic;
4375
4376 /*
4377 * Get the second variable.
4378 */
4379 *arg = skipwhite(p + len);
4380 if (eval5(arg, &var2, evaluate) == FAIL)
4381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 return FAIL;
4384 }
4385
4386 if (evaluate)
4387 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004388 if (type_is && rettv->v_type != var2.v_type)
4389 {
4390 /* For "is" a different type always means FALSE, for "notis"
4391 * it means TRUE. */
4392 n1 = (type == TYPE_NEQUAL);
4393 }
4394 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4395 {
4396 if (type_is)
4397 {
4398 n1 = (rettv->v_type == var2.v_type
4399 && rettv->vval.v_list == var2.vval.v_list);
4400 if (type == TYPE_NEQUAL)
4401 n1 = !n1;
4402 }
4403 else if (rettv->v_type != var2.v_type
4404 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4405 {
4406 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004407 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004408 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004409 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004410 clear_tv(rettv);
4411 clear_tv(&var2);
4412 return FAIL;
4413 }
4414 else
4415 {
4416 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004417 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4418 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004419 if (type == TYPE_NEQUAL)
4420 n1 = !n1;
4421 }
4422 }
4423
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004424 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4425 {
4426 if (type_is)
4427 {
4428 n1 = (rettv->v_type == var2.v_type
4429 && rettv->vval.v_dict == var2.vval.v_dict);
4430 if (type == TYPE_NEQUAL)
4431 n1 = !n1;
4432 }
4433 else if (rettv->v_type != var2.v_type
4434 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4435 {
4436 if (rettv->v_type != var2.v_type)
4437 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4438 else
4439 EMSG(_("E736: Invalid operation for Dictionary"));
4440 clear_tv(rettv);
4441 clear_tv(&var2);
4442 return FAIL;
4443 }
4444 else
4445 {
4446 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004447 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4448 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004449 if (type == TYPE_NEQUAL)
4450 n1 = !n1;
4451 }
4452 }
4453
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004454 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4455 {
4456 if (rettv->v_type != var2.v_type
4457 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4458 {
4459 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004460 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004461 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004462 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004463 clear_tv(rettv);
4464 clear_tv(&var2);
4465 return FAIL;
4466 }
4467 else
4468 {
4469 /* Compare two Funcrefs for being equal or unequal. */
4470 if (rettv->vval.v_string == NULL
4471 || var2.vval.v_string == NULL)
4472 n1 = FALSE;
4473 else
4474 n1 = STRCMP(rettv->vval.v_string,
4475 var2.vval.v_string) == 0;
4476 if (type == TYPE_NEQUAL)
4477 n1 = !n1;
4478 }
4479 }
4480
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004481#ifdef FEAT_FLOAT
4482 /*
4483 * If one of the two variables is a float, compare as a float.
4484 * When using "=~" or "!~", always compare as string.
4485 */
4486 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4487 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4488 {
4489 float_T f1, f2;
4490
4491 if (rettv->v_type == VAR_FLOAT)
4492 f1 = rettv->vval.v_float;
4493 else
4494 f1 = get_tv_number(rettv);
4495 if (var2.v_type == VAR_FLOAT)
4496 f2 = var2.vval.v_float;
4497 else
4498 f2 = get_tv_number(&var2);
4499 n1 = FALSE;
4500 switch (type)
4501 {
4502 case TYPE_EQUAL: n1 = (f1 == f2); break;
4503 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4504 case TYPE_GREATER: n1 = (f1 > f2); break;
4505 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4506 case TYPE_SMALLER: n1 = (f1 < f2); break;
4507 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4508 case TYPE_UNKNOWN:
4509 case TYPE_MATCH:
4510 case TYPE_NOMATCH: break; /* avoid gcc warning */
4511 }
4512 }
4513#endif
4514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /*
4516 * If one of the two variables is a number, compare as a number.
4517 * When using "=~" or "!~", always compare as string.
4518 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004519 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 n1 = get_tv_number(rettv);
4523 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 switch (type)
4525 {
4526 case TYPE_EQUAL: n1 = (n1 == n2); break;
4527 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4528 case TYPE_GREATER: n1 = (n1 > n2); break;
4529 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4530 case TYPE_SMALLER: n1 = (n1 < n2); break;
4531 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4532 case TYPE_UNKNOWN:
4533 case TYPE_MATCH:
4534 case TYPE_NOMATCH: break; /* avoid gcc warning */
4535 }
4536 }
4537 else
4538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004539 s1 = get_tv_string_buf(rettv, buf1);
4540 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4542 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4543 else
4544 i = 0;
4545 n1 = FALSE;
4546 switch (type)
4547 {
4548 case TYPE_EQUAL: n1 = (i == 0); break;
4549 case TYPE_NEQUAL: n1 = (i != 0); break;
4550 case TYPE_GREATER: n1 = (i > 0); break;
4551 case TYPE_GEQUAL: n1 = (i >= 0); break;
4552 case TYPE_SMALLER: n1 = (i < 0); break;
4553 case TYPE_SEQUAL: n1 = (i <= 0); break;
4554
4555 case TYPE_MATCH:
4556 case TYPE_NOMATCH:
4557 /* avoid 'l' flag in 'cpoptions' */
4558 save_cpo = p_cpo;
4559 p_cpo = (char_u *)"";
4560 regmatch.regprog = vim_regcomp(s2,
4561 RE_MAGIC + RE_STRING);
4562 regmatch.rm_ic = ic;
4563 if (regmatch.regprog != NULL)
4564 {
4565 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4566 vim_free(regmatch.regprog);
4567 if (type == TYPE_NOMATCH)
4568 n1 = !n1;
4569 }
4570 p_cpo = save_cpo;
4571 break;
4572
4573 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4574 }
4575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 clear_tv(rettv);
4577 clear_tv(&var2);
4578 rettv->v_type = VAR_NUMBER;
4579 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 }
4582
4583 return OK;
4584}
4585
4586/*
4587 * Handle fourth level expression:
4588 * + number addition
4589 * - number subtraction
4590 * . string concatenation
4591 *
4592 * "arg" must point to the first non-white of the expression.
4593 * "arg" is advanced to the next non-white after the recognized expression.
4594 *
4595 * Return OK or FAIL.
4596 */
4597 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 int evaluate;
4602{
Bram Moolenaar33570922005-01-25 22:26:29 +00004603 typval_T var2;
4604 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 int op;
4606 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004607#ifdef FEAT_FLOAT
4608 float_T f1 = 0, f2 = 0;
4609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 char_u *s1, *s2;
4611 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4612 char_u *p;
4613
4614 /*
4615 * Get the first variable.
4616 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004617 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 return FAIL;
4619
4620 /*
4621 * Repeat computing, until no '+', '-' or '.' is following.
4622 */
4623 for (;;)
4624 {
4625 op = **arg;
4626 if (op != '+' && op != '-' && op != '.')
4627 break;
4628
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629 if ((op != '+' || rettv->v_type != VAR_LIST)
4630#ifdef FEAT_FLOAT
4631 && (op == '.' || rettv->v_type != VAR_FLOAT)
4632#endif
4633 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 {
4635 /* For "list + ...", an illegal use of the first operand as
4636 * a number cannot be determined before evaluating the 2nd
4637 * operand: if this is also a list, all is ok.
4638 * For "something . ...", "something - ..." or "non-list + ...",
4639 * we know that the first operand needs to be a string or number
4640 * without evaluating the 2nd operand. So check before to avoid
4641 * side effects after an error. */
4642 if (evaluate && get_tv_string_chk(rettv) == NULL)
4643 {
4644 clear_tv(rettv);
4645 return FAIL;
4646 }
4647 }
4648
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 /*
4650 * Get the second variable.
4651 */
4652 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004653 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004655 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 return FAIL;
4657 }
4658
4659 if (evaluate)
4660 {
4661 /*
4662 * Compute the result.
4663 */
4664 if (op == '.')
4665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004666 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4667 s2 = get_tv_string_buf_chk(&var2, buf2);
4668 if (s2 == NULL) /* type error ? */
4669 {
4670 clear_tv(rettv);
4671 clear_tv(&var2);
4672 return FAIL;
4673 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004674 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 clear_tv(rettv);
4676 rettv->v_type = VAR_STRING;
4677 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004679 else if (op == '+' && rettv->v_type == VAR_LIST
4680 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004681 {
4682 /* concatenate Lists */
4683 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4684 &var3) == FAIL)
4685 {
4686 clear_tv(rettv);
4687 clear_tv(&var2);
4688 return FAIL;
4689 }
4690 clear_tv(rettv);
4691 *rettv = var3;
4692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 else
4694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004695 int error = FALSE;
4696
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#ifdef FEAT_FLOAT
4698 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004700 f1 = rettv->vval.v_float;
4701 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004703 else
4704#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004705 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004706 n1 = get_tv_number_chk(rettv, &error);
4707 if (error)
4708 {
4709 /* This can only happen for "list + non-list". For
4710 * "non-list + ..." or "something - ...", we returned
4711 * before evaluating the 2nd operand. */
4712 clear_tv(rettv);
4713 return FAIL;
4714 }
4715#ifdef FEAT_FLOAT
4716 if (var2.v_type == VAR_FLOAT)
4717 f1 = n1;
4718#endif
4719 }
4720#ifdef FEAT_FLOAT
4721 if (var2.v_type == VAR_FLOAT)
4722 {
4723 f2 = var2.vval.v_float;
4724 n2 = 0;
4725 }
4726 else
4727#endif
4728 {
4729 n2 = get_tv_number_chk(&var2, &error);
4730 if (error)
4731 {
4732 clear_tv(rettv);
4733 clear_tv(&var2);
4734 return FAIL;
4735 }
4736#ifdef FEAT_FLOAT
4737 if (rettv->v_type == VAR_FLOAT)
4738 f2 = n2;
4739#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004742
4743#ifdef FEAT_FLOAT
4744 /* If there is a float on either side the result is a float. */
4745 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4746 {
4747 if (op == '+')
4748 f1 = f1 + f2;
4749 else
4750 f1 = f1 - f2;
4751 rettv->v_type = VAR_FLOAT;
4752 rettv->vval.v_float = f1;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004755#endif
4756 {
4757 if (op == '+')
4758 n1 = n1 + n2;
4759 else
4760 n1 = n1 - n2;
4761 rettv->v_type = VAR_NUMBER;
4762 rettv->vval.v_number = n1;
4763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767 }
4768 return OK;
4769}
4770
4771/*
4772 * Handle fifth level expression:
4773 * * number multiplication
4774 * / number division
4775 * % number modulo
4776 *
4777 * "arg" must point to the first non-white of the expression.
4778 * "arg" is advanced to the next non-white after the recognized expression.
4779 *
4780 * Return OK or FAIL.
4781 */
4782 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004783eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004787 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
Bram Moolenaar33570922005-01-25 22:26:29 +00004789 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 int op;
4791 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792#ifdef FEAT_FLOAT
4793 int use_float = FALSE;
4794 float_T f1 = 0, f2;
4795#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004796 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 /*
4799 * Get the first variable.
4800 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004801 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 return FAIL;
4803
4804 /*
4805 * Repeat computing, until no '*', '/' or '%' is following.
4806 */
4807 for (;;)
4808 {
4809 op = **arg;
4810 if (op != '*' && op != '/' && op != '%')
4811 break;
4812
4813 if (evaluate)
4814 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815#ifdef FEAT_FLOAT
4816 if (rettv->v_type == VAR_FLOAT)
4817 {
4818 f1 = rettv->vval.v_float;
4819 use_float = TRUE;
4820 n1 = 0;
4821 }
4822 else
4823#endif
4824 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 if (error)
4827 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 else
4830 n1 = 0;
4831
4832 /*
4833 * Get the second variable.
4834 */
4835 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004836 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 return FAIL;
4838
4839 if (evaluate)
4840 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#ifdef FEAT_FLOAT
4842 if (var2.v_type == VAR_FLOAT)
4843 {
4844 if (!use_float)
4845 {
4846 f1 = n1;
4847 use_float = TRUE;
4848 }
4849 f2 = var2.vval.v_float;
4850 n2 = 0;
4851 }
4852 else
4853#endif
4854 {
4855 n2 = get_tv_number_chk(&var2, &error);
4856 clear_tv(&var2);
4857 if (error)
4858 return FAIL;
4859#ifdef FEAT_FLOAT
4860 if (use_float)
4861 f2 = n2;
4862#endif
4863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
4865 /*
4866 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869#ifdef FEAT_FLOAT
4870 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872 if (op == '*')
4873 f1 = f1 * f2;
4874 else if (op == '/')
4875 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004876# ifdef VMS
4877 /* VMS crashes on divide by zero, work around it */
4878 if (f2 == 0.0)
4879 {
4880 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004881 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004882 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004883 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004884 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004885 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004886 }
4887 else
4888 f1 = f1 / f2;
4889# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890 /* We rely on the floating point library to handle divide
4891 * by zero to result in "inf" and not a crash. */
4892 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004893# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004896 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004897 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 return FAIL;
4899 }
4900 rettv->v_type = VAR_FLOAT;
4901 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906 if (op == '*')
4907 n1 = n1 * n2;
4908 else if (op == '/')
4909 {
4910 if (n2 == 0) /* give an error message? */
4911 {
4912 if (n1 == 0)
4913 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4914 else if (n1 < 0)
4915 n1 = -0x7fffffffL;
4916 else
4917 n1 = 0x7fffffffL;
4918 }
4919 else
4920 n1 = n1 / n2;
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923 {
4924 if (n2 == 0) /* give an error message? */
4925 n1 = 0;
4926 else
4927 n1 = n1 % n2;
4928 }
4929 rettv->v_type = VAR_NUMBER;
4930 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 }
4934
4935 return OK;
4936}
4937
4938/*
4939 * Handle sixth level expression:
4940 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004941 * "string" string constant
4942 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 * &option-name option value
4944 * @r register contents
4945 * identifier variable value
4946 * function() function call
4947 * $VAR environment variable
4948 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004949 * [expr, expr] List
4950 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 *
4952 * Also handle:
4953 * ! in front logical NOT
4954 * - in front unary minus
4955 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004956 * trailing [] subscript in String or List
4957 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 *
4959 * "arg" must point to the first non-white of the expression.
4960 * "arg" is advanced to the next non-white after the recognized expression.
4961 *
4962 * Return OK or FAIL.
4963 */
4964 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004965eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004969 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 long n;
4972 int len;
4973 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 char_u *start_leader, *end_leader;
4975 int ret = OK;
4976 char_u *alias;
4977
4978 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004979 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004980 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
4984 /*
4985 * Skip '!' and '-' characters. They are handled later.
4986 */
4987 start_leader = *arg;
4988 while (**arg == '!' || **arg == '-' || **arg == '+')
4989 *arg = skipwhite(*arg + 1);
4990 end_leader = *arg;
4991
4992 switch (**arg)
4993 {
4994 /*
4995 * Number constant.
4996 */
4997 case '0':
4998 case '1':
4999 case '2':
5000 case '3':
5001 case '4':
5002 case '5':
5003 case '6':
5004 case '7':
5005 case '8':
5006 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 {
5008#ifdef FEAT_FLOAT
5009 char_u *p = skipdigits(*arg + 1);
5010 int get_float = FALSE;
5011
5012 /* We accept a float when the format matches
5013 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005014 * strict to avoid backwards compatibility problems.
5015 * Don't look for a float after the "." operator, so that
5016 * ":let vers = 1.2.3" doesn't fail. */
5017 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019 get_float = TRUE;
5020 p = skipdigits(p + 2);
5021 if (*p == 'e' || *p == 'E')
5022 {
5023 ++p;
5024 if (*p == '-' || *p == '+')
5025 ++p;
5026 if (!vim_isdigit(*p))
5027 get_float = FALSE;
5028 else
5029 p = skipdigits(p + 1);
5030 }
5031 if (ASCII_ISALPHA(*p) || *p == '.')
5032 get_float = FALSE;
5033 }
5034 if (get_float)
5035 {
5036 float_T f;
5037
5038 *arg += string2float(*arg, &f);
5039 if (evaluate)
5040 {
5041 rettv->v_type = VAR_FLOAT;
5042 rettv->vval.v_float = f;
5043 }
5044 }
5045 else
5046#endif
5047 {
5048 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5049 *arg += len;
5050 if (evaluate)
5051 {
5052 rettv->v_type = VAR_NUMBER;
5053 rettv->vval.v_number = n;
5054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 /*
5060 * String constant: "string".
5061 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 break;
5064
5065 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 break;
5070
5071 /*
5072 * List: [expr, expr]
5073 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 break;
5076
5077 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 * Dictionary: {key: val, key: val}
5079 */
5080 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5081 break;
5082
5083 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005084 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005086 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 break;
5088
5089 /*
5090 * Environment variable: $VAR.
5091 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005092 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 break;
5094
5095 /*
5096 * Register contents: @r.
5097 */
5098 case '@': ++*arg;
5099 if (evaluate)
5100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005102 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104 if (**arg != NUL)
5105 ++*arg;
5106 break;
5107
5108 /*
5109 * nested expression: (expression).
5110 */
5111 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (**arg == ')')
5114 ++*arg;
5115 else if (ret == OK)
5116 {
5117 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 ret = FAIL;
5120 }
5121 break;
5122
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
5125 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126
5127 if (ret == NOTDONE)
5128 {
5129 /*
5130 * Must be a variable or function name.
5131 * Can also be a curly-braces kind of name: {expr}.
5132 */
5133 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005134 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 if (alias != NULL)
5136 s = alias;
5137
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005138 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 ret = FAIL;
5140 else
5141 {
5142 if (**arg == '(') /* recursive! */
5143 {
5144 /* If "s" is the name of a variable of type VAR_FUNC
5145 * use its contents. */
5146 s = deref_func_name(s, &len);
5147
5148 /* Invoke the function. */
5149 ret = get_func_tv(s, len, rettv, arg,
5150 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005151 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005152
5153 /* If evaluate is FALSE rettv->v_type was not set in
5154 * get_func_tv, but it's needed in handle_subscript() to parse
5155 * what follows. So set it here. */
5156 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5157 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005158 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005159 rettv->v_type = VAR_FUNC;
5160 }
5161
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 /* Stop the expression evaluation when immediately
5163 * aborting on error, or when an interrupt occurred or
5164 * an exception was thrown but not caught. */
5165 if (aborting())
5166 {
5167 if (ret == OK)
5168 clear_tv(rettv);
5169 ret = FAIL;
5170 }
5171 }
5172 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005173 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005174 else
5175 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005177 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 }
5179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 *arg = skipwhite(*arg);
5181
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005182 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5183 * expr(expr). */
5184 if (ret == OK)
5185 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
5187 /*
5188 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5189 */
5190 if (ret == OK && evaluate && end_leader > start_leader)
5191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005192 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005193 int val = 0;
5194#ifdef FEAT_FLOAT
5195 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 if (rettv->v_type == VAR_FLOAT)
5198 f = rettv->vval.v_float;
5199 else
5200#endif
5201 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005204 clear_tv(rettv);
5205 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 else
5208 {
5209 while (end_leader > start_leader)
5210 {
5211 --end_leader;
5212 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 {
5214#ifdef FEAT_FLOAT
5215 if (rettv->v_type == VAR_FLOAT)
5216 f = !f;
5217 else
5218#endif
5219 val = !val;
5220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005221 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005222 {
5223#ifdef FEAT_FLOAT
5224 if (rettv->v_type == VAR_FLOAT)
5225 f = -f;
5226 else
5227#endif
5228 val = -val;
5229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005231#ifdef FEAT_FLOAT
5232 if (rettv->v_type == VAR_FLOAT)
5233 {
5234 clear_tv(rettv);
5235 rettv->vval.v_float = f;
5236 }
5237 else
5238#endif
5239 {
5240 clear_tv(rettv);
5241 rettv->v_type = VAR_NUMBER;
5242 rettv->vval.v_number = val;
5243 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246
5247 return ret;
5248}
5249
5250/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005251 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5252 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5254 */
5255 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005256eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005258 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261{
5262 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 long len = -1;
5266 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005270 if (rettv->v_type == VAR_FUNC
5271#ifdef FEAT_FLOAT
5272 || rettv->v_type == VAR_FLOAT
5273#endif
5274 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 if (verbose)
5277 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 return FAIL;
5279 }
5280
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 /*
5284 * dict.name
5285 */
5286 key = *arg + 1;
5287 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5288 ;
5289 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 }
5293 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 /*
5296 * something[idx]
5297 *
5298 * Get the (first) variable from inside the [].
5299 */
5300 *arg = skipwhite(*arg + 1);
5301 if (**arg == ':')
5302 empty1 = TRUE;
5303 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5304 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5306 {
5307 /* not a number or string */
5308 clear_tv(&var1);
5309 return FAIL;
5310 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311
5312 /*
5313 * Get the second variable from inside the [:].
5314 */
5315 if (**arg == ':')
5316 {
5317 range = TRUE;
5318 *arg = skipwhite(*arg + 1);
5319 if (**arg == ']')
5320 empty2 = TRUE;
5321 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005323 if (!empty1)
5324 clear_tv(&var1);
5325 return FAIL;
5326 }
5327 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5328 {
5329 /* not a number or string */
5330 if (!empty1)
5331 clear_tv(&var1);
5332 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 return FAIL;
5334 }
5335 }
5336
5337 /* Check for the ']'. */
5338 if (**arg != ']')
5339 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005340 if (verbose)
5341 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 clear_tv(&var1);
5343 if (range)
5344 clear_tv(&var2);
5345 return FAIL;
5346 }
5347 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 }
5349
5350 if (evaluate)
5351 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 n1 = 0;
5353 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005355 n1 = get_tv_number(&var1);
5356 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358 if (range)
5359 {
5360 if (empty2)
5361 n2 = -1;
5362 else
5363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 n2 = get_tv_number(&var2);
5365 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 }
5367 }
5368
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005369 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 {
5371 case VAR_NUMBER:
5372 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 len = (long)STRLEN(s);
5375 if (range)
5376 {
5377 /* The resulting variable is a substring. If the indexes
5378 * are out of range the result is empty. */
5379 if (n1 < 0)
5380 {
5381 n1 = len + n1;
5382 if (n1 < 0)
5383 n1 = 0;
5384 }
5385 if (n2 < 0)
5386 n2 = len + n2;
5387 else if (n2 >= len)
5388 n2 = len;
5389 if (n1 >= len || n2 < 0 || n1 > n2)
5390 s = NULL;
5391 else
5392 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5393 }
5394 else
5395 {
5396 /* The resulting variable is a string of a single
5397 * character. If the index is too big or negative the
5398 * result is empty. */
5399 if (n1 >= len || n1 < 0)
5400 s = NULL;
5401 else
5402 s = vim_strnsave(s + n1, 1);
5403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005404 clear_tv(rettv);
5405 rettv->v_type = VAR_STRING;
5406 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 break;
5408
5409 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 if (n1 < 0)
5412 n1 = len + n1;
5413 if (!empty1 && (n1 < 0 || n1 >= len))
5414 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005415 /* For a range we allow invalid values and return an empty
5416 * list. A list index out of range is an error. */
5417 if (!range)
5418 {
5419 if (verbose)
5420 EMSGN(_(e_listidx), n1);
5421 return FAIL;
5422 }
5423 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 }
5425 if (range)
5426 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 list_T *l;
5428 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429
5430 if (n2 < 0)
5431 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005432 else if (n2 >= len)
5433 n2 = len - 1;
5434 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005435 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 l = list_alloc();
5437 if (l == NULL)
5438 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005439 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 n1 <= n2; ++n1)
5441 {
5442 if (list_append_tv(l, &item->li_tv) == FAIL)
5443 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005444 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 return FAIL;
5446 }
5447 item = item->li_next;
5448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 clear_tv(rettv);
5450 rettv->v_type = VAR_LIST;
5451 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005452 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 }
5454 else
5455 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005456 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 clear_tv(rettv);
5458 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461
5462 case VAR_DICT:
5463 if (range)
5464 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005465 if (verbose)
5466 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467 if (len == -1)
5468 clear_tv(&var1);
5469 return FAIL;
5470 }
5471 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005472 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005473
5474 if (len == -1)
5475 {
5476 key = get_tv_string(&var1);
5477 if (*key == NUL)
5478 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005479 if (verbose)
5480 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 clear_tv(&var1);
5482 return FAIL;
5483 }
5484 }
5485
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005486 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005488 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005489 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 if (len == -1)
5491 clear_tv(&var1);
5492 if (item == NULL)
5493 return FAIL;
5494
5495 copy_tv(&item->di_tv, &var1);
5496 clear_tv(rettv);
5497 *rettv = var1;
5498 }
5499 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 }
5501 }
5502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 return OK;
5504}
5505
5506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 * Get an option value.
5508 * "arg" points to the '&' or '+' before the option name.
5509 * "arg" is advanced to character after the option name.
5510 * Return OK or FAIL.
5511 */
5512 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005515 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 int evaluate;
5517{
5518 char_u *option_end;
5519 long numval;
5520 char_u *stringval;
5521 int opt_type;
5522 int c;
5523 int working = (**arg == '+'); /* has("+option") */
5524 int ret = OK;
5525 int opt_flags;
5526
5527 /*
5528 * Isolate the option name and find its value.
5529 */
5530 option_end = find_option_end(arg, &opt_flags);
5531 if (option_end == NULL)
5532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 EMSG2(_("E112: Option name missing: %s"), *arg);
5535 return FAIL;
5536 }
5537
5538 if (!evaluate)
5539 {
5540 *arg = option_end;
5541 return OK;
5542 }
5543
5544 c = *option_end;
5545 *option_end = NUL;
5546 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 if (opt_type == -3) /* invalid name */
5550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 EMSG2(_("E113: Unknown option: %s"), *arg);
5553 ret = FAIL;
5554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
5557 if (opt_type == -2) /* hidden string option */
5558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 rettv->v_type = VAR_STRING;
5560 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 }
5562 else if (opt_type == -1) /* hidden number option */
5563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 rettv->v_type = VAR_NUMBER;
5565 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567 else if (opt_type == 1) /* number option */
5568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 rettv->v_type = VAR_NUMBER;
5570 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572 else /* string option */
5573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 rettv->v_type = VAR_STRING;
5575 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 }
5578 else if (working && (opt_type == -2 || opt_type == -1))
5579 ret = FAIL;
5580
5581 *option_end = c; /* put back for error messages */
5582 *arg = option_end;
5583
5584 return ret;
5585}
5586
5587/*
5588 * Allocate a variable for a string constant.
5589 * Return OK or FAIL.
5590 */
5591 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 int evaluate;
5596{
5597 char_u *p;
5598 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 int extra = 0;
5600
5601 /*
5602 * Find the end of the string, skipping backslashed characters.
5603 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005604 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
5606 if (*p == '\\' && p[1] != NUL)
5607 {
5608 ++p;
5609 /* A "\<x>" form occupies at least 4 characters, and produces up
5610 * to 6 characters: reserve space for 2 extra */
5611 if (*p == '<')
5612 extra += 2;
5613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
5615
5616 if (*p != '"')
5617 {
5618 EMSG2(_("E114: Missing quote: %s"), *arg);
5619 return FAIL;
5620 }
5621
5622 /* If only parsing, set *arg and return here */
5623 if (!evaluate)
5624 {
5625 *arg = p + 1;
5626 return OK;
5627 }
5628
5629 /*
5630 * Copy the string into allocated memory, handling backslashed
5631 * characters.
5632 */
5633 name = alloc((unsigned)(p - *arg + extra));
5634 if (name == NULL)
5635 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 rettv->v_type = VAR_STRING;
5637 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
Bram Moolenaar8c711452005-01-14 21:53:12 +00005639 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
5641 if (*p == '\\')
5642 {
5643 switch (*++p)
5644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 case 'b': *name++ = BS; ++p; break;
5646 case 'e': *name++ = ESC; ++p; break;
5647 case 'f': *name++ = FF; ++p; break;
5648 case 'n': *name++ = NL; ++p; break;
5649 case 'r': *name++ = CAR; ++p; break;
5650 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
5652 case 'X': /* hex: "\x1", "\x12" */
5653 case 'x':
5654 case 'u': /* Unicode: "\u0023" */
5655 case 'U':
5656 if (vim_isxdigit(p[1]))
5657 {
5658 int n, nr;
5659 int c = toupper(*p);
5660
5661 if (c == 'X')
5662 n = 2;
5663 else
5664 n = 4;
5665 nr = 0;
5666 while (--n >= 0 && vim_isxdigit(p[1]))
5667 {
5668 ++p;
5669 nr = (nr << 4) + hex2nr(*p);
5670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672#ifdef FEAT_MBYTE
5673 /* For "\u" store the number according to
5674 * 'encoding'. */
5675 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 else
5678#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 break;
5682
5683 /* octal: "\1", "\12", "\123" */
5684 case '0':
5685 case '1':
5686 case '2':
5687 case '3':
5688 case '4':
5689 case '5':
5690 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 case '7': *name = *p++ - '0';
5692 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 *name = (*name << 3) + *p++ - '0';
5695 if (*p >= '0' && *p <= '7')
5696 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 break;
5700
5701 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 if (extra != 0)
5704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 break;
5707 }
5708 /* FALLTHROUGH */
5709
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712 }
5713 }
5714 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 *arg = p + 1;
5720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 return OK;
5722}
5723
5724/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 * Return OK or FAIL.
5727 */
5728 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005729get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 int evaluate;
5733{
5734 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005736 int reduce = 0;
5737
5738 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005740 */
5741 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 break;
5747 ++reduce;
5748 ++p;
5749 }
5750 }
5751
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 return FAIL;
5756 }
5757
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 if (!evaluate)
5760 {
5761 *arg = p + 1;
5762 return OK;
5763 }
5764
5765 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 */
5768 str = alloc((unsigned)((p - *arg) - reduce));
5769 if (str == NULL)
5770 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 rettv->v_type = VAR_STRING;
5772 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 break;
5780 ++p;
5781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 *arg = p + 1;
5786
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 return OK;
5788}
5789
5790/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005791 * Allocate a variable for a List and fill it from "*arg".
5792 * Return OK or FAIL.
5793 */
5794 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005795get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005797 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 int evaluate;
5799{
Bram Moolenaar33570922005-01-25 22:26:29 +00005800 list_T *l = NULL;
5801 typval_T tv;
5802 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803
5804 if (evaluate)
5805 {
5806 l = list_alloc();
5807 if (l == NULL)
5808 return FAIL;
5809 }
5810
5811 *arg = skipwhite(*arg + 1);
5812 while (**arg != ']' && **arg != NUL)
5813 {
5814 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5815 goto failret;
5816 if (evaluate)
5817 {
5818 item = listitem_alloc();
5819 if (item != NULL)
5820 {
5821 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005822 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 list_append(l, item);
5824 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005825 else
5826 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 }
5828
5829 if (**arg == ']')
5830 break;
5831 if (**arg != ',')
5832 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 goto failret;
5835 }
5836 *arg = skipwhite(*arg + 1);
5837 }
5838
5839 if (**arg != ']')
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842failret:
5843 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005844 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845 return FAIL;
5846 }
5847
5848 *arg = skipwhite(*arg + 1);
5849 if (evaluate)
5850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005851 rettv->v_type = VAR_LIST;
5852 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 ++l->lv_refcount;
5854 }
5855
5856 return OK;
5857}
5858
5859/*
5860 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005861 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005863 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864list_alloc()
5865{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005866 list_T *l;
5867
5868 l = (list_T *)alloc_clear(sizeof(list_T));
5869 if (l != NULL)
5870 {
5871 /* Prepend the list to the list of lists for garbage collection. */
5872 if (first_list != NULL)
5873 first_list->lv_used_prev = l;
5874 l->lv_used_prev = NULL;
5875 l->lv_used_next = first_list;
5876 first_list = l;
5877 }
5878 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879}
5880
5881/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005882 * Allocate an empty list for a return value.
5883 * Returns OK or FAIL.
5884 */
5885 static int
5886rettv_list_alloc(rettv)
5887 typval_T *rettv;
5888{
5889 list_T *l = list_alloc();
5890
5891 if (l == NULL)
5892 return FAIL;
5893
5894 rettv->vval.v_list = l;
5895 rettv->v_type = VAR_LIST;
5896 ++l->lv_refcount;
5897 return OK;
5898}
5899
5900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * Unreference a list: decrement the reference count and free it when it
5902 * becomes zero.
5903 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005904 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005908 if (l != NULL && --l->lv_refcount <= 0)
5909 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910}
5911
5912/*
5913 * Free a list, including all items it points to.
5914 * Ignores the reference count.
5915 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005916 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005917list_free(l, recurse)
5918 list_T *l;
5919 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920{
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005922
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005923 /* Remove the list from the list of lists for garbage collection. */
5924 if (l->lv_used_prev == NULL)
5925 first_list = l->lv_used_next;
5926 else
5927 l->lv_used_prev->lv_used_next = l->lv_used_next;
5928 if (l->lv_used_next != NULL)
5929 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5930
Bram Moolenaard9fba312005-06-26 22:34:35 +00005931 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005933 /* Remove the item before deleting it. */
5934 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005935 if (recurse || (item->li_tv.v_type != VAR_LIST
5936 && item->li_tv.v_type != VAR_DICT))
5937 clear_tv(&item->li_tv);
5938 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 }
5940 vim_free(l);
5941}
5942
5943/*
5944 * Allocate a list item.
5945 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005946 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947listitem_alloc()
5948{
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950}
5951
5952/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005953 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005955 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005959 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 vim_free(item);
5961}
5962
5963/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005964 * Remove a list item from a List and free it. Also clears the value.
5965 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005966 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005967listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005968 list_T *l;
5969 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005970{
5971 list_remove(l, item, item);
5972 listitem_free(item);
5973}
5974
5975/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 * Get the number of items in a list.
5977 */
5978 static long
5979list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 if (l == NULL)
5983 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005984 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985}
5986
5987/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005988 * Return TRUE when two lists have exactly the same values.
5989 */
5990 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005991list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 list_T *l1;
5993 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005994 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005995 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996{
Bram Moolenaar33570922005-01-25 22:26:29 +00005997 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005999 if (l1 == NULL || l2 == NULL)
6000 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006001 if (l1 == l2)
6002 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006003 if (list_len(l1) != list_len(l2))
6004 return FALSE;
6005
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006 for (item1 = l1->lv_first, item2 = l2->lv_first;
6007 item1 != NULL && item2 != NULL;
6008 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006009 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006010 return FALSE;
6011 return item1 == NULL && item2 == NULL;
6012}
6013
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006014#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6015 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006016/*
6017 * Return the dictitem that an entry in a hashtable points to.
6018 */
6019 dictitem_T *
6020dict_lookup(hi)
6021 hashitem_T *hi;
6022{
6023 return HI2DI(hi);
6024}
6025#endif
6026
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006027/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006028 * Return TRUE when two dictionaries have exactly the same key/values.
6029 */
6030 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006031dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 dict_T *d1;
6033 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006034 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006035 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 hashitem_T *hi;
6038 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006039 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006040
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006041 if (d1 == NULL || d2 == NULL)
6042 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006043 if (d1 == d2)
6044 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006045 if (dict_len(d1) != dict_len(d2))
6046 return FALSE;
6047
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006048 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006049 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006050 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006051 if (!HASHITEM_EMPTY(hi))
6052 {
6053 item2 = dict_find(d2, hi->hi_key, -1);
6054 if (item2 == NULL)
6055 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006056 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006057 return FALSE;
6058 --todo;
6059 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060 }
6061 return TRUE;
6062}
6063
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006064static int tv_equal_recurse_limit;
6065
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006067 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006068 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006069 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006070 */
6071 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 typval_T *tv1;
6074 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006075 int ic; /* ignore case */
6076 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006077{
6078 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006079 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006080 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006081 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006083 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006084 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006085
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006086 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006087 * recursiveness to a limit. We guess they are equal then.
6088 * A fixed limit has the problem of still taking an awful long time.
6089 * Reduce the limit every time running into it. That should work fine for
6090 * deeply linked structures that are not recursively linked and catch
6091 * recursiveness quickly. */
6092 if (!recursive)
6093 tv_equal_recurse_limit = 1000;
6094 if (recursive_cnt >= tv_equal_recurse_limit)
6095 {
6096 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006097 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006098 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006099
6100 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006102 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 ++recursive_cnt;
6104 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6105 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006106 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107
6108 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109 ++recursive_cnt;
6110 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6111 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006112 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006113
6114 case VAR_FUNC:
6115 return (tv1->vval.v_string != NULL
6116 && tv2->vval.v_string != NULL
6117 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6118
6119 case VAR_NUMBER:
6120 return tv1->vval.v_number == tv2->vval.v_number;
6121
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006122#ifdef FEAT_FLOAT
6123 case VAR_FLOAT:
6124 return tv1->vval.v_float == tv2->vval.v_float;
6125#endif
6126
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006127 case VAR_STRING:
6128 s1 = get_tv_string_buf(tv1, buf1);
6129 s2 = get_tv_string_buf(tv2, buf2);
6130 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132
6133 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006134 return TRUE;
6135}
6136
6137/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138 * Locate item with index "n" in list "l" and return it.
6139 * A negative index is counted from the end; -1 is the last item.
6140 * Returns NULL when "n" is out of range.
6141 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006142 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 long n;
6146{
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148 long idx;
6149
6150 if (l == NULL)
6151 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006152
6153 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006155 n = l->lv_len + n;
6156
6157 /* Check for index out of range. */
6158 if (n < 0 || n >= l->lv_len)
6159 return NULL;
6160
6161 /* When there is a cached index may start search from there. */
6162 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006164 if (n < l->lv_idx / 2)
6165 {
6166 /* closest to the start of the list */
6167 item = l->lv_first;
6168 idx = 0;
6169 }
6170 else if (n > (l->lv_idx + l->lv_len) / 2)
6171 {
6172 /* closest to the end of the list */
6173 item = l->lv_last;
6174 idx = l->lv_len - 1;
6175 }
6176 else
6177 {
6178 /* closest to the cached index */
6179 item = l->lv_idx_item;
6180 idx = l->lv_idx;
6181 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182 }
6183 else
6184 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006185 if (n < l->lv_len / 2)
6186 {
6187 /* closest to the start of the list */
6188 item = l->lv_first;
6189 idx = 0;
6190 }
6191 else
6192 {
6193 /* closest to the end of the list */
6194 item = l->lv_last;
6195 idx = l->lv_len - 1;
6196 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006198
6199 while (n > idx)
6200 {
6201 /* search forward */
6202 item = item->li_next;
6203 ++idx;
6204 }
6205 while (n < idx)
6206 {
6207 /* search backward */
6208 item = item->li_prev;
6209 --idx;
6210 }
6211
6212 /* cache the used index */
6213 l->lv_idx = idx;
6214 l->lv_idx_item = item;
6215
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216 return item;
6217}
6218
6219/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006220 * Get list item "l[idx]" as a number.
6221 */
6222 static long
6223list_find_nr(l, idx, errorp)
6224 list_T *l;
6225 long idx;
6226 int *errorp; /* set to TRUE when something wrong */
6227{
6228 listitem_T *li;
6229
6230 li = list_find(l, idx);
6231 if (li == NULL)
6232 {
6233 if (errorp != NULL)
6234 *errorp = TRUE;
6235 return -1L;
6236 }
6237 return get_tv_number_chk(&li->li_tv, errorp);
6238}
6239
6240/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006241 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6242 */
6243 char_u *
6244list_find_str(l, idx)
6245 list_T *l;
6246 long idx;
6247{
6248 listitem_T *li;
6249
6250 li = list_find(l, idx - 1);
6251 if (li == NULL)
6252 {
6253 EMSGN(_(e_listidx), idx);
6254 return NULL;
6255 }
6256 return get_tv_string(&li->li_tv);
6257}
6258
6259/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006260 * Locate "item" list "l" and return its index.
6261 * Returns -1 when "item" is not in the list.
6262 */
6263 static long
6264list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 list_T *l;
6266 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006267{
6268 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006269 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006270
6271 if (l == NULL)
6272 return -1;
6273 idx = 0;
6274 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6275 ++idx;
6276 if (li == NULL)
6277 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006278 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006279}
6280
6281/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282 * Append item "item" to the end of list "l".
6283 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006284 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 list_T *l;
6287 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288{
6289 if (l->lv_last == NULL)
6290 {
6291 /* empty list */
6292 l->lv_first = item;
6293 l->lv_last = item;
6294 item->li_prev = NULL;
6295 }
6296 else
6297 {
6298 l->lv_last->li_next = item;
6299 item->li_prev = l->lv_last;
6300 l->lv_last = item;
6301 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006302 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303 item->li_next = NULL;
6304}
6305
6306/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006307 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006310 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 list_T *l;
6313 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006315 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316
Bram Moolenaar05159a02005-02-26 23:04:13 +00006317 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006319 copy_tv(tv, &li->li_tv);
6320 list_append(l, li);
6321 return OK;
6322}
6323
6324/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006325 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006326 * Return FAIL when out of memory.
6327 */
6328 int
6329list_append_dict(list, dict)
6330 list_T *list;
6331 dict_T *dict;
6332{
6333 listitem_T *li = listitem_alloc();
6334
6335 if (li == NULL)
6336 return FAIL;
6337 li->li_tv.v_type = VAR_DICT;
6338 li->li_tv.v_lock = 0;
6339 li->li_tv.vval.v_dict = dict;
6340 list_append(list, li);
6341 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 return OK;
6343}
6344
6345/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006346 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006347 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006348 * Returns FAIL when out of memory.
6349 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006350 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006351list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006352 list_T *l;
6353 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006354 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006355{
6356 listitem_T *li = listitem_alloc();
6357
6358 if (li == NULL)
6359 return FAIL;
6360 list_append(l, li);
6361 li->li_tv.v_type = VAR_STRING;
6362 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006363 if (str == NULL)
6364 li->li_tv.vval.v_string = NULL;
6365 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006366 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006367 return FAIL;
6368 return OK;
6369}
6370
6371/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006372 * Append "n" to list "l".
6373 * Returns FAIL when out of memory.
6374 */
6375 static int
6376list_append_number(l, n)
6377 list_T *l;
6378 varnumber_T n;
6379{
6380 listitem_T *li;
6381
6382 li = listitem_alloc();
6383 if (li == NULL)
6384 return FAIL;
6385 li->li_tv.v_type = VAR_NUMBER;
6386 li->li_tv.v_lock = 0;
6387 li->li_tv.vval.v_number = n;
6388 list_append(l, li);
6389 return OK;
6390}
6391
6392/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006393 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006394 * If "item" is NULL append at the end.
6395 * Return FAIL when out of memory.
6396 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006397 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006398list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 list_T *l;
6400 typval_T *tv;
6401 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402{
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006404
6405 if (ni == NULL)
6406 return FAIL;
6407 copy_tv(tv, &ni->li_tv);
6408 if (item == NULL)
6409 /* Append new item at end of list. */
6410 list_append(l, ni);
6411 else
6412 {
6413 /* Insert new item before existing item. */
6414 ni->li_prev = item->li_prev;
6415 ni->li_next = item;
6416 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006417 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006419 ++l->lv_idx;
6420 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006422 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006424 l->lv_idx_item = NULL;
6425 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006427 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 }
6429 return OK;
6430}
6431
6432/*
6433 * Extend "l1" with "l2".
6434 * If "bef" is NULL append at the end, otherwise insert before this item.
6435 * Returns FAIL when out of memory.
6436 */
6437 static int
6438list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006439 list_T *l1;
6440 list_T *l2;
6441 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006442{
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006444 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006446 /* We also quit the loop when we have inserted the original item count of
6447 * the list, avoid a hang when we extend a list with itself. */
6448 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6450 return FAIL;
6451 return OK;
6452}
6453
6454/*
6455 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6456 * Return FAIL when out of memory.
6457 */
6458 static int
6459list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006460 list_T *l1;
6461 list_T *l2;
6462 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463{
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006466 if (l1 == NULL || l2 == NULL)
6467 return FAIL;
6468
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006470 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471 if (l == NULL)
6472 return FAIL;
6473 tv->v_type = VAR_LIST;
6474 tv->vval.v_list = l;
6475
6476 /* append all items from the second list */
6477 return list_extend(l, l2, NULL);
6478}
6479
6480/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006481 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006483 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006484 * Returns NULL when out of memory.
6485 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006487list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006490 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491{
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 list_T *copy;
6493 listitem_T *item;
6494 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495
6496 if (orig == NULL)
6497 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498
6499 copy = list_alloc();
6500 if (copy != NULL)
6501 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006502 if (copyID != 0)
6503 {
6504 /* Do this before adding the items, because one of the items may
6505 * refer back to this list. */
6506 orig->lv_copyID = copyID;
6507 orig->lv_copylist = copy;
6508 }
6509 for (item = orig->lv_first; item != NULL && !got_int;
6510 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 {
6512 ni = listitem_alloc();
6513 if (ni == NULL)
6514 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006515 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006516 {
6517 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6518 {
6519 vim_free(ni);
6520 break;
6521 }
6522 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006524 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525 list_append(copy, ni);
6526 }
6527 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006528 if (item != NULL)
6529 {
6530 list_unref(copy);
6531 copy = NULL;
6532 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 }
6534
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535 return copy;
6536}
6537
6538/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006540 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006542 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006543list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 list_T *l;
6545 listitem_T *item;
6546 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547{
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 /* notify watchers */
6551 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006553 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 list_fix_watch(l, ip);
6555 if (ip == item2)
6556 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558
6559 if (item2->li_next == NULL)
6560 l->lv_last = item->li_prev;
6561 else
6562 item2->li_next->li_prev = item->li_prev;
6563 if (item->li_prev == NULL)
6564 l->lv_first = item2->li_next;
6565 else
6566 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006567 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568}
6569
6570/*
6571 * Return an allocated string with the string representation of a list.
6572 * May return NULL.
6573 */
6574 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006575list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006577 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578{
6579 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580
6581 if (tv->vval.v_list == NULL)
6582 return NULL;
6583 ga_init2(&ga, (int)sizeof(char), 80);
6584 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006585 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 {
6587 vim_free(ga.ga_data);
6588 return NULL;
6589 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590 ga_append(&ga, ']');
6591 ga_append(&ga, NUL);
6592 return (char_u *)ga.ga_data;
6593}
6594
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006595typedef struct join_S {
6596 char_u *s;
6597 char_u *tofree;
6598} join_T;
6599
6600 static int
6601list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6602 garray_T *gap; /* to store the result in */
6603 list_T *l;
6604 char_u *sep;
6605 int echo_style;
6606 int copyID;
6607 garray_T *join_gap; /* to keep each list item string */
6608{
6609 int i;
6610 join_T *p;
6611 int len;
6612 int sumlen = 0;
6613 int first = TRUE;
6614 char_u *tofree;
6615 char_u numbuf[NUMBUFLEN];
6616 listitem_T *item;
6617 char_u *s;
6618
6619 /* Stringify each item in the list. */
6620 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6621 {
6622 if (echo_style)
6623 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6624 else
6625 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6626 if (s == NULL)
6627 return FAIL;
6628
6629 len = (int)STRLEN(s);
6630 sumlen += len;
6631
6632 ga_grow(join_gap, 1);
6633 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6634 if (tofree != NULL || s != numbuf)
6635 {
6636 p->s = s;
6637 p->tofree = tofree;
6638 }
6639 else
6640 {
6641 p->s = vim_strnsave(s, len);
6642 p->tofree = p->s;
6643 }
6644
6645 line_breakcheck();
6646 }
6647
6648 /* Allocate result buffer with its total size, avoid re-allocation and
6649 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6650 if (join_gap->ga_len >= 2)
6651 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6652 if (ga_grow(gap, sumlen + 2) == FAIL)
6653 return FAIL;
6654
6655 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6656 {
6657 if (first)
6658 first = FALSE;
6659 else
6660 ga_concat(gap, sep);
6661 p = ((join_T *)join_gap->ga_data) + i;
6662
6663 if (p->s != NULL)
6664 ga_concat(gap, p->s);
6665 line_breakcheck();
6666 }
6667
6668 return OK;
6669}
6670
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006672 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006673 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006674 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006675 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006676 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006677list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006678 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006679 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006681 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006684 garray_T join_ga;
6685 int retval;
6686 join_T *p;
6687 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006689 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6690 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6691
6692 /* Dispose each item in join_ga. */
6693 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006694 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006695 p = (join_T *)join_ga.ga_data;
6696 for (i = 0; i < join_ga.ga_len; ++i)
6697 {
6698 vim_free(p->tofree);
6699 ++p;
6700 }
6701 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006702 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006703
6704 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006705}
6706
6707/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006708 * Garbage collection for lists and dictionaries.
6709 *
6710 * We use reference counts to be able to free most items right away when they
6711 * are no longer used. But for composite items it's possible that it becomes
6712 * unused while the reference count is > 0: When there is a recursive
6713 * reference. Example:
6714 * :let l = [1, 2, 3]
6715 * :let d = {9: l}
6716 * :let l[1] = d
6717 *
6718 * Since this is quite unusual we handle this with garbage collection: every
6719 * once in a while find out which lists and dicts are not referenced from any
6720 * variable.
6721 *
6722 * Here is a good reference text about garbage collection (refers to Python
6723 * but it applies to all reference-counting mechanisms):
6724 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006725 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006726
6727/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 * Do garbage collection for lists and dicts.
6729 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006730 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006731 int
6732garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006734 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006735 buf_T *buf;
6736 win_T *wp;
6737 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006738 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006739 int did_free;
6740 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006741#ifdef FEAT_WINDOWS
6742 tabpage_T *tp;
6743#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006745 /* Only do this once. */
6746 want_garbage_collect = FALSE;
6747 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006748 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006749
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006750 /* We advance by two because we add one for items referenced through
6751 * previous_funccal. */
6752 current_copyID += COPYID_INC;
6753 copyID = current_copyID;
6754
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006755 /*
6756 * 1. Go through all accessible variables and mark all lists and dicts
6757 * with copyID.
6758 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006759
6760 /* Don't free variables in the previous_funccal list unless they are only
6761 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006762 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006763 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6764 {
6765 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6766 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6767 }
6768
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006769 /* script-local variables */
6770 for (i = 1; i <= ga_scripts.ga_len; ++i)
6771 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6772
6773 /* buffer-local variables */
6774 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006775 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006776
6777 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006778 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006779 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006780#ifdef FEAT_AUTOCMD
6781 if (aucmd_win != NULL)
6782 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6783#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006785#ifdef FEAT_WINDOWS
6786 /* tabpage-local variables */
6787 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006788 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006789#endif
6790
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 /* global variables */
6792 set_ref_in_ht(&globvarht, copyID);
6793
6794 /* function-local variables */
6795 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6796 {
6797 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6798 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6799 }
6800
Bram Moolenaard812df62008-11-09 12:46:09 +00006801 /* v: vars */
6802 set_ref_in_ht(&vimvarht, copyID);
6803
Bram Moolenaar1dced572012-04-05 16:54:08 +02006804#ifdef FEAT_LUA
6805 set_ref_in_lua(copyID);
6806#endif
6807
Bram Moolenaardb913952012-06-29 12:54:53 +02006808#ifdef FEAT_PYTHON
6809 set_ref_in_python(copyID);
6810#endif
6811
6812#ifdef FEAT_PYTHON3
6813 set_ref_in_python3(copyID);
6814#endif
6815
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006816 /*
6817 * 2. Free lists and dictionaries that are not referenced.
6818 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006819 did_free = free_unref_items(copyID);
6820
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006821 /*
6822 * 3. Check if any funccal can be freed now.
6823 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006824 for (pfc = &previous_funccal; *pfc != NULL; )
6825 {
6826 if (can_free_funccal(*pfc, copyID))
6827 {
6828 fc = *pfc;
6829 *pfc = fc->caller;
6830 free_funccal(fc, TRUE);
6831 did_free = TRUE;
6832 did_free_funccal = TRUE;
6833 }
6834 else
6835 pfc = &(*pfc)->caller;
6836 }
6837 if (did_free_funccal)
6838 /* When a funccal was freed some more items might be garbage
6839 * collected, so run again. */
6840 (void)garbage_collect();
6841
6842 return did_free;
6843}
6844
6845/*
6846 * Free lists and dictionaries that are no longer referenced.
6847 */
6848 static int
6849free_unref_items(copyID)
6850 int copyID;
6851{
6852 dict_T *dd;
6853 list_T *ll;
6854 int did_free = FALSE;
6855
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 */
6859 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006862 /* Free the Dictionary and ordinary items it contains, but don't
6863 * recurse into Lists and Dictionaries, they will be in the list
6864 * of dicts or list of lists. */
6865 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 did_free = TRUE;
6867
6868 /* restart, next dict may also have been freed */
6869 dd = first_dict;
6870 }
6871 else
6872 dd = dd->dv_used_next;
6873
6874 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 * Go through the list of lists and free items without the copyID.
6876 * But don't free a list that has a watcher (used in a for loop), these
6877 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 */
6879 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6881 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006883 /* Free the List and ordinary items it contains, but don't recurse
6884 * into Lists and Dictionaries, they will be in the list of dicts
6885 * or list of lists. */
6886 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 did_free = TRUE;
6888
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006889 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 ll = first_list;
6891 }
6892 else
6893 ll = ll->lv_used_next;
6894
6895 return did_free;
6896}
6897
6898/*
6899 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6900 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006901 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902set_ref_in_ht(ht, copyID)
6903 hashtab_T *ht;
6904 int copyID;
6905{
6906 int todo;
6907 hashitem_T *hi;
6908
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006909 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 for (hi = ht->ht_array; todo > 0; ++hi)
6911 if (!HASHITEM_EMPTY(hi))
6912 {
6913 --todo;
6914 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6915 }
6916}
6917
6918/*
6919 * Mark all lists and dicts referenced through list "l" with "copyID".
6920 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006921 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922set_ref_in_list(l, copyID)
6923 list_T *l;
6924 int copyID;
6925{
6926 listitem_T *li;
6927
6928 for (li = l->lv_first; li != NULL; li = li->li_next)
6929 set_ref_in_item(&li->li_tv, copyID);
6930}
6931
6932/*
6933 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6934 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006935 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936set_ref_in_item(tv, copyID)
6937 typval_T *tv;
6938 int copyID;
6939{
6940 dict_T *dd;
6941 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006942
6943 switch (tv->v_type)
6944 {
6945 case VAR_DICT:
6946 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006947 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006948 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 /* Didn't see this dict yet. */
6950 dd->dv_copyID = copyID;
6951 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006952 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006954
6955 case VAR_LIST:
6956 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006957 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006958 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 /* Didn't see this list yet. */
6960 ll->lv_copyID = copyID;
6961 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006962 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006964 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006966}
6967
6968/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969 * Allocate an empty header for a dictionary.
6970 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006971 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972dict_alloc()
6973{
Bram Moolenaar33570922005-01-25 22:26:29 +00006974 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006977 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006978 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006979 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 if (first_dict != NULL)
6981 first_dict->dv_used_prev = d;
6982 d->dv_used_next = first_dict;
6983 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006984 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985
Bram Moolenaar33570922005-01-25 22:26:29 +00006986 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006987 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006988 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006989 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006990 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006991 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006992 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006993}
6994
6995/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006996 * Allocate an empty dict for a return value.
6997 * Returns OK or FAIL.
6998 */
6999 static int
7000rettv_dict_alloc(rettv)
7001 typval_T *rettv;
7002{
7003 dict_T *d = dict_alloc();
7004
7005 if (d == NULL)
7006 return FAIL;
7007
7008 rettv->vval.v_dict = d;
7009 rettv->v_type = VAR_DICT;
7010 ++d->dv_refcount;
7011 return OK;
7012}
7013
7014
7015/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007016 * Unreference a Dictionary: decrement the reference count and free it when it
7017 * becomes zero.
7018 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007019 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007020dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007021 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007023 if (d != NULL && --d->dv_refcount <= 0)
7024 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025}
7026
7027/*
7028 * Free a Dictionary, including all items it contains.
7029 * Ignores the reference count.
7030 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007031 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007032dict_free(d, recurse)
7033 dict_T *d;
7034 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007036 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007038 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007039
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 /* Remove the dict from the list of dicts for garbage collection. */
7041 if (d->dv_used_prev == NULL)
7042 first_dict = d->dv_used_next;
7043 else
7044 d->dv_used_prev->dv_used_next = d->dv_used_next;
7045 if (d->dv_used_next != NULL)
7046 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7047
7048 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007050 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007051 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007053 if (!HASHITEM_EMPTY(hi))
7054 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007055 /* Remove the item before deleting it, just in case there is
7056 * something recursive causing trouble. */
7057 di = HI2DI(hi);
7058 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007059 if (recurse || (di->di_tv.v_type != VAR_LIST
7060 && di->di_tv.v_type != VAR_DICT))
7061 clear_tv(&di->di_tv);
7062 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007063 --todo;
7064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007067 vim_free(d);
7068}
7069
7070/*
7071 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007072 * The "key" is copied to the new item.
7073 * Note that the value of the item "di_tv" still needs to be initialized!
7074 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007076 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007077dictitem_alloc(key)
7078 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007079{
Bram Moolenaar33570922005-01-25 22:26:29 +00007080 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007082 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007086 di->di_flags = 0;
7087 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089}
7090
7091/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092 * Make a copy of a Dictionary item.
7093 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007094 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007095dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007096 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007097{
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007100 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7101 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007102 if (di != NULL)
7103 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007104 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007105 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106 copy_tv(&org->di_tv, &di->di_tv);
7107 }
7108 return di;
7109}
7110
7111/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 * Remove item "item" from Dictionary "dict" and free it.
7113 */
7114 static void
7115dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 dict_T *dict;
7117 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118{
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 if (HASHITEM_EMPTY(hi))
7123 EMSG2(_(e_intern2), "dictitem_remove()");
7124 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126 dictitem_free(item);
7127}
7128
7129/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 * Free a dict item. Also clears the value.
7131 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007132 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136 clear_tv(&item->di_tv);
7137 vim_free(item);
7138}
7139
7140/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7142 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007143 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 * Returns NULL when out of memory.
7145 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007147dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007150 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007151{
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 dict_T *copy;
7153 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007154 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007156
7157 if (orig == NULL)
7158 return NULL;
7159
7160 copy = dict_alloc();
7161 if (copy != NULL)
7162 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007163 if (copyID != 0)
7164 {
7165 orig->dv_copyID = copyID;
7166 orig->dv_copydict = copy;
7167 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007168 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007169 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007171 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007172 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007173 --todo;
7174
7175 di = dictitem_alloc(hi->hi_key);
7176 if (di == NULL)
7177 break;
7178 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007179 {
7180 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7181 copyID) == FAIL)
7182 {
7183 vim_free(di);
7184 break;
7185 }
7186 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007187 else
7188 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7189 if (dict_add(copy, di) == FAIL)
7190 {
7191 dictitem_free(di);
7192 break;
7193 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007195 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 if (todo > 0)
7199 {
7200 dict_unref(copy);
7201 copy = NULL;
7202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
7204
7205 return copy;
7206}
7207
7208/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007210 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007212 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 dict_T *d;
7215 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216{
Bram Moolenaar33570922005-01-25 22:26:29 +00007217 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218}
7219
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007221 * Add a number or string entry to dictionary "d".
7222 * When "str" is NULL use number "nr", otherwise use "str".
7223 * Returns FAIL when out of memory and when key already exists.
7224 */
7225 int
7226dict_add_nr_str(d, key, nr, str)
7227 dict_T *d;
7228 char *key;
7229 long nr;
7230 char_u *str;
7231{
7232 dictitem_T *item;
7233
7234 item = dictitem_alloc((char_u *)key);
7235 if (item == NULL)
7236 return FAIL;
7237 item->di_tv.v_lock = 0;
7238 if (str == NULL)
7239 {
7240 item->di_tv.v_type = VAR_NUMBER;
7241 item->di_tv.vval.v_number = nr;
7242 }
7243 else
7244 {
7245 item->di_tv.v_type = VAR_STRING;
7246 item->di_tv.vval.v_string = vim_strsave(str);
7247 }
7248 if (dict_add(d, item) == FAIL)
7249 {
7250 dictitem_free(item);
7251 return FAIL;
7252 }
7253 return OK;
7254}
7255
7256/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007257 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007258 * Returns FAIL when out of memory and when key already exists.
7259 */
7260 int
7261dict_add_list(d, key, list)
7262 dict_T *d;
7263 char *key;
7264 list_T *list;
7265{
7266 dictitem_T *item;
7267
7268 item = dictitem_alloc((char_u *)key);
7269 if (item == NULL)
7270 return FAIL;
7271 item->di_tv.v_lock = 0;
7272 item->di_tv.v_type = VAR_LIST;
7273 item->di_tv.vval.v_list = list;
7274 if (dict_add(d, item) == FAIL)
7275 {
7276 dictitem_free(item);
7277 return FAIL;
7278 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007279 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007280 return OK;
7281}
7282
7283/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007284 * Get the number of items in a Dictionary.
7285 */
7286 static long
7287dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 if (d == NULL)
7291 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007292 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293}
7294
7295/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296 * Find item "key[len]" in Dictionary "d".
7297 * If "len" is negative use strlen(key).
7298 * Returns NULL when not found.
7299 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007300 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303 char_u *key;
7304 int len;
7305{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306#define AKEYLEN 200
7307 char_u buf[AKEYLEN];
7308 char_u *akey;
7309 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 if (len < 0)
7313 akey = key;
7314 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007315 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 tofree = akey = vim_strnsave(key, len);
7317 if (akey == NULL)
7318 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007319 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 else
7321 {
7322 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007323 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 akey = buf;
7325 }
7326
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 vim_free(tofree);
7329 if (HASHITEM_EMPTY(hi))
7330 return NULL;
7331 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332}
7333
7334/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007335 * Get a string item from a dictionary.
7336 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007337 * Returns NULL if the entry doesn't exist or out of memory.
7338 */
7339 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007340get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007341 dict_T *d;
7342 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007344{
7345 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007347
7348 di = dict_find(d, key, -1);
7349 if (di == NULL)
7350 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351 s = get_tv_string(&di->di_tv);
7352 if (save && s != NULL)
7353 s = vim_strsave(s);
7354 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355}
7356
7357/*
7358 * Get a number item from a dictionary.
7359 * Returns 0 if the entry doesn't exist or out of memory.
7360 */
7361 long
7362get_dict_number(d, key)
7363 dict_T *d;
7364 char_u *key;
7365{
7366 dictitem_T *di;
7367
7368 di = dict_find(d, key, -1);
7369 if (di == NULL)
7370 return 0;
7371 return get_tv_number(&di->di_tv);
7372}
7373
7374/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 * Return an allocated string with the string representation of a Dictionary.
7376 * May return NULL.
7377 */
7378 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007379dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007381 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382{
7383 garray_T ga;
7384 int first = TRUE;
7385 char_u *tofree;
7386 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007387 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 return NULL;
7394 ga_init2(&ga, (int)sizeof(char), 80);
7395 ga_append(&ga, '{');
7396
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007397 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007398 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 --todo;
7403
7404 if (first)
7405 first = FALSE;
7406 else
7407 ga_concat(&ga, (char_u *)", ");
7408
7409 tofree = string_quote(hi->hi_key, FALSE);
7410 if (tofree != NULL)
7411 {
7412 ga_concat(&ga, tofree);
7413 vim_free(tofree);
7414 }
7415 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007416 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007417 if (s != NULL)
7418 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 if (s == NULL)
7421 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007424 if (todo > 0)
7425 {
7426 vim_free(ga.ga_data);
7427 return NULL;
7428 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429
7430 ga_append(&ga, '}');
7431 ga_append(&ga, NUL);
7432 return (char_u *)ga.ga_data;
7433}
7434
7435/*
7436 * Allocate a variable for a Dictionary and fill it from "*arg".
7437 * Return OK or FAIL. Returns NOTDONE for {expr}.
7438 */
7439 static int
7440get_dict_tv(arg, rettv, evaluate)
7441 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 int evaluate;
7444{
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 dict_T *d = NULL;
7446 typval_T tvkey;
7447 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007448 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007451 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452
7453 /*
7454 * First check if it's not a curly-braces thing: {expr}.
7455 * Must do this without evaluating, otherwise a function may be called
7456 * twice. Unfortunately this means we need to call eval1() twice for the
7457 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007458 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007460 if (*start != '}')
7461 {
7462 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7463 return FAIL;
7464 if (*start == '}')
7465 return NOTDONE;
7466 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467
7468 if (evaluate)
7469 {
7470 d = dict_alloc();
7471 if (d == NULL)
7472 return FAIL;
7473 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007474 tvkey.v_type = VAR_UNKNOWN;
7475 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476
7477 *arg = skipwhite(*arg + 1);
7478 while (**arg != '}' && **arg != NUL)
7479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481 goto failret;
7482 if (**arg != ':')
7483 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007484 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 goto failret;
7487 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007488 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007490 key = get_tv_string_buf_chk(&tvkey, buf);
7491 if (key == NULL || *key == NUL)
7492 {
7493 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7494 if (key != NULL)
7495 EMSG(_(e_emptykey));
7496 clear_tv(&tvkey);
7497 goto failret;
7498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500
7501 *arg = skipwhite(*arg + 1);
7502 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7503 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007504 if (evaluate)
7505 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 goto failret;
7507 }
7508 if (evaluate)
7509 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 if (item != NULL)
7512 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007513 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 clear_tv(&tv);
7516 goto failret;
7517 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 item = dictitem_alloc(key);
7519 clear_tv(&tvkey);
7520 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007523 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 if (dict_add(d, item) == FAIL)
7525 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 }
7527 }
7528
7529 if (**arg == '}')
7530 break;
7531 if (**arg != ',')
7532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007533 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 goto failret;
7535 }
7536 *arg = skipwhite(*arg + 1);
7537 }
7538
7539 if (**arg != '}')
7540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007541 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542failret:
7543 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007544 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 return FAIL;
7546 }
7547
7548 *arg = skipwhite(*arg + 1);
7549 if (evaluate)
7550 {
7551 rettv->v_type = VAR_DICT;
7552 rettv->vval.v_dict = d;
7553 ++d->dv_refcount;
7554 }
7555
7556 return OK;
7557}
7558
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007560 * Return a string with the string representation of a variable.
7561 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007562 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007563 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007564 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007565 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007566 */
7567 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007568echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007570 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007571 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007572 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007573{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 static int recurse = 0;
7575 char_u *r = NULL;
7576
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007578 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007579 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007580 *tofree = NULL;
7581 return NULL;
7582 }
7583 ++recurse;
7584
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007585 switch (tv->v_type)
7586 {
7587 case VAR_FUNC:
7588 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 r = tv->vval.v_string;
7590 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007591
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007592 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 if (tv->vval.v_list == NULL)
7594 {
7595 *tofree = NULL;
7596 r = NULL;
7597 }
7598 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7599 {
7600 *tofree = NULL;
7601 r = (char_u *)"[...]";
7602 }
7603 else
7604 {
7605 tv->vval.v_list->lv_copyID = copyID;
7606 *tofree = list2string(tv, copyID);
7607 r = *tofree;
7608 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007610
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007612 if (tv->vval.v_dict == NULL)
7613 {
7614 *tofree = NULL;
7615 r = NULL;
7616 }
7617 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7618 {
7619 *tofree = NULL;
7620 r = (char_u *)"{...}";
7621 }
7622 else
7623 {
7624 tv->vval.v_dict->dv_copyID = copyID;
7625 *tofree = dict2string(tv, copyID);
7626 r = *tofree;
7627 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630 case VAR_STRING:
7631 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007632 *tofree = NULL;
7633 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007635
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007636#ifdef FEAT_FLOAT
7637 case VAR_FLOAT:
7638 *tofree = NULL;
7639 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7640 r = numbuf;
7641 break;
7642#endif
7643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007644 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007645 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007646 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648
7649 --recurse;
7650 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007651}
7652
7653/*
7654 * Return a string with the string representation of a variable.
7655 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7656 * "numbuf" is used for a number.
7657 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007658 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007659 */
7660 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007661tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007663 char_u **tofree;
7664 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007665 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007666{
7667 switch (tv->v_type)
7668 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007669 case VAR_FUNC:
7670 *tofree = string_quote(tv->vval.v_string, TRUE);
7671 return *tofree;
7672 case VAR_STRING:
7673 *tofree = string_quote(tv->vval.v_string, FALSE);
7674 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#ifdef FEAT_FLOAT
7676 case VAR_FLOAT:
7677 *tofree = NULL;
7678 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7679 return numbuf;
7680#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007685 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007687 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007688 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007689}
7690
7691/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 * Return string "str" in ' quotes, doubling ' characters.
7693 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695 */
7696 static char_u *
7697string_quote(str, function)
7698 char_u *str;
7699 int function;
7700{
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702 char_u *p, *r, *s;
7703
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 len = (function ? 13 : 3);
7705 if (str != NULL)
7706 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007707 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 for (p = str; *p != NUL; mb_ptr_adv(p))
7709 if (*p == '\'')
7710 ++len;
7711 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007712 s = r = alloc(len);
7713 if (r != NULL)
7714 {
7715 if (function)
7716 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007718 r += 10;
7719 }
7720 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 if (str != NULL)
7723 for (p = str; *p != NUL; )
7724 {
7725 if (*p == '\'')
7726 *r++ = '\'';
7727 MB_COPY_CHAR(p, r);
7728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007730 if (function)
7731 *r++ = ')';
7732 *r++ = NUL;
7733 }
7734 return s;
7735}
7736
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007737#ifdef FEAT_FLOAT
7738/*
7739 * Convert the string "text" to a floating point number.
7740 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7741 * this always uses a decimal point.
7742 * Returns the length of the text that was consumed.
7743 */
7744 static int
7745string2float(text, value)
7746 char_u *text;
7747 float_T *value; /* result stored here */
7748{
7749 char *s = (char *)text;
7750 float_T f;
7751
7752 f = strtod(s, &s);
7753 *value = f;
7754 return (int)((char_u *)s - text);
7755}
7756#endif
7757
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 * Get the value of an environment variable.
7760 * "arg" is pointing to the '$'. It is advanced to after the name.
7761 * If the environment variable was not set, silently assume it is empty.
7762 * Always return OK.
7763 */
7764 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 int evaluate;
7769{
7770 char_u *string = NULL;
7771 int len;
7772 int cc;
7773 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007774 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
7776 ++*arg;
7777 name = *arg;
7778 len = get_env_len(arg);
7779 if (evaluate)
7780 {
7781 if (len != 0)
7782 {
7783 cc = name[len];
7784 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 /* first try vim_getenv(), fast for normal environment vars */
7786 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007788 {
7789 if (!mustfree)
7790 string = vim_strsave(string);
7791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 else
7793 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007794 if (mustfree)
7795 vim_free(string);
7796
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 /* next try expanding things like $VIM and ${HOME} */
7798 string = expand_env_save(name - 1);
7799 if (string != NULL && *string == '$')
7800 {
7801 vim_free(string);
7802 string = NULL;
7803 }
7804 }
7805 name[len] = cc;
7806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807 rettv->v_type = VAR_STRING;
7808 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 }
7810
7811 return OK;
7812}
7813
7814/*
7815 * Array with names and number of arguments of all internal functions
7816 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7817 */
7818static struct fst
7819{
7820 char *f_name; /* function name */
7821 char f_min_argc; /* minimal number of arguments */
7822 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007823 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007824 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825} functions[] =
7826{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827#ifdef FEAT_FLOAT
7828 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007829 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007830#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007831 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007832 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {"append", 2, 2, f_append},
7834 {"argc", 0, 0, f_argc},
7835 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007836 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007837#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007838 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007839 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007840 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007843 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"bufexists", 1, 1, f_bufexists},
7845 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7846 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7847 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7848 {"buflisted", 1, 1, f_buflisted},
7849 {"bufloaded", 1, 1, f_bufloaded},
7850 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007851 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"bufwinnr", 1, 1, f_bufwinnr},
7853 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007854 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007856#ifdef FEAT_FLOAT
7857 {"ceil", 1, 1, f_ceil},
7858#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007859 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007860 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007862 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007864#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007865 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007866 {"complete_add", 1, 1, f_complete_add},
7867 {"complete_check", 0, 0, f_complete_check},
7868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007870 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007873 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007875 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007877 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007878 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"delete", 1, 1, f_delete},
7880 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007881 {"diff_filler", 1, 1, f_diff_filler},
7882 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007883 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007885 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"eventhandler", 0, 0, f_eventhandler},
7887 {"executable", 1, 1, f_executable},
7888 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007889#ifdef FEAT_FLOAT
7890 {"exp", 1, 1, f_exp},
7891#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007892 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007893 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007894 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7896 {"filereadable", 1, 1, f_filereadable},
7897 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007899 {"finddir", 1, 3, f_finddir},
7900 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#ifdef FEAT_FLOAT
7902 {"float2nr", 1, 1, f_float2nr},
7903 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007904 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007905#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007906 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {"fnamemodify", 2, 2, f_fnamemodify},
7908 {"foldclosed", 1, 1, f_foldclosed},
7909 {"foldclosedend", 1, 1, f_foldclosedend},
7910 {"foldlevel", 1, 1, f_foldlevel},
7911 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007912 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007915 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007916 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007917 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007918 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"getchar", 0, 1, f_getchar},
7920 {"getcharmod", 0, 0, f_getcharmod},
7921 {"getcmdline", 0, 0, f_getcmdline},
7922 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007923 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007925 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007926 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"getfsize", 1, 1, f_getfsize},
7928 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007929 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007930 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007931 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007932 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007933 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007934 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007935 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007936 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007938 {"gettabvar", 2, 3, f_gettabvar},
7939 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {"getwinposx", 0, 0, f_getwinposx},
7941 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007942 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007943 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007944 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007946 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007947 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007948 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7950 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7951 {"histadd", 2, 2, f_histadd},
7952 {"histdel", 1, 2, f_histdel},
7953 {"histget", 1, 2, f_histget},
7954 {"histnr", 1, 1, f_histnr},
7955 {"hlID", 1, 1, f_hlID},
7956 {"hlexists", 1, 1, f_hlexists},
7957 {"hostname", 0, 0, f_hostname},
7958 {"iconv", 3, 3, f_iconv},
7959 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007961 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007963 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 {"inputrestore", 0, 0, f_inputrestore},
7965 {"inputsave", 0, 0, f_inputsave},
7966 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007968 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007970 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007973 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {"libcall", 3, 3, f_libcall},
7977 {"libcallnr", 3, 3, f_libcallnr},
7978 {"line", 1, 1, f_line},
7979 {"line2byte", 1, 1, f_line2byte},
7980 {"lispindent", 1, 1, f_lispindent},
7981 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007982#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007983 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007984 {"log10", 1, 1, f_log10},
7985#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007986#ifdef FEAT_LUA
7987 {"luaeval", 1, 2, f_luaeval},
7988#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007989 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007990 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007991 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007992 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007993 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007994 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007995 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007996 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007997 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007998 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007999 {"max", 1, 1, f_max},
8000 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008001#ifdef vim_mkdir
8002 {"mkdir", 1, 3, f_mkdir},
8003#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008004 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008005#ifdef FEAT_MZSCHEME
8006 {"mzeval", 1, 1, f_mzeval},
8007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008009 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008010 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008011 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012#ifdef FEAT_FLOAT
8013 {"pow", 2, 2, f_pow},
8014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008016 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008017 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008018#ifdef FEAT_PYTHON3
8019 {"py3eval", 1, 1, f_py3eval},
8020#endif
8021#ifdef FEAT_PYTHON
8022 {"pyeval", 1, 1, f_pyeval},
8023#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008024 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008025 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008026 {"reltime", 0, 2, f_reltime},
8027 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {"remote_expr", 2, 3, f_remote_expr},
8029 {"remote_foreground", 1, 1, f_remote_foreground},
8030 {"remote_peek", 1, 2, f_remote_peek},
8031 {"remote_read", 1, 1, f_remote_read},
8032 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008033 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008035 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008037 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008038#ifdef FEAT_FLOAT
8039 {"round", 1, 1, f_round},
8040#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008041 {"screencol", 0, 0, f_screencol},
8042 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008043 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008044 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008045 {"searchpair", 3, 7, f_searchpair},
8046 {"searchpairpos", 3, 7, f_searchpairpos},
8047 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {"server2client", 2, 2, f_server2client},
8049 {"serverlist", 0, 0, f_serverlist},
8050 {"setbufvar", 3, 3, f_setbufvar},
8051 {"setcmdpos", 1, 1, f_setcmdpos},
8052 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008053 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008054 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008055 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008056 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008058 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008059 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008061#ifdef FEAT_CRYPT
8062 {"sha256", 1, 1, f_sha256},
8063#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008064 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008065 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067#ifdef FEAT_FLOAT
8068 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008069 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008071 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008072 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008073 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008074 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008075 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"sqrt", 1, 1, f_sqrt},
8078 {"str2float", 1, 1, f_str2float},
8079#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008080 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008081 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008082 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083#ifdef HAVE_STRFTIME
8084 {"strftime", 1, 2, f_strftime},
8085#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008086 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008087 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"strlen", 1, 1, f_strlen},
8089 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008090 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008092 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"submatch", 1, 1, f_submatch},
8094 {"substitute", 4, 4, f_substitute},
8095 {"synID", 3, 3, f_synID},
8096 {"synIDattr", 2, 3, f_synIDattr},
8097 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008098 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008099 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008100 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008101 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008102 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008103 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008104 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008105 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008106#ifdef FEAT_FLOAT
8107 {"tan", 1, 1, f_tan},
8108 {"tanh", 1, 1, f_tanh},
8109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008111 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"tolower", 1, 1, f_tolower},
8113 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008114 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008115#ifdef FEAT_FLOAT
8116 {"trunc", 1, 1, f_trunc},
8117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008119 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008120 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008121 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"virtcol", 1, 1, f_virtcol},
8123 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008124 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"winbufnr", 1, 1, f_winbufnr},
8126 {"wincol", 0, 0, f_wincol},
8127 {"winheight", 1, 1, f_winheight},
8128 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008129 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008131 {"winrestview", 1, 1, f_winrestview},
8132 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008134 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008135 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136};
8137
8138#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8139
8140/*
8141 * Function given to ExpandGeneric() to obtain the list of internal
8142 * or user defined function names.
8143 */
8144 char_u *
8145get_function_name(xp, idx)
8146 expand_T *xp;
8147 int idx;
8148{
8149 static int intidx = -1;
8150 char_u *name;
8151
8152 if (idx == 0)
8153 intidx = -1;
8154 if (intidx < 0)
8155 {
8156 name = get_user_func_name(xp, idx);
8157 if (name != NULL)
8158 return name;
8159 }
8160 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8161 {
8162 STRCPY(IObuff, functions[intidx].f_name);
8163 STRCAT(IObuff, "(");
8164 if (functions[intidx].f_max_argc == 0)
8165 STRCAT(IObuff, ")");
8166 return IObuff;
8167 }
8168
8169 return NULL;
8170}
8171
8172/*
8173 * Function given to ExpandGeneric() to obtain the list of internal or
8174 * user defined variable or function names.
8175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 char_u *
8177get_expr_name(xp, idx)
8178 expand_T *xp;
8179 int idx;
8180{
8181 static int intidx = -1;
8182 char_u *name;
8183
8184 if (idx == 0)
8185 intidx = -1;
8186 if (intidx < 0)
8187 {
8188 name = get_function_name(xp, idx);
8189 if (name != NULL)
8190 return name;
8191 }
8192 return get_user_var_name(xp, ++intidx);
8193}
8194
8195#endif /* FEAT_CMDL_COMPL */
8196
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008197#if defined(EBCDIC) || defined(PROTO)
8198/*
8199 * Compare struct fst by function name.
8200 */
8201 static int
8202compare_func_name(s1, s2)
8203 const void *s1;
8204 const void *s2;
8205{
8206 struct fst *p1 = (struct fst *)s1;
8207 struct fst *p2 = (struct fst *)s2;
8208
8209 return STRCMP(p1->f_name, p2->f_name);
8210}
8211
8212/*
8213 * Sort the function table by function name.
8214 * The sorting of the table above is ASCII dependant.
8215 * On machines using EBCDIC we have to sort it.
8216 */
8217 static void
8218sortFunctions()
8219{
8220 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8221
8222 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8223}
8224#endif
8225
8226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227/*
8228 * Find internal function in table above.
8229 * Return index, or -1 if not found
8230 */
8231 static int
8232find_internal_func(name)
8233 char_u *name; /* name of the function */
8234{
8235 int first = 0;
8236 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8237 int cmp;
8238 int x;
8239
8240 /*
8241 * Find the function name in the table. Binary search.
8242 */
8243 while (first <= last)
8244 {
8245 x = first + ((unsigned)(last - first) >> 1);
8246 cmp = STRCMP(name, functions[x].f_name);
8247 if (cmp < 0)
8248 last = x - 1;
8249 else if (cmp > 0)
8250 first = x + 1;
8251 else
8252 return x;
8253 }
8254 return -1;
8255}
8256
8257/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008258 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8259 * name it contains, otherwise return "name".
8260 */
8261 static char_u *
8262deref_func_name(name, lenp)
8263 char_u *name;
8264 int *lenp;
8265{
Bram Moolenaar33570922005-01-25 22:26:29 +00008266 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008267 int cc;
8268
8269 cc = name[*lenp];
8270 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008271 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008272 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008273 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008274 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008275 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008276 {
8277 *lenp = 0;
8278 return (char_u *)""; /* just in case */
8279 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008280 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 }
8283
8284 return name;
8285}
8286
8287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 * Allocate a variable for the result of a function.
8289 * Return OK or FAIL.
8290 */
8291 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8293 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 char_u *name; /* name of the function */
8295 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 char_u **arg; /* argument, pointing to the '(' */
8298 linenr_T firstline; /* first line of range */
8299 linenr_T lastline; /* last line of range */
8300 int *doesrange; /* return: function handled range */
8301 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 char_u *argp;
8305 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008306 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 int argcount = 0; /* number of arguments found */
8308
8309 /*
8310 * Get the arguments.
8311 */
8312 argp = *arg;
8313 while (argcount < MAX_FUNC_ARGS)
8314 {
8315 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8316 if (*argp == ')' || *argp == ',' || *argp == NUL)
8317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8319 {
8320 ret = FAIL;
8321 break;
8322 }
8323 ++argcount;
8324 if (*argp != ',')
8325 break;
8326 }
8327 if (*argp == ')')
8328 ++argp;
8329 else
8330 ret = FAIL;
8331
8332 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008333 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008334 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008336 {
8337 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008338 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008340 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
8343 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345
8346 *arg = skipwhite(argp);
8347 return ret;
8348}
8349
8350
8351/*
8352 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008353 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008354 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 */
8356 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008357call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008358 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008359 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008363 typval_T *argvars; /* vars for arguments, must have "argcount"
8364 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 linenr_T firstline; /* first line of range */
8366 linenr_T lastline; /* last line of range */
8367 int *doesrange; /* return: function handled range */
8368 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372#define ERROR_UNKNOWN 0
8373#define ERROR_TOOMANY 1
8374#define ERROR_TOOFEW 2
8375#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008376#define ERROR_DICT 4
8377#define ERROR_NONE 5
8378#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 int error = ERROR_NONE;
8380 int i;
8381 int llen;
8382 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#define FLEN_FIXED 40
8384 char_u fname_buf[FLEN_FIXED + 1];
8385 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008386 char_u *name;
8387
8388 /* Make a copy of the name, if it comes from a funcref variable it could
8389 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008390 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008391 if (name == NULL)
8392 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
8394 /*
8395 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8396 * Change <SNR>123_name() to K_SNR 123_name().
8397 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 llen = eval_fname_script(name);
8400 if (llen > 0)
8401 {
8402 fname_buf[0] = K_SPECIAL;
8403 fname_buf[1] = KS_EXTRA;
8404 fname_buf[2] = (int)KE_SNR;
8405 i = 3;
8406 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8407 {
8408 if (current_SID <= 0)
8409 error = ERROR_SCRIPT;
8410 else
8411 {
8412 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8413 i = (int)STRLEN(fname_buf);
8414 }
8415 }
8416 if (i + STRLEN(name + llen) < FLEN_FIXED)
8417 {
8418 STRCPY(fname_buf + i, name + llen);
8419 fname = fname_buf;
8420 }
8421 else
8422 {
8423 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8424 if (fname == NULL)
8425 error = ERROR_OTHER;
8426 else
8427 {
8428 mch_memmove(fname, fname_buf, (size_t)i);
8429 STRCPY(fname + i, name + llen);
8430 }
8431 }
8432 }
8433 else
8434 fname = name;
8435
8436 *doesrange = FALSE;
8437
8438
8439 /* execute the function if no errors detected and executing */
8440 if (evaluate && error == ERROR_NONE)
8441 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008442 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8443 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 error = ERROR_UNKNOWN;
8445
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008446 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
8448 /*
8449 * User defined function.
8450 */
8451 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008452
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008454 /* Trigger FuncUndefined event, may load the function. */
8455 if (fp == NULL
8456 && apply_autocmds(EVENT_FUNCUNDEFINED,
8457 fname, fname, TRUE, NULL)
8458 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008460 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 fp = find_func(fname);
8462 }
8463#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008464 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008465 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008466 {
8467 /* loaded a package, search for the function again */
8468 fp = find_func(fname);
8469 }
8470
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 if (fp != NULL)
8472 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008473 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008475 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008477 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008479 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008480 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 else
8482 {
8483 /*
8484 * Call the user function.
8485 * Save and restore search patterns, script variables and
8486 * redo buffer.
8487 */
8488 save_search_patterns();
8489 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008490 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008492 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008493 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8494 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8495 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008496 /* Function was unreferenced while being used, free it
8497 * now. */
8498 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 restoreRedobuff();
8500 restore_search_patterns();
8501 error = ERROR_NONE;
8502 }
8503 }
8504 }
8505 else
8506 {
8507 /*
8508 * Find the function name in the table, call its implementation.
8509 */
8510 i = find_internal_func(fname);
8511 if (i >= 0)
8512 {
8513 if (argcount < functions[i].f_min_argc)
8514 error = ERROR_TOOFEW;
8515 else if (argcount > functions[i].f_max_argc)
8516 error = ERROR_TOOMANY;
8517 else
8518 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008519 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008520 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 error = ERROR_NONE;
8522 }
8523 }
8524 }
8525 /*
8526 * The function call (or "FuncUndefined" autocommand sequence) might
8527 * have been aborted by an error, an interrupt, or an explicitly thrown
8528 * exception that has not been caught so far. This situation can be
8529 * tested for by calling aborting(). For an error in an internal
8530 * function or for the "E132" error in call_user_func(), however, the
8531 * throw point at which the "force_abort" flag (temporarily reset by
8532 * emsg()) is normally updated has not been reached yet. We need to
8533 * update that flag first to make aborting() reliable.
8534 */
8535 update_force_abort();
8536 }
8537 if (error == ERROR_NONE)
8538 ret = OK;
8539
8540 /*
8541 * Report an error unless the argument evaluation or function call has been
8542 * cancelled due to an aborting error, an interrupt, or an exception.
8543 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008544 if (!aborting())
8545 {
8546 switch (error)
8547 {
8548 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008549 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008550 break;
8551 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008552 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008553 break;
8554 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008555 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008556 name);
8557 break;
8558 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008559 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008560 name);
8561 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008562 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008563 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008564 name);
8565 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008566 }
8567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 if (fname != name && fname != fname_buf)
8570 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008571 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572
8573 return ret;
8574}
8575
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008576/*
8577 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008578 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008579 */
8580 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008581emsg_funcname(ermsg, name)
8582 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008583 char_u *name;
8584{
8585 char_u *p;
8586
8587 if (*name == K_SPECIAL)
8588 p = concat_str((char_u *)"<SNR>", name + 3);
8589 else
8590 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008591 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008592 if (p != name)
8593 vim_free(p);
8594}
8595
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008596/*
8597 * Return TRUE for a non-zero Number and a non-empty String.
8598 */
8599 static int
8600non_zero_arg(argvars)
8601 typval_T *argvars;
8602{
8603 return ((argvars[0].v_type == VAR_NUMBER
8604 && argvars[0].vval.v_number != 0)
8605 || (argvars[0].v_type == VAR_STRING
8606 && argvars[0].vval.v_string != NULL
8607 && *argvars[0].vval.v_string != NUL));
8608}
8609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610/*********************************************
8611 * Implementation of the built-in functions
8612 */
8613
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008615static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8616
8617/*
8618 * Get the float value of "argvars[0]" into "f".
8619 * Returns FAIL when the argument is not a Number or Float.
8620 */
8621 static int
8622get_float_arg(argvars, f)
8623 typval_T *argvars;
8624 float_T *f;
8625{
8626 if (argvars[0].v_type == VAR_FLOAT)
8627 {
8628 *f = argvars[0].vval.v_float;
8629 return OK;
8630 }
8631 if (argvars[0].v_type == VAR_NUMBER)
8632 {
8633 *f = (float_T)argvars[0].vval.v_number;
8634 return OK;
8635 }
8636 EMSG(_("E808: Number or Float required"));
8637 return FAIL;
8638}
8639
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008640/*
8641 * "abs(expr)" function
8642 */
8643 static void
8644f_abs(argvars, rettv)
8645 typval_T *argvars;
8646 typval_T *rettv;
8647{
8648 if (argvars[0].v_type == VAR_FLOAT)
8649 {
8650 rettv->v_type = VAR_FLOAT;
8651 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8652 }
8653 else
8654 {
8655 varnumber_T n;
8656 int error = FALSE;
8657
8658 n = get_tv_number_chk(&argvars[0], &error);
8659 if (error)
8660 rettv->vval.v_number = -1;
8661 else if (n > 0)
8662 rettv->vval.v_number = n;
8663 else
8664 rettv->vval.v_number = -n;
8665 }
8666}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008667
8668/*
8669 * "acos()" function
8670 */
8671 static void
8672f_acos(argvars, rettv)
8673 typval_T *argvars;
8674 typval_T *rettv;
8675{
8676 float_T f;
8677
8678 rettv->v_type = VAR_FLOAT;
8679 if (get_float_arg(argvars, &f) == OK)
8680 rettv->vval.v_float = acos(f);
8681 else
8682 rettv->vval.v_float = 0.0;
8683}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008684#endif
8685
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008687 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 */
8689 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008690f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008691 typval_T *argvars;
8692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693{
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008697 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008699 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008700 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008701 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008703 }
8704 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008705 EMSG(_(e_listreq));
8706}
8707
8708/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008709 * "and(expr, expr)" function
8710 */
8711 static void
8712f_and(argvars, rettv)
8713 typval_T *argvars;
8714 typval_T *rettv;
8715{
8716 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8717 & get_tv_number_chk(&argvars[1], NULL);
8718}
8719
8720/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008721 * "append(lnum, string/list)" function
8722 */
8723 static void
8724f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008725 typval_T *argvars;
8726 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008727{
8728 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008729 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 list_T *l = NULL;
8731 listitem_T *li = NULL;
8732 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008733 long added = 0;
8734
Bram Moolenaar0d660222005-01-07 21:51:51 +00008735 lnum = get_tv_lnum(argvars);
8736 if (lnum >= 0
8737 && lnum <= curbuf->b_ml.ml_line_count
8738 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008739 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008740 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008741 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008742 l = argvars[1].vval.v_list;
8743 if (l == NULL)
8744 return;
8745 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008746 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747 for (;;)
8748 {
8749 if (l == NULL)
8750 tv = &argvars[1]; /* append a string */
8751 else if (li == NULL)
8752 break; /* end of list */
8753 else
8754 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008755 line = get_tv_string_chk(tv);
8756 if (line == NULL) /* type error */
8757 {
8758 rettv->vval.v_number = 1; /* Failed */
8759 break;
8760 }
8761 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008762 ++added;
8763 if (l == NULL)
8764 break;
8765 li = li->li_next;
8766 }
8767
8768 appended_lines_mark(lnum, added);
8769 if (curwin->w_cursor.lnum > lnum)
8770 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 else
8773 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774}
8775
8776/*
8777 * "argc()" function
8778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785}
8786
8787/*
8788 * "argidx()" function
8789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008792 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796}
8797
8798/*
8799 * "argv(nr)" function
8800 */
8801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *argvars;
8804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
8806 int idx;
8807
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008808 if (argvars[0].v_type != VAR_UNKNOWN)
8809 {
8810 idx = get_tv_number_chk(&argvars[0], NULL);
8811 if (idx >= 0 && idx < ARGCOUNT)
8812 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8813 else
8814 rettv->vval.v_string = NULL;
8815 rettv->v_type = VAR_STRING;
8816 }
8817 else if (rettv_list_alloc(rettv) == OK)
8818 for (idx = 0; idx < ARGCOUNT; ++idx)
8819 list_append_string(rettv->vval.v_list,
8820 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821}
8822
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008823#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008824/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008825 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008826 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008827 static void
8828f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008829 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008830 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008831{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008832 float_T f;
8833
8834 rettv->v_type = VAR_FLOAT;
8835 if (get_float_arg(argvars, &f) == OK)
8836 rettv->vval.v_float = asin(f);
8837 else
8838 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008839}
8840
8841/*
8842 * "atan()" function
8843 */
8844 static void
8845f_atan(argvars, rettv)
8846 typval_T *argvars;
8847 typval_T *rettv;
8848{
8849 float_T f;
8850
8851 rettv->v_type = VAR_FLOAT;
8852 if (get_float_arg(argvars, &f) == OK)
8853 rettv->vval.v_float = atan(f);
8854 else
8855 rettv->vval.v_float = 0.0;
8856}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008857
8858/*
8859 * "atan2()" function
8860 */
8861 static void
8862f_atan2(argvars, rettv)
8863 typval_T *argvars;
8864 typval_T *rettv;
8865{
8866 float_T fx, fy;
8867
8868 rettv->v_type = VAR_FLOAT;
8869 if (get_float_arg(argvars, &fx) == OK
8870 && get_float_arg(&argvars[1], &fy) == OK)
8871 rettv->vval.v_float = atan2(fx, fy);
8872 else
8873 rettv->vval.v_float = 0.0;
8874}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008875#endif
8876
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877/*
8878 * "browse(save, title, initdir, default)" function
8879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008882 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885#ifdef FEAT_BROWSE
8886 int save;
8887 char_u *title;
8888 char_u *initdir;
8889 char_u *defname;
8890 char_u buf[NUMBUFLEN];
8891 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008892 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008894 save = get_tv_number_chk(&argvars[0], &error);
8895 title = get_tv_string_chk(&argvars[1]);
8896 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8897 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 if (error || title == NULL || initdir == NULL || defname == NULL)
8900 rettv->vval.v_string = NULL;
8901 else
8902 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008903 do_browse(save ? BROWSE_SAVE : 0,
8904 title, defname, NULL, initdir, NULL, curbuf);
8905#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008907#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008908 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008909}
8910
8911/*
8912 * "browsedir(title, initdir)" function
8913 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008916 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008917 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008918{
8919#ifdef FEAT_BROWSE
8920 char_u *title;
8921 char_u *initdir;
8922 char_u buf[NUMBUFLEN];
8923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008924 title = get_tv_string_chk(&argvars[0]);
8925 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008927 if (title == NULL || initdir == NULL)
8928 rettv->vval.v_string = NULL;
8929 else
8930 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008931 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936}
8937
Bram Moolenaar33570922005-01-25 22:26:29 +00008938static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940/*
8941 * Find a buffer by number or exact name.
8942 */
8943 static buf_T *
8944find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008949 if (avar->v_type == VAR_NUMBER)
8950 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008951 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008953 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008954 if (buf == NULL)
8955 {
8956 /* No full path name match, try a match with a URL or a "nofile"
8957 * buffer, these don't use the full path. */
8958 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8959 if (buf->b_fname != NULL
8960 && (path_with_url(buf->b_fname)
8961#ifdef FEAT_QUICKFIX
8962 || bt_nofile(buf)
8963#endif
8964 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008965 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008966 break;
8967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 }
8969 return buf;
8970}
8971
8972/*
8973 * "bufexists(expr)" function
8974 */
8975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 typval_T *argvars;
8978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981}
8982
8983/*
8984 * "buflisted(expr)" function
8985 */
8986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 typval_T *argvars;
8989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
8991 buf_T *buf;
8992
8993 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995}
8996
8997/*
8998 * "bufloaded(expr)" function
8999 */
9000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 typval_T *argvars;
9003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
9005 buf_T *buf;
9006
9007 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009}
9010
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009011static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009012
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013/*
9014 * Get buffer by number or pattern.
9015 */
9016 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009017get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009019 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 int save_magic;
9023 char_u *save_cpo;
9024 buf_T *buf;
9025
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 if (tv->v_type == VAR_NUMBER)
9027 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009028 if (tv->v_type != VAR_STRING)
9029 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 if (name == NULL || *name == NUL)
9031 return curbuf;
9032 if (name[0] == '$' && name[1] == NUL)
9033 return lastbuf;
9034
9035 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9036 save_magic = p_magic;
9037 p_magic = TRUE;
9038 save_cpo = p_cpo;
9039 p_cpo = (char_u *)"";
9040
9041 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009042 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
9044 p_magic = save_magic;
9045 p_cpo = save_cpo;
9046
9047 /* If not found, try expanding the name, like done for bufexists(). */
9048 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
9051 return buf;
9052}
9053
9054/*
9055 * "bufname(expr)" function
9056 */
9057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *argvars;
9060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 buf_T *buf;
9063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009064 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009066 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 --emsg_off;
9073}
9074
9075/*
9076 * "bufnr(expr)" function
9077 */
9078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 typval_T *argvars;
9081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
9083 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009084 int error = FALSE;
9085 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009087 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009089 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009090 --emsg_off;
9091
9092 /* If the buffer isn't found and the second argument is not zero create a
9093 * new buffer. */
9094 if (buf == NULL
9095 && argvars[1].v_type != VAR_UNKNOWN
9096 && get_tv_number_chk(&argvars[1], &error) != 0
9097 && !error
9098 && (name = get_tv_string_chk(&argvars[0])) != NULL
9099 && !error)
9100 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9101
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106}
9107
9108/*
9109 * "bufwinnr(nr)" function
9110 */
9111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009113 typval_T *argvars;
9114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115{
9116#ifdef FEAT_WINDOWS
9117 win_T *wp;
9118 int winnr = 0;
9119#endif
9120 buf_T *buf;
9121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009122 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009124 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125#ifdef FEAT_WINDOWS
9126 for (wp = firstwin; wp; wp = wp->w_next)
9127 {
9128 ++winnr;
9129 if (wp->w_buffer == buf)
9130 break;
9131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#endif
9136 --emsg_off;
9137}
9138
9139/*
9140 * "byte2line(byte)" function
9141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149#else
9150 long boff = 0;
9151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009152 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 (linenr_T)0, &boff);
9158#endif
9159}
9160
9161/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009162 * "byteidx()" function
9163 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009166 typval_T *argvars;
9167 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009168{
9169#ifdef FEAT_MBYTE
9170 char_u *t;
9171#endif
9172 char_u *str;
9173 long idx;
9174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 str = get_tv_string_chk(&argvars[0]);
9176 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009177 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009178 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009179 return;
9180
9181#ifdef FEAT_MBYTE
9182 t = str;
9183 for ( ; idx > 0; idx--)
9184 {
9185 if (*t == NUL) /* EOL reached */
9186 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009187 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009188 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009189 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009190#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009191 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009193#endif
9194}
9195
Bram Moolenaardb913952012-06-29 12:54:53 +02009196 int
9197func_call(name, args, selfdict, rettv)
9198 char_u *name;
9199 typval_T *args;
9200 dict_T *selfdict;
9201 typval_T *rettv;
9202{
9203 listitem_T *item;
9204 typval_T argv[MAX_FUNC_ARGS + 1];
9205 int argc = 0;
9206 int dummy;
9207 int r = 0;
9208
9209 for (item = args->vval.v_list->lv_first; item != NULL;
9210 item = item->li_next)
9211 {
9212 if (argc == MAX_FUNC_ARGS)
9213 {
9214 EMSG(_("E699: Too many arguments"));
9215 break;
9216 }
9217 /* Make a copy of each argument. This is needed to be able to set
9218 * v_lock to VAR_FIXED in the copy without changing the original list.
9219 */
9220 copy_tv(&item->li_tv, &argv[argc++]);
9221 }
9222
9223 if (item == NULL)
9224 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9225 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9226 &dummy, TRUE, selfdict);
9227
9228 /* Free the arguments. */
9229 while (argc > 0)
9230 clear_tv(&argv[--argc]);
9231
9232 return r;
9233}
9234
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009235/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009236 * "call(func, arglist)" function
9237 */
9238 static void
9239f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009240 typval_T *argvars;
9241 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009242{
9243 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009244 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009245
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009246 if (argvars[1].v_type != VAR_LIST)
9247 {
9248 EMSG(_(e_listreq));
9249 return;
9250 }
9251 if (argvars[1].vval.v_list == NULL)
9252 return;
9253
9254 if (argvars[0].v_type == VAR_FUNC)
9255 func = argvars[0].vval.v_string;
9256 else
9257 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 if (*func == NUL)
9259 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009260
Bram Moolenaare9a41262005-01-15 22:18:47 +00009261 if (argvars[2].v_type != VAR_UNKNOWN)
9262 {
9263 if (argvars[2].v_type != VAR_DICT)
9264 {
9265 EMSG(_(e_dictreq));
9266 return;
9267 }
9268 selfdict = argvars[2].vval.v_dict;
9269 }
9270
Bram Moolenaardb913952012-06-29 12:54:53 +02009271 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009272}
9273
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009274#ifdef FEAT_FLOAT
9275/*
9276 * "ceil({float})" function
9277 */
9278 static void
9279f_ceil(argvars, rettv)
9280 typval_T *argvars;
9281 typval_T *rettv;
9282{
9283 float_T f;
9284
9285 rettv->v_type = VAR_FLOAT;
9286 if (get_float_arg(argvars, &f) == OK)
9287 rettv->vval.v_float = ceil(f);
9288 else
9289 rettv->vval.v_float = 0.0;
9290}
9291#endif
9292
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009293/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009294 * "changenr()" function
9295 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009296 static void
9297f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009298 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009299 typval_T *rettv;
9300{
9301 rettv->vval.v_number = curbuf->b_u_seq_cur;
9302}
9303
9304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 * "char2nr(string)" function
9306 */
9307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009309 typval_T *argvars;
9310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311{
9312#ifdef FEAT_MBYTE
9313 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009314 {
9315 int utf8 = 0;
9316
9317 if (argvars[1].v_type != VAR_UNKNOWN)
9318 utf8 = get_tv_number_chk(&argvars[1], NULL);
9319
9320 if (utf8)
9321 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9322 else
9323 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 else
9326#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328}
9329
9330/*
9331 * "cindent(lnum)" function
9332 */
9333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009335 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337{
9338#ifdef FEAT_CINDENT
9339 pos_T pos;
9340 linenr_T lnum;
9341
9342 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9345 {
9346 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009347 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 curwin->w_cursor = pos;
9349 }
9350 else
9351#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353}
9354
9355/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009356 * "clearmatches()" function
9357 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009358 static void
9359f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009360 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009361 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009362{
9363#ifdef FEAT_SEARCH_EXTRA
9364 clear_matches(curwin);
9365#endif
9366}
9367
9368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 * "col(string)" function
9370 */
9371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *argvars;
9374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376 colnr_T col = 0;
9377 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009378 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009380 fp = var2fpos(&argvars[0], FALSE, &fnum);
9381 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 {
9383 if (fp->col == MAXCOL)
9384 {
9385 /* '> can be MAXCOL, get the length of the line then */
9386 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009387 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 else
9389 col = MAXCOL;
9390 }
9391 else
9392 {
9393 col = fp->col + 1;
9394#ifdef FEAT_VIRTUALEDIT
9395 /* col(".") when the cursor is on the NUL at the end of the line
9396 * because of "coladd" can be seen as an extra column. */
9397 if (virtual_active() && fp == &curwin->w_cursor)
9398 {
9399 char_u *p = ml_get_cursor();
9400
9401 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9402 curwin->w_virtcol - curwin->w_cursor.coladd))
9403 {
9404# ifdef FEAT_MBYTE
9405 int l;
9406
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009407 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 col += l;
9409# else
9410 if (*p != NUL && p[1] == NUL)
9411 ++col;
9412# endif
9413 }
9414 }
9415#endif
9416 }
9417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419}
9420
Bram Moolenaar572cb562005-08-05 21:35:02 +00009421#if defined(FEAT_INS_EXPAND)
9422/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009423 * "complete()" function
9424 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009425 static void
9426f_complete(argvars, rettv)
9427 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009428 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009429{
9430 int startcol;
9431
9432 if ((State & INSERT) == 0)
9433 {
9434 EMSG(_("E785: complete() can only be used in Insert mode"));
9435 return;
9436 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009437
9438 /* Check for undo allowed here, because if something was already inserted
9439 * the line was already saved for undo and this check isn't done. */
9440 if (!undo_allowed())
9441 return;
9442
Bram Moolenaarade00832006-03-10 21:46:58 +00009443 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9444 {
9445 EMSG(_(e_invarg));
9446 return;
9447 }
9448
9449 startcol = get_tv_number_chk(&argvars[0], NULL);
9450 if (startcol <= 0)
9451 return;
9452
9453 set_completion(startcol - 1, argvars[1].vval.v_list);
9454}
9455
9456/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009457 * "complete_add()" function
9458 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009459 static void
9460f_complete_add(argvars, rettv)
9461 typval_T *argvars;
9462 typval_T *rettv;
9463{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009464 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009465}
9466
9467/*
9468 * "complete_check()" function
9469 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009470 static void
9471f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009472 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009473 typval_T *rettv;
9474{
9475 int saved = RedrawingDisabled;
9476
9477 RedrawingDisabled = 0;
9478 ins_compl_check_keys(0);
9479 rettv->vval.v_number = compl_interrupted;
9480 RedrawingDisabled = saved;
9481}
9482#endif
9483
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484/*
9485 * "confirm(message, buttons[, default [, type]])" function
9486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009489 typval_T *argvars UNUSED;
9490 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
9492#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9493 char_u *message;
9494 char_u *buttons = NULL;
9495 char_u buf[NUMBUFLEN];
9496 char_u buf2[NUMBUFLEN];
9497 int def = 1;
9498 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009499 char_u *typestr;
9500 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 message = get_tv_string_chk(&argvars[0]);
9503 if (message == NULL)
9504 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009505 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9508 if (buttons == NULL)
9509 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009510 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009515 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9516 if (typestr == NULL)
9517 error = TRUE;
9518 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009520 switch (TOUPPER_ASC(*typestr))
9521 {
9522 case 'E': type = VIM_ERROR; break;
9523 case 'Q': type = VIM_QUESTION; break;
9524 case 'I': type = VIM_INFO; break;
9525 case 'W': type = VIM_WARNING; break;
9526 case 'G': type = VIM_GENERIC; break;
9527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 }
9529 }
9530 }
9531 }
9532
9533 if (buttons == NULL || *buttons == NUL)
9534 buttons = (char_u *)_("&Ok");
9535
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009536 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009537 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009538 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539#endif
9540}
9541
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009542/*
9543 * "copy()" function
9544 */
9545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *argvars;
9548 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009549{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009550 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009553#ifdef FEAT_FLOAT
9554/*
9555 * "cos()" function
9556 */
9557 static void
9558f_cos(argvars, rettv)
9559 typval_T *argvars;
9560 typval_T *rettv;
9561{
9562 float_T f;
9563
9564 rettv->v_type = VAR_FLOAT;
9565 if (get_float_arg(argvars, &f) == OK)
9566 rettv->vval.v_float = cos(f);
9567 else
9568 rettv->vval.v_float = 0.0;
9569}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009570
9571/*
9572 * "cosh()" function
9573 */
9574 static void
9575f_cosh(argvars, rettv)
9576 typval_T *argvars;
9577 typval_T *rettv;
9578{
9579 float_T f;
9580
9581 rettv->v_type = VAR_FLOAT;
9582 if (get_float_arg(argvars, &f) == OK)
9583 rettv->vval.v_float = cosh(f);
9584 else
9585 rettv->vval.v_float = 0.0;
9586}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009587#endif
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009590 * "count()" function
9591 */
9592 static void
9593f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009594 typval_T *argvars;
9595 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009596{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009597 long n = 0;
9598 int ic = FALSE;
9599
Bram Moolenaare9a41262005-01-15 22:18:47 +00009600 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009601 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009602 listitem_T *li;
9603 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009604 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009605
Bram Moolenaare9a41262005-01-15 22:18:47 +00009606 if ((l = argvars[0].vval.v_list) != NULL)
9607 {
9608 li = l->lv_first;
9609 if (argvars[2].v_type != VAR_UNKNOWN)
9610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 int error = FALSE;
9612
9613 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 if (argvars[3].v_type != VAR_UNKNOWN)
9615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 idx = get_tv_number_chk(&argvars[3], &error);
9617 if (!error)
9618 {
9619 li = list_find(l, idx);
9620 if (li == NULL)
9621 EMSGN(_(e_listidx), idx);
9622 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009623 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009624 if (error)
9625 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009626 }
9627
9628 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009629 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009630 ++n;
9631 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009632 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 else if (argvars[0].v_type == VAR_DICT)
9634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009635 int todo;
9636 dict_T *d;
9637 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009639 if ((d = argvars[0].vval.v_dict) != NULL)
9640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 int error = FALSE;
9642
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 if (argvars[2].v_type != VAR_UNKNOWN)
9644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646 if (argvars[3].v_type != VAR_UNKNOWN)
9647 EMSG(_(e_invarg));
9648 }
9649
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009650 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009651 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009652 {
9653 if (!HASHITEM_EMPTY(hi))
9654 {
9655 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009656 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009657 ++n;
9658 }
9659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009660 }
9661 }
9662 else
9663 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009664 rettv->vval.v_number = n;
9665}
9666
9667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9669 *
9670 * Checks the existence of a cscope connection.
9671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009674 typval_T *argvars UNUSED;
9675 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
9677#ifdef FEAT_CSCOPE
9678 int num = 0;
9679 char_u *dbpath = NULL;
9680 char_u *prepend = NULL;
9681 char_u buf[NUMBUFLEN];
9682
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009683 if (argvars[0].v_type != VAR_UNKNOWN
9684 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 num = (int)get_tv_number(&argvars[0]);
9687 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009688 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 }
9691
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693#endif
9694}
9695
9696/*
9697 * "cursor(lnum, col)" function
9698 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009699 * Moves the cursor to the specified line and column.
9700 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009704 typval_T *argvars;
9705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009708#ifdef FEAT_VIRTUALEDIT
9709 long coladd = 0;
9710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009712 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009713 if (argvars[1].v_type == VAR_UNKNOWN)
9714 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009715 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009716
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009717 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009718 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009719 line = pos.lnum;
9720 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009721#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009722 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009723#endif
9724 }
9725 else
9726 {
9727 line = get_tv_lnum(argvars);
9728 col = get_tv_number_chk(&argvars[1], NULL);
9729#ifdef FEAT_VIRTUALEDIT
9730 if (argvars[2].v_type != VAR_UNKNOWN)
9731 coladd = get_tv_number_chk(&argvars[2], NULL);
9732#endif
9733 }
9734 if (line < 0 || col < 0
9735#ifdef FEAT_VIRTUALEDIT
9736 || coladd < 0
9737#endif
9738 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 if (line > 0)
9741 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 if (col > 0)
9743 curwin->w_cursor.col = col - 1;
9744#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009745 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746#endif
9747
9748 /* Make sure the cursor is in a valid position. */
9749 check_cursor();
9750#ifdef FEAT_MBYTE
9751 /* Correct cursor for multi-byte character. */
9752 if (has_mbyte)
9753 mb_adjust_cursor();
9754#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009755
9756 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009757 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758}
9759
9760/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009761 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 */
9763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 typval_T *argvars;
9766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009768 int noref = 0;
9769
9770 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009771 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009772 if (noref < 0 || noref > 1)
9773 EMSG(_(e_invarg));
9774 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009775 {
9776 current_copyID += COPYID_INC;
9777 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779}
9780
9781/*
9782 * "delete()" function
9783 */
9784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009786 typval_T *argvars;
9787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
9789 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793}
9794
9795/*
9796 * "did_filetype()" function
9797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009800 typval_T *argvars UNUSED;
9801 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802{
9803#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805#endif
9806}
9807
9808/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009809 * "diff_filler()" function
9810 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009813 typval_T *argvars UNUSED;
9814 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009815{
9816#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009818#endif
9819}
9820
9821/*
9822 * "diff_hlID()" function
9823 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009826 typval_T *argvars UNUSED;
9827 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828{
9829#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009831 static linenr_T prev_lnum = 0;
9832 static int changedtick = 0;
9833 static int fnum = 0;
9834 static int change_start = 0;
9835 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009836 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009837 int filler_lines;
9838 int col;
9839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 if (lnum < 0) /* ignore type error in {lnum} arg */
9841 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009842 if (lnum != prev_lnum
9843 || changedtick != curbuf->b_changedtick
9844 || fnum != curbuf->b_fnum)
9845 {
9846 /* New line, buffer, change: need to get the values. */
9847 filler_lines = diff_check(curwin, lnum);
9848 if (filler_lines < 0)
9849 {
9850 if (filler_lines == -1)
9851 {
9852 change_start = MAXCOL;
9853 change_end = -1;
9854 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9855 hlID = HLF_ADD; /* added line */
9856 else
9857 hlID = HLF_CHD; /* changed line */
9858 }
9859 else
9860 hlID = HLF_ADD; /* added line */
9861 }
9862 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009863 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009864 prev_lnum = lnum;
9865 changedtick = curbuf->b_changedtick;
9866 fnum = curbuf->b_fnum;
9867 }
9868
9869 if (hlID == HLF_CHD || hlID == HLF_TXD)
9870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009871 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009872 if (col >= change_start && col <= change_end)
9873 hlID = HLF_TXD; /* changed text */
9874 else
9875 hlID = HLF_CHD; /* changed line */
9876 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009877 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009878#endif
9879}
9880
9881/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009882 * "empty({expr})" function
9883 */
9884 static void
9885f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009886 typval_T *argvars;
9887 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009888{
9889 int n;
9890
9891 switch (argvars[0].v_type)
9892 {
9893 case VAR_STRING:
9894 case VAR_FUNC:
9895 n = argvars[0].vval.v_string == NULL
9896 || *argvars[0].vval.v_string == NUL;
9897 break;
9898 case VAR_NUMBER:
9899 n = argvars[0].vval.v_number == 0;
9900 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009901#ifdef FEAT_FLOAT
9902 case VAR_FLOAT:
9903 n = argvars[0].vval.v_float == 0.0;
9904 break;
9905#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009906 case VAR_LIST:
9907 n = argvars[0].vval.v_list == NULL
9908 || argvars[0].vval.v_list->lv_first == NULL;
9909 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009910 case VAR_DICT:
9911 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009912 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009914 default:
9915 EMSG2(_(e_intern2), "f_empty()");
9916 n = 0;
9917 }
9918
9919 rettv->vval.v_number = n;
9920}
9921
9922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 * "escape({string}, {chars})" function
9924 */
9925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009927 typval_T *argvars;
9928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929{
9930 char_u buf[NUMBUFLEN];
9931
Bram Moolenaar758711c2005-02-02 23:11:38 +00009932 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9933 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935}
9936
9937/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009938 * "eval()" function
9939 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009940 static void
9941f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009942 typval_T *argvars;
9943 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009944{
9945 char_u *s;
9946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 s = get_tv_string_chk(&argvars[0]);
9948 if (s != NULL)
9949 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9952 {
9953 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009956 else if (*s != NUL)
9957 EMSG(_(e_trailing));
9958}
9959
9960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 * "eventhandler()" function
9962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009964f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969}
9970
9971/*
9972 * "executable()" function
9973 */
9974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *argvars;
9977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980}
9981
9982/*
9983 * "exists()" function
9984 */
9985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989{
9990 char_u *p;
9991 char_u *name;
9992 int n = FALSE;
9993 int len = 0;
9994
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009995 no_autoload = TRUE;
9996
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 if (*p == '$') /* environment variable */
9999 {
10000 /* first try "normal" environment variables (fast) */
10001 if (mch_getenv(p + 1) != NULL)
10002 n = TRUE;
10003 else
10004 {
10005 /* try expanding things like $VIM and ${HOME} */
10006 p = expand_env_save(p);
10007 if (p != NULL && *p != '$')
10008 n = TRUE;
10009 vim_free(p);
10010 }
10011 }
10012 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010014 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010015 if (*skipwhite(p) != NUL)
10016 n = FALSE; /* trailing garbage */
10017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 else if (*p == '*') /* internal or user defined function */
10019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010020 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 }
10022 else if (*p == ':')
10023 {
10024 n = cmd_exists(p + 1);
10025 }
10026 else if (*p == '#')
10027 {
10028#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010029 if (p[1] == '#')
10030 n = autocmd_supported(p + 2);
10031 else
10032 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033#endif
10034 }
10035 else /* internal variable */
10036 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010037 char_u *tofree;
10038 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010040 /* get_name_len() takes care of expanding curly braces */
10041 name = p;
10042 len = get_name_len(&p, &tofree, TRUE, FALSE);
10043 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010045 if (tofree != NULL)
10046 name = tofree;
10047 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10048 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010050 /* handle d.key, l[idx], f(expr) */
10051 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10052 if (n)
10053 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 }
10055 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010056 if (*p != NUL)
10057 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010059 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 }
10061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010063
10064 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065}
10066
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010067#ifdef FEAT_FLOAT
10068/*
10069 * "exp()" function
10070 */
10071 static void
10072f_exp(argvars, rettv)
10073 typval_T *argvars;
10074 typval_T *rettv;
10075{
10076 float_T f;
10077
10078 rettv->v_type = VAR_FLOAT;
10079 if (get_float_arg(argvars, &f) == OK)
10080 rettv->vval.v_float = exp(f);
10081 else
10082 rettv->vval.v_float = 0.0;
10083}
10084#endif
10085
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086/*
10087 * "expand()" function
10088 */
10089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010091 typval_T *argvars;
10092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
10094 char_u *s;
10095 int len;
10096 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010097 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010099 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010100 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010103 if (argvars[1].v_type != VAR_UNKNOWN
10104 && argvars[2].v_type != VAR_UNKNOWN
10105 && get_tv_number_chk(&argvars[2], &error)
10106 && !error)
10107 {
10108 rettv->v_type = VAR_LIST;
10109 rettv->vval.v_list = NULL;
10110 }
10111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 if (*s == '%' || *s == '#' || *s == '<')
10114 {
10115 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010116 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010118 if (rettv->v_type == VAR_LIST)
10119 {
10120 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10121 list_append_string(rettv->vval.v_list, result, -1);
10122 else
10123 vim_free(result);
10124 }
10125 else
10126 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 }
10128 else
10129 {
10130 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010131 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 if (argvars[1].v_type != VAR_UNKNOWN
10133 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010134 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 if (!error)
10136 {
10137 ExpandInit(&xpc);
10138 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010139 if (p_wic)
10140 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010141 if (rettv->v_type == VAR_STRING)
10142 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10143 options, WILD_ALL);
10144 else if (rettv_list_alloc(rettv) != FAIL)
10145 {
10146 int i;
10147
10148 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10149 for (i = 0; i < xpc.xp_numfiles; i++)
10150 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10151 ExpandCleanup(&xpc);
10152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 }
10154 else
10155 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 }
10157}
10158
10159/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010160 * Go over all entries in "d2" and add them to "d1".
10161 * When "action" is "error" then a duplicate key is an error.
10162 * When "action" is "force" then a duplicate key is overwritten.
10163 * Otherwise duplicate keys are ignored ("action" is "keep").
10164 */
10165 void
10166dict_extend(d1, d2, action)
10167 dict_T *d1;
10168 dict_T *d2;
10169 char_u *action;
10170{
10171 dictitem_T *di1;
10172 hashitem_T *hi2;
10173 int todo;
10174
10175 todo = (int)d2->dv_hashtab.ht_used;
10176 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10177 {
10178 if (!HASHITEM_EMPTY(hi2))
10179 {
10180 --todo;
10181 di1 = dict_find(d1, hi2->hi_key, -1);
10182 if (d1->dv_scope != 0)
10183 {
10184 /* Disallow replacing a builtin function in l: and g:.
10185 * Check the key to be valid when adding to any
10186 * scope. */
10187 if (d1->dv_scope == VAR_DEF_SCOPE
10188 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10189 && var_check_func_name(hi2->hi_key,
10190 di1 == NULL))
10191 break;
10192 if (!valid_varname(hi2->hi_key))
10193 break;
10194 }
10195 if (di1 == NULL)
10196 {
10197 di1 = dictitem_copy(HI2DI(hi2));
10198 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10199 dictitem_free(di1);
10200 }
10201 else if (*action == 'e')
10202 {
10203 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10204 break;
10205 }
10206 else if (*action == 'f' && HI2DI(hi2) != di1)
10207 {
10208 clear_tv(&di1->di_tv);
10209 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10210 }
10211 }
10212 }
10213}
10214
10215/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010216 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010217 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010218 */
10219 static void
10220f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010221 typval_T *argvars;
10222 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010223{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010224 char *arg_errmsg = N_("extend() argument");
10225
Bram Moolenaare9a41262005-01-15 22:18:47 +000010226 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010227 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010228 list_T *l1, *l2;
10229 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010232
Bram Moolenaare9a41262005-01-15 22:18:47 +000010233 l1 = argvars[0].vval.v_list;
10234 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010235 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010236 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010237 {
10238 if (argvars[2].v_type != VAR_UNKNOWN)
10239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 before = get_tv_number_chk(&argvars[2], &error);
10241 if (error)
10242 return; /* type error; errmsg already given */
10243
Bram Moolenaar758711c2005-02-02 23:11:38 +000010244 if (before == l1->lv_len)
10245 item = NULL;
10246 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010247 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010248 item = list_find(l1, before);
10249 if (item == NULL)
10250 {
10251 EMSGN(_(e_listidx), before);
10252 return;
10253 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010254 }
10255 }
10256 else
10257 item = NULL;
10258 list_extend(l1, l2, item);
10259
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 copy_tv(&argvars[0], rettv);
10261 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010263 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10264 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010265 dict_T *d1, *d2;
10266 char_u *action;
10267 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268
10269 d1 = argvars[0].vval.v_dict;
10270 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010271 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010272 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 {
10274 /* Check the third argument. */
10275 if (argvars[2].v_type != VAR_UNKNOWN)
10276 {
10277 static char *(av[]) = {"keep", "force", "error"};
10278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 action = get_tv_string_chk(&argvars[2]);
10280 if (action == NULL)
10281 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 for (i = 0; i < 3; ++i)
10283 if (STRCMP(action, av[i]) == 0)
10284 break;
10285 if (i == 3)
10286 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010287 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010288 return;
10289 }
10290 }
10291 else
10292 action = (char_u *)"force";
10293
Bram Moolenaara9922d62013-05-30 13:01:18 +020010294 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 copy_tv(&argvars[0], rettv);
10297 }
10298 }
10299 else
10300 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010301}
10302
10303/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010304 * "feedkeys()" function
10305 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010306 static void
10307f_feedkeys(argvars, rettv)
10308 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010309 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010310{
10311 int remap = TRUE;
10312 char_u *keys, *flags;
10313 char_u nbuf[NUMBUFLEN];
10314 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010315 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010316
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010317 /* This is not allowed in the sandbox. If the commands would still be
10318 * executed in the sandbox it would be OK, but it probably happens later,
10319 * when "sandbox" is no longer set. */
10320 if (check_secure())
10321 return;
10322
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010323 keys = get_tv_string(&argvars[0]);
10324 if (*keys != NUL)
10325 {
10326 if (argvars[1].v_type != VAR_UNKNOWN)
10327 {
10328 flags = get_tv_string_buf(&argvars[1], nbuf);
10329 for ( ; *flags != NUL; ++flags)
10330 {
10331 switch (*flags)
10332 {
10333 case 'n': remap = FALSE; break;
10334 case 'm': remap = TRUE; break;
10335 case 't': typed = TRUE; break;
10336 }
10337 }
10338 }
10339
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010340 /* Need to escape K_SPECIAL and CSI before putting the string in the
10341 * typeahead buffer. */
10342 keys_esc = vim_strsave_escape_csi(keys);
10343 if (keys_esc != NULL)
10344 {
10345 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010346 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010347 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010348 if (vgetc_busy)
10349 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010350 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010351 }
10352}
10353
10354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 * "filereadable()" function
10356 */
10357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010359 typval_T *argvars;
10360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010362 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 char_u *p;
10364 int n;
10365
Bram Moolenaarc236c162008-07-13 17:41:49 +000010366#ifndef O_NONBLOCK
10367# define O_NONBLOCK 0
10368#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010369 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010370 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10371 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 {
10373 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010374 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 }
10376 else
10377 n = FALSE;
10378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380}
10381
10382/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010383 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 * rights to write into.
10385 */
10386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010387f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010388 typval_T *argvars;
10389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010391 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392}
10393
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010394static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010395
10396 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010397findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010399 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010400 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010401{
10402#ifdef FEAT_SEARCHPATH
10403 char_u *fname;
10404 char_u *fresult = NULL;
10405 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10406 char_u *p;
10407 char_u pathbuf[NUMBUFLEN];
10408 int count = 1;
10409 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010410 int error = FALSE;
10411#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010412
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010413 rettv->vval.v_string = NULL;
10414 rettv->v_type = VAR_STRING;
10415
10416#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010418
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010419 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10422 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010423 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424 else
10425 {
10426 if (*p != NUL)
10427 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010430 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010432 }
10433
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010434 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10435 error = TRUE;
10436
10437 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 do
10440 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010441 if (rettv->v_type == VAR_STRING)
10442 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 fresult = find_file_in_path_option(first ? fname : NULL,
10444 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010445 0, first, path,
10446 find_what,
10447 curbuf->b_ffname,
10448 find_what == FINDFILE_DIR
10449 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010451
10452 if (fresult != NULL && rettv->v_type == VAR_LIST)
10453 list_append_string(rettv->vval.v_list, fresult, -1);
10454
10455 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010457
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010458 if (rettv->v_type == VAR_STRING)
10459 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010460#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010461}
10462
Bram Moolenaar33570922005-01-25 22:26:29 +000010463static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10464static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010465
10466/*
10467 * Implementation of map() and filter().
10468 */
10469 static void
10470filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010473 int map;
10474{
10475 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010476 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010477 listitem_T *li, *nli;
10478 list_T *l = NULL;
10479 dictitem_T *di;
10480 hashtab_T *ht;
10481 hashitem_T *hi;
10482 dict_T *d = NULL;
10483 typval_T save_val;
10484 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010485 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010486 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010487 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10488 char *arg_errmsg = (map ? N_("map() argument")
10489 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010490 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010491 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010492
Bram Moolenaare9a41262005-01-15 22:18:47 +000010493 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010494 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010495 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010496 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010497 return;
10498 }
10499 else if (argvars[0].v_type == VAR_DICT)
10500 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010501 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010502 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 return;
10504 }
10505 else
10506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010507 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010508 return;
10509 }
10510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 expr = get_tv_string_buf_chk(&argvars[1], buf);
10512 /* On type errors, the preceding call has already displayed an error
10513 * message. Avoid a misleading error message for an empty string that
10514 * was not passed as argument. */
10515 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 prepare_vimvar(VV_VAL, &save_val);
10518 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010519
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010520 /* We reset "did_emsg" to be able to detect whether an error
10521 * occurred during evaluation of the expression. */
10522 save_did_emsg = did_emsg;
10523 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010524
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010525 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010526 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010528 vimvars[VV_KEY].vv_type = VAR_STRING;
10529
10530 ht = &d->dv_hashtab;
10531 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010532 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010533 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 if (!HASHITEM_EMPTY(hi))
10536 {
10537 --todo;
10538 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010539 if (tv_check_lock(di->di_tv.v_lock,
10540 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 break;
10542 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010543 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010544 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 break;
10546 if (!map && rem)
10547 dictitem_remove(d, di);
10548 clear_tv(&vimvars[VV_KEY].vv_tv);
10549 }
10550 }
10551 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 }
10553 else
10554 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010555 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010557 for (li = l->lv_first; li != NULL; li = nli)
10558 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010559 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010560 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010562 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010563 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010564 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010565 break;
10566 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010568 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010569 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010570 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010571
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010572 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010573 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010574
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010575 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010576 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577
10578 copy_tv(&argvars[0], rettv);
10579}
10580
10581 static int
10582filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 char_u *expr;
10585 int map;
10586 int *remp;
10587{
Bram Moolenaar33570922005-01-25 22:26:29 +000010588 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010589 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010590 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591
Bram Moolenaar33570922005-01-25 22:26:29 +000010592 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 s = expr;
10594 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010595 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 if (*s != NUL) /* check for trailing chars after expr */
10597 {
10598 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010599 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 }
10601 if (map)
10602 {
10603 /* map(): replace the list item value */
10604 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010605 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 *tv = rettv;
10607 }
10608 else
10609 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 int error = FALSE;
10611
Bram Moolenaare9a41262005-01-15 22:18:47 +000010612 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010613 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010614 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010615 /* On type error, nothing has been removed; return FAIL to stop the
10616 * loop. The error message was given by get_tv_number_chk(). */
10617 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010618 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010619 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010620 retval = OK;
10621theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010623 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010624}
10625
10626/*
10627 * "filter()" function
10628 */
10629 static void
10630f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *argvars;
10632 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010633{
10634 filter_map(argvars, rettv, FALSE);
10635}
10636
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010638 * "finddir({fname}[, {path}[, {count}]])" function
10639 */
10640 static void
10641f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 typval_T *argvars;
10643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010644{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010645 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010646}
10647
10648/*
10649 * "findfile({fname}[, {path}[, {count}]])" function
10650 */
10651 static void
10652f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010653 typval_T *argvars;
10654 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010655{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010656 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010657}
10658
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010659#ifdef FEAT_FLOAT
10660/*
10661 * "float2nr({float})" function
10662 */
10663 static void
10664f_float2nr(argvars, rettv)
10665 typval_T *argvars;
10666 typval_T *rettv;
10667{
10668 float_T f;
10669
10670 if (get_float_arg(argvars, &f) == OK)
10671 {
10672 if (f < -0x7fffffff)
10673 rettv->vval.v_number = -0x7fffffff;
10674 else if (f > 0x7fffffff)
10675 rettv->vval.v_number = 0x7fffffff;
10676 else
10677 rettv->vval.v_number = (varnumber_T)f;
10678 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010679}
10680
10681/*
10682 * "floor({float})" function
10683 */
10684 static void
10685f_floor(argvars, rettv)
10686 typval_T *argvars;
10687 typval_T *rettv;
10688{
10689 float_T f;
10690
10691 rettv->v_type = VAR_FLOAT;
10692 if (get_float_arg(argvars, &f) == OK)
10693 rettv->vval.v_float = floor(f);
10694 else
10695 rettv->vval.v_float = 0.0;
10696}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010697
10698/*
10699 * "fmod()" function
10700 */
10701 static void
10702f_fmod(argvars, rettv)
10703 typval_T *argvars;
10704 typval_T *rettv;
10705{
10706 float_T fx, fy;
10707
10708 rettv->v_type = VAR_FLOAT;
10709 if (get_float_arg(argvars, &fx) == OK
10710 && get_float_arg(&argvars[1], &fy) == OK)
10711 rettv->vval.v_float = fmod(fx, fy);
10712 else
10713 rettv->vval.v_float = 0.0;
10714}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010715#endif
10716
Bram Moolenaar0d660222005-01-07 21:51:51 +000010717/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010718 * "fnameescape({string})" function
10719 */
10720 static void
10721f_fnameescape(argvars, rettv)
10722 typval_T *argvars;
10723 typval_T *rettv;
10724{
10725 rettv->vval.v_string = vim_strsave_fnameescape(
10726 get_tv_string(&argvars[0]), FALSE);
10727 rettv->v_type = VAR_STRING;
10728}
10729
10730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 * "fnamemodify({fname}, {mods})" function
10732 */
10733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010734f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010735 typval_T *argvars;
10736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737{
10738 char_u *fname;
10739 char_u *mods;
10740 int usedlen = 0;
10741 int len;
10742 char_u *fbuf = NULL;
10743 char_u buf[NUMBUFLEN];
10744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010745 fname = get_tv_string_chk(&argvars[0]);
10746 mods = get_tv_string_buf_chk(&argvars[1], buf);
10747 if (fname == NULL || mods == NULL)
10748 fname = NULL;
10749 else
10750 {
10751 len = (int)STRLEN(fname);
10752 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 vim_free(fbuf);
10761}
10762
Bram Moolenaar33570922005-01-25 22:26:29 +000010763static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764
10765/*
10766 * "foldclosed()" function
10767 */
10768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010771 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010772 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773{
10774#ifdef FEAT_FOLDING
10775 linenr_T lnum;
10776 linenr_T first, last;
10777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10780 {
10781 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10782 {
10783 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 return;
10788 }
10789 }
10790#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792}
10793
10794/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010795 * "foldclosed()" function
10796 */
10797 static void
10798f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010799 typval_T *argvars;
10800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010801{
10802 foldclosed_both(argvars, rettv, FALSE);
10803}
10804
10805/*
10806 * "foldclosedend()" function
10807 */
10808 static void
10809f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010810 typval_T *argvars;
10811 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010812{
10813 foldclosed_both(argvars, rettv, TRUE);
10814}
10815
10816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 * "foldlevel()" function
10818 */
10819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010821 typval_T *argvars UNUSED;
10822 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823{
10824#ifdef FEAT_FOLDING
10825 linenr_T lnum;
10826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831}
10832
10833/*
10834 * "foldtext()" function
10835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010837f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010838 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840{
10841#ifdef FEAT_FOLDING
10842 linenr_T lnum;
10843 char_u *s;
10844 char_u *r;
10845 int len;
10846 char *txt;
10847#endif
10848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010849 rettv->v_type = VAR_STRING;
10850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010852 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10853 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10854 <= curbuf->b_ml.ml_line_count
10855 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 {
10857 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10859 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 {
10861 if (!linewhite(lnum))
10862 break;
10863 ++lnum;
10864 }
10865
10866 /* Find interesting text in this line. */
10867 s = skipwhite(ml_get(lnum));
10868 /* skip C comment-start */
10869 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010872 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010873 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010874 {
10875 s = skipwhite(ml_get(lnum + 1));
10876 if (*s == '*')
10877 s = skipwhite(s + 1);
10878 }
10879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 txt = _("+-%s%3ld lines: ");
10881 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010882 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 + 20 /* for %3ld */
10884 + STRLEN(s))); /* concatenated */
10885 if (r != NULL)
10886 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010887 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10888 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10889 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 len = (int)STRLEN(r);
10891 STRCAT(r, s);
10892 /* remove 'foldmarker' and 'commentstring' */
10893 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 }
10896 }
10897#endif
10898}
10899
10900/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010901 * "foldtextresult(lnum)" function
10902 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010905 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010906 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010907{
10908#ifdef FEAT_FOLDING
10909 linenr_T lnum;
10910 char_u *text;
10911 char_u buf[51];
10912 foldinfo_T foldinfo;
10913 int fold_count;
10914#endif
10915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 rettv->v_type = VAR_STRING;
10917 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010918#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010919 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 /* treat illegal types and illegal string values for {lnum} the same */
10921 if (lnum < 0)
10922 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010923 fold_count = foldedCount(curwin, lnum, &foldinfo);
10924 if (fold_count > 0)
10925 {
10926 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10927 &foldinfo, buf);
10928 if (text == buf)
10929 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010931 }
10932#endif
10933}
10934
10935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 * "foreground()" function
10937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010940 typval_T *argvars UNUSED;
10941 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943#ifdef FEAT_GUI
10944 if (gui.in_use)
10945 gui_mch_set_foreground();
10946#else
10947# ifdef WIN32
10948 win32_set_foreground();
10949# endif
10950#endif
10951}
10952
10953/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010954 * "function()" function
10955 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010958 typval_T *argvars;
10959 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010960{
10961 char_u *s;
Bram Moolenaara1544c02013-05-30 12:35:52 +020010962 char_u *name = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010965 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010966 EMSG2(_(e_invarg2), s);
Bram Moolenaara1544c02013-05-30 12:35:52 +020010967 /* Don't check an autoload name for existence here, but still expand it
10968 * checking for validity */
10969 else if ((name = get_expanded_name(s, vim_strchr(s, AUTOLOAD_CHAR) == NULL))
10970 == NULL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010971 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010972 else
10973 {
Bram Moolenaara1544c02013-05-30 12:35:52 +020010974 if (name == NULL)
10975 /* Autoload function, need to copy string */
10976 rettv->vval.v_string = vim_strsave(s);
10977 else
10978 /* Function found by get_expanded_name, string allocated by
10979 * trans_function_name: no need to copy */
10980 rettv->vval.v_string = name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010981 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010982 }
10983}
10984
10985/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010986 * "garbagecollect()" function
10987 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010988 static void
10989f_garbagecollect(argvars, rettv)
10990 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010991 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010992{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010993 /* This is postponed until we are back at the toplevel, because we may be
10994 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10995 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010996
10997 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10998 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010999}
11000
11001/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011002 * "get()" function
11003 */
11004 static void
11005f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 typval_T *argvars;
11007 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011008{
Bram Moolenaar33570922005-01-25 22:26:29 +000011009 listitem_T *li;
11010 list_T *l;
11011 dictitem_T *di;
11012 dict_T *d;
11013 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011014
Bram Moolenaare9a41262005-01-15 22:18:47 +000011015 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011016 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011017 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011019 int error = FALSE;
11020
11021 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11022 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011023 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011025 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011026 else if (argvars[0].v_type == VAR_DICT)
11027 {
11028 if ((d = argvars[0].vval.v_dict) != NULL)
11029 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011030 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011031 if (di != NULL)
11032 tv = &di->di_tv;
11033 }
11034 }
11035 else
11036 EMSG2(_(e_listdictarg), "get()");
11037
11038 if (tv == NULL)
11039 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011041 copy_tv(&argvars[2], rettv);
11042 }
11043 else
11044 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011045}
11046
Bram Moolenaar342337a2005-07-21 21:11:17 +000011047static 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 +000011048
11049/*
11050 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011051 * Return a range (from start to end) of lines in rettv from the specified
11052 * buffer.
11053 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011054 */
11055 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011056get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011057 buf_T *buf;
11058 linenr_T start;
11059 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011060 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011061 typval_T *rettv;
11062{
11063 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011064
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011065 if (retlist && rettv_list_alloc(rettv) == FAIL)
11066 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011067
11068 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11069 return;
11070
11071 if (!retlist)
11072 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011073 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11074 p = ml_get_buf(buf, start, FALSE);
11075 else
11076 p = (char_u *)"";
11077
11078 rettv->v_type = VAR_STRING;
11079 rettv->vval.v_string = vim_strsave(p);
11080 }
11081 else
11082 {
11083 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011084 return;
11085
11086 if (start < 1)
11087 start = 1;
11088 if (end > buf->b_ml.ml_line_count)
11089 end = buf->b_ml.ml_line_count;
11090 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011091 if (list_append_string(rettv->vval.v_list,
11092 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011093 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011094 }
11095}
11096
11097/*
11098 * "getbufline()" function
11099 */
11100 static void
11101f_getbufline(argvars, rettv)
11102 typval_T *argvars;
11103 typval_T *rettv;
11104{
11105 linenr_T lnum;
11106 linenr_T end;
11107 buf_T *buf;
11108
11109 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11110 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011111 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011112 --emsg_off;
11113
Bram Moolenaar661b1822005-07-28 22:36:45 +000011114 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011115 if (argvars[2].v_type == VAR_UNKNOWN)
11116 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011117 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011118 end = get_tv_lnum_buf(&argvars[2], buf);
11119
Bram Moolenaar342337a2005-07-21 21:11:17 +000011120 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011121}
11122
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123/*
11124 * "getbufvar()" function
11125 */
11126 static void
11127f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *argvars;
11129 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011130{
11131 buf_T *buf;
11132 buf_T *save_curbuf;
11133 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011135 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011137 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11138 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011139 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011140 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011141
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011142 rettv->v_type = VAR_STRING;
11143 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011144
11145 if (buf != NULL && varname != NULL)
11146 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011147 /* set curbuf to be our buf, temporarily */
11148 save_curbuf = curbuf;
11149 curbuf = buf;
11150
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011152 {
11153 if (get_option_tv(&varname, rettv, TRUE) == OK)
11154 done = TRUE;
11155 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011156 else if (STRCMP(varname, "changedtick") == 0)
11157 {
11158 rettv->v_type = VAR_NUMBER;
11159 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011160 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011161 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011162 else
11163 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011164 /* Look up the variable. */
11165 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11166 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11167 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011168 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011169 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011170 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011171 done = TRUE;
11172 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011174
11175 /* restore previous notion of curbuf */
11176 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011177 }
11178
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011179 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11180 /* use the default value */
11181 copy_tv(&argvars[2], rettv);
11182
Bram Moolenaar0d660222005-01-07 21:51:51 +000011183 --emsg_off;
11184}
11185
11186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 * "getchar()" function
11188 */
11189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011191 typval_T *argvars;
11192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
11194 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011195 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011197 /* Position the cursor. Needed after a message that ends in a space. */
11198 windgoto(msg_row, msg_col);
11199
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 ++no_mapping;
11201 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011202 for (;;)
11203 {
11204 if (argvars[0].v_type == VAR_UNKNOWN)
11205 /* getchar(): blocking wait. */
11206 n = safe_vgetc();
11207 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11208 /* getchar(1): only check if char avail */
11209 n = vpeekc();
11210 else if (error || vpeekc() == NUL)
11211 /* illegal argument or getchar(0) and no char avail: return zero */
11212 n = 0;
11213 else
11214 /* getchar(0) and char avail: return char */
11215 n = safe_vgetc();
11216 if (n == K_IGNORE)
11217 continue;
11218 break;
11219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 --no_mapping;
11221 --allow_keys;
11222
Bram Moolenaar219b8702006-11-01 14:32:36 +000011223 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11224 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11225 vimvars[VV_MOUSE_COL].vv_nr = 0;
11226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 if (IS_SPECIAL(n) || mod_mask != 0)
11229 {
11230 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11231 int i = 0;
11232
11233 /* Turn a special key into three bytes, plus modifier. */
11234 if (mod_mask != 0)
11235 {
11236 temp[i++] = K_SPECIAL;
11237 temp[i++] = KS_MODIFIER;
11238 temp[i++] = mod_mask;
11239 }
11240 if (IS_SPECIAL(n))
11241 {
11242 temp[i++] = K_SPECIAL;
11243 temp[i++] = K_SECOND(n);
11244 temp[i++] = K_THIRD(n);
11245 }
11246#ifdef FEAT_MBYTE
11247 else if (has_mbyte)
11248 i += (*mb_char2bytes)(n, temp + i);
11249#endif
11250 else
11251 temp[i++] = n;
11252 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253 rettv->v_type = VAR_STRING;
11254 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011255
11256#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011257 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011258 {
11259 int row = mouse_row;
11260 int col = mouse_col;
11261 win_T *win;
11262 linenr_T lnum;
11263# ifdef FEAT_WINDOWS
11264 win_T *wp;
11265# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011266 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011267
11268 if (row >= 0 && col >= 0)
11269 {
11270 /* Find the window at the mouse coordinates and compute the
11271 * text position. */
11272 win = mouse_find_win(&row, &col);
11273 (void)mouse_comp_pos(win, &row, &col, &lnum);
11274# ifdef FEAT_WINDOWS
11275 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011276 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011277# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011278 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011279 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11280 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11281 }
11282 }
11283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 }
11285}
11286
11287/*
11288 * "getcharmod()" function
11289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011291f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296}
11297
11298/*
11299 * "getcmdline()" function
11300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011302f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011303 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 rettv->v_type = VAR_STRING;
11307 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308}
11309
11310/*
11311 * "getcmdpos()" function
11312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011315 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319}
11320
11321/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011322 * "getcmdtype()" function
11323 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011324 static void
11325f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011326 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011327 typval_T *rettv;
11328{
11329 rettv->v_type = VAR_STRING;
11330 rettv->vval.v_string = alloc(2);
11331 if (rettv->vval.v_string != NULL)
11332 {
11333 rettv->vval.v_string[0] = get_cmdline_type();
11334 rettv->vval.v_string[1] = NUL;
11335 }
11336}
11337
11338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 * "getcwd()" function
11340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011343 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011346 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011349 rettv->vval.v_string = NULL;
11350 cwd = alloc(MAXPATHL);
11351 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011353 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11354 {
11355 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011357 if (rettv->vval.v_string != NULL)
11358 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011360 }
11361 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 }
11363}
11364
11365/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011366 * "getfontname()" function
11367 */
11368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011370 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011371 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011372{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 rettv->v_type = VAR_STRING;
11374 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011375#ifdef FEAT_GUI
11376 if (gui.in_use)
11377 {
11378 GuiFont font;
11379 char_u *name = NULL;
11380
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011381 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011382 {
11383 /* Get the "Normal" font. Either the name saved by
11384 * hl_set_font_name() or from the font ID. */
11385 font = gui.norm_font;
11386 name = hl_get_font_name();
11387 }
11388 else
11389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011391 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11392 return;
11393 font = gui_mch_get_font(name, FALSE);
11394 if (font == NOFONT)
11395 return; /* Invalid font name, return empty string. */
11396 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011398 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011399 gui_mch_free_font(font);
11400 }
11401#endif
11402}
11403
11404/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011405 * "getfperm({fname})" function
11406 */
11407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011409 typval_T *argvars;
11410 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011411{
11412 char_u *fname;
11413 struct stat st;
11414 char_u *perm = NULL;
11415 char_u flags[] = "rwx";
11416 int i;
11417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011421 if (mch_stat((char *)fname, &st) >= 0)
11422 {
11423 perm = vim_strsave((char_u *)"---------");
11424 if (perm != NULL)
11425 {
11426 for (i = 0; i < 9; i++)
11427 {
11428 if (st.st_mode & (1 << (8 - i)))
11429 perm[i] = flags[i % 3];
11430 }
11431 }
11432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011434}
11435
11436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 * "getfsize({fname})" function
11438 */
11439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 typval_T *argvars;
11442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
11444 char_u *fname;
11445 struct stat st;
11446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450
11451 if (mch_stat((char *)fname, &st) >= 0)
11452 {
11453 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011458
11459 /* non-perfect check for overflow */
11460 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11461 rettv->vval.v_number = -2;
11462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 }
11464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466}
11467
11468/*
11469 * "getftime({fname})" function
11470 */
11471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011473 typval_T *argvars;
11474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475{
11476 char_u *fname;
11477 struct stat st;
11478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480
11481 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485}
11486
11487/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011488 * "getftype({fname})" function
11489 */
11490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *argvars;
11493 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011494{
11495 char_u *fname;
11496 struct stat st;
11497 char_u *type = NULL;
11498 char *t;
11499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011503 if (mch_lstat((char *)fname, &st) >= 0)
11504 {
11505#ifdef S_ISREG
11506 if (S_ISREG(st.st_mode))
11507 t = "file";
11508 else if (S_ISDIR(st.st_mode))
11509 t = "dir";
11510# ifdef S_ISLNK
11511 else if (S_ISLNK(st.st_mode))
11512 t = "link";
11513# endif
11514# ifdef S_ISBLK
11515 else if (S_ISBLK(st.st_mode))
11516 t = "bdev";
11517# endif
11518# ifdef S_ISCHR
11519 else if (S_ISCHR(st.st_mode))
11520 t = "cdev";
11521# endif
11522# ifdef S_ISFIFO
11523 else if (S_ISFIFO(st.st_mode))
11524 t = "fifo";
11525# endif
11526# ifdef S_ISSOCK
11527 else if (S_ISSOCK(st.st_mode))
11528 t = "fifo";
11529# endif
11530 else
11531 t = "other";
11532#else
11533# ifdef S_IFMT
11534 switch (st.st_mode & S_IFMT)
11535 {
11536 case S_IFREG: t = "file"; break;
11537 case S_IFDIR: t = "dir"; break;
11538# ifdef S_IFLNK
11539 case S_IFLNK: t = "link"; break;
11540# endif
11541# ifdef S_IFBLK
11542 case S_IFBLK: t = "bdev"; break;
11543# endif
11544# ifdef S_IFCHR
11545 case S_IFCHR: t = "cdev"; break;
11546# endif
11547# ifdef S_IFIFO
11548 case S_IFIFO: t = "fifo"; break;
11549# endif
11550# ifdef S_IFSOCK
11551 case S_IFSOCK: t = "socket"; break;
11552# endif
11553 default: t = "other";
11554 }
11555# else
11556 if (mch_isdir(fname))
11557 t = "dir";
11558 else
11559 t = "file";
11560# endif
11561#endif
11562 type = vim_strsave((char_u *)t);
11563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011565}
11566
11567/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011568 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011569 */
11570 static void
11571f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011572 typval_T *argvars;
11573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011574{
11575 linenr_T lnum;
11576 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011577 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011578
11579 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011580 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011581 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011582 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011583 retlist = FALSE;
11584 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011585 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011586 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011587 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011588 retlist = TRUE;
11589 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011590
Bram Moolenaar342337a2005-07-21 21:11:17 +000011591 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011592}
11593
11594/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011595 * "getmatches()" function
11596 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011597 static void
11598f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011599 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011600 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011601{
11602#ifdef FEAT_SEARCH_EXTRA
11603 dict_T *dict;
11604 matchitem_T *cur = curwin->w_match_head;
11605
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011606 if (rettv_list_alloc(rettv) == OK)
11607 {
11608 while (cur != NULL)
11609 {
11610 dict = dict_alloc();
11611 if (dict == NULL)
11612 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011613 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11614 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11615 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11616 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11617 list_append_dict(rettv->vval.v_list, dict);
11618 cur = cur->next;
11619 }
11620 }
11621#endif
11622}
11623
11624/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011625 * "getpid()" function
11626 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011627 static void
11628f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011629 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011630 typval_T *rettv;
11631{
11632 rettv->vval.v_number = mch_get_pid();
11633}
11634
11635/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011636 * "getpos(string)" function
11637 */
11638 static void
11639f_getpos(argvars, rettv)
11640 typval_T *argvars;
11641 typval_T *rettv;
11642{
11643 pos_T *fp;
11644 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011645 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011646
11647 if (rettv_list_alloc(rettv) == OK)
11648 {
11649 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011650 fp = var2fpos(&argvars[0], TRUE, &fnum);
11651 if (fnum != -1)
11652 list_append_number(l, (varnumber_T)fnum);
11653 else
11654 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011655 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11656 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011657 list_append_number(l, (fp != NULL)
11658 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011659 : (varnumber_T)0);
11660 list_append_number(l,
11661#ifdef FEAT_VIRTUALEDIT
11662 (fp != NULL) ? (varnumber_T)fp->coladd :
11663#endif
11664 (varnumber_T)0);
11665 }
11666 else
11667 rettv->vval.v_number = FALSE;
11668}
11669
11670/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011671 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011672 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011673 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011674f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011675 typval_T *argvars UNUSED;
11676 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011677{
11678#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011679 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011680#endif
11681
Bram Moolenaar2641f772005-03-25 21:58:17 +000011682#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011683 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011684 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011685 wp = NULL;
11686 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11687 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011688 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011689 if (wp == NULL)
11690 return;
11691 }
11692
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011693 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011694 }
11695#endif
11696}
11697
11698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 * "getreg()" function
11700 */
11701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011703 typval_T *argvars;
11704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705{
11706 char_u *strregname;
11707 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011708 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011711 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713 strregname = get_tv_string_chk(&argvars[0]);
11714 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011715 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011719 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 regname = (strregname == NULL ? '"' : *strregname);
11721 if (regname == 0)
11722 regname = '"';
11723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011724 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011725 rettv->vval.v_string = error ? NULL :
11726 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727}
11728
11729/*
11730 * "getregtype()" function
11731 */
11732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011734 typval_T *argvars;
11735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736{
11737 char_u *strregname;
11738 int regname;
11739 char_u buf[NUMBUFLEN + 2];
11740 long reglen = 0;
11741
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011742 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011743 {
11744 strregname = get_tv_string_chk(&argvars[0]);
11745 if (strregname == NULL) /* type error; errmsg already given */
11746 {
11747 rettv->v_type = VAR_STRING;
11748 rettv->vval.v_string = NULL;
11749 return;
11750 }
11751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 else
11753 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755
11756 regname = (strregname == NULL ? '"' : *strregname);
11757 if (regname == 0)
11758 regname = '"';
11759
11760 buf[0] = NUL;
11761 buf[1] = NUL;
11762 switch (get_reg_type(regname, &reglen))
11763 {
11764 case MLINE: buf[0] = 'V'; break;
11765 case MCHAR: buf[0] = 'v'; break;
11766#ifdef FEAT_VISUAL
11767 case MBLOCK:
11768 buf[0] = Ctrl_V;
11769 sprintf((char *)buf + 1, "%ld", reglen + 1);
11770 break;
11771#endif
11772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773 rettv->v_type = VAR_STRING;
11774 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775}
11776
11777/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011778 * "gettabvar()" function
11779 */
11780 static void
11781f_gettabvar(argvars, rettv)
11782 typval_T *argvars;
11783 typval_T *rettv;
11784{
11785 tabpage_T *tp;
11786 dictitem_T *v;
11787 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011788 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011789
11790 rettv->v_type = VAR_STRING;
11791 rettv->vval.v_string = NULL;
11792
11793 varname = get_tv_string_chk(&argvars[1]);
11794 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11795 if (tp != NULL && varname != NULL)
11796 {
11797 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011798 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011799 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011800 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011801 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011802 done = TRUE;
11803 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011804 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011805
11806 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011807 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011808}
11809
11810/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011811 * "gettabwinvar()" function
11812 */
11813 static void
11814f_gettabwinvar(argvars, rettv)
11815 typval_T *argvars;
11816 typval_T *rettv;
11817{
11818 getwinvar(argvars, rettv, 1);
11819}
11820
11821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 * "getwinposx()" function
11823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011826 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830#ifdef FEAT_GUI
11831 if (gui.in_use)
11832 {
11833 int x, y;
11834
11835 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 }
11838#endif
11839}
11840
11841/*
11842 * "getwinposy()" function
11843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850#ifdef FEAT_GUI
11851 if (gui.in_use)
11852 {
11853 int x, y;
11854
11855 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 }
11858#endif
11859}
11860
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011861/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011862 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011863 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011864 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011865find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011866 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011867 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011868{
11869#ifdef FEAT_WINDOWS
11870 win_T *wp;
11871#endif
11872 int nr;
11873
11874 nr = get_tv_number_chk(vp, NULL);
11875
11876#ifdef FEAT_WINDOWS
11877 if (nr < 0)
11878 return NULL;
11879 if (nr == 0)
11880 return curwin;
11881
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011882 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11883 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011884 if (--nr <= 0)
11885 break;
11886 return wp;
11887#else
11888 if (nr == 0 || nr == 1)
11889 return curwin;
11890 return NULL;
11891#endif
11892}
11893
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894/*
11895 * "getwinvar()" function
11896 */
11897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011899 typval_T *argvars;
11900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011902 getwinvar(argvars, rettv, 0);
11903}
11904
11905/*
11906 * getwinvar() and gettabwinvar()
11907 */
11908 static void
11909getwinvar(argvars, rettv, off)
11910 typval_T *argvars;
11911 typval_T *rettv;
11912 int off; /* 1 for gettabwinvar() */
11913{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 win_T *win, *oldcurwin;
11915 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011916 dictitem_T *v;
Bram Moolenaar105bc352013-05-17 16:03:57 +020011917 tabpage_T *tp, *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011918 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011920#ifdef FEAT_WINDOWS
11921 if (off == 1)
11922 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11923 else
11924 tp = curtab;
11925#endif
11926 win = find_win_by_nr(&argvars[off], tp);
11927 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011928 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011930 rettv->v_type = VAR_STRING;
11931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932
11933 if (win != NULL && varname != NULL)
11934 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011935 /* Set curwin to be our win, temporarily. Also set the tabpage,
11936 * otherwise the window is not valid. */
11937 switch_win(&oldcurwin, &oldtabpage, win, tp);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011938
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011940 {
11941 if (get_option_tv(&varname, rettv, 1) == OK)
11942 done = TRUE;
11943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 else
11945 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011946 /* Look up the variable. */
11947 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11948 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011950 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011951 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011952 done = TRUE;
11953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011955
11956 /* restore previous notion of curwin */
Bram Moolenaar105bc352013-05-17 16:03:57 +020011957 restore_win(oldcurwin, oldtabpage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958 }
11959
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011960 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11961 /* use the default return value */
11962 copy_tv(&argvars[off + 2], rettv);
11963
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964 --emsg_off;
11965}
11966
11967/*
11968 * "glob()" function
11969 */
11970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011971f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011972 typval_T *argvars;
11973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011975 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011977 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011979 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011980 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011982 if (argvars[1].v_type != VAR_UNKNOWN)
11983 {
11984 if (get_tv_number_chk(&argvars[1], &error))
11985 options |= WILD_KEEP_ALL;
11986 if (argvars[2].v_type != VAR_UNKNOWN
11987 && get_tv_number_chk(&argvars[2], &error))
11988 {
11989 rettv->v_type = VAR_LIST;
11990 rettv->vval.v_list = NULL;
11991 }
11992 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011993 if (!error)
11994 {
11995 ExpandInit(&xpc);
11996 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011997 if (p_wic)
11998 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011999 if (rettv->v_type == VAR_STRING)
12000 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012001 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012002 else if (rettv_list_alloc(rettv) != FAIL)
12003 {
12004 int i;
12005
12006 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12007 NULL, options, WILD_ALL_KEEP);
12008 for (i = 0; i < xpc.xp_numfiles; i++)
12009 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12010
12011 ExpandCleanup(&xpc);
12012 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012013 }
12014 else
12015 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016}
12017
12018/*
12019 * "globpath()" function
12020 */
12021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012023 typval_T *argvars;
12024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012026 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012029 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012031 /* When the optional second argument is non-zero, don't remove matches
12032 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12033 if (argvars[2].v_type != VAR_UNKNOWN
12034 && get_tv_number_chk(&argvars[2], &error))
12035 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012037 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012038 rettv->vval.v_string = NULL;
12039 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012040 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12041 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042}
12043
12044/*
12045 * "has()" function
12046 */
12047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012049 typval_T *argvars;
12050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052 int i;
12053 char_u *name;
12054 int n = FALSE;
12055 static char *(has_list[]) =
12056 {
12057#ifdef AMIGA
12058 "amiga",
12059# ifdef FEAT_ARP
12060 "arp",
12061# endif
12062#endif
12063#ifdef __BEOS__
12064 "beos",
12065#endif
12066#ifdef MSDOS
12067# ifdef DJGPP
12068 "dos32",
12069# else
12070 "dos16",
12071# endif
12072#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012073#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 "mac",
12075#endif
12076#if defined(MACOS_X_UNIX)
12077 "macunix",
12078#endif
12079#ifdef OS2
12080 "os2",
12081#endif
12082#ifdef __QNX__
12083 "qnx",
12084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085#ifdef UNIX
12086 "unix",
12087#endif
12088#ifdef VMS
12089 "vms",
12090#endif
12091#ifdef WIN16
12092 "win16",
12093#endif
12094#ifdef WIN32
12095 "win32",
12096#endif
12097#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12098 "win32unix",
12099#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012100#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 "win64",
12102#endif
12103#ifdef EBCDIC
12104 "ebcdic",
12105#endif
12106#ifndef CASE_INSENSITIVE_FILENAME
12107 "fname_case",
12108#endif
12109#ifdef FEAT_ARABIC
12110 "arabic",
12111#endif
12112#ifdef FEAT_AUTOCMD
12113 "autocmd",
12114#endif
12115#ifdef FEAT_BEVAL
12116 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012117# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12118 "balloon_multiline",
12119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120#endif
12121#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12122 "builtin_terms",
12123# ifdef ALL_BUILTIN_TCAPS
12124 "all_builtin_terms",
12125# endif
12126#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012127#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12128 || defined(FEAT_GUI_W32) \
12129 || defined(FEAT_GUI_MOTIF))
12130 "browsefilter",
12131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132#ifdef FEAT_BYTEOFF
12133 "byte_offset",
12134#endif
12135#ifdef FEAT_CINDENT
12136 "cindent",
12137#endif
12138#ifdef FEAT_CLIENTSERVER
12139 "clientserver",
12140#endif
12141#ifdef FEAT_CLIPBOARD
12142 "clipboard",
12143#endif
12144#ifdef FEAT_CMDL_COMPL
12145 "cmdline_compl",
12146#endif
12147#ifdef FEAT_CMDHIST
12148 "cmdline_hist",
12149#endif
12150#ifdef FEAT_COMMENTS
12151 "comments",
12152#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012153#ifdef FEAT_CONCEAL
12154 "conceal",
12155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156#ifdef FEAT_CRYPT
12157 "cryptv",
12158#endif
12159#ifdef FEAT_CSCOPE
12160 "cscope",
12161#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012162#ifdef FEAT_CURSORBIND
12163 "cursorbind",
12164#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012165#ifdef CURSOR_SHAPE
12166 "cursorshape",
12167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168#ifdef DEBUG
12169 "debug",
12170#endif
12171#ifdef FEAT_CON_DIALOG
12172 "dialog_con",
12173#endif
12174#ifdef FEAT_GUI_DIALOG
12175 "dialog_gui",
12176#endif
12177#ifdef FEAT_DIFF
12178 "diff",
12179#endif
12180#ifdef FEAT_DIGRAPHS
12181 "digraphs",
12182#endif
12183#ifdef FEAT_DND
12184 "dnd",
12185#endif
12186#ifdef FEAT_EMACS_TAGS
12187 "emacs_tags",
12188#endif
12189 "eval", /* always present, of course! */
12190#ifdef FEAT_EX_EXTRA
12191 "ex_extra",
12192#endif
12193#ifdef FEAT_SEARCH_EXTRA
12194 "extra_search",
12195#endif
12196#ifdef FEAT_FKMAP
12197 "farsi",
12198#endif
12199#ifdef FEAT_SEARCHPATH
12200 "file_in_path",
12201#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012202#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012203 "filterpipe",
12204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205#ifdef FEAT_FIND_ID
12206 "find_in_path",
12207#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012208#ifdef FEAT_FLOAT
12209 "float",
12210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211#ifdef FEAT_FOLDING
12212 "folding",
12213#endif
12214#ifdef FEAT_FOOTER
12215 "footer",
12216#endif
12217#if !defined(USE_SYSTEM) && defined(UNIX)
12218 "fork",
12219#endif
12220#ifdef FEAT_GETTEXT
12221 "gettext",
12222#endif
12223#ifdef FEAT_GUI
12224 "gui",
12225#endif
12226#ifdef FEAT_GUI_ATHENA
12227# ifdef FEAT_GUI_NEXTAW
12228 "gui_neXtaw",
12229# else
12230 "gui_athena",
12231# endif
12232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233#ifdef FEAT_GUI_GTK
12234 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012237#ifdef FEAT_GUI_GNOME
12238 "gui_gnome",
12239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240#ifdef FEAT_GUI_MAC
12241 "gui_mac",
12242#endif
12243#ifdef FEAT_GUI_MOTIF
12244 "gui_motif",
12245#endif
12246#ifdef FEAT_GUI_PHOTON
12247 "gui_photon",
12248#endif
12249#ifdef FEAT_GUI_W16
12250 "gui_win16",
12251#endif
12252#ifdef FEAT_GUI_W32
12253 "gui_win32",
12254#endif
12255#ifdef FEAT_HANGULIN
12256 "hangul_input",
12257#endif
12258#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12259 "iconv",
12260#endif
12261#ifdef FEAT_INS_EXPAND
12262 "insert_expand",
12263#endif
12264#ifdef FEAT_JUMPLIST
12265 "jumplist",
12266#endif
12267#ifdef FEAT_KEYMAP
12268 "keymap",
12269#endif
12270#ifdef FEAT_LANGMAP
12271 "langmap",
12272#endif
12273#ifdef FEAT_LIBCALL
12274 "libcall",
12275#endif
12276#ifdef FEAT_LINEBREAK
12277 "linebreak",
12278#endif
12279#ifdef FEAT_LISP
12280 "lispindent",
12281#endif
12282#ifdef FEAT_LISTCMDS
12283 "listcmds",
12284#endif
12285#ifdef FEAT_LOCALMAP
12286 "localmap",
12287#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012288#ifdef FEAT_LUA
12289# ifndef DYNAMIC_LUA
12290 "lua",
12291# endif
12292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293#ifdef FEAT_MENU
12294 "menu",
12295#endif
12296#ifdef FEAT_SESSION
12297 "mksession",
12298#endif
12299#ifdef FEAT_MODIFY_FNAME
12300 "modify_fname",
12301#endif
12302#ifdef FEAT_MOUSE
12303 "mouse",
12304#endif
12305#ifdef FEAT_MOUSESHAPE
12306 "mouseshape",
12307#endif
12308#if defined(UNIX) || defined(VMS)
12309# ifdef FEAT_MOUSE_DEC
12310 "mouse_dec",
12311# endif
12312# ifdef FEAT_MOUSE_GPM
12313 "mouse_gpm",
12314# endif
12315# ifdef FEAT_MOUSE_JSB
12316 "mouse_jsbterm",
12317# endif
12318# ifdef FEAT_MOUSE_NET
12319 "mouse_netterm",
12320# endif
12321# ifdef FEAT_MOUSE_PTERM
12322 "mouse_pterm",
12323# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012324# ifdef FEAT_MOUSE_SGR
12325 "mouse_sgr",
12326# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012327# ifdef FEAT_SYSMOUSE
12328 "mouse_sysmouse",
12329# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012330# ifdef FEAT_MOUSE_URXVT
12331 "mouse_urxvt",
12332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333# ifdef FEAT_MOUSE_XTERM
12334 "mouse_xterm",
12335# endif
12336#endif
12337#ifdef FEAT_MBYTE
12338 "multi_byte",
12339#endif
12340#ifdef FEAT_MBYTE_IME
12341 "multi_byte_ime",
12342#endif
12343#ifdef FEAT_MULTI_LANG
12344 "multi_lang",
12345#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012346#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012347#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012348 "mzscheme",
12349#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351#ifdef FEAT_OLE
12352 "ole",
12353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354#ifdef FEAT_PATH_EXTRA
12355 "path_extra",
12356#endif
12357#ifdef FEAT_PERL
12358#ifndef DYNAMIC_PERL
12359 "perl",
12360#endif
12361#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012362#ifdef FEAT_PERSISTENT_UNDO
12363 "persistent_undo",
12364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365#ifdef FEAT_PYTHON
12366#ifndef DYNAMIC_PYTHON
12367 "python",
12368#endif
12369#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012370#ifdef FEAT_PYTHON3
12371#ifndef DYNAMIC_PYTHON3
12372 "python3",
12373#endif
12374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375#ifdef FEAT_POSTSCRIPT
12376 "postscript",
12377#endif
12378#ifdef FEAT_PRINTER
12379 "printer",
12380#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012381#ifdef FEAT_PROFILE
12382 "profile",
12383#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012384#ifdef FEAT_RELTIME
12385 "reltime",
12386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387#ifdef FEAT_QUICKFIX
12388 "quickfix",
12389#endif
12390#ifdef FEAT_RIGHTLEFT
12391 "rightleft",
12392#endif
12393#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12394 "ruby",
12395#endif
12396#ifdef FEAT_SCROLLBIND
12397 "scrollbind",
12398#endif
12399#ifdef FEAT_CMDL_INFO
12400 "showcmd",
12401 "cmdline_info",
12402#endif
12403#ifdef FEAT_SIGNS
12404 "signs",
12405#endif
12406#ifdef FEAT_SMARTINDENT
12407 "smartindent",
12408#endif
12409#ifdef FEAT_SNIFF
12410 "sniff",
12411#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012412#ifdef STARTUPTIME
12413 "startuptime",
12414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415#ifdef FEAT_STL_OPT
12416 "statusline",
12417#endif
12418#ifdef FEAT_SUN_WORKSHOP
12419 "sun_workshop",
12420#endif
12421#ifdef FEAT_NETBEANS_INTG
12422 "netbeans_intg",
12423#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012424#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012425 "spell",
12426#endif
12427#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 "syntax",
12429#endif
12430#if defined(USE_SYSTEM) || !defined(UNIX)
12431 "system",
12432#endif
12433#ifdef FEAT_TAG_BINS
12434 "tag_binary",
12435#endif
12436#ifdef FEAT_TAG_OLDSTATIC
12437 "tag_old_static",
12438#endif
12439#ifdef FEAT_TAG_ANYWHITE
12440 "tag_any_white",
12441#endif
12442#ifdef FEAT_TCL
12443# ifndef DYNAMIC_TCL
12444 "tcl",
12445# endif
12446#endif
12447#ifdef TERMINFO
12448 "terminfo",
12449#endif
12450#ifdef FEAT_TERMRESPONSE
12451 "termresponse",
12452#endif
12453#ifdef FEAT_TEXTOBJ
12454 "textobjects",
12455#endif
12456#ifdef HAVE_TGETENT
12457 "tgetent",
12458#endif
12459#ifdef FEAT_TITLE
12460 "title",
12461#endif
12462#ifdef FEAT_TOOLBAR
12463 "toolbar",
12464#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012465#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12466 "unnamedplus",
12467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468#ifdef FEAT_USR_CMDS
12469 "user-commands", /* was accidentally included in 5.4 */
12470 "user_commands",
12471#endif
12472#ifdef FEAT_VIMINFO
12473 "viminfo",
12474#endif
12475#ifdef FEAT_VERTSPLIT
12476 "vertsplit",
12477#endif
12478#ifdef FEAT_VIRTUALEDIT
12479 "virtualedit",
12480#endif
12481#ifdef FEAT_VISUAL
12482 "visual",
12483#endif
12484#ifdef FEAT_VISUALEXTRA
12485 "visualextra",
12486#endif
12487#ifdef FEAT_VREPLACE
12488 "vreplace",
12489#endif
12490#ifdef FEAT_WILDIGN
12491 "wildignore",
12492#endif
12493#ifdef FEAT_WILDMENU
12494 "wildmenu",
12495#endif
12496#ifdef FEAT_WINDOWS
12497 "windows",
12498#endif
12499#ifdef FEAT_WAK
12500 "winaltkeys",
12501#endif
12502#ifdef FEAT_WRITEBACKUP
12503 "writebackup",
12504#endif
12505#ifdef FEAT_XIM
12506 "xim",
12507#endif
12508#ifdef FEAT_XFONTSET
12509 "xfontset",
12510#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012511#ifdef FEAT_XPM_W32
12512 "xpm_w32",
12513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514#ifdef USE_XSMP
12515 "xsmp",
12516#endif
12517#ifdef USE_XSMP_INTERACT
12518 "xsmp_interact",
12519#endif
12520#ifdef FEAT_XCLIPBOARD
12521 "xterm_clipboard",
12522#endif
12523#ifdef FEAT_XTERM_SAVE
12524 "xterm_save",
12525#endif
12526#if defined(UNIX) && defined(FEAT_X11)
12527 "X11",
12528#endif
12529 NULL
12530 };
12531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 for (i = 0; has_list[i] != NULL; ++i)
12534 if (STRICMP(name, has_list[i]) == 0)
12535 {
12536 n = TRUE;
12537 break;
12538 }
12539
12540 if (n == FALSE)
12541 {
12542 if (STRNICMP(name, "patch", 5) == 0)
12543 n = has_patch(atoi((char *)name + 5));
12544 else if (STRICMP(name, "vim_starting") == 0)
12545 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012546#ifdef FEAT_MBYTE
12547 else if (STRICMP(name, "multi_byte_encoding") == 0)
12548 n = has_mbyte;
12549#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012550#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12551 else if (STRICMP(name, "balloon_multiline") == 0)
12552 n = multiline_balloon_available();
12553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554#ifdef DYNAMIC_TCL
12555 else if (STRICMP(name, "tcl") == 0)
12556 n = tcl_enabled(FALSE);
12557#endif
12558#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12559 else if (STRICMP(name, "iconv") == 0)
12560 n = iconv_enabled(FALSE);
12561#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012562#ifdef DYNAMIC_LUA
12563 else if (STRICMP(name, "lua") == 0)
12564 n = lua_enabled(FALSE);
12565#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012566#ifdef DYNAMIC_MZSCHEME
12567 else if (STRICMP(name, "mzscheme") == 0)
12568 n = mzscheme_enabled(FALSE);
12569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570#ifdef DYNAMIC_RUBY
12571 else if (STRICMP(name, "ruby") == 0)
12572 n = ruby_enabled(FALSE);
12573#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012574#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575#ifdef DYNAMIC_PYTHON
12576 else if (STRICMP(name, "python") == 0)
12577 n = python_enabled(FALSE);
12578#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012579#endif
12580#ifdef FEAT_PYTHON3
12581#ifdef DYNAMIC_PYTHON3
12582 else if (STRICMP(name, "python3") == 0)
12583 n = python3_enabled(FALSE);
12584#endif
12585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586#ifdef DYNAMIC_PERL
12587 else if (STRICMP(name, "perl") == 0)
12588 n = perl_enabled(FALSE);
12589#endif
12590#ifdef FEAT_GUI
12591 else if (STRICMP(name, "gui_running") == 0)
12592 n = (gui.in_use || gui.starting);
12593# ifdef FEAT_GUI_W32
12594 else if (STRICMP(name, "gui_win32s") == 0)
12595 n = gui_is_win32s();
12596# endif
12597# ifdef FEAT_BROWSE
12598 else if (STRICMP(name, "browse") == 0)
12599 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12600# endif
12601#endif
12602#ifdef FEAT_SYN_HL
12603 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012604 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605#endif
12606#if defined(WIN3264)
12607 else if (STRICMP(name, "win95") == 0)
12608 n = mch_windows95();
12609#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012610#ifdef FEAT_NETBEANS_INTG
12611 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012612 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 }
12615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617}
12618
12619/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012620 * "has_key()" function
12621 */
12622 static void
12623f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012624 typval_T *argvars;
12625 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012626{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012627 if (argvars[0].v_type != VAR_DICT)
12628 {
12629 EMSG(_(e_dictreq));
12630 return;
12631 }
12632 if (argvars[0].vval.v_dict == NULL)
12633 return;
12634
12635 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012636 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012637}
12638
12639/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012640 * "haslocaldir()" function
12641 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012642 static void
12643f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012644 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012645 typval_T *rettv;
12646{
12647 rettv->vval.v_number = (curwin->w_localdir != NULL);
12648}
12649
12650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 * "hasmapto()" function
12652 */
12653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012655 typval_T *argvars;
12656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657{
12658 char_u *name;
12659 char_u *mode;
12660 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012661 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012663 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012664 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 mode = (char_u *)"nvo";
12666 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012669 if (argvars[2].v_type != VAR_UNKNOWN)
12670 abbr = get_tv_number(&argvars[2]);
12671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672
Bram Moolenaar2c932302006-03-18 21:42:09 +000012673 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677}
12678
12679/*
12680 * "histadd()" function
12681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012683f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012684 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686{
12687#ifdef FEAT_CMDHIST
12688 int histype;
12689 char_u *str;
12690 char_u buf[NUMBUFLEN];
12691#endif
12692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012693 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 if (check_restricted() || check_secure())
12695 return;
12696#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012697 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12698 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 if (histype >= 0)
12700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 if (*str != NUL)
12703 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012704 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 return;
12708 }
12709 }
12710#endif
12711}
12712
12713/*
12714 * "histdel()" function
12715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012718 typval_T *argvars UNUSED;
12719 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720{
12721#ifdef FEAT_CMDHIST
12722 int n;
12723 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12727 if (str == NULL)
12728 n = 0;
12729 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012732 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012734 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 else
12737 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 get_tv_string_buf(&argvars[1], buf));
12740 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741#endif
12742}
12743
12744/*
12745 * "histget()" function
12746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012749 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751{
12752#ifdef FEAT_CMDHIST
12753 int type;
12754 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012755 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012757 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12758 if (str == NULL)
12759 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012761 {
12762 type = get_histtype(str);
12763 if (argvars[1].v_type == VAR_UNKNOWN)
12764 idx = get_history_idx(type);
12765 else
12766 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12767 /* -1 on type error */
12768 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774}
12775
12776/*
12777 * "histnr()" function
12778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783{
12784 int i;
12785
12786#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012787 char_u *history = get_tv_string_chk(&argvars[0]);
12788
12789 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 if (i >= HIST_CMD && i < HIST_COUNT)
12791 i = get_history_idx(i);
12792 else
12793#endif
12794 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796}
12797
12798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 * "highlightID(name)" function
12800 */
12801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012803 typval_T *argvars;
12804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807}
12808
12809/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012810 * "highlight_exists()" function
12811 */
12812 static void
12813f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012814 typval_T *argvars;
12815 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012816{
12817 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12818}
12819
12820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 * "hostname()" function
12822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827{
12828 char_u hostname[256];
12829
12830 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 rettv->v_type = VAR_STRING;
12832 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833}
12834
12835/*
12836 * iconv() function
12837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012840 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842{
12843#ifdef FEAT_MBYTE
12844 char_u buf1[NUMBUFLEN];
12845 char_u buf2[NUMBUFLEN];
12846 char_u *from, *to, *str;
12847 vimconv_T vimconv;
12848#endif
12849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->v_type = VAR_STRING;
12851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852
12853#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 str = get_tv_string(&argvars[0]);
12855 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12856 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 vimconv.vc_type = CONV_NONE;
12858 convert_setup(&vimconv, from, to);
12859
12860 /* If the encodings are equal, no conversion needed. */
12861 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865
12866 convert_setup(&vimconv, NULL, NULL);
12867 vim_free(from);
12868 vim_free(to);
12869#endif
12870}
12871
12872/*
12873 * "indent()" function
12874 */
12875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879{
12880 linenr_T lnum;
12881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887}
12888
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012889/*
12890 * "index()" function
12891 */
12892 static void
12893f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012894 typval_T *argvars;
12895 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012896{
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 list_T *l;
12898 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012899 long idx = 0;
12900 int ic = FALSE;
12901
12902 rettv->vval.v_number = -1;
12903 if (argvars[0].v_type != VAR_LIST)
12904 {
12905 EMSG(_(e_listreq));
12906 return;
12907 }
12908 l = argvars[0].vval.v_list;
12909 if (l != NULL)
12910 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012911 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012912 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012914 int error = FALSE;
12915
Bram Moolenaar758711c2005-02-02 23:11:38 +000012916 /* Start at specified item. Use the cached index that list_find()
12917 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012918 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012919 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012920 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012921 ic = get_tv_number_chk(&argvars[3], &error);
12922 if (error)
12923 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012924 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012925
Bram Moolenaar758711c2005-02-02 23:11:38 +000012926 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012927 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012928 {
12929 rettv->vval.v_number = idx;
12930 break;
12931 }
12932 }
12933}
12934
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935static int inputsecret_flag = 0;
12936
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012937static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12938
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012940 * This function is used by f_input() and f_inputdialog() functions. The third
12941 * argument to f_input() specifies the type of completion to use at the
12942 * prompt. The third argument to f_inputdialog() specifies the value to return
12943 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944 */
12945 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012946get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012947 typval_T *argvars;
12948 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012949 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012951 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 char_u *p = NULL;
12953 int c;
12954 char_u buf[NUMBUFLEN];
12955 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012956 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012957 int xp_type = EXPAND_NOTHING;
12958 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962
12963#ifdef NO_CONSOLE_INPUT
12964 /* While starting up, there is no place to enter text. */
12965 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967#endif
12968
12969 cmd_silent = FALSE; /* Want to see the prompt. */
12970 if (prompt != NULL)
12971 {
12972 /* Only the part of the message after the last NL is considered as
12973 * prompt for the command line */
12974 p = vim_strrchr(prompt, '\n');
12975 if (p == NULL)
12976 p = prompt;
12977 else
12978 {
12979 ++p;
12980 c = *p;
12981 *p = NUL;
12982 msg_start();
12983 msg_clr_eos();
12984 msg_puts_attr(prompt, echo_attr);
12985 msg_didout = FALSE;
12986 msg_starthere();
12987 *p = c;
12988 }
12989 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012991 if (argvars[1].v_type != VAR_UNKNOWN)
12992 {
12993 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12994 if (defstr != NULL)
12995 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012997 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012998 {
12999 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013000 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013001 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013002
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013003 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013004 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013005
Bram Moolenaar4463f292005-09-25 22:20:24 +000013006 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13007 if (xp_name == NULL)
13008 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013009
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013010 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013011
Bram Moolenaar4463f292005-09-25 22:20:24 +000013012 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13013 &xp_arg) == FAIL)
13014 return;
13015 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013016 }
13017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013018 if (defstr != NULL)
13019 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013020 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13021 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013022 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013023 && argvars[1].v_type != VAR_UNKNOWN
13024 && argvars[2].v_type != VAR_UNKNOWN)
13025 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13026 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013027
13028 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013030 /* since the user typed this, no need to wait for return */
13031 need_wait_return = FALSE;
13032 msg_didout = FALSE;
13033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 cmd_silent = cmd_silent_save;
13035}
13036
13037/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013038 * "input()" function
13039 * Also handles inputsecret() when inputsecret is set.
13040 */
13041 static void
13042f_input(argvars, rettv)
13043 typval_T *argvars;
13044 typval_T *rettv;
13045{
13046 get_user_input(argvars, rettv, FALSE);
13047}
13048
13049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 * "inputdialog()" function
13051 */
13052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013054 typval_T *argvars;
13055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056{
13057#if defined(FEAT_GUI_TEXTDIALOG)
13058 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13059 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13060 {
13061 char_u *message;
13062 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013063 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013065 message = get_tv_string_chk(&argvars[0]);
13066 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013067 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013068 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 else
13070 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013071 if (message != NULL && defstr != NULL
13072 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013073 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 else
13076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013077 if (message != NULL && defstr != NULL
13078 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013079 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 rettv->vval.v_string = vim_strsave(
13081 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013083 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 }
13087 else
13088#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013089 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090}
13091
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013092/*
13093 * "inputlist()" function
13094 */
13095 static void
13096f_inputlist(argvars, rettv)
13097 typval_T *argvars;
13098 typval_T *rettv;
13099{
13100 listitem_T *li;
13101 int selected;
13102 int mouse_used;
13103
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013104#ifdef NO_CONSOLE_INPUT
13105 /* While starting up, there is no place to enter text. */
13106 if (no_console_input())
13107 return;
13108#endif
13109 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13110 {
13111 EMSG2(_(e_listarg), "inputlist()");
13112 return;
13113 }
13114
13115 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013116 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013117 lines_left = Rows; /* avoid more prompt */
13118 msg_scroll = TRUE;
13119 msg_clr_eos();
13120
13121 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13122 {
13123 msg_puts(get_tv_string(&li->li_tv));
13124 msg_putchar('\n');
13125 }
13126
13127 /* Ask for choice. */
13128 selected = prompt_for_number(&mouse_used);
13129 if (mouse_used)
13130 selected -= lines_left;
13131
13132 rettv->vval.v_number = selected;
13133}
13134
13135
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13137
13138/*
13139 * "inputrestore()" function
13140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013142f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013143 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145{
13146 if (ga_userinput.ga_len > 0)
13147 {
13148 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13150 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013151 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 }
13153 else if (p_verbose > 1)
13154 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013155 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 }
13158}
13159
13160/*
13161 * "inputsave()" function
13162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013165 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013168 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 if (ga_grow(&ga_userinput, 1) == OK)
13170 {
13171 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13172 + ga_userinput.ga_len);
13173 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013174 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 }
13176 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178}
13179
13180/*
13181 * "inputsecret()" function
13182 */
13183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013184f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013185 typval_T *argvars;
13186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187{
13188 ++cmdline_star;
13189 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013190 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 --cmdline_star;
13192 --inputsecret_flag;
13193}
13194
13195/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013196 * "insert()" function
13197 */
13198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 typval_T *argvars;
13201 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013202{
13203 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013204 listitem_T *item;
13205 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013207
13208 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013209 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013210 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013211 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013212 {
13213 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013214 before = get_tv_number_chk(&argvars[2], &error);
13215 if (error)
13216 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013217
Bram Moolenaar758711c2005-02-02 23:11:38 +000013218 if (before == l->lv_len)
13219 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013220 else
13221 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013222 item = list_find(l, before);
13223 if (item == NULL)
13224 {
13225 EMSGN(_(e_listidx), before);
13226 l = NULL;
13227 }
13228 }
13229 if (l != NULL)
13230 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013231 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013232 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013233 }
13234 }
13235}
13236
13237/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013238 * "invert(expr)" function
13239 */
13240 static void
13241f_invert(argvars, rettv)
13242 typval_T *argvars;
13243 typval_T *rettv;
13244{
13245 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13246}
13247
13248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249 * "isdirectory()" function
13250 */
13251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013253 typval_T *argvars;
13254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257}
13258
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013259/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013260 * "islocked()" function
13261 */
13262 static void
13263f_islocked(argvars, rettv)
13264 typval_T *argvars;
13265 typval_T *rettv;
13266{
13267 lval_T lv;
13268 char_u *end;
13269 dictitem_T *di;
13270
13271 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013272 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13273 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013274 if (end != NULL && lv.ll_name != NULL)
13275 {
13276 if (*end != NUL)
13277 EMSG(_(e_trailing));
13278 else
13279 {
13280 if (lv.ll_tv == NULL)
13281 {
13282 if (check_changedtick(lv.ll_name))
13283 rettv->vval.v_number = 1; /* always locked */
13284 else
13285 {
13286 di = find_var(lv.ll_name, NULL);
13287 if (di != NULL)
13288 {
13289 /* Consider a variable locked when:
13290 * 1. the variable itself is locked
13291 * 2. the value of the variable is locked.
13292 * 3. the List or Dict value is locked.
13293 */
13294 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13295 || tv_islocked(&di->di_tv));
13296 }
13297 }
13298 }
13299 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013300 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013301 else if (lv.ll_newkey != NULL)
13302 EMSG2(_(e_dictkey), lv.ll_newkey);
13303 else if (lv.ll_list != NULL)
13304 /* List item. */
13305 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13306 else
13307 /* Dictionary item. */
13308 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13309 }
13310 }
13311
13312 clear_lval(&lv);
13313}
13314
Bram Moolenaar33570922005-01-25 22:26:29 +000013315static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013316
13317/*
13318 * Turn a dict into a list:
13319 * "what" == 0: list of keys
13320 * "what" == 1: list of values
13321 * "what" == 2: list of items
13322 */
13323 static void
13324dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013325 typval_T *argvars;
13326 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013327 int what;
13328{
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 list_T *l2;
13330 dictitem_T *di;
13331 hashitem_T *hi;
13332 listitem_T *li;
13333 listitem_T *li2;
13334 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013335 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013336
Bram Moolenaar8c711452005-01-14 21:53:12 +000013337 if (argvars[0].v_type != VAR_DICT)
13338 {
13339 EMSG(_(e_dictreq));
13340 return;
13341 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013342 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013343 return;
13344
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013345 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013346 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013347
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013348 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013350 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013351 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013352 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013353 --todo;
13354 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013355
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013356 li = listitem_alloc();
13357 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013358 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013359 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013360
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013361 if (what == 0)
13362 {
13363 /* keys() */
13364 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013365 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013366 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13367 }
13368 else if (what == 1)
13369 {
13370 /* values() */
13371 copy_tv(&di->di_tv, &li->li_tv);
13372 }
13373 else
13374 {
13375 /* items() */
13376 l2 = list_alloc();
13377 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013378 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013379 li->li_tv.vval.v_list = l2;
13380 if (l2 == NULL)
13381 break;
13382 ++l2->lv_refcount;
13383
13384 li2 = listitem_alloc();
13385 if (li2 == NULL)
13386 break;
13387 list_append(l2, li2);
13388 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013389 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013390 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13391
13392 li2 = listitem_alloc();
13393 if (li2 == NULL)
13394 break;
13395 list_append(l2, li2);
13396 copy_tv(&di->di_tv, &li2->li_tv);
13397 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013398 }
13399 }
13400}
13401
13402/*
13403 * "items(dict)" function
13404 */
13405 static void
13406f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013407 typval_T *argvars;
13408 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013409{
13410 dict_list(argvars, rettv, 2);
13411}
13412
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013414 * "join()" function
13415 */
13416 static void
13417f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013418 typval_T *argvars;
13419 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013420{
13421 garray_T ga;
13422 char_u *sep;
13423
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013424 if (argvars[0].v_type != VAR_LIST)
13425 {
13426 EMSG(_(e_listreq));
13427 return;
13428 }
13429 if (argvars[0].vval.v_list == NULL)
13430 return;
13431 if (argvars[1].v_type == VAR_UNKNOWN)
13432 sep = (char_u *)" ";
13433 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013435
13436 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013437
13438 if (sep != NULL)
13439 {
13440 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013441 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442 ga_append(&ga, NUL);
13443 rettv->vval.v_string = (char_u *)ga.ga_data;
13444 }
13445 else
13446 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013447}
13448
13449/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013450 * "keys()" function
13451 */
13452 static void
13453f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013454 typval_T *argvars;
13455 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013456{
13457 dict_list(argvars, rettv, 0);
13458}
13459
13460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 * "last_buffer_nr()" function.
13462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013464f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013465 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467{
13468 int n = 0;
13469 buf_T *buf;
13470
13471 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13472 if (n < buf->b_fnum)
13473 n = buf->b_fnum;
13474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013476}
13477
13478/*
13479 * "len()" function
13480 */
13481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013483 typval_T *argvars;
13484 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013485{
13486 switch (argvars[0].v_type)
13487 {
13488 case VAR_STRING:
13489 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 rettv->vval.v_number = (varnumber_T)STRLEN(
13491 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013492 break;
13493 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013495 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013496 case VAR_DICT:
13497 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13498 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013499 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013500 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013501 break;
13502 }
13503}
13504
Bram Moolenaar33570922005-01-25 22:26:29 +000013505static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013506
13507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013508libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013509 typval_T *argvars;
13510 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013511 int type;
13512{
13513#ifdef FEAT_LIBCALL
13514 char_u *string_in;
13515 char_u **string_result;
13516 int nr_result;
13517#endif
13518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013520 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013522
13523 if (check_restricted() || check_secure())
13524 return;
13525
13526#ifdef FEAT_LIBCALL
13527 /* The first two args must be strings, otherwise its meaningless */
13528 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13529 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013530 string_in = NULL;
13531 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013532 string_in = argvars[2].vval.v_string;
13533 if (type == VAR_NUMBER)
13534 string_result = NULL;
13535 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013536 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013537 if (mch_libcall(argvars[0].vval.v_string,
13538 argvars[1].vval.v_string,
13539 string_in,
13540 argvars[2].vval.v_number,
13541 string_result,
13542 &nr_result) == OK
13543 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013544 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013545 }
13546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547}
13548
13549/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013550 * "libcall()" function
13551 */
13552 static void
13553f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *argvars;
13555 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013556{
13557 libcall_common(argvars, rettv, VAR_STRING);
13558}
13559
13560/*
13561 * "libcallnr()" function
13562 */
13563 static void
13564f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013565 typval_T *argvars;
13566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013567{
13568 libcall_common(argvars, rettv, VAR_NUMBER);
13569}
13570
13571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 * "line(string)" function
13573 */
13574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013575f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013576 typval_T *argvars;
13577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578{
13579 linenr_T lnum = 0;
13580 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013581 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013583 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 if (fp != NULL)
13585 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587}
13588
13589/*
13590 * "line2byte(lnum)" function
13591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013594 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596{
13597#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599#else
13600 linenr_T lnum;
13601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13607 if (rettv->vval.v_number >= 0)
13608 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609#endif
13610}
13611
13612/*
13613 * "lispindent(lnum)" function
13614 */
13615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013617 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619{
13620#ifdef FEAT_LISP
13621 pos_T pos;
13622 linenr_T lnum;
13623
13624 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13627 {
13628 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 curwin->w_cursor = pos;
13631 }
13632 else
13633#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635}
13636
13637/*
13638 * "localtime()" function
13639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013642 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646}
13647
Bram Moolenaar33570922005-01-25 22:26:29 +000013648static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
13650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013652 typval_T *argvars;
13653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 int exact;
13655{
13656 char_u *keys;
13657 char_u *which;
13658 char_u buf[NUMBUFLEN];
13659 char_u *keys_buf = NULL;
13660 char_u *rhs;
13661 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013662 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013663 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013664 mapblock_T *mp;
13665 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666
13667 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->v_type = VAR_STRING;
13669 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 if (*keys == NUL)
13673 return;
13674
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013675 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013677 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013678 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013679 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013680 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013681 if (argvars[3].v_type != VAR_UNKNOWN)
13682 get_dict = get_tv_number(&argvars[3]);
13683 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 else
13686 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 if (which == NULL)
13688 return;
13689
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 mode = get_map_mode(&which, 0);
13691
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013692 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013693 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013695
13696 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013698 /* Return a string. */
13699 if (rhs != NULL)
13700 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701
Bram Moolenaarbd743252010-10-20 21:23:33 +020013702 }
13703 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13704 {
13705 /* Return a dictionary. */
13706 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13707 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13708 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709
Bram Moolenaarbd743252010-10-20 21:23:33 +020013710 dict_add_nr_str(dict, "lhs", 0L, lhs);
13711 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13712 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13713 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13714 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13715 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13716 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13717 dict_add_nr_str(dict, "mode", 0L, mapmode);
13718
13719 vim_free(lhs);
13720 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 }
13722}
13723
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013724#ifdef FEAT_FLOAT
13725/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013726 * "log()" function
13727 */
13728 static void
13729f_log(argvars, rettv)
13730 typval_T *argvars;
13731 typval_T *rettv;
13732{
13733 float_T f;
13734
13735 rettv->v_type = VAR_FLOAT;
13736 if (get_float_arg(argvars, &f) == OK)
13737 rettv->vval.v_float = log(f);
13738 else
13739 rettv->vval.v_float = 0.0;
13740}
13741
13742/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013743 * "log10()" function
13744 */
13745 static void
13746f_log10(argvars, rettv)
13747 typval_T *argvars;
13748 typval_T *rettv;
13749{
13750 float_T f;
13751
13752 rettv->v_type = VAR_FLOAT;
13753 if (get_float_arg(argvars, &f) == OK)
13754 rettv->vval.v_float = log10(f);
13755 else
13756 rettv->vval.v_float = 0.0;
13757}
13758#endif
13759
Bram Moolenaar1dced572012-04-05 16:54:08 +020013760#ifdef FEAT_LUA
13761/*
13762 * "luaeval()" function
13763 */
13764 static void
13765f_luaeval(argvars, rettv)
13766 typval_T *argvars;
13767 typval_T *rettv;
13768{
13769 char_u *str;
13770 char_u buf[NUMBUFLEN];
13771
13772 str = get_tv_string_buf(&argvars[0], buf);
13773 do_luaeval(str, argvars + 1, rettv);
13774}
13775#endif
13776
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013778 * "map()" function
13779 */
13780 static void
13781f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013782 typval_T *argvars;
13783 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013784{
13785 filter_map(argvars, rettv, TRUE);
13786}
13787
13788/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790 */
13791 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013792f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013793 typval_T *argvars;
13794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013796 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797}
13798
13799/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013800 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 */
13802 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013803f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013804 typval_T *argvars;
13805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013807 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808}
13809
Bram Moolenaar33570922005-01-25 22:26:29 +000013810static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811
13812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013813find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013814 typval_T *argvars;
13815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 int type;
13817{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013818 char_u *str = NULL;
13819 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 char_u *pat;
13821 regmatch_T regmatch;
13822 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013823 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 char_u *save_cpo;
13825 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013826 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013827 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013828 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013829 list_T *l = NULL;
13830 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013831 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013832 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
13834 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13835 save_cpo = p_cpo;
13836 p_cpo = (char_u *)"";
13837
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013838 rettv->vval.v_number = -1;
13839 if (type == 3)
13840 {
13841 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013842 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013843 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013844 }
13845 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013847 rettv->v_type = VAR_STRING;
13848 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013851 if (argvars[0].v_type == VAR_LIST)
13852 {
13853 if ((l = argvars[0].vval.v_list) == NULL)
13854 goto theend;
13855 li = l->lv_first;
13856 }
13857 else
13858 expr = str = get_tv_string(&argvars[0]);
13859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013860 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13861 if (pat == NULL)
13862 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013863
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013864 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013866 int error = FALSE;
13867
13868 start = get_tv_number_chk(&argvars[2], &error);
13869 if (error)
13870 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013871 if (l != NULL)
13872 {
13873 li = list_find(l, start);
13874 if (li == NULL)
13875 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013876 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013877 }
13878 else
13879 {
13880 if (start < 0)
13881 start = 0;
13882 if (start > (long)STRLEN(str))
13883 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013884 /* When "count" argument is there ignore matches before "start",
13885 * otherwise skip part of the string. Differs when pattern is "^"
13886 * or "\<". */
13887 if (argvars[3].v_type != VAR_UNKNOWN)
13888 startcol = start;
13889 else
13890 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013891 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013892
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013893 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013894 nth = get_tv_number_chk(&argvars[3], &error);
13895 if (error)
13896 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 }
13898
13899 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13900 if (regmatch.regprog != NULL)
13901 {
13902 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013903
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013904 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013905 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013906 if (l != NULL)
13907 {
13908 if (li == NULL)
13909 {
13910 match = FALSE;
13911 break;
13912 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013913 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013914 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013915 if (str == NULL)
13916 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013917 }
13918
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013919 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013921 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013922 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923 if (l == NULL && !match)
13924 break;
13925
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013926 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013927 if (l != NULL)
13928 {
13929 li = li->li_next;
13930 ++idx;
13931 }
13932 else
13933 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013934#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013935 startcol = (colnr_T)(regmatch.startp[0]
13936 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013937#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013938 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013939#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013940 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013941 }
13942
13943 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013945 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013946 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013947 int i;
13948
13949 /* return list with matched string and submatches */
13950 for (i = 0; i < NSUBEXP; ++i)
13951 {
13952 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013953 {
13954 if (list_append_string(rettv->vval.v_list,
13955 (char_u *)"", 0) == FAIL)
13956 break;
13957 }
13958 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013959 regmatch.startp[i],
13960 (int)(regmatch.endp[i] - regmatch.startp[i]))
13961 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013962 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013963 }
13964 }
13965 else if (type == 2)
13966 {
13967 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968 if (l != NULL)
13969 copy_tv(&li->li_tv, rettv);
13970 else
13971 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013973 }
13974 else if (l != NULL)
13975 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 else
13977 {
13978 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 (varnumber_T)(regmatch.startp[0] - str);
13981 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013982 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013984 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 }
13986 }
13987 vim_free(regmatch.regprog);
13988 }
13989
13990theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013991 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992 p_cpo = save_cpo;
13993}
13994
13995/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013996 * "match()" function
13997 */
13998 static void
13999f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014000 typval_T *argvars;
14001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014002{
14003 find_some_match(argvars, rettv, 1);
14004}
14005
14006/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014007 * "matchadd()" function
14008 */
14009 static void
14010f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014011 typval_T *argvars UNUSED;
14012 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014013{
14014#ifdef FEAT_SEARCH_EXTRA
14015 char_u buf[NUMBUFLEN];
14016 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14017 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14018 int prio = 10; /* default priority */
14019 int id = -1;
14020 int error = FALSE;
14021
14022 rettv->vval.v_number = -1;
14023
14024 if (grp == NULL || pat == NULL)
14025 return;
14026 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014027 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014028 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014029 if (argvars[3].v_type != VAR_UNKNOWN)
14030 id = get_tv_number_chk(&argvars[3], &error);
14031 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014032 if (error == TRUE)
14033 return;
14034 if (id >= 1 && id <= 3)
14035 {
14036 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14037 return;
14038 }
14039
14040 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14041#endif
14042}
14043
14044/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014045 * "matcharg()" function
14046 */
14047 static void
14048f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014049 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014050 typval_T *rettv;
14051{
14052 if (rettv_list_alloc(rettv) == OK)
14053 {
14054#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014055 int id = get_tv_number(&argvars[0]);
14056 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014057
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014058 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014059 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014060 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14061 {
14062 list_append_string(rettv->vval.v_list,
14063 syn_id2name(m->hlg_id), -1);
14064 list_append_string(rettv->vval.v_list, m->pattern, -1);
14065 }
14066 else
14067 {
14068 list_append_string(rettv->vval.v_list, NUL, -1);
14069 list_append_string(rettv->vval.v_list, NUL, -1);
14070 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014071 }
14072#endif
14073 }
14074}
14075
14076/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014077 * "matchdelete()" function
14078 */
14079 static void
14080f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014081 typval_T *argvars UNUSED;
14082 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014083{
14084#ifdef FEAT_SEARCH_EXTRA
14085 rettv->vval.v_number = match_delete(curwin,
14086 (int)get_tv_number(&argvars[0]), TRUE);
14087#endif
14088}
14089
14090/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014091 * "matchend()" function
14092 */
14093 static void
14094f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014095 typval_T *argvars;
14096 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014097{
14098 find_some_match(argvars, rettv, 0);
14099}
14100
14101/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014102 * "matchlist()" function
14103 */
14104 static void
14105f_matchlist(argvars, rettv)
14106 typval_T *argvars;
14107 typval_T *rettv;
14108{
14109 find_some_match(argvars, rettv, 3);
14110}
14111
14112/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014113 * "matchstr()" function
14114 */
14115 static void
14116f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014117 typval_T *argvars;
14118 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014119{
14120 find_some_match(argvars, rettv, 2);
14121}
14122
Bram Moolenaar33570922005-01-25 22:26:29 +000014123static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014124
14125 static void
14126max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014127 typval_T *argvars;
14128 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014129 int domax;
14130{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014131 long n = 0;
14132 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014133 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014134
14135 if (argvars[0].v_type == VAR_LIST)
14136 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014137 list_T *l;
14138 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014139
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014140 l = argvars[0].vval.v_list;
14141 if (l != NULL)
14142 {
14143 li = l->lv_first;
14144 if (li != NULL)
14145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014147 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014148 {
14149 li = li->li_next;
14150 if (li == NULL)
14151 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014153 if (domax ? i > n : i < n)
14154 n = i;
14155 }
14156 }
14157 }
14158 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014159 else if (argvars[0].v_type == VAR_DICT)
14160 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014161 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014162 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014163 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014164 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014165
14166 d = argvars[0].vval.v_dict;
14167 if (d != NULL)
14168 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014169 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014170 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014172 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014174 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014176 if (first)
14177 {
14178 n = i;
14179 first = FALSE;
14180 }
14181 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014182 n = i;
14183 }
14184 }
14185 }
14186 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014187 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014188 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014190}
14191
14192/*
14193 * "max()" function
14194 */
14195 static void
14196f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014197 typval_T *argvars;
14198 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014199{
14200 max_min(argvars, rettv, TRUE);
14201}
14202
14203/*
14204 * "min()" function
14205 */
14206 static void
14207f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014208 typval_T *argvars;
14209 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014210{
14211 max_min(argvars, rettv, FALSE);
14212}
14213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014214static int mkdir_recurse __ARGS((char_u *dir, int prot));
14215
14216/*
14217 * Create the directory in which "dir" is located, and higher levels when
14218 * needed.
14219 */
14220 static int
14221mkdir_recurse(dir, prot)
14222 char_u *dir;
14223 int prot;
14224{
14225 char_u *p;
14226 char_u *updir;
14227 int r = FAIL;
14228
14229 /* Get end of directory name in "dir".
14230 * We're done when it's "/" or "c:/". */
14231 p = gettail_sep(dir);
14232 if (p <= get_past_head(dir))
14233 return OK;
14234
14235 /* If the directory exists we're done. Otherwise: create it.*/
14236 updir = vim_strnsave(dir, (int)(p - dir));
14237 if (updir == NULL)
14238 return FAIL;
14239 if (mch_isdir(updir))
14240 r = OK;
14241 else if (mkdir_recurse(updir, prot) == OK)
14242 r = vim_mkdir_emsg(updir, prot);
14243 vim_free(updir);
14244 return r;
14245}
14246
14247#ifdef vim_mkdir
14248/*
14249 * "mkdir()" function
14250 */
14251 static void
14252f_mkdir(argvars, rettv)
14253 typval_T *argvars;
14254 typval_T *rettv;
14255{
14256 char_u *dir;
14257 char_u buf[NUMBUFLEN];
14258 int prot = 0755;
14259
14260 rettv->vval.v_number = FAIL;
14261 if (check_restricted() || check_secure())
14262 return;
14263
14264 dir = get_tv_string_buf(&argvars[0], buf);
14265 if (argvars[1].v_type != VAR_UNKNOWN)
14266 {
14267 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268 prot = get_tv_number_chk(&argvars[2], NULL);
14269 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014270 mkdir_recurse(dir, prot);
14271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014272 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014273}
14274#endif
14275
Bram Moolenaar0d660222005-01-07 21:51:51 +000014276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 * "mode()" function
14278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014280f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014281 typval_T *argvars;
14282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014284 char_u buf[3];
14285
14286 buf[1] = NUL;
14287 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288
14289#ifdef FEAT_VISUAL
14290 if (VIsual_active)
14291 {
14292 if (VIsual_select)
14293 buf[0] = VIsual_mode + 's' - 'v';
14294 else
14295 buf[0] = VIsual_mode;
14296 }
14297 else
14298#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014299 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14300 || State == CONFIRM)
14301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014303 if (State == ASKMORE)
14304 buf[1] = 'm';
14305 else if (State == CONFIRM)
14306 buf[1] = '?';
14307 }
14308 else if (State == EXTERNCMD)
14309 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 else if (State & INSERT)
14311 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014312#ifdef FEAT_VREPLACE
14313 if (State & VREPLACE_FLAG)
14314 {
14315 buf[0] = 'R';
14316 buf[1] = 'v';
14317 }
14318 else
14319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 if (State & REPLACE_FLAG)
14321 buf[0] = 'R';
14322 else
14323 buf[0] = 'i';
14324 }
14325 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014328 if (exmode_active)
14329 buf[1] = 'v';
14330 }
14331 else if (exmode_active)
14332 {
14333 buf[0] = 'c';
14334 buf[1] = 'e';
14335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014339 if (finish_op)
14340 buf[1] = 'o';
14341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014343 /* Clear out the minor mode when the argument is not a non-zero number or
14344 * non-empty string. */
14345 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014346 buf[1] = NUL;
14347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348 rettv->vval.v_string = vim_strsave(buf);
14349 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350}
14351
Bram Moolenaar429fa852013-04-15 12:27:36 +020014352#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014353/*
14354 * "mzeval()" function
14355 */
14356 static void
14357f_mzeval(argvars, rettv)
14358 typval_T *argvars;
14359 typval_T *rettv;
14360{
14361 char_u *str;
14362 char_u buf[NUMBUFLEN];
14363
14364 str = get_tv_string_buf(&argvars[0], buf);
14365 do_mzeval(str, rettv);
14366}
Bram Moolenaar75676462013-01-30 14:55:42 +010014367
14368 void
14369mzscheme_call_vim(name, args, rettv)
14370 char_u *name;
14371 typval_T *args;
14372 typval_T *rettv;
14373{
14374 typval_T argvars[3];
14375
14376 argvars[0].v_type = VAR_STRING;
14377 argvars[0].vval.v_string = name;
14378 copy_tv(args, &argvars[1]);
14379 argvars[2].v_type = VAR_UNKNOWN;
14380 f_call(argvars, rettv);
14381 clear_tv(&argvars[1]);
14382}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014383#endif
14384
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014386 * "nextnonblank()" function
14387 */
14388 static void
14389f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014390 typval_T *argvars;
14391 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014392{
14393 linenr_T lnum;
14394
14395 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014397 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014398 {
14399 lnum = 0;
14400 break;
14401 }
14402 if (*skipwhite(ml_get(lnum)) != NUL)
14403 break;
14404 }
14405 rettv->vval.v_number = lnum;
14406}
14407
14408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 * "nr2char()" function
14410 */
14411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014412f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014413 typval_T *argvars;
14414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415{
14416 char_u buf[NUMBUFLEN];
14417
14418#ifdef FEAT_MBYTE
14419 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014420 {
14421 int utf8 = 0;
14422
14423 if (argvars[1].v_type != VAR_UNKNOWN)
14424 utf8 = get_tv_number_chk(&argvars[1], NULL);
14425 if (utf8)
14426 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14427 else
14428 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 else
14431#endif
14432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 buf[1] = NUL;
14435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 rettv->v_type = VAR_STRING;
14437 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014438}
14439
14440/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014441 * "or(expr, expr)" function
14442 */
14443 static void
14444f_or(argvars, rettv)
14445 typval_T *argvars;
14446 typval_T *rettv;
14447{
14448 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14449 | get_tv_number_chk(&argvars[1], NULL);
14450}
14451
14452/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014453 * "pathshorten()" function
14454 */
14455 static void
14456f_pathshorten(argvars, rettv)
14457 typval_T *argvars;
14458 typval_T *rettv;
14459{
14460 char_u *p;
14461
14462 rettv->v_type = VAR_STRING;
14463 p = get_tv_string_chk(&argvars[0]);
14464 if (p == NULL)
14465 rettv->vval.v_string = NULL;
14466 else
14467 {
14468 p = vim_strsave(p);
14469 rettv->vval.v_string = p;
14470 if (p != NULL)
14471 shorten_dir(p);
14472 }
14473}
14474
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014475#ifdef FEAT_FLOAT
14476/*
14477 * "pow()" function
14478 */
14479 static void
14480f_pow(argvars, rettv)
14481 typval_T *argvars;
14482 typval_T *rettv;
14483{
14484 float_T fx, fy;
14485
14486 rettv->v_type = VAR_FLOAT;
14487 if (get_float_arg(argvars, &fx) == OK
14488 && get_float_arg(&argvars[1], &fy) == OK)
14489 rettv->vval.v_float = pow(fx, fy);
14490 else
14491 rettv->vval.v_float = 0.0;
14492}
14493#endif
14494
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496 * "prevnonblank()" function
14497 */
14498 static void
14499f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 typval_T *argvars;
14501 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014502{
14503 linenr_T lnum;
14504
14505 lnum = get_tv_lnum(argvars);
14506 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14507 lnum = 0;
14508 else
14509 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14510 --lnum;
14511 rettv->vval.v_number = lnum;
14512}
14513
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014514#ifdef HAVE_STDARG_H
14515/* This dummy va_list is here because:
14516 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14517 * - locally in the function results in a "used before set" warning
14518 * - using va_start() to initialize it gives "function with fixed args" error */
14519static va_list ap;
14520#endif
14521
Bram Moolenaar8c711452005-01-14 21:53:12 +000014522/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014523 * "printf()" function
14524 */
14525 static void
14526f_printf(argvars, rettv)
14527 typval_T *argvars;
14528 typval_T *rettv;
14529{
14530 rettv->v_type = VAR_STRING;
14531 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014532#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014533 {
14534 char_u buf[NUMBUFLEN];
14535 int len;
14536 char_u *s;
14537 int saved_did_emsg = did_emsg;
14538 char *fmt;
14539
14540 /* Get the required length, allocate the buffer and do it for real. */
14541 did_emsg = FALSE;
14542 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014543 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014544 if (!did_emsg)
14545 {
14546 s = alloc(len + 1);
14547 if (s != NULL)
14548 {
14549 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014550 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014551 }
14552 }
14553 did_emsg |= saved_did_emsg;
14554 }
14555#endif
14556}
14557
14558/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014559 * "pumvisible()" function
14560 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014561 static void
14562f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014563 typval_T *argvars UNUSED;
14564 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014565{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014566#ifdef FEAT_INS_EXPAND
14567 if (pum_visible())
14568 rettv->vval.v_number = 1;
14569#endif
14570}
14571
Bram Moolenaardb913952012-06-29 12:54:53 +020014572#ifdef FEAT_PYTHON3
14573/*
14574 * "py3eval()" function
14575 */
14576 static void
14577f_py3eval(argvars, rettv)
14578 typval_T *argvars;
14579 typval_T *rettv;
14580{
14581 char_u *str;
14582 char_u buf[NUMBUFLEN];
14583
14584 str = get_tv_string_buf(&argvars[0], buf);
14585 do_py3eval(str, rettv);
14586}
14587#endif
14588
14589#ifdef FEAT_PYTHON
14590/*
14591 * "pyeval()" function
14592 */
14593 static void
14594f_pyeval(argvars, rettv)
14595 typval_T *argvars;
14596 typval_T *rettv;
14597{
14598 char_u *str;
14599 char_u buf[NUMBUFLEN];
14600
14601 str = get_tv_string_buf(&argvars[0], buf);
14602 do_pyeval(str, rettv);
14603}
14604#endif
14605
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014606/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014607 * "range()" function
14608 */
14609 static void
14610f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014611 typval_T *argvars;
14612 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014613{
14614 long start;
14615 long end;
14616 long stride = 1;
14617 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014620 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014621 if (argvars[1].v_type == VAR_UNKNOWN)
14622 {
14623 end = start - 1;
14624 start = 0;
14625 }
14626 else
14627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014628 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014629 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014631 }
14632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 if (error)
14634 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014635 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014636 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014637 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014638 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014639 else
14640 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014641 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014642 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014643 if (list_append_number(rettv->vval.v_list,
14644 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014645 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014646 }
14647}
14648
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014649/*
14650 * "readfile()" function
14651 */
14652 static void
14653f_readfile(argvars, rettv)
14654 typval_T *argvars;
14655 typval_T *rettv;
14656{
14657 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014658 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014659 char_u *fname;
14660 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014661 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14662 int io_size = sizeof(buf);
14663 int readlen; /* size of last fread() */
14664 char_u *prev = NULL; /* previously read bytes, if any */
14665 long prevlen = 0; /* length of data in prev */
14666 long prevsize = 0; /* size of prev buffer */
14667 long maxline = MAXLNUM;
14668 long cnt = 0;
14669 char_u *p; /* position in buf */
14670 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014671
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014672 if (argvars[1].v_type != VAR_UNKNOWN)
14673 {
14674 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14675 binary = TRUE;
14676 if (argvars[2].v_type != VAR_UNKNOWN)
14677 maxline = get_tv_number(&argvars[2]);
14678 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014679
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014680 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014681 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014682
14683 /* Always open the file in binary mode, library functions have a mind of
14684 * their own about CR-LF conversion. */
14685 fname = get_tv_string(&argvars[0]);
14686 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14687 {
14688 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14689 return;
14690 }
14691
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014692 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014693 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014694 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014695
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014696 /* This for loop processes what was read, but is also entered at end
14697 * of file so that either:
14698 * - an incomplete line gets written
14699 * - a "binary" file gets an empty line at the end if it ends in a
14700 * newline. */
14701 for (p = buf, start = buf;
14702 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14703 ++p)
14704 {
14705 if (*p == '\n' || readlen <= 0)
14706 {
14707 listitem_T *li;
14708 char_u *s = NULL;
14709 long_u len = p - start;
14710
14711 /* Finished a line. Remove CRs before NL. */
14712 if (readlen > 0 && !binary)
14713 {
14714 while (len > 0 && start[len - 1] == '\r')
14715 --len;
14716 /* removal may cross back to the "prev" string */
14717 if (len == 0)
14718 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14719 --prevlen;
14720 }
14721 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014722 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014723 else
14724 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014725 /* Change "prev" buffer to be the right size. This way
14726 * the bytes are only copied once, and very long lines are
14727 * allocated only once. */
14728 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014729 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014730 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014731 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014732 prev = NULL; /* the list will own the string */
14733 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014734 }
14735 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014736 if (s == NULL)
14737 {
14738 do_outofmem_msg((long_u) prevlen + len + 1);
14739 failed = TRUE;
14740 break;
14741 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014742
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014743 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014744 {
14745 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014746 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014747 break;
14748 }
14749 li->li_tv.v_type = VAR_STRING;
14750 li->li_tv.v_lock = 0;
14751 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014752 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014753
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014754 start = p + 1; /* step over newline */
14755 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014756 break;
14757 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 else if (*p == NUL)
14759 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014760#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014761 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14762 * when finding the BF and check the previous two bytes. */
14763 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014764 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014765 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14766 * + 1, these may be in the "prev" string. */
14767 char_u back1 = p >= buf + 1 ? p[-1]
14768 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14769 char_u back2 = p >= buf + 2 ? p[-2]
14770 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14771 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014772
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014773 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014774 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014775 char_u *dest = p - 2;
14776
14777 /* Usually a BOM is at the beginning of a file, and so at
14778 * the beginning of a line; then we can just step over it.
14779 */
14780 if (start == dest)
14781 start = p + 1;
14782 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014783 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014784 /* have to shuffle buf to close gap */
14785 int adjust_prevlen = 0;
14786
14787 if (dest < buf)
14788 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014789 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014790 dest = buf;
14791 }
14792 if (readlen > p - buf + 1)
14793 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14794 readlen -= 3 - adjust_prevlen;
14795 prevlen -= adjust_prevlen;
14796 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014797 }
14798 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014799 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014800#endif
14801 } /* for */
14802
14803 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14804 break;
14805 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014806 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014807 /* There's part of a line in buf, store it in "prev". */
14808 if (p - start + prevlen >= prevsize)
14809 {
14810 /* need bigger "prev" buffer */
14811 char_u *newprev;
14812
14813 /* A common use case is ordinary text files and "prev" gets a
14814 * fragment of a line, so the first allocation is made
14815 * small, to avoid repeatedly 'allocing' large and
14816 * 'reallocing' small. */
14817 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014818 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014819 else
14820 {
14821 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014822 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014823 prevsize = grow50pc > growmin ? grow50pc : growmin;
14824 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014825 newprev = prev == NULL ? alloc(prevsize)
14826 : vim_realloc(prev, prevsize);
14827 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014828 {
14829 do_outofmem_msg((long_u)prevsize);
14830 failed = TRUE;
14831 break;
14832 }
14833 prev = newprev;
14834 }
14835 /* Add the line part to end of "prev". */
14836 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014837 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014838 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014839 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014840
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014841 /*
14842 * For a negative line count use only the lines at the end of the file,
14843 * free the rest.
14844 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014845 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014846 while (cnt > -maxline)
14847 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014848 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014849 --cnt;
14850 }
14851
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014852 if (failed)
14853 {
14854 list_free(rettv->vval.v_list, TRUE);
14855 /* readfile doc says an empty list is returned on error */
14856 rettv->vval.v_list = list_alloc();
14857 }
14858
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014859 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014860 fclose(fd);
14861}
14862
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014863#if defined(FEAT_RELTIME)
14864static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14865
14866/*
14867 * Convert a List to proftime_T.
14868 * Return FAIL when there is something wrong.
14869 */
14870 static int
14871list2proftime(arg, tm)
14872 typval_T *arg;
14873 proftime_T *tm;
14874{
14875 long n1, n2;
14876 int error = FALSE;
14877
14878 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14879 || arg->vval.v_list->lv_len != 2)
14880 return FAIL;
14881 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14882 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14883# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014884 tm->HighPart = n1;
14885 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014886# else
14887 tm->tv_sec = n1;
14888 tm->tv_usec = n2;
14889# endif
14890 return error ? FAIL : OK;
14891}
14892#endif /* FEAT_RELTIME */
14893
14894/*
14895 * "reltime()" function
14896 */
14897 static void
14898f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014899 typval_T *argvars UNUSED;
14900 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014901{
14902#ifdef FEAT_RELTIME
14903 proftime_T res;
14904 proftime_T start;
14905
14906 if (argvars[0].v_type == VAR_UNKNOWN)
14907 {
14908 /* No arguments: get current time. */
14909 profile_start(&res);
14910 }
14911 else if (argvars[1].v_type == VAR_UNKNOWN)
14912 {
14913 if (list2proftime(&argvars[0], &res) == FAIL)
14914 return;
14915 profile_end(&res);
14916 }
14917 else
14918 {
14919 /* Two arguments: compute the difference. */
14920 if (list2proftime(&argvars[0], &start) == FAIL
14921 || list2proftime(&argvars[1], &res) == FAIL)
14922 return;
14923 profile_sub(&res, &start);
14924 }
14925
14926 if (rettv_list_alloc(rettv) == OK)
14927 {
14928 long n1, n2;
14929
14930# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014931 n1 = res.HighPart;
14932 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014933# else
14934 n1 = res.tv_sec;
14935 n2 = res.tv_usec;
14936# endif
14937 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14938 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14939 }
14940#endif
14941}
14942
14943/*
14944 * "reltimestr()" function
14945 */
14946 static void
14947f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014948 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014949 typval_T *rettv;
14950{
14951#ifdef FEAT_RELTIME
14952 proftime_T tm;
14953#endif
14954
14955 rettv->v_type = VAR_STRING;
14956 rettv->vval.v_string = NULL;
14957#ifdef FEAT_RELTIME
14958 if (list2proftime(&argvars[0], &tm) == OK)
14959 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14960#endif
14961}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014962
Bram Moolenaar0d660222005-01-07 21:51:51 +000014963#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14964static void make_connection __ARGS((void));
14965static int check_connection __ARGS((void));
14966
14967 static void
14968make_connection()
14969{
14970 if (X_DISPLAY == NULL
14971# ifdef FEAT_GUI
14972 && !gui.in_use
14973# endif
14974 )
14975 {
14976 x_force_connect = TRUE;
14977 setup_term_clip();
14978 x_force_connect = FALSE;
14979 }
14980}
14981
14982 static int
14983check_connection()
14984{
14985 make_connection();
14986 if (X_DISPLAY == NULL)
14987 {
14988 EMSG(_("E240: No connection to Vim server"));
14989 return FAIL;
14990 }
14991 return OK;
14992}
14993#endif
14994
14995#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014996static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997
14998 static void
14999remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015000 typval_T *argvars;
15001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015002 int expr;
15003{
15004 char_u *server_name;
15005 char_u *keys;
15006 char_u *r = NULL;
15007 char_u buf[NUMBUFLEN];
15008# ifdef WIN32
15009 HWND w;
15010# else
15011 Window w;
15012# endif
15013
15014 if (check_restricted() || check_secure())
15015 return;
15016
15017# ifdef FEAT_X11
15018 if (check_connection() == FAIL)
15019 return;
15020# endif
15021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015022 server_name = get_tv_string_chk(&argvars[0]);
15023 if (server_name == NULL)
15024 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025 keys = get_tv_string_buf(&argvars[1], buf);
15026# ifdef WIN32
15027 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15028# else
15029 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15030 < 0)
15031# endif
15032 {
15033 if (r != NULL)
15034 EMSG(r); /* sending worked but evaluation failed */
15035 else
15036 EMSG2(_("E241: Unable to send to %s"), server_name);
15037 return;
15038 }
15039
15040 rettv->vval.v_string = r;
15041
15042 if (argvars[2].v_type != VAR_UNKNOWN)
15043 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015044 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015045 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015046 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015047
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015048 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015049 v.di_tv.v_type = VAR_STRING;
15050 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015051 idvar = get_tv_string_chk(&argvars[2]);
15052 if (idvar != NULL)
15053 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015054 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 }
15056}
15057#endif
15058
15059/*
15060 * "remote_expr()" function
15061 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015062 static void
15063f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015064 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015065 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066{
15067 rettv->v_type = VAR_STRING;
15068 rettv->vval.v_string = NULL;
15069#ifdef FEAT_CLIENTSERVER
15070 remote_common(argvars, rettv, TRUE);
15071#endif
15072}
15073
15074/*
15075 * "remote_foreground()" function
15076 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 static void
15078f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015079 typval_T *argvars UNUSED;
15080 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082#ifdef FEAT_CLIENTSERVER
15083# ifdef WIN32
15084 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015085 {
15086 char_u *server_name = get_tv_string_chk(&argvars[0]);
15087
15088 if (server_name != NULL)
15089 serverForeground(server_name);
15090 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091# else
15092 /* Send a foreground() expression to the server. */
15093 argvars[1].v_type = VAR_STRING;
15094 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15095 argvars[2].v_type = VAR_UNKNOWN;
15096 remote_common(argvars, rettv, TRUE);
15097 vim_free(argvars[1].vval.v_string);
15098# endif
15099#endif
15100}
15101
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 static void
15103f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015104 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106{
15107#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109 char_u *s = NULL;
15110# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015111 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015113 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015114
15115 if (check_restricted() || check_secure())
15116 {
15117 rettv->vval.v_number = -1;
15118 return;
15119 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015120 serverid = get_tv_string_chk(&argvars[0]);
15121 if (serverid == NULL)
15122 {
15123 rettv->vval.v_number = -1;
15124 return; /* type error; errmsg already given */
15125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015126# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015127 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128 if (n == 0)
15129 rettv->vval.v_number = -1;
15130 else
15131 {
15132 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15133 rettv->vval.v_number = (s != NULL);
15134 }
15135# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136 if (check_connection() == FAIL)
15137 return;
15138
15139 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015140 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015141# endif
15142
15143 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015145 char_u *retvar;
15146
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 v.di_tv.v_type = VAR_STRING;
15148 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015149 retvar = get_tv_string_chk(&argvars[1]);
15150 if (retvar != NULL)
15151 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015152 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 }
15154#else
15155 rettv->vval.v_number = -1;
15156#endif
15157}
15158
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 static void
15160f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015161 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015162 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163{
15164 char_u *r = NULL;
15165
15166#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015167 char_u *serverid = get_tv_string_chk(&argvars[0]);
15168
15169 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015170 {
15171# ifdef WIN32
15172 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015173 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015175 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 if (n != 0)
15177 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15178 if (r == NULL)
15179# else
15180 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015181 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015182# endif
15183 EMSG(_("E277: Unable to read a server reply"));
15184 }
15185#endif
15186 rettv->v_type = VAR_STRING;
15187 rettv->vval.v_string = r;
15188}
15189
15190/*
15191 * "remote_send()" function
15192 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193 static void
15194f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015195 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015196 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015197{
15198 rettv->v_type = VAR_STRING;
15199 rettv->vval.v_string = NULL;
15200#ifdef FEAT_CLIENTSERVER
15201 remote_common(argvars, rettv, FALSE);
15202#endif
15203}
15204
15205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015206 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015207 */
15208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015209f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 typval_T *argvars;
15211 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015212{
Bram Moolenaar33570922005-01-25 22:26:29 +000015213 list_T *l;
15214 listitem_T *item, *item2;
15215 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015216 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015217 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015218 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 dict_T *d;
15220 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015221 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015222
Bram Moolenaar8c711452005-01-14 21:53:12 +000015223 if (argvars[0].v_type == VAR_DICT)
15224 {
15225 if (argvars[2].v_type != VAR_UNKNOWN)
15226 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015227 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015228 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 key = get_tv_string_chk(&argvars[1]);
15231 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 di = dict_find(d, key, -1);
15234 if (di == NULL)
15235 EMSG2(_(e_dictkey), key);
15236 else
15237 {
15238 *rettv = di->di_tv;
15239 init_tv(&di->di_tv);
15240 dictitem_remove(d, di);
15241 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015243 }
15244 }
15245 else if (argvars[0].v_type != VAR_LIST)
15246 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015247 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015248 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 int error = FALSE;
15251
15252 idx = get_tv_number_chk(&argvars[1], &error);
15253 if (error)
15254 ; /* type error: do nothing, errmsg already given */
15255 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015256 EMSGN(_(e_listidx), idx);
15257 else
15258 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015259 if (argvars[2].v_type == VAR_UNKNOWN)
15260 {
15261 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015262 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015263 *rettv = item->li_tv;
15264 vim_free(item);
15265 }
15266 else
15267 {
15268 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015269 end = get_tv_number_chk(&argvars[2], &error);
15270 if (error)
15271 ; /* type error: do nothing */
15272 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015273 EMSGN(_(e_listidx), end);
15274 else
15275 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015276 int cnt = 0;
15277
15278 for (li = item; li != NULL; li = li->li_next)
15279 {
15280 ++cnt;
15281 if (li == item2)
15282 break;
15283 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015284 if (li == NULL) /* didn't find "item2" after "item" */
15285 EMSG(_(e_invrange));
15286 else
15287 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015288 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015289 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015290 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015291 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015292 l->lv_first = item;
15293 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015294 item->li_prev = NULL;
15295 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015296 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015297 }
15298 }
15299 }
15300 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015301 }
15302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303}
15304
15305/*
15306 * "rename({from}, {to})" function
15307 */
15308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015310 typval_T *argvars;
15311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312{
15313 char_u buf[NUMBUFLEN];
15314
15315 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015318 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15319 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320}
15321
15322/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015323 * "repeat()" function
15324 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015325 static void
15326f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015327 typval_T *argvars;
15328 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015329{
15330 char_u *p;
15331 int n;
15332 int slen;
15333 int len;
15334 char_u *r;
15335 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015336
15337 n = get_tv_number(&argvars[1]);
15338 if (argvars[0].v_type == VAR_LIST)
15339 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015340 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015341 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015342 if (list_extend(rettv->vval.v_list,
15343 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015344 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015345 }
15346 else
15347 {
15348 p = get_tv_string(&argvars[0]);
15349 rettv->v_type = VAR_STRING;
15350 rettv->vval.v_string = NULL;
15351
15352 slen = (int)STRLEN(p);
15353 len = slen * n;
15354 if (len <= 0)
15355 return;
15356
15357 r = alloc(len + 1);
15358 if (r != NULL)
15359 {
15360 for (i = 0; i < n; i++)
15361 mch_memmove(r + i * slen, p, (size_t)slen);
15362 r[len] = NUL;
15363 }
15364
15365 rettv->vval.v_string = r;
15366 }
15367}
15368
15369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 * "resolve()" function
15371 */
15372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015373f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015374 typval_T *argvars;
15375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376{
15377 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015378#ifdef HAVE_READLINK
15379 char_u *buf = NULL;
15380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383#ifdef FEAT_SHORTCUT
15384 {
15385 char_u *v = NULL;
15386
15387 v = mch_resolve_shortcut(p);
15388 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 }
15393#else
15394# ifdef HAVE_READLINK
15395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 char_u *cpy;
15397 int len;
15398 char_u *remain = NULL;
15399 char_u *q;
15400 int is_relative_to_current = FALSE;
15401 int has_trailing_pathsep = FALSE;
15402 int limit = 100;
15403
15404 p = vim_strsave(p);
15405
15406 if (p[0] == '.' && (vim_ispathsep(p[1])
15407 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15408 is_relative_to_current = TRUE;
15409
15410 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015411 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015414 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416
15417 q = getnextcomp(p);
15418 if (*q != NUL)
15419 {
15420 /* Separate the first path component in "p", and keep the
15421 * remainder (beginning with the path separator). */
15422 remain = vim_strsave(q - 1);
15423 q[-1] = NUL;
15424 }
15425
Bram Moolenaard9462e32011-04-11 21:35:11 +020015426 buf = alloc(MAXPATHL + 1);
15427 if (buf == NULL)
15428 goto fail;
15429
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 for (;;)
15431 {
15432 for (;;)
15433 {
15434 len = readlink((char *)p, (char *)buf, MAXPATHL);
15435 if (len <= 0)
15436 break;
15437 buf[len] = NUL;
15438
15439 if (limit-- == 0)
15440 {
15441 vim_free(p);
15442 vim_free(remain);
15443 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 goto fail;
15446 }
15447
15448 /* Ensure that the result will have a trailing path separator
15449 * if the argument has one. */
15450 if (remain == NULL && has_trailing_pathsep)
15451 add_pathsep(buf);
15452
15453 /* Separate the first path component in the link value and
15454 * concatenate the remainders. */
15455 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15456 if (*q != NUL)
15457 {
15458 if (remain == NULL)
15459 remain = vim_strsave(q - 1);
15460 else
15461 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015462 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 if (cpy != NULL)
15464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465 vim_free(remain);
15466 remain = cpy;
15467 }
15468 }
15469 q[-1] = NUL;
15470 }
15471
15472 q = gettail(p);
15473 if (q > p && *q == NUL)
15474 {
15475 /* Ignore trailing path separator. */
15476 q[-1] = NUL;
15477 q = gettail(p);
15478 }
15479 if (q > p && !mch_isFullName(buf))
15480 {
15481 /* symlink is relative to directory of argument */
15482 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15483 if (cpy != NULL)
15484 {
15485 STRCPY(cpy, p);
15486 STRCPY(gettail(cpy), buf);
15487 vim_free(p);
15488 p = cpy;
15489 }
15490 }
15491 else
15492 {
15493 vim_free(p);
15494 p = vim_strsave(buf);
15495 }
15496 }
15497
15498 if (remain == NULL)
15499 break;
15500
15501 /* Append the first path component of "remain" to "p". */
15502 q = getnextcomp(remain + 1);
15503 len = q - remain - (*q != NUL);
15504 cpy = vim_strnsave(p, STRLEN(p) + len);
15505 if (cpy != NULL)
15506 {
15507 STRNCAT(cpy, remain, len);
15508 vim_free(p);
15509 p = cpy;
15510 }
15511 /* Shorten "remain". */
15512 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015513 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 else
15515 {
15516 vim_free(remain);
15517 remain = NULL;
15518 }
15519 }
15520
15521 /* If the result is a relative path name, make it explicitly relative to
15522 * the current directory if and only if the argument had this form. */
15523 if (!vim_ispathsep(*p))
15524 {
15525 if (is_relative_to_current
15526 && *p != NUL
15527 && !(p[0] == '.'
15528 && (p[1] == NUL
15529 || vim_ispathsep(p[1])
15530 || (p[1] == '.'
15531 && (p[2] == NUL
15532 || vim_ispathsep(p[2]))))))
15533 {
15534 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015535 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 if (cpy != NULL)
15537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 vim_free(p);
15539 p = cpy;
15540 }
15541 }
15542 else if (!is_relative_to_current)
15543 {
15544 /* Strip leading "./". */
15545 q = p;
15546 while (q[0] == '.' && vim_ispathsep(q[1]))
15547 q += 2;
15548 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015549 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 }
15551 }
15552
15553 /* Ensure that the result will have no trailing path separator
15554 * if the argument had none. But keep "/" or "//". */
15555 if (!has_trailing_pathsep)
15556 {
15557 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015558 if (after_pathsep(p, q))
15559 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 }
15561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 }
15564# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566# endif
15567#endif
15568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570
15571#ifdef HAVE_READLINK
15572fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015573 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015575 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576}
15577
15578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015579 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 */
15581 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015583 typval_T *argvars;
15584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585{
Bram Moolenaar33570922005-01-25 22:26:29 +000015586 list_T *l;
15587 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588
Bram Moolenaar0d660222005-01-07 21:51:51 +000015589 if (argvars[0].v_type != VAR_LIST)
15590 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015591 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015592 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593 {
15594 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015595 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015596 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597 while (li != NULL)
15598 {
15599 ni = li->li_prev;
15600 list_append(l, li);
15601 li = ni;
15602 }
15603 rettv->vval.v_list = l;
15604 rettv->v_type = VAR_LIST;
15605 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015606 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608}
15609
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015610#define SP_NOMOVE 0x01 /* don't move cursor */
15611#define SP_REPEAT 0x02 /* repeat to find outer pair */
15612#define SP_RETCOUNT 0x04 /* return matchcount */
15613#define SP_SETPCMARK 0x08 /* set previous context mark */
15614#define SP_START 0x10 /* accept match at start position */
15615#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15616#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015617
Bram Moolenaar33570922005-01-25 22:26:29 +000015618static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015619
15620/*
15621 * Get flags for a search function.
15622 * Possibly sets "p_ws".
15623 * Returns BACKWARD, FORWARD or zero (for an error).
15624 */
15625 static int
15626get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015627 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015628 int *flagsp;
15629{
15630 int dir = FORWARD;
15631 char_u *flags;
15632 char_u nbuf[NUMBUFLEN];
15633 int mask;
15634
15635 if (varp->v_type != VAR_UNKNOWN)
15636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 flags = get_tv_string_buf_chk(varp, nbuf);
15638 if (flags == NULL)
15639 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640 while (*flags != NUL)
15641 {
15642 switch (*flags)
15643 {
15644 case 'b': dir = BACKWARD; break;
15645 case 'w': p_ws = TRUE; break;
15646 case 'W': p_ws = FALSE; break;
15647 default: mask = 0;
15648 if (flagsp != NULL)
15649 switch (*flags)
15650 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015651 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015652 case 'e': mask = SP_END; break;
15653 case 'm': mask = SP_RETCOUNT; break;
15654 case 'n': mask = SP_NOMOVE; break;
15655 case 'p': mask = SP_SUBPAT; break;
15656 case 'r': mask = SP_REPEAT; break;
15657 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658 }
15659 if (mask == 0)
15660 {
15661 EMSG2(_(e_invarg2), flags);
15662 dir = 0;
15663 }
15664 else
15665 *flagsp |= mask;
15666 }
15667 if (dir == 0)
15668 break;
15669 ++flags;
15670 }
15671 }
15672 return dir;
15673}
15674
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015676 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015678 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015679search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015681 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015682 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015684 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 char_u *pat;
15686 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015687 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 int save_p_ws = p_ws;
15689 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015690 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015691 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015692 proftime_T tm;
15693#ifdef FEAT_RELTIME
15694 long time_limit = 0;
15695#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015696 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015697 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015699 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015700 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015701 if (dir == 0)
15702 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015703 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015704 if (flags & SP_START)
15705 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015706 if (flags & SP_END)
15707 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708
Bram Moolenaar76929292008-01-06 19:07:36 +000015709 /* Optional arguments: line number to stop searching and timeout. */
15710 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015711 {
15712 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15713 if (lnum_stop < 0)
15714 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015715#ifdef FEAT_RELTIME
15716 if (argvars[3].v_type != VAR_UNKNOWN)
15717 {
15718 time_limit = get_tv_number_chk(&argvars[3], NULL);
15719 if (time_limit < 0)
15720 goto theend;
15721 }
15722#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015723 }
15724
Bram Moolenaar76929292008-01-06 19:07:36 +000015725#ifdef FEAT_RELTIME
15726 /* Set the time limit, if there is one. */
15727 profile_setlimit(time_limit, &tm);
15728#endif
15729
Bram Moolenaar231334e2005-07-25 20:46:57 +000015730 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015731 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015732 * Check to make sure only those flags are set.
15733 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15734 * flags cannot be set. Check for that condition also.
15735 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015736 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015737 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015739 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015740 goto theend;
15741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015743 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015744 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015745 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015746 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015748 if (flags & SP_SUBPAT)
15749 retval = subpatnum;
15750 else
15751 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015752 if (flags & SP_SETPCMARK)
15753 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015755 if (match_pos != NULL)
15756 {
15757 /* Store the match cursor position */
15758 match_pos->lnum = pos.lnum;
15759 match_pos->col = pos.col + 1;
15760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 /* "/$" will put the cursor after the end of the line, may need to
15762 * correct that here */
15763 check_cursor();
15764 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015765
15766 /* If 'n' flag is used: restore cursor position. */
15767 if (flags & SP_NOMOVE)
15768 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015769 else
15770 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015771theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015773
15774 return retval;
15775}
15776
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015777#ifdef FEAT_FLOAT
15778/*
15779 * "round({float})" function
15780 */
15781 static void
15782f_round(argvars, rettv)
15783 typval_T *argvars;
15784 typval_T *rettv;
15785{
15786 float_T f;
15787
15788 rettv->v_type = VAR_FLOAT;
15789 if (get_float_arg(argvars, &f) == OK)
15790 /* round() is not in C90, use ceil() or floor() instead. */
15791 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15792 else
15793 rettv->vval.v_float = 0.0;
15794}
15795#endif
15796
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015797/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015798 * "screencol()" function
15799 *
15800 * First column is 1 to be consistent with virtcol().
15801 */
15802 static void
15803f_screencol(argvars, rettv)
15804 typval_T *argvars UNUSED;
15805 typval_T *rettv;
15806{
15807 rettv->vval.v_number = screen_screencol() + 1;
15808}
15809
15810/*
15811 * "screenrow()" function
15812 */
15813 static void
15814f_screenrow(argvars, rettv)
15815 typval_T *argvars UNUSED;
15816 typval_T *rettv;
15817{
15818 rettv->vval.v_number = screen_screenrow() + 1;
15819}
15820
15821/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015822 * "search()" function
15823 */
15824 static void
15825f_search(argvars, rettv)
15826 typval_T *argvars;
15827 typval_T *rettv;
15828{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015829 int flags = 0;
15830
15831 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832}
15833
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015835 * "searchdecl()" function
15836 */
15837 static void
15838f_searchdecl(argvars, rettv)
15839 typval_T *argvars;
15840 typval_T *rettv;
15841{
15842 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015843 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015844 int error = FALSE;
15845 char_u *name;
15846
15847 rettv->vval.v_number = 1; /* default: FAIL */
15848
15849 name = get_tv_string_chk(&argvars[0]);
15850 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015851 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015852 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015853 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15854 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15855 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015856 if (!error && name != NULL)
15857 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015858 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015859}
15860
15861/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015862 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015864 static int
15865searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015866 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015867 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868{
15869 char_u *spat, *mpat, *epat;
15870 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872 int dir;
15873 int flags = 0;
15874 char_u nbuf1[NUMBUFLEN];
15875 char_u nbuf2[NUMBUFLEN];
15876 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015877 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015878 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015879 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015882 spat = get_tv_string_chk(&argvars[0]);
15883 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15884 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15885 if (spat == NULL || mpat == NULL || epat == NULL)
15886 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 /* Handle the optional fourth argument: flags */
15889 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015890 if (dir == 0)
15891 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015892
15893 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015894 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15895 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015896 if ((flags & (SP_END | SP_SUBPAT)) != 0
15897 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015898 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015899 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015900 goto theend;
15901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015903 /* Using 'r' implies 'W', otherwise it doesn't work. */
15904 if (flags & SP_REPEAT)
15905 p_ws = FALSE;
15906
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015907 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015908 if (argvars[3].v_type == VAR_UNKNOWN
15909 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910 skip = (char_u *)"";
15911 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015913 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015914 if (argvars[5].v_type != VAR_UNKNOWN)
15915 {
15916 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15917 if (lnum_stop < 0)
15918 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015919#ifdef FEAT_RELTIME
15920 if (argvars[6].v_type != VAR_UNKNOWN)
15921 {
15922 time_limit = get_tv_number_chk(&argvars[6], NULL);
15923 if (time_limit < 0)
15924 goto theend;
15925 }
15926#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015927 }
15928 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015929 if (skip == NULL)
15930 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015932 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015933 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015934
15935theend:
15936 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015937
15938 return retval;
15939}
15940
15941/*
15942 * "searchpair()" function
15943 */
15944 static void
15945f_searchpair(argvars, rettv)
15946 typval_T *argvars;
15947 typval_T *rettv;
15948{
15949 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15950}
15951
15952/*
15953 * "searchpairpos()" function
15954 */
15955 static void
15956f_searchpairpos(argvars, rettv)
15957 typval_T *argvars;
15958 typval_T *rettv;
15959{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015960 pos_T match_pos;
15961 int lnum = 0;
15962 int col = 0;
15963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015964 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015965 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015966
15967 if (searchpair_cmn(argvars, &match_pos) > 0)
15968 {
15969 lnum = match_pos.lnum;
15970 col = match_pos.col;
15971 }
15972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015973 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15974 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015975}
15976
15977/*
15978 * Search for a start/middle/end thing.
15979 * Used by searchpair(), see its documentation for the details.
15980 * Returns 0 or -1 for no match,
15981 */
15982 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015983do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15984 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015985 char_u *spat; /* start pattern */
15986 char_u *mpat; /* middle pattern */
15987 char_u *epat; /* end pattern */
15988 int dir; /* BACKWARD or FORWARD */
15989 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015990 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015991 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015992 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015993 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015994{
15995 char_u *save_cpo;
15996 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15997 long retval = 0;
15998 pos_T pos;
15999 pos_T firstpos;
16000 pos_T foundpos;
16001 pos_T save_cursor;
16002 pos_T save_pos;
16003 int n;
16004 int r;
16005 int nest = 1;
16006 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016007 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016008 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016009
16010 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16011 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016012 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016013
Bram Moolenaar76929292008-01-06 19:07:36 +000016014#ifdef FEAT_RELTIME
16015 /* Set the time limit, if there is one. */
16016 profile_setlimit(time_limit, &tm);
16017#endif
16018
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016019 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16020 * start/middle/end (pat3, for the top pair). */
16021 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16022 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16023 if (pat2 == NULL || pat3 == NULL)
16024 goto theend;
16025 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16026 if (*mpat == NUL)
16027 STRCPY(pat3, pat2);
16028 else
16029 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16030 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016031 if (flags & SP_START)
16032 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016033
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 save_cursor = curwin->w_cursor;
16035 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016036 clearpos(&firstpos);
16037 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 pat = pat3;
16039 for (;;)
16040 {
16041 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016042 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16044 /* didn't find it or found the first match again: FAIL */
16045 break;
16046
16047 if (firstpos.lnum == 0)
16048 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016049 if (equalpos(pos, foundpos))
16050 {
16051 /* Found the same position again. Can happen with a pattern that
16052 * has "\zs" at the end and searching backwards. Advance one
16053 * character and try again. */
16054 if (dir == BACKWARD)
16055 decl(&pos);
16056 else
16057 incl(&pos);
16058 }
16059 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016061 /* clear the start flag to avoid getting stuck here */
16062 options &= ~SEARCH_START;
16063
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 /* If the skip pattern matches, ignore this match. */
16065 if (*skip != NUL)
16066 {
16067 save_pos = curwin->w_cursor;
16068 curwin->w_cursor = pos;
16069 r = eval_to_bool(skip, &err, NULL, FALSE);
16070 curwin->w_cursor = save_pos;
16071 if (err)
16072 {
16073 /* Evaluating {skip} caused an error, break here. */
16074 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016075 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076 break;
16077 }
16078 if (r)
16079 continue;
16080 }
16081
16082 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16083 {
16084 /* Found end when searching backwards or start when searching
16085 * forward: nested pair. */
16086 ++nest;
16087 pat = pat2; /* nested, don't search for middle */
16088 }
16089 else
16090 {
16091 /* Found end when searching forward or start when searching
16092 * backward: end of (nested) pair; or found middle in outer pair. */
16093 if (--nest == 1)
16094 pat = pat3; /* outer level, search for middle */
16095 }
16096
16097 if (nest == 0)
16098 {
16099 /* Found the match: return matchcount or line number. */
16100 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016101 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016103 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016104 if (flags & SP_SETPCMARK)
16105 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106 curwin->w_cursor = pos;
16107 if (!(flags & SP_REPEAT))
16108 break;
16109 nest = 1; /* search for next unmatched */
16110 }
16111 }
16112
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016113 if (match_pos != NULL)
16114 {
16115 /* Store the match cursor position */
16116 match_pos->lnum = curwin->w_cursor.lnum;
16117 match_pos->col = curwin->w_cursor.col + 1;
16118 }
16119
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016121 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 curwin->w_cursor = save_cursor;
16123
16124theend:
16125 vim_free(pat2);
16126 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016127 if (p_cpo == empty_option)
16128 p_cpo = save_cpo;
16129 else
16130 /* Darn, evaluating the {skip} expression changed the value. */
16131 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016132
16133 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134}
16135
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016136/*
16137 * "searchpos()" function
16138 */
16139 static void
16140f_searchpos(argvars, rettv)
16141 typval_T *argvars;
16142 typval_T *rettv;
16143{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016144 pos_T match_pos;
16145 int lnum = 0;
16146 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016147 int n;
16148 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016149
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016150 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016151 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016152
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016153 n = search_cmn(argvars, &match_pos, &flags);
16154 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016155 {
16156 lnum = match_pos.lnum;
16157 col = match_pos.col;
16158 }
16159
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016160 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16161 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016162 if (flags & SP_SUBPAT)
16163 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016164}
16165
16166
Bram Moolenaar0d660222005-01-07 21:51:51 +000016167 static void
16168f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016169 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172#ifdef FEAT_CLIENTSERVER
16173 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016174 char_u *server = get_tv_string_chk(&argvars[0]);
16175 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016178 if (server == NULL || reply == NULL)
16179 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016180 if (check_restricted() || check_secure())
16181 return;
16182# ifdef FEAT_X11
16183 if (check_connection() == FAIL)
16184 return;
16185# endif
16186
16187 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 EMSG(_("E258: Unable to send to client"));
16190 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016192 rettv->vval.v_number = 0;
16193#else
16194 rettv->vval.v_number = -1;
16195#endif
16196}
16197
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198 static void
16199f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016200 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016201 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016202{
16203 char_u *r = NULL;
16204
16205#ifdef FEAT_CLIENTSERVER
16206# ifdef WIN32
16207 r = serverGetVimNames();
16208# else
16209 make_connection();
16210 if (X_DISPLAY != NULL)
16211 r = serverGetVimNames(X_DISPLAY);
16212# endif
16213#endif
16214 rettv->v_type = VAR_STRING;
16215 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216}
16217
16218/*
16219 * "setbufvar()" function
16220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016223 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016224 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225{
16226 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016229 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 char_u nbuf[NUMBUFLEN];
16231
16232 if (check_restricted() || check_secure())
16233 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016234 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16235 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016236 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 varp = &argvars[2];
16238
16239 if (buf != NULL && varname != NULL && varp != NULL)
16240 {
16241 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243
16244 if (*varname == '&')
16245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 long numval;
16247 char_u *strval;
16248 int error = FALSE;
16249
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016251 numval = get_tv_number_chk(varp, &error);
16252 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016253 if (!error && strval != NULL)
16254 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 }
16256 else
16257 {
16258 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16259 if (bufvarname != NULL)
16260 {
16261 STRCPY(bufvarname, "b:");
16262 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016263 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 vim_free(bufvarname);
16265 }
16266 }
16267
16268 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271}
16272
16273/*
16274 * "setcmdpos()" function
16275 */
16276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016277f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016278 typval_T *argvars;
16279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016281 int pos = (int)get_tv_number(&argvars[0]) - 1;
16282
16283 if (pos >= 0)
16284 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285}
16286
16287/*
16288 * "setline()" function
16289 */
16290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 typval_T *argvars;
16293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294{
16295 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016296 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016297 list_T *l = NULL;
16298 listitem_T *li = NULL;
16299 long added = 0;
16300 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016302 lnum = get_tv_lnum(&argvars[0]);
16303 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016305 l = argvars[1].vval.v_list;
16306 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016308 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016309 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016310
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016311 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016312 for (;;)
16313 {
16314 if (l != NULL)
16315 {
16316 /* list argument, get next string */
16317 if (li == NULL)
16318 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016319 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016320 li = li->li_next;
16321 }
16322
16323 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016324 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016325 break;
16326 if (lnum <= curbuf->b_ml.ml_line_count)
16327 {
16328 /* existing line, replace it */
16329 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16330 {
16331 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016332 if (lnum == curwin->w_cursor.lnum)
16333 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016334 rettv->vval.v_number = 0; /* OK */
16335 }
16336 }
16337 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16338 {
16339 /* lnum is one past the last line, append the line */
16340 ++added;
16341 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16342 rettv->vval.v_number = 0; /* OK */
16343 }
16344
16345 if (l == NULL) /* only one string argument */
16346 break;
16347 ++lnum;
16348 }
16349
16350 if (added > 0)
16351 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352}
16353
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016354static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16355
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016357 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016358 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016359 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016360set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016361 win_T *wp UNUSED;
16362 typval_T *list_arg UNUSED;
16363 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016364 typval_T *rettv;
16365{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016366#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016367 char_u *act;
16368 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016369#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016370
Bram Moolenaar2641f772005-03-25 21:58:17 +000016371 rettv->vval.v_number = -1;
16372
16373#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016374 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016375 EMSG(_(e_listreq));
16376 else
16377 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016378 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016379
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016380 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016381 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016382 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016383 if (act == NULL)
16384 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016385 if (*act == 'a' || *act == 'r')
16386 action = *act;
16387 }
16388
Bram Moolenaar81484f42012-12-05 15:16:47 +010016389 if (l != NULL && set_errorlist(wp, l, action,
16390 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016391 rettv->vval.v_number = 0;
16392 }
16393#endif
16394}
16395
16396/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016397 * "setloclist()" function
16398 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016399 static void
16400f_setloclist(argvars, rettv)
16401 typval_T *argvars;
16402 typval_T *rettv;
16403{
16404 win_T *win;
16405
16406 rettv->vval.v_number = -1;
16407
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016408 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016409 if (win != NULL)
16410 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16411}
16412
16413/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016414 * "setmatches()" function
16415 */
16416 static void
16417f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016418 typval_T *argvars UNUSED;
16419 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016420{
16421#ifdef FEAT_SEARCH_EXTRA
16422 list_T *l;
16423 listitem_T *li;
16424 dict_T *d;
16425
16426 rettv->vval.v_number = -1;
16427 if (argvars[0].v_type != VAR_LIST)
16428 {
16429 EMSG(_(e_listreq));
16430 return;
16431 }
16432 if ((l = argvars[0].vval.v_list) != NULL)
16433 {
16434
16435 /* To some extent make sure that we are dealing with a list from
16436 * "getmatches()". */
16437 li = l->lv_first;
16438 while (li != NULL)
16439 {
16440 if (li->li_tv.v_type != VAR_DICT
16441 || (d = li->li_tv.vval.v_dict) == NULL)
16442 {
16443 EMSG(_(e_invarg));
16444 return;
16445 }
16446 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16447 && dict_find(d, (char_u *)"pattern", -1) != NULL
16448 && dict_find(d, (char_u *)"priority", -1) != NULL
16449 && dict_find(d, (char_u *)"id", -1) != NULL))
16450 {
16451 EMSG(_(e_invarg));
16452 return;
16453 }
16454 li = li->li_next;
16455 }
16456
16457 clear_matches(curwin);
16458 li = l->lv_first;
16459 while (li != NULL)
16460 {
16461 d = li->li_tv.vval.v_dict;
16462 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16463 get_dict_string(d, (char_u *)"pattern", FALSE),
16464 (int)get_dict_number(d, (char_u *)"priority"),
16465 (int)get_dict_number(d, (char_u *)"id"));
16466 li = li->li_next;
16467 }
16468 rettv->vval.v_number = 0;
16469 }
16470#endif
16471}
16472
16473/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016474 * "setpos()" function
16475 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016476 static void
16477f_setpos(argvars, rettv)
16478 typval_T *argvars;
16479 typval_T *rettv;
16480{
16481 pos_T pos;
16482 int fnum;
16483 char_u *name;
16484
Bram Moolenaar08250432008-02-13 11:42:46 +000016485 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016486 name = get_tv_string_chk(argvars);
16487 if (name != NULL)
16488 {
16489 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16490 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016491 if (--pos.col < 0)
16492 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016493 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016494 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016495 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016496 if (fnum == curbuf->b_fnum)
16497 {
16498 curwin->w_cursor = pos;
16499 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016500 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016501 }
16502 else
16503 EMSG(_(e_invarg));
16504 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016505 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16506 {
16507 /* set mark */
16508 if (setmark_pos(name[1], &pos, fnum) == OK)
16509 rettv->vval.v_number = 0;
16510 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016511 else
16512 EMSG(_(e_invarg));
16513 }
16514 }
16515}
16516
16517/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016518 * "setqflist()" function
16519 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016520 static void
16521f_setqflist(argvars, rettv)
16522 typval_T *argvars;
16523 typval_T *rettv;
16524{
16525 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16526}
16527
16528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 * "setreg()" function
16530 */
16531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016532f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016533 typval_T *argvars;
16534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535{
16536 int regname;
16537 char_u *strregname;
16538 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 int append;
16541 char_u yank_type;
16542 long block_len;
16543
16544 block_len = -1;
16545 yank_type = MAUTO;
16546 append = FALSE;
16547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016548 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016549 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016551 if (strregname == NULL)
16552 return; /* type error; errmsg already given */
16553 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 if (regname == 0 || regname == '@')
16555 regname = '"';
16556 else if (regname == '=')
16557 return;
16558
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016561 stropt = get_tv_string_chk(&argvars[2]);
16562 if (stropt == NULL)
16563 return; /* type error */
16564 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 switch (*stropt)
16566 {
16567 case 'a': case 'A': /* append */
16568 append = TRUE;
16569 break;
16570 case 'v': case 'c': /* character-wise selection */
16571 yank_type = MCHAR;
16572 break;
16573 case 'V': case 'l': /* line-wise selection */
16574 yank_type = MLINE;
16575 break;
16576#ifdef FEAT_VISUAL
16577 case 'b': case Ctrl_V: /* block-wise selection */
16578 yank_type = MBLOCK;
16579 if (VIM_ISDIGIT(stropt[1]))
16580 {
16581 ++stropt;
16582 block_len = getdigits(&stropt) - 1;
16583 --stropt;
16584 }
16585 break;
16586#endif
16587 }
16588 }
16589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016590 strval = get_tv_string_chk(&argvars[1]);
16591 if (strval != NULL)
16592 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595}
16596
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016597/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016598 * "settabvar()" function
16599 */
16600 static void
16601f_settabvar(argvars, rettv)
16602 typval_T *argvars;
16603 typval_T *rettv;
16604{
16605 tabpage_T *save_curtab;
16606 char_u *varname, *tabvarname;
16607 typval_T *varp;
16608 tabpage_T *tp;
16609
16610 rettv->vval.v_number = 0;
16611
16612 if (check_restricted() || check_secure())
16613 return;
16614
16615 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16616 varname = get_tv_string_chk(&argvars[1]);
16617 varp = &argvars[2];
16618
16619 if (tp != NULL && varname != NULL && varp != NULL)
16620 {
16621 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016622 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016623
16624 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16625 if (tabvarname != NULL)
16626 {
16627 STRCPY(tabvarname, "t:");
16628 STRCPY(tabvarname + 2, varname);
16629 set_var(tabvarname, varp, TRUE);
16630 vim_free(tabvarname);
16631 }
16632
16633 /* Restore current tabpage */
16634 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016635 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016636 }
16637}
16638
16639/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016640 * "settabwinvar()" function
16641 */
16642 static void
16643f_settabwinvar(argvars, rettv)
16644 typval_T *argvars;
16645 typval_T *rettv;
16646{
16647 setwinvar(argvars, rettv, 1);
16648}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649
16650/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016651 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016654f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016655 typval_T *argvars;
16656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016658 setwinvar(argvars, rettv, 0);
16659}
16660
16661/*
16662 * "setwinvar()" and "settabwinvar()" functions
16663 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016664
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016665 static void
16666setwinvar(argvars, rettv, off)
16667 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016668 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016669 int off;
16670{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 win_T *win;
16672#ifdef FEAT_WINDOWS
16673 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016674 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675#endif
16676 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016679 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680
16681 if (check_restricted() || check_secure())
16682 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016683
16684#ifdef FEAT_WINDOWS
16685 if (off == 1)
16686 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16687 else
16688 tp = curtab;
16689#endif
16690 win = find_win_by_nr(&argvars[off], tp);
16691 varname = get_tv_string_chk(&argvars[off + 1]);
16692 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693
16694 if (win != NULL && varname != NULL && varp != NULL)
16695 {
16696#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016697 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016698 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699#endif
16700
16701 if (*varname == '&')
16702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 long numval;
16704 char_u *strval;
16705 int error = FALSE;
16706
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708 numval = get_tv_number_chk(varp, &error);
16709 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016710 if (!error && strval != NULL)
16711 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 }
16713 else
16714 {
16715 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16716 if (winvarname != NULL)
16717 {
16718 STRCPY(winvarname, "w:");
16719 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016720 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 vim_free(winvarname);
16722 }
16723 }
16724
16725#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016726 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727#endif
16728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729}
16730
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016731#ifdef FEAT_CRYPT
16732/*
16733 * "sha256({string})" function
16734 */
16735 static void
16736f_sha256(argvars, rettv)
16737 typval_T *argvars;
16738 typval_T *rettv;
16739{
16740 char_u *p;
16741
16742 p = get_tv_string(&argvars[0]);
16743 rettv->vval.v_string = vim_strsave(
16744 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16745 rettv->v_type = VAR_STRING;
16746}
16747#endif /* FEAT_CRYPT */
16748
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016750 * "shellescape({string})" function
16751 */
16752 static void
16753f_shellescape(argvars, rettv)
16754 typval_T *argvars;
16755 typval_T *rettv;
16756{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016757 rettv->vval.v_string = vim_strsave_shellescape(
16758 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016759 rettv->v_type = VAR_STRING;
16760}
16761
16762/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016763 * shiftwidth() function
16764 */
16765 static void
16766f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016767 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016768 typval_T *rettv;
16769{
16770 rettv->vval.v_number = get_sw_value();
16771}
16772
16773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016774 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 */
16776 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016777f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016778 typval_T *argvars;
16779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783 p = get_tv_string(&argvars[0]);
16784 rettv->vval.v_string = vim_strsave(p);
16785 simplify_filename(rettv->vval.v_string); /* simplify in place */
16786 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787}
16788
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016789#ifdef FEAT_FLOAT
16790/*
16791 * "sin()" function
16792 */
16793 static void
16794f_sin(argvars, rettv)
16795 typval_T *argvars;
16796 typval_T *rettv;
16797{
16798 float_T f;
16799
16800 rettv->v_type = VAR_FLOAT;
16801 if (get_float_arg(argvars, &f) == OK)
16802 rettv->vval.v_float = sin(f);
16803 else
16804 rettv->vval.v_float = 0.0;
16805}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016806
16807/*
16808 * "sinh()" function
16809 */
16810 static void
16811f_sinh(argvars, rettv)
16812 typval_T *argvars;
16813 typval_T *rettv;
16814{
16815 float_T f;
16816
16817 rettv->v_type = VAR_FLOAT;
16818 if (get_float_arg(argvars, &f) == OK)
16819 rettv->vval.v_float = sinh(f);
16820 else
16821 rettv->vval.v_float = 0.0;
16822}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016823#endif
16824
Bram Moolenaar0d660222005-01-07 21:51:51 +000016825static int
16826#ifdef __BORLANDC__
16827 _RTLENTRYF
16828#endif
16829 item_compare __ARGS((const void *s1, const void *s2));
16830static int
16831#ifdef __BORLANDC__
16832 _RTLENTRYF
16833#endif
16834 item_compare2 __ARGS((const void *s1, const void *s2));
16835
16836static int item_compare_ic;
16837static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016838static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016839static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016840#define ITEM_COMPARE_FAIL 999
16841
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016843 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016845 static int
16846#ifdef __BORLANDC__
16847_RTLENTRYF
16848#endif
16849item_compare(s1, s2)
16850 const void *s1;
16851 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016853 char_u *p1, *p2;
16854 char_u *tofree1, *tofree2;
16855 int res;
16856 char_u numbuf1[NUMBUFLEN];
16857 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016859 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16860 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016861 if (p1 == NULL)
16862 p1 = (char_u *)"";
16863 if (p2 == NULL)
16864 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 if (item_compare_ic)
16866 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016868 res = STRCMP(p1, p2);
16869 vim_free(tofree1);
16870 vim_free(tofree2);
16871 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872}
16873
16874 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875#ifdef __BORLANDC__
16876_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878item_compare2(s1, s2)
16879 const void *s1;
16880 const void *s2;
16881{
16882 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016883 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016884 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016887 /* shortcut after failure in previous call; compare all items equal */
16888 if (item_compare_func_err)
16889 return 0;
16890
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016891 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16892 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016893 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16894 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895
16896 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016897 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016898 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16899 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016900 clear_tv(&argv[0]);
16901 clear_tv(&argv[1]);
16902
16903 if (res == FAIL)
16904 res = ITEM_COMPARE_FAIL;
16905 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016906 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16907 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016908 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909 clear_tv(&rettv);
16910 return res;
16911}
16912
16913/*
16914 * "sort({list})" function
16915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016918 typval_T *argvars;
16919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920{
Bram Moolenaar33570922005-01-25 22:26:29 +000016921 list_T *l;
16922 listitem_T *li;
16923 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016924 long len;
16925 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927 if (argvars[0].v_type != VAR_LIST)
16928 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 else
16930 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016932 if (l == NULL || tv_check_lock(l->lv_lock,
16933 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 return;
16935 rettv->vval.v_list = l;
16936 rettv->v_type = VAR_LIST;
16937 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 len = list_len(l);
16940 if (len <= 1)
16941 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943 item_compare_ic = FALSE;
16944 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016945 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946 if (argvars[1].v_type != VAR_UNKNOWN)
16947 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016948 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951 else
16952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953 int error = FALSE;
16954
16955 i = get_tv_number_chk(&argvars[1], &error);
16956 if (error)
16957 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 if (i == 1)
16959 item_compare_ic = TRUE;
16960 else
16961 item_compare_func = get_tv_string(&argvars[1]);
16962 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016963
16964 if (argvars[2].v_type != VAR_UNKNOWN)
16965 {
16966 /* optional third argument: {dict} */
16967 if (argvars[2].v_type != VAR_DICT)
16968 {
16969 EMSG(_(e_dictreq));
16970 return;
16971 }
16972 item_compare_selfdict = argvars[2].vval.v_dict;
16973 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978 if (ptrs == NULL)
16979 return;
16980 i = 0;
16981 for (li = l->lv_first; li != NULL; li = li->li_next)
16982 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985 /* test the compare function */
16986 if (item_compare_func != NULL
16987 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16988 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016989 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991 {
16992 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016993 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994 item_compare_func == NULL ? item_compare : item_compare2);
16995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016996 if (!item_compare_func_err)
16997 {
16998 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016999 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017000 l->lv_len = 0;
17001 for (i = 0; i < len; ++i)
17002 list_append(l, ptrs[i]);
17003 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004 }
17005
17006 vim_free(ptrs);
17007 }
17008}
17009
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017010/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017011 * "soundfold({word})" function
17012 */
17013 static void
17014f_soundfold(argvars, rettv)
17015 typval_T *argvars;
17016 typval_T *rettv;
17017{
17018 char_u *s;
17019
17020 rettv->v_type = VAR_STRING;
17021 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017022#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017023 rettv->vval.v_string = eval_soundfold(s);
17024#else
17025 rettv->vval.v_string = vim_strsave(s);
17026#endif
17027}
17028
17029/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017030 * "spellbadword()" function
17031 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017032 static void
17033f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017034 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017035 typval_T *rettv;
17036{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017037 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017038 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017039 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017040
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017041 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017042 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017043
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017044#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017045 if (argvars[0].v_type == VAR_UNKNOWN)
17046 {
17047 /* Find the start and length of the badly spelled word. */
17048 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17049 if (len != 0)
17050 word = ml_get_cursor();
17051 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017052 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017053 {
17054 char_u *str = get_tv_string_chk(&argvars[0]);
17055 int capcol = -1;
17056
17057 if (str != NULL)
17058 {
17059 /* Check the argument for spelling. */
17060 while (*str != NUL)
17061 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017062 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017063 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017064 {
17065 word = str;
17066 break;
17067 }
17068 str += len;
17069 }
17070 }
17071 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017072#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017073
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017074 list_append_string(rettv->vval.v_list, word, len);
17075 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017076 attr == HLF_SPB ? "bad" :
17077 attr == HLF_SPR ? "rare" :
17078 attr == HLF_SPL ? "local" :
17079 attr == HLF_SPC ? "caps" :
17080 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017081}
17082
17083/*
17084 * "spellsuggest()" function
17085 */
17086 static void
17087f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017088 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017089 typval_T *rettv;
17090{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017091#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017092 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017093 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017094 int maxcount;
17095 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017096 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017097 listitem_T *li;
17098 int need_capital = FALSE;
17099#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017100
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017101 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017102 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017103
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017104#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017105 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017106 {
17107 str = get_tv_string(&argvars[0]);
17108 if (argvars[1].v_type != VAR_UNKNOWN)
17109 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017110 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017111 if (maxcount <= 0)
17112 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017113 if (argvars[2].v_type != VAR_UNKNOWN)
17114 {
17115 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17116 if (typeerr)
17117 return;
17118 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017119 }
17120 else
17121 maxcount = 25;
17122
Bram Moolenaar4770d092006-01-12 23:22:24 +000017123 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017124
17125 for (i = 0; i < ga.ga_len; ++i)
17126 {
17127 str = ((char_u **)ga.ga_data)[i];
17128
17129 li = listitem_alloc();
17130 if (li == NULL)
17131 vim_free(str);
17132 else
17133 {
17134 li->li_tv.v_type = VAR_STRING;
17135 li->li_tv.v_lock = 0;
17136 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017137 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017138 }
17139 }
17140 ga_clear(&ga);
17141 }
17142#endif
17143}
17144
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017146f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 typval_T *argvars;
17148 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149{
17150 char_u *str;
17151 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017152 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153 regmatch_T regmatch;
17154 char_u patbuf[NUMBUFLEN];
17155 char_u *save_cpo;
17156 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017158 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017159 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160
17161 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17162 save_cpo = p_cpo;
17163 p_cpo = (char_u *)"";
17164
17165 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017166 if (argvars[1].v_type != VAR_UNKNOWN)
17167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017168 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17169 if (pat == NULL)
17170 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017171 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017172 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017173 }
17174 if (pat == NULL || *pat == NUL)
17175 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017177 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 if (typeerr)
17180 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181
Bram Moolenaar0d660222005-01-07 21:51:51 +000017182 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17183 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017186 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017187 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017188 if (*str == NUL)
17189 match = FALSE; /* empty item at the end */
17190 else
17191 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017192 if (match)
17193 end = regmatch.startp[0];
17194 else
17195 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017196 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17197 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017199 if (list_append_string(rettv->vval.v_list, str,
17200 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017202 }
17203 if (!match)
17204 break;
17205 /* Advance to just after the match. */
17206 if (regmatch.endp[0] > str)
17207 col = 0;
17208 else
17209 {
17210 /* Don't get stuck at the same match. */
17211#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017212 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017213#else
17214 col = 1;
17215#endif
17216 }
17217 str = regmatch.endp[0];
17218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219
Bram Moolenaar0d660222005-01-07 21:51:51 +000017220 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224}
17225
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017226#ifdef FEAT_FLOAT
17227/*
17228 * "sqrt()" function
17229 */
17230 static void
17231f_sqrt(argvars, rettv)
17232 typval_T *argvars;
17233 typval_T *rettv;
17234{
17235 float_T f;
17236
17237 rettv->v_type = VAR_FLOAT;
17238 if (get_float_arg(argvars, &f) == OK)
17239 rettv->vval.v_float = sqrt(f);
17240 else
17241 rettv->vval.v_float = 0.0;
17242}
17243
17244/*
17245 * "str2float()" function
17246 */
17247 static void
17248f_str2float(argvars, rettv)
17249 typval_T *argvars;
17250 typval_T *rettv;
17251{
17252 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17253
17254 if (*p == '+')
17255 p = skipwhite(p + 1);
17256 (void)string2float(p, &rettv->vval.v_float);
17257 rettv->v_type = VAR_FLOAT;
17258}
17259#endif
17260
Bram Moolenaar2c932302006-03-18 21:42:09 +000017261/*
17262 * "str2nr()" function
17263 */
17264 static void
17265f_str2nr(argvars, rettv)
17266 typval_T *argvars;
17267 typval_T *rettv;
17268{
17269 int base = 10;
17270 char_u *p;
17271 long n;
17272
17273 if (argvars[1].v_type != VAR_UNKNOWN)
17274 {
17275 base = get_tv_number(&argvars[1]);
17276 if (base != 8 && base != 10 && base != 16)
17277 {
17278 EMSG(_(e_invarg));
17279 return;
17280 }
17281 }
17282
17283 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017284 if (*p == '+')
17285 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017286 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17287 rettv->vval.v_number = n;
17288}
17289
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290#ifdef HAVE_STRFTIME
17291/*
17292 * "strftime({format}[, {time}])" function
17293 */
17294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017295f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017296 typval_T *argvars;
17297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298{
17299 char_u result_buf[256];
17300 struct tm *curtime;
17301 time_t seconds;
17302 char_u *p;
17303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017304 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017306 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017307 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 seconds = time(NULL);
17309 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017310 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 curtime = localtime(&seconds);
17312 /* MSVC returns NULL for an invalid value of seconds. */
17313 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017314 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 else
17316 {
17317# ifdef FEAT_MBYTE
17318 vimconv_T conv;
17319 char_u *enc;
17320
17321 conv.vc_type = CONV_NONE;
17322 enc = enc_locale();
17323 convert_setup(&conv, p_enc, enc);
17324 if (conv.vc_type != CONV_NONE)
17325 p = string_convert(&conv, p, NULL);
17326# endif
17327 if (p != NULL)
17328 (void)strftime((char *)result_buf, sizeof(result_buf),
17329 (char *)p, curtime);
17330 else
17331 result_buf[0] = NUL;
17332
17333# ifdef FEAT_MBYTE
17334 if (conv.vc_type != CONV_NONE)
17335 vim_free(p);
17336 convert_setup(&conv, enc, p_enc);
17337 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 else
17340# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017341 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342
17343# ifdef FEAT_MBYTE
17344 /* Release conversion descriptors */
17345 convert_setup(&conv, NULL, NULL);
17346 vim_free(enc);
17347# endif
17348 }
17349}
17350#endif
17351
17352/*
17353 * "stridx()" function
17354 */
17355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017356f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017357 typval_T *argvars;
17358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359{
17360 char_u buf[NUMBUFLEN];
17361 char_u *needle;
17362 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017363 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 needle = get_tv_string_chk(&argvars[1]);
17368 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017369 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017370 if (needle == NULL || haystack == NULL)
17371 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
Bram Moolenaar33570922005-01-25 22:26:29 +000017373 if (argvars[2].v_type != VAR_UNKNOWN)
17374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017375 int error = FALSE;
17376
17377 start_idx = get_tv_number_chk(&argvars[2], &error);
17378 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017380 if (start_idx >= 0)
17381 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 }
17383
17384 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17385 if (pos != NULL)
17386 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387}
17388
17389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017390 * "string()" function
17391 */
17392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017394 typval_T *argvars;
17395 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017396{
17397 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017398 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017400 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017401 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017402 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017403 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017404 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405}
17406
17407/*
17408 * "strlen()" function
17409 */
17410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017411f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017412 typval_T *argvars;
17413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017415 rettv->vval.v_number = (varnumber_T)(STRLEN(
17416 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417}
17418
17419/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017420 * "strchars()" function
17421 */
17422 static void
17423f_strchars(argvars, rettv)
17424 typval_T *argvars;
17425 typval_T *rettv;
17426{
17427 char_u *s = get_tv_string(&argvars[0]);
17428#ifdef FEAT_MBYTE
17429 varnumber_T len = 0;
17430
17431 while (*s != NUL)
17432 {
17433 mb_cptr2char_adv(&s);
17434 ++len;
17435 }
17436 rettv->vval.v_number = len;
17437#else
17438 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17439#endif
17440}
17441
17442/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017443 * "strdisplaywidth()" function
17444 */
17445 static void
17446f_strdisplaywidth(argvars, rettv)
17447 typval_T *argvars;
17448 typval_T *rettv;
17449{
17450 char_u *s = get_tv_string(&argvars[0]);
17451 int col = 0;
17452
17453 if (argvars[1].v_type != VAR_UNKNOWN)
17454 col = get_tv_number(&argvars[1]);
17455
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017456 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017457}
17458
17459/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017460 * "strwidth()" function
17461 */
17462 static void
17463f_strwidth(argvars, rettv)
17464 typval_T *argvars;
17465 typval_T *rettv;
17466{
17467 char_u *s = get_tv_string(&argvars[0]);
17468
17469 rettv->vval.v_number = (varnumber_T)(
17470#ifdef FEAT_MBYTE
17471 mb_string2cells(s, -1)
17472#else
17473 STRLEN(s)
17474#endif
17475 );
17476}
17477
17478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 * "strpart()" function
17480 */
17481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017482f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017483 typval_T *argvars;
17484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485{
17486 char_u *p;
17487 int n;
17488 int len;
17489 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017490 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 slen = (int)STRLEN(p);
17494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017495 n = get_tv_number_chk(&argvars[1], &error);
17496 if (error)
17497 len = 0;
17498 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017499 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 else
17501 len = slen - n; /* default len: all bytes that are available. */
17502
17503 /*
17504 * Only return the overlap between the specified part and the actual
17505 * string.
17506 */
17507 if (n < 0)
17508 {
17509 len += n;
17510 n = 0;
17511 }
17512 else if (n > slen)
17513 n = slen;
17514 if (len < 0)
17515 len = 0;
17516 else if (n + len > slen)
17517 len = slen - n;
17518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017519 rettv->v_type = VAR_STRING;
17520 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521}
17522
17523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017524 * "strridx()" function
17525 */
17526 static void
17527f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 typval_T *argvars;
17529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017530{
17531 char_u buf[NUMBUFLEN];
17532 char_u *needle;
17533 char_u *haystack;
17534 char_u *rest;
17535 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017536 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017538 needle = get_tv_string_chk(&argvars[1]);
17539 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017540
17541 rettv->vval.v_number = -1;
17542 if (needle == NULL || haystack == NULL)
17543 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017544
17545 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017546 if (argvars[2].v_type != VAR_UNKNOWN)
17547 {
17548 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017549 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017550 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017551 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017552 }
17553 else
17554 end_idx = haystack_len;
17555
Bram Moolenaar0d660222005-01-07 21:51:51 +000017556 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017557 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017558 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017559 lastmatch = haystack + end_idx;
17560 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017561 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017562 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017563 for (rest = haystack; *rest != '\0'; ++rest)
17564 {
17565 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017566 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 break;
17568 lastmatch = rest;
17569 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017570 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017571
17572 if (lastmatch == NULL)
17573 rettv->vval.v_number = -1;
17574 else
17575 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17576}
17577
17578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 * "strtrans()" function
17580 */
17581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017583 typval_T *argvars;
17584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586 rettv->v_type = VAR_STRING;
17587 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588}
17589
17590/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017591 * "submatch()" function
17592 */
17593 static void
17594f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017595 typval_T *argvars;
17596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597{
17598 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017599 rettv->vval.v_string =
17600 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017601}
17602
17603/*
17604 * "substitute()" function
17605 */
17606 static void
17607f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017608 typval_T *argvars;
17609 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017610{
17611 char_u patbuf[NUMBUFLEN];
17612 char_u subbuf[NUMBUFLEN];
17613 char_u flagsbuf[NUMBUFLEN];
17614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017615 char_u *str = get_tv_string_chk(&argvars[0]);
17616 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17617 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17618 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17619
Bram Moolenaar0d660222005-01-07 21:51:51 +000017620 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017621 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17622 rettv->vval.v_string = NULL;
17623 else
17624 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017625}
17626
17627/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017628 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017631f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017632 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634{
17635 int id = 0;
17636#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017637 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 long col;
17639 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017640 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017642 lnum = get_tv_lnum(argvars); /* -1 on type error */
17643 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17644 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017646 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017647 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017648 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649#endif
17650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017651 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652}
17653
17654/*
17655 * "synIDattr(id, what [, mode])" function
17656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017658f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017659 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661{
17662 char_u *p = NULL;
17663#ifdef FEAT_SYN_HL
17664 int id;
17665 char_u *what;
17666 char_u *mode;
17667 char_u modebuf[NUMBUFLEN];
17668 int modec;
17669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017670 id = get_tv_number(&argvars[0]);
17671 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017672 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017676 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 modec = 0; /* replace invalid with current */
17678 }
17679 else
17680 {
17681#ifdef FEAT_GUI
17682 if (gui.in_use)
17683 modec = 'g';
17684 else
17685#endif
17686 if (t_colors > 1)
17687 modec = 'c';
17688 else
17689 modec = 't';
17690 }
17691
17692
17693 switch (TOLOWER_ASC(what[0]))
17694 {
17695 case 'b':
17696 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17697 p = highlight_color(id, what, modec);
17698 else /* bold */
17699 p = highlight_has_attr(id, HL_BOLD, modec);
17700 break;
17701
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017702 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 p = highlight_color(id, what, modec);
17704 break;
17705
17706 case 'i':
17707 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17708 p = highlight_has_attr(id, HL_INVERSE, modec);
17709 else /* italic */
17710 p = highlight_has_attr(id, HL_ITALIC, modec);
17711 break;
17712
17713 case 'n': /* name */
17714 p = get_highlight_name(NULL, id - 1);
17715 break;
17716
17717 case 'r': /* reverse */
17718 p = highlight_has_attr(id, HL_INVERSE, modec);
17719 break;
17720
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017721 case 's':
17722 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17723 p = highlight_color(id, what, modec);
17724 else /* standout */
17725 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 break;
17727
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017728 case 'u':
17729 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17730 /* underline */
17731 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17732 else
17733 /* undercurl */
17734 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 break;
17736 }
17737
17738 if (p != NULL)
17739 p = vim_strsave(p);
17740#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017741 rettv->v_type = VAR_STRING;
17742 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743}
17744
17745/*
17746 * "synIDtrans(id)" function
17747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752{
17753 int id;
17754
17755#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017756 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757
17758 if (id > 0)
17759 id = syn_get_final_id(id);
17760 else
17761#endif
17762 id = 0;
17763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765}
17766
17767/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017768 * "synconcealed(lnum, col)" function
17769 */
17770 static void
17771f_synconcealed(argvars, rettv)
17772 typval_T *argvars UNUSED;
17773 typval_T *rettv;
17774{
17775#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17776 long lnum;
17777 long col;
17778 int syntax_flags = 0;
17779 int cchar;
17780 int matchid = 0;
17781 char_u str[NUMBUFLEN];
17782#endif
17783
17784 rettv->v_type = VAR_LIST;
17785 rettv->vval.v_list = NULL;
17786
17787#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17788 lnum = get_tv_lnum(argvars); /* -1 on type error */
17789 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17790
17791 vim_memset(str, NUL, sizeof(str));
17792
17793 if (rettv_list_alloc(rettv) != FAIL)
17794 {
17795 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17796 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17797 && curwin->w_p_cole > 0)
17798 {
17799 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17800 syntax_flags = get_syntax_info(&matchid);
17801
17802 /* get the conceal character */
17803 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17804 {
17805 cchar = syn_get_sub_char();
17806 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17807 cchar = lcs_conceal;
17808 if (cchar != NUL)
17809 {
17810# ifdef FEAT_MBYTE
17811 if (has_mbyte)
17812 (*mb_char2bytes)(cchar, str);
17813 else
17814# endif
17815 str[0] = cchar;
17816 }
17817 }
17818 }
17819
17820 list_append_number(rettv->vval.v_list,
17821 (syntax_flags & HL_CONCEAL) != 0);
17822 /* -1 to auto-determine strlen */
17823 list_append_string(rettv->vval.v_list, str, -1);
17824 list_append_number(rettv->vval.v_list, matchid);
17825 }
17826#endif
17827}
17828
17829/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017830 * "synstack(lnum, col)" function
17831 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017832 static void
17833f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017834 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017835 typval_T *rettv;
17836{
17837#ifdef FEAT_SYN_HL
17838 long lnum;
17839 long col;
17840 int i;
17841 int id;
17842#endif
17843
17844 rettv->v_type = VAR_LIST;
17845 rettv->vval.v_list = NULL;
17846
17847#ifdef FEAT_SYN_HL
17848 lnum = get_tv_lnum(argvars); /* -1 on type error */
17849 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17850
17851 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017852 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017853 && rettv_list_alloc(rettv) != FAIL)
17854 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017855 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017856 for (i = 0; ; ++i)
17857 {
17858 id = syn_get_stack_item(i);
17859 if (id < 0)
17860 break;
17861 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17862 break;
17863 }
17864 }
17865#endif
17866}
17867
17868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 * "system()" function
17870 */
17871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017872f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017873 typval_T *argvars;
17874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017876 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017878 char_u *infile = NULL;
17879 char_u buf[NUMBUFLEN];
17880 int err = FALSE;
17881 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017883 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017884 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017885
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017886 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017887 {
17888 /*
17889 * Write the string to a temp file, to be used for input of the shell
17890 * command.
17891 */
17892 if ((infile = vim_tempname('i')) == NULL)
17893 {
17894 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017895 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017896 }
17897
17898 fd = mch_fopen((char *)infile, WRITEBIN);
17899 if (fd == NULL)
17900 {
17901 EMSG2(_(e_notopen), infile);
17902 goto done;
17903 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017904 p = get_tv_string_buf_chk(&argvars[1], buf);
17905 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017906 {
17907 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017908 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017909 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017910 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17911 err = TRUE;
17912 if (fclose(fd) != 0)
17913 err = TRUE;
17914 if (err)
17915 {
17916 EMSG(_("E677: Error writing temp file"));
17917 goto done;
17918 }
17919 }
17920
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017921 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17922 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017923
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924#ifdef USE_CR
17925 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017926 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 {
17928 char_u *s;
17929
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017930 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 {
17932 if (*s == CAR)
17933 *s = NL;
17934 }
17935 }
17936#else
17937# ifdef USE_CRNL
17938 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017939 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 {
17941 char_u *s, *d;
17942
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017943 d = res;
17944 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 {
17946 if (s[0] == CAR && s[1] == NL)
17947 ++s;
17948 *d++ = *s;
17949 }
17950 *d = NUL;
17951 }
17952# endif
17953#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017954
17955done:
17956 if (infile != NULL)
17957 {
17958 mch_remove(infile);
17959 vim_free(infile);
17960 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017961 rettv->v_type = VAR_STRING;
17962 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963}
17964
17965/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017966 * "tabpagebuflist()" function
17967 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017968 static void
17969f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017970 typval_T *argvars UNUSED;
17971 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017972{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017973#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017974 tabpage_T *tp;
17975 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017976
17977 if (argvars[0].v_type == VAR_UNKNOWN)
17978 wp = firstwin;
17979 else
17980 {
17981 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17982 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017983 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017984 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017985 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017986 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017987 for (; wp != NULL; wp = wp->w_next)
17988 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017989 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017990 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017991 }
17992#endif
17993}
17994
17995
17996/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017997 * "tabpagenr()" function
17998 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017999 static void
18000f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018001 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018002 typval_T *rettv;
18003{
18004 int nr = 1;
18005#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018006 char_u *arg;
18007
18008 if (argvars[0].v_type != VAR_UNKNOWN)
18009 {
18010 arg = get_tv_string_chk(&argvars[0]);
18011 nr = 0;
18012 if (arg != NULL)
18013 {
18014 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018015 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018016 else
18017 EMSG2(_(e_invexpr2), arg);
18018 }
18019 }
18020 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018021 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018022#endif
18023 rettv->vval.v_number = nr;
18024}
18025
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018026
18027#ifdef FEAT_WINDOWS
18028static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18029
18030/*
18031 * Common code for tabpagewinnr() and winnr().
18032 */
18033 static int
18034get_winnr(tp, argvar)
18035 tabpage_T *tp;
18036 typval_T *argvar;
18037{
18038 win_T *twin;
18039 int nr = 1;
18040 win_T *wp;
18041 char_u *arg;
18042
18043 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18044 if (argvar->v_type != VAR_UNKNOWN)
18045 {
18046 arg = get_tv_string_chk(argvar);
18047 if (arg == NULL)
18048 nr = 0; /* type error; errmsg already given */
18049 else if (STRCMP(arg, "$") == 0)
18050 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18051 else if (STRCMP(arg, "#") == 0)
18052 {
18053 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18054 if (twin == NULL)
18055 nr = 0;
18056 }
18057 else
18058 {
18059 EMSG2(_(e_invexpr2), arg);
18060 nr = 0;
18061 }
18062 }
18063
18064 if (nr > 0)
18065 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18066 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018067 {
18068 if (wp == NULL)
18069 {
18070 /* didn't find it in this tabpage */
18071 nr = 0;
18072 break;
18073 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018074 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018075 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018076 return nr;
18077}
18078#endif
18079
18080/*
18081 * "tabpagewinnr()" function
18082 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018083 static void
18084f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018085 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018086 typval_T *rettv;
18087{
18088 int nr = 1;
18089#ifdef FEAT_WINDOWS
18090 tabpage_T *tp;
18091
18092 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18093 if (tp == NULL)
18094 nr = 0;
18095 else
18096 nr = get_winnr(tp, &argvars[1]);
18097#endif
18098 rettv->vval.v_number = nr;
18099}
18100
18101
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018102/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018103 * "tagfiles()" function
18104 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018105 static void
18106f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018107 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018108 typval_T *rettv;
18109{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018110 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018111 tagname_T tn;
18112 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018113
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018114 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018115 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018116 fname = alloc(MAXPATHL);
18117 if (fname == NULL)
18118 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018119
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018120 for (first = TRUE; ; first = FALSE)
18121 if (get_tagfname(&tn, first, fname) == FAIL
18122 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018123 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018124 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018125 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018126}
18127
18128/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018129 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018130 */
18131 static void
18132f_taglist(argvars, rettv)
18133 typval_T *argvars;
18134 typval_T *rettv;
18135{
18136 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018137
18138 tag_pattern = get_tv_string(&argvars[0]);
18139
18140 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018141 if (*tag_pattern == NUL)
18142 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018143
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018144 if (rettv_list_alloc(rettv) == OK)
18145 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018146}
18147
18148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 * "tempname()" function
18150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018152f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018153 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155{
18156 static int x = 'A';
18157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018158 rettv->v_type = VAR_STRING;
18159 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160
18161 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18162 * names. Skip 'I' and 'O', they are used for shell redirection. */
18163 do
18164 {
18165 if (x == 'Z')
18166 x = '0';
18167 else if (x == '9')
18168 x = 'A';
18169 else
18170 {
18171#ifdef EBCDIC
18172 if (x == 'I')
18173 x = 'J';
18174 else if (x == 'R')
18175 x = 'S';
18176 else
18177#endif
18178 ++x;
18179 }
18180 } while (x == 'I' || x == 'O');
18181}
18182
18183/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018184 * "test(list)" function: Just checking the walls...
18185 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018186 static void
18187f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018188 typval_T *argvars UNUSED;
18189 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018190{
18191 /* Used for unit testing. Change the code below to your liking. */
18192#if 0
18193 listitem_T *li;
18194 list_T *l;
18195 char_u *bad, *good;
18196
18197 if (argvars[0].v_type != VAR_LIST)
18198 return;
18199 l = argvars[0].vval.v_list;
18200 if (l == NULL)
18201 return;
18202 li = l->lv_first;
18203 if (li == NULL)
18204 return;
18205 bad = get_tv_string(&li->li_tv);
18206 li = li->li_next;
18207 if (li == NULL)
18208 return;
18209 good = get_tv_string(&li->li_tv);
18210 rettv->vval.v_number = test_edit_score(bad, good);
18211#endif
18212}
18213
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018214#ifdef FEAT_FLOAT
18215/*
18216 * "tan()" function
18217 */
18218 static void
18219f_tan(argvars, rettv)
18220 typval_T *argvars;
18221 typval_T *rettv;
18222{
18223 float_T f;
18224
18225 rettv->v_type = VAR_FLOAT;
18226 if (get_float_arg(argvars, &f) == OK)
18227 rettv->vval.v_float = tan(f);
18228 else
18229 rettv->vval.v_float = 0.0;
18230}
18231
18232/*
18233 * "tanh()" function
18234 */
18235 static void
18236f_tanh(argvars, rettv)
18237 typval_T *argvars;
18238 typval_T *rettv;
18239{
18240 float_T f;
18241
18242 rettv->v_type = VAR_FLOAT;
18243 if (get_float_arg(argvars, &f) == OK)
18244 rettv->vval.v_float = tanh(f);
18245 else
18246 rettv->vval.v_float = 0.0;
18247}
18248#endif
18249
Bram Moolenaard52d9742005-08-21 22:20:28 +000018250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 * "tolower(string)" function
18252 */
18253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018254f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018255 typval_T *argvars;
18256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257{
18258 char_u *p;
18259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018260 p = vim_strsave(get_tv_string(&argvars[0]));
18261 rettv->v_type = VAR_STRING;
18262 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263
18264 if (p != NULL)
18265 while (*p != NUL)
18266 {
18267#ifdef FEAT_MBYTE
18268 int l;
18269
18270 if (enc_utf8)
18271 {
18272 int c, lc;
18273
18274 c = utf_ptr2char(p);
18275 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018276 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277 /* TODO: reallocate string when byte count changes. */
18278 if (utf_char2len(lc) == l)
18279 utf_char2bytes(lc, p);
18280 p += l;
18281 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018282 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 p += l; /* skip multi-byte character */
18284 else
18285#endif
18286 {
18287 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18288 ++p;
18289 }
18290 }
18291}
18292
18293/*
18294 * "toupper(string)" function
18295 */
18296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018297f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018298 typval_T *argvars;
18299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018301 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018302 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303}
18304
18305/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018306 * "tr(string, fromstr, tostr)" function
18307 */
18308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018309f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018310 typval_T *argvars;
18311 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018312{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018313 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018314 char_u *fromstr;
18315 char_u *tostr;
18316 char_u *p;
18317#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018318 int inlen;
18319 int fromlen;
18320 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018321 int idx;
18322 char_u *cpstr;
18323 int cplen;
18324 int first = TRUE;
18325#endif
18326 char_u buf[NUMBUFLEN];
18327 char_u buf2[NUMBUFLEN];
18328 garray_T ga;
18329
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018330 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018331 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18332 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018333
18334 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018335 rettv->v_type = VAR_STRING;
18336 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018337 if (fromstr == NULL || tostr == NULL)
18338 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018339 ga_init2(&ga, (int)sizeof(char), 80);
18340
18341#ifdef FEAT_MBYTE
18342 if (!has_mbyte)
18343#endif
18344 /* not multi-byte: fromstr and tostr must be the same length */
18345 if (STRLEN(fromstr) != STRLEN(tostr))
18346 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018347#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018348error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018349#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018350 EMSG2(_(e_invarg2), fromstr);
18351 ga_clear(&ga);
18352 return;
18353 }
18354
18355 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018356 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018357 {
18358#ifdef FEAT_MBYTE
18359 if (has_mbyte)
18360 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018361 inlen = (*mb_ptr2len)(in_str);
18362 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018363 cplen = inlen;
18364 idx = 0;
18365 for (p = fromstr; *p != NUL; p += fromlen)
18366 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018367 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018368 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018369 {
18370 for (p = tostr; *p != NUL; p += tolen)
18371 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018372 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018373 if (idx-- == 0)
18374 {
18375 cplen = tolen;
18376 cpstr = p;
18377 break;
18378 }
18379 }
18380 if (*p == NUL) /* tostr is shorter than fromstr */
18381 goto error;
18382 break;
18383 }
18384 ++idx;
18385 }
18386
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018387 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018388 {
18389 /* Check that fromstr and tostr have the same number of
18390 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018391 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018392 first = FALSE;
18393 for (p = tostr; *p != NUL; p += tolen)
18394 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018395 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018396 --idx;
18397 }
18398 if (idx != 0)
18399 goto error;
18400 }
18401
18402 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018403 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018404 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018405
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018406 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018407 }
18408 else
18409#endif
18410 {
18411 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018412 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018413 if (p != NULL)
18414 ga_append(&ga, tostr[p - fromstr]);
18415 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018416 ga_append(&ga, *in_str);
18417 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018418 }
18419 }
18420
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018421 /* add a terminating NUL */
18422 ga_grow(&ga, 1);
18423 ga_append(&ga, NUL);
18424
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018425 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018426}
18427
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018428#ifdef FEAT_FLOAT
18429/*
18430 * "trunc({float})" function
18431 */
18432 static void
18433f_trunc(argvars, rettv)
18434 typval_T *argvars;
18435 typval_T *rettv;
18436{
18437 float_T f;
18438
18439 rettv->v_type = VAR_FLOAT;
18440 if (get_float_arg(argvars, &f) == OK)
18441 /* trunc() is not in C90, use floor() or ceil() instead. */
18442 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18443 else
18444 rettv->vval.v_float = 0.0;
18445}
18446#endif
18447
Bram Moolenaar8299df92004-07-10 09:47:34 +000018448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 * "type(expr)" function
18450 */
18451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018452f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018453 typval_T *argvars;
18454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018456 int n;
18457
18458 switch (argvars[0].v_type)
18459 {
18460 case VAR_NUMBER: n = 0; break;
18461 case VAR_STRING: n = 1; break;
18462 case VAR_FUNC: n = 2; break;
18463 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018464 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018465#ifdef FEAT_FLOAT
18466 case VAR_FLOAT: n = 5; break;
18467#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018468 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18469 }
18470 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471}
18472
18473/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018474 * "undofile(name)" function
18475 */
18476 static void
18477f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018478 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018479 typval_T *rettv;
18480{
18481 rettv->v_type = VAR_STRING;
18482#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018483 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018484 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018485
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018486 if (*fname == NUL)
18487 {
18488 /* If there is no file name there will be no undo file. */
18489 rettv->vval.v_string = NULL;
18490 }
18491 else
18492 {
18493 char_u *ffname = FullName_save(fname, FALSE);
18494
18495 if (ffname != NULL)
18496 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18497 vim_free(ffname);
18498 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018499 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018500#else
18501 rettv->vval.v_string = NULL;
18502#endif
18503}
18504
18505/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018506 * "undotree()" function
18507 */
18508 static void
18509f_undotree(argvars, rettv)
18510 typval_T *argvars UNUSED;
18511 typval_T *rettv;
18512{
18513 if (rettv_dict_alloc(rettv) == OK)
18514 {
18515 dict_T *dict = rettv->vval.v_dict;
18516 list_T *list;
18517
Bram Moolenaar730cde92010-06-27 05:18:54 +020018518 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018519 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018520 dict_add_nr_str(dict, "save_last",
18521 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018522 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18523 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018524 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018525
18526 list = list_alloc();
18527 if (list != NULL)
18528 {
18529 u_eval_tree(curbuf->b_u_oldhead, list);
18530 dict_add_list(dict, "entries", list);
18531 }
18532 }
18533}
18534
18535/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018536 * "values(dict)" function
18537 */
18538 static void
18539f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018540 typval_T *argvars;
18541 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018542{
18543 dict_list(argvars, rettv, 1);
18544}
18545
18546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 * "virtcol(string)" function
18548 */
18549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018550f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018551 typval_T *argvars;
18552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553{
18554 colnr_T vcol = 0;
18555 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018556 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018558 fp = var2fpos(&argvars[0], FALSE, &fnum);
18559 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18560 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 {
18562 getvvcol(curwin, fp, NULL, NULL, &vcol);
18563 ++vcol;
18564 }
18565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567}
18568
18569/*
18570 * "visualmode()" function
18571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018574 typval_T *argvars UNUSED;
18575 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576{
18577#ifdef FEAT_VISUAL
18578 char_u str[2];
18579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018580 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 str[0] = curbuf->b_visual_mode_eval;
18582 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584
18585 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018586 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588#endif
18589}
18590
18591/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018592 * "wildmenumode()" function
18593 */
18594 static void
18595f_wildmenumode(argvars, rettv)
18596 typval_T *argvars UNUSED;
18597 typval_T *rettv UNUSED;
18598{
18599#ifdef FEAT_WILDMENU
18600 if (wild_menu_showing)
18601 rettv->vval.v_number = 1;
18602#endif
18603}
18604
18605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 * "winbufnr(nr)" function
18607 */
18608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018609f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018610 typval_T *argvars;
18611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612{
18613 win_T *wp;
18614
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018615 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018619 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620}
18621
18622/*
18623 * "wincol()" function
18624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018627 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629{
18630 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018631 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632}
18633
18634/*
18635 * "winheight(nr)" function
18636 */
18637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018638f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018639 typval_T *argvars;
18640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641{
18642 win_T *wp;
18643
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018644 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018648 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649}
18650
18651/*
18652 * "winline()" function
18653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018655f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018656 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658{
18659 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661}
18662
18663/*
18664 * "winnr()" function
18665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018667f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018668 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670{
18671 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018672
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018674 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677}
18678
18679/*
18680 * "winrestcmd()" function
18681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018684 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686{
18687#ifdef FEAT_WINDOWS
18688 win_T *wp;
18689 int winnr = 1;
18690 garray_T ga;
18691 char_u buf[50];
18692
18693 ga_init2(&ga, (int)sizeof(char), 70);
18694 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18695 {
18696 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18697 ga_concat(&ga, buf);
18698# ifdef FEAT_VERTSPLIT
18699 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18700 ga_concat(&ga, buf);
18701# endif
18702 ++winnr;
18703 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018704 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018708 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018710 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711}
18712
18713/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018714 * "winrestview()" function
18715 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018716 static void
18717f_winrestview(argvars, rettv)
18718 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018719 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018720{
18721 dict_T *dict;
18722
18723 if (argvars[0].v_type != VAR_DICT
18724 || (dict = argvars[0].vval.v_dict) == NULL)
18725 EMSG(_(e_invarg));
18726 else
18727 {
18728 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18729 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18730#ifdef FEAT_VIRTUALEDIT
18731 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18732#endif
18733 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018734 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018735
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018736 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018737#ifdef FEAT_DIFF
18738 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18739#endif
18740 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18741 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18742
18743 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018744 win_new_height(curwin, curwin->w_height);
18745# ifdef FEAT_VERTSPLIT
18746 win_new_width(curwin, W_WIDTH(curwin));
18747# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018748 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018749
18750 if (curwin->w_topline == 0)
18751 curwin->w_topline = 1;
18752 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18753 curwin->w_topline = curbuf->b_ml.ml_line_count;
18754#ifdef FEAT_DIFF
18755 check_topfill(curwin, TRUE);
18756#endif
18757 }
18758}
18759
18760/*
18761 * "winsaveview()" function
18762 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018763 static void
18764f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018765 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018766 typval_T *rettv;
18767{
18768 dict_T *dict;
18769
Bram Moolenaara800b422010-06-27 01:15:55 +020018770 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018771 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018772 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018773
18774 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18775 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18776#ifdef FEAT_VIRTUALEDIT
18777 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18778#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018779 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018780 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18781
18782 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18783#ifdef FEAT_DIFF
18784 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18785#endif
18786 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18787 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18788}
18789
18790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 * "winwidth(nr)" function
18792 */
18793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018794f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018795 typval_T *argvars;
18796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797{
18798 win_T *wp;
18799
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018800 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 else
18804#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018807 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808#endif
18809}
18810
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018812 * "writefile()" function
18813 */
18814 static void
18815f_writefile(argvars, rettv)
18816 typval_T *argvars;
18817 typval_T *rettv;
18818{
18819 int binary = FALSE;
18820 char_u *fname;
18821 FILE *fd;
18822 listitem_T *li;
18823 char_u *s;
18824 int ret = 0;
18825 int c;
18826
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018827 if (check_restricted() || check_secure())
18828 return;
18829
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018830 if (argvars[0].v_type != VAR_LIST)
18831 {
18832 EMSG2(_(e_listarg), "writefile()");
18833 return;
18834 }
18835 if (argvars[0].vval.v_list == NULL)
18836 return;
18837
18838 if (argvars[2].v_type != VAR_UNKNOWN
18839 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18840 binary = TRUE;
18841
18842 /* Always open the file in binary mode, library functions have a mind of
18843 * their own about CR-LF conversion. */
18844 fname = get_tv_string(&argvars[1]);
18845 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18846 {
18847 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18848 ret = -1;
18849 }
18850 else
18851 {
18852 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18853 li = li->li_next)
18854 {
18855 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18856 {
18857 if (*s == '\n')
18858 c = putc(NUL, fd);
18859 else
18860 c = putc(*s, fd);
18861 if (c == EOF)
18862 {
18863 ret = -1;
18864 break;
18865 }
18866 }
18867 if (!binary || li->li_next != NULL)
18868 if (putc('\n', fd) == EOF)
18869 {
18870 ret = -1;
18871 break;
18872 }
18873 if (ret < 0)
18874 {
18875 EMSG(_(e_write));
18876 break;
18877 }
18878 }
18879 fclose(fd);
18880 }
18881
18882 rettv->vval.v_number = ret;
18883}
18884
18885/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018886 * "xor(expr, expr)" function
18887 */
18888 static void
18889f_xor(argvars, rettv)
18890 typval_T *argvars;
18891 typval_T *rettv;
18892{
18893 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18894 ^ get_tv_number_chk(&argvars[1], NULL);
18895}
18896
18897
18898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018900 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 */
18902 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018903var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018904 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018905 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018906 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018908 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018910 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911
Bram Moolenaara5525202006-03-02 22:52:09 +000018912 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018913 if (varp->v_type == VAR_LIST)
18914 {
18915 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018916 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018917 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018918 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018919
18920 l = varp->vval.v_list;
18921 if (l == NULL)
18922 return NULL;
18923
18924 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018925 pos.lnum = list_find_nr(l, 0L, &error);
18926 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018927 return NULL; /* invalid line number */
18928
18929 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018930 pos.col = list_find_nr(l, 1L, &error);
18931 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018932 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018933 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018934
18935 /* We accept "$" for the column number: last column. */
18936 li = list_find(l, 1L);
18937 if (li != NULL && li->li_tv.v_type == VAR_STRING
18938 && li->li_tv.vval.v_string != NULL
18939 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18940 pos.col = len + 1;
18941
Bram Moolenaara5525202006-03-02 22:52:09 +000018942 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018943 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018944 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018945 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018946
Bram Moolenaara5525202006-03-02 22:52:09 +000018947#ifdef FEAT_VIRTUALEDIT
18948 /* Get the virtual offset. Defaults to zero. */
18949 pos.coladd = list_find_nr(l, 2L, &error);
18950 if (error)
18951 pos.coladd = 0;
18952#endif
18953
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018954 return &pos;
18955 }
18956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018957 name = get_tv_string_chk(varp);
18958 if (name == NULL)
18959 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018960 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018962#ifdef FEAT_VISUAL
18963 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18964 {
18965 if (VIsual_active)
18966 return &VIsual;
18967 return &curwin->w_cursor;
18968 }
18969#endif
18970 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018972 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18974 return NULL;
18975 return pp;
18976 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018977
18978#ifdef FEAT_VIRTUALEDIT
18979 pos.coladd = 0;
18980#endif
18981
Bram Moolenaar477933c2007-07-17 14:32:23 +000018982 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018983 {
18984 pos.col = 0;
18985 if (name[1] == '0') /* "w0": first visible line */
18986 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018987 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018988 pos.lnum = curwin->w_topline;
18989 return &pos;
18990 }
18991 else if (name[1] == '$') /* "w$": last visible line */
18992 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018993 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018994 pos.lnum = curwin->w_botline - 1;
18995 return &pos;
18996 }
18997 }
18998 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019000 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 {
19002 pos.lnum = curbuf->b_ml.ml_line_count;
19003 pos.col = 0;
19004 }
19005 else
19006 {
19007 pos.lnum = curwin->w_cursor.lnum;
19008 pos.col = (colnr_T)STRLEN(ml_get_curline());
19009 }
19010 return &pos;
19011 }
19012 return NULL;
19013}
19014
19015/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019016 * Convert list in "arg" into a position and optional file number.
19017 * When "fnump" is NULL there is no file number, only 3 items.
19018 * Note that the column is passed on as-is, the caller may want to decrement
19019 * it to use 1 for the first column.
19020 * Return FAIL when conversion is not possible, doesn't check the position for
19021 * validity.
19022 */
19023 static int
19024list2fpos(arg, posp, fnump)
19025 typval_T *arg;
19026 pos_T *posp;
19027 int *fnump;
19028{
19029 list_T *l = arg->vval.v_list;
19030 long i = 0;
19031 long n;
19032
Bram Moolenaarbde35262006-07-23 20:12:24 +000019033 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19034 * when "fnump" isn't NULL and "coladd" is optional. */
19035 if (arg->v_type != VAR_LIST
19036 || l == NULL
19037 || l->lv_len < (fnump == NULL ? 2 : 3)
19038 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019039 return FAIL;
19040
19041 if (fnump != NULL)
19042 {
19043 n = list_find_nr(l, i++, NULL); /* fnum */
19044 if (n < 0)
19045 return FAIL;
19046 if (n == 0)
19047 n = curbuf->b_fnum; /* current buffer */
19048 *fnump = n;
19049 }
19050
19051 n = list_find_nr(l, i++, NULL); /* lnum */
19052 if (n < 0)
19053 return FAIL;
19054 posp->lnum = n;
19055
19056 n = list_find_nr(l, i++, NULL); /* col */
19057 if (n < 0)
19058 return FAIL;
19059 posp->col = n;
19060
19061#ifdef FEAT_VIRTUALEDIT
19062 n = list_find_nr(l, i, NULL);
19063 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019064 posp->coladd = 0;
19065 else
19066 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019067#endif
19068
19069 return OK;
19070}
19071
19072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 * Get the length of an environment variable name.
19074 * Advance "arg" to the first character after the name.
19075 * Return 0 for error.
19076 */
19077 static int
19078get_env_len(arg)
19079 char_u **arg;
19080{
19081 char_u *p;
19082 int len;
19083
19084 for (p = *arg; vim_isIDc(*p); ++p)
19085 ;
19086 if (p == *arg) /* no name found */
19087 return 0;
19088
19089 len = (int)(p - *arg);
19090 *arg = p;
19091 return len;
19092}
19093
19094/*
19095 * Get the length of the name of a function or internal variable.
19096 * "arg" is advanced to the first non-white character after the name.
19097 * Return 0 if something is wrong.
19098 */
19099 static int
19100get_id_len(arg)
19101 char_u **arg;
19102{
19103 char_u *p;
19104 int len;
19105
19106 /* Find the end of the name. */
19107 for (p = *arg; eval_isnamec(*p); ++p)
19108 ;
19109 if (p == *arg) /* no name found */
19110 return 0;
19111
19112 len = (int)(p - *arg);
19113 *arg = skipwhite(p);
19114
19115 return len;
19116}
19117
19118/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019119 * Get the length of the name of a variable or function.
19120 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019122 * Return -1 if curly braces expansion failed.
19123 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 * If the name contains 'magic' {}'s, expand them and return the
19125 * expanded name in an allocated string via 'alias' - caller must free.
19126 */
19127 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019128get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 char_u **arg;
19130 char_u **alias;
19131 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019132 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133{
19134 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 char_u *p;
19136 char_u *expr_start;
19137 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138
19139 *alias = NULL; /* default to no alias */
19140
19141 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19142 && (*arg)[2] == (int)KE_SNR)
19143 {
19144 /* hard coded <SNR>, already translated */
19145 *arg += 3;
19146 return get_id_len(arg) + 3;
19147 }
19148 len = eval_fname_script(*arg);
19149 if (len > 0)
19150 {
19151 /* literal "<SID>", "s:" or "<SNR>" */
19152 *arg += len;
19153 }
19154
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019156 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019158 p = find_name_end(*arg, &expr_start, &expr_end,
19159 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 if (expr_start != NULL)
19161 {
19162 char_u *temp_string;
19163
19164 if (!evaluate)
19165 {
19166 len += (int)(p - *arg);
19167 *arg = skipwhite(p);
19168 return len;
19169 }
19170
19171 /*
19172 * Include any <SID> etc in the expanded string:
19173 * Thus the -len here.
19174 */
19175 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19176 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019177 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 *alias = temp_string;
19179 *arg = skipwhite(p);
19180 return (int)STRLEN(temp_string);
19181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182
19183 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019184 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 EMSG2(_(e_invexpr2), *arg);
19186
19187 return len;
19188}
19189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019190/*
19191 * Find the end of a variable or function name, taking care of magic braces.
19192 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19193 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019194 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019195 * Return a pointer to just after the name. Equal to "arg" if there is no
19196 * valid name.
19197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019199find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 char_u *arg;
19201 char_u **expr_start;
19202 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019203 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019205 int mb_nest = 0;
19206 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 char_u *p;
19208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019209 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211 *expr_start = NULL;
19212 *expr_end = NULL;
19213 }
19214
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019215 /* Quick check for valid starting character. */
19216 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19217 return arg;
19218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219 for (p = arg; *p != NUL
19220 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019221 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019222 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019223 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019224 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019225 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019226 if (*p == '\'')
19227 {
19228 /* skip over 'string' to avoid counting [ and ] inside it. */
19229 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19230 ;
19231 if (*p == NUL)
19232 break;
19233 }
19234 else if (*p == '"')
19235 {
19236 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19237 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19238 if (*p == '\\' && p[1] != NUL)
19239 ++p;
19240 if (*p == NUL)
19241 break;
19242 }
19243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019246 if (*p == '[')
19247 ++br_nest;
19248 else if (*p == ']')
19249 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 if (*p == '{')
19255 {
19256 mb_nest++;
19257 if (expr_start != NULL && *expr_start == NULL)
19258 *expr_start = p;
19259 }
19260 else if (*p == '}')
19261 {
19262 mb_nest--;
19263 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19264 *expr_end = p;
19265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267 }
19268
19269 return p;
19270}
19271
19272/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019273 * Expands out the 'magic' {}'s in a variable/function name.
19274 * Note that this can call itself recursively, to deal with
19275 * constructs like foo{bar}{baz}{bam}
19276 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19277 * "in_start" ^
19278 * "expr_start" ^
19279 * "expr_end" ^
19280 * "in_end" ^
19281 *
19282 * Returns a new allocated string, which the caller must free.
19283 * Returns NULL for failure.
19284 */
19285 static char_u *
19286make_expanded_name(in_start, expr_start, expr_end, in_end)
19287 char_u *in_start;
19288 char_u *expr_start;
19289 char_u *expr_end;
19290 char_u *in_end;
19291{
19292 char_u c1;
19293 char_u *retval = NULL;
19294 char_u *temp_result;
19295 char_u *nextcmd = NULL;
19296
19297 if (expr_end == NULL || in_end == NULL)
19298 return NULL;
19299 *expr_start = NUL;
19300 *expr_end = NUL;
19301 c1 = *in_end;
19302 *in_end = NUL;
19303
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019304 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019305 if (temp_result != NULL && nextcmd == NULL)
19306 {
19307 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19308 + (in_end - expr_end) + 1));
19309 if (retval != NULL)
19310 {
19311 STRCPY(retval, in_start);
19312 STRCAT(retval, temp_result);
19313 STRCAT(retval, expr_end + 1);
19314 }
19315 }
19316 vim_free(temp_result);
19317
19318 *in_end = c1; /* put char back for error messages */
19319 *expr_start = '{';
19320 *expr_end = '}';
19321
19322 if (retval != NULL)
19323 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019324 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019325 if (expr_start != NULL)
19326 {
19327 /* Further expansion! */
19328 temp_result = make_expanded_name(retval, expr_start,
19329 expr_end, temp_result);
19330 vim_free(retval);
19331 retval = temp_result;
19332 }
19333 }
19334
19335 return retval;
19336}
19337
19338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019340 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 */
19342 static int
19343eval_isnamec(c)
19344 int c;
19345{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019346 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19347}
19348
19349/*
19350 * Return TRUE if character "c" can be used as the first character in a
19351 * variable or function name (excluding '{' and '}').
19352 */
19353 static int
19354eval_isnamec1(c)
19355 int c;
19356{
19357 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358}
19359
19360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361 * Set number v: variable to "val".
19362 */
19363 void
19364set_vim_var_nr(idx, val)
19365 int idx;
19366 long val;
19367{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019368 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369}
19370
19371/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019372 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 */
19374 long
19375get_vim_var_nr(idx)
19376 int idx;
19377{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019378 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379}
19380
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019381/*
19382 * Get string v: variable value. Uses a static buffer, can only be used once.
19383 */
19384 char_u *
19385get_vim_var_str(idx)
19386 int idx;
19387{
19388 return get_tv_string(&vimvars[idx].vv_tv);
19389}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019390
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019392 * Get List v: variable value. Caller must take care of reference count when
19393 * needed.
19394 */
19395 list_T *
19396get_vim_var_list(idx)
19397 int idx;
19398{
19399 return vimvars[idx].vv_list;
19400}
19401
19402/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019403 * Set v:char to character "c".
19404 */
19405 void
19406set_vim_var_char(c)
19407 int c;
19408{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019409 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019410
19411#ifdef FEAT_MBYTE
19412 if (has_mbyte)
19413 buf[(*mb_char2bytes)(c, buf)] = NUL;
19414 else
19415#endif
19416 {
19417 buf[0] = c;
19418 buf[1] = NUL;
19419 }
19420 set_vim_var_string(VV_CHAR, buf, -1);
19421}
19422
19423/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019424 * Set v:count to "count" and v:count1 to "count1".
19425 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 */
19427 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019428set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 long count;
19430 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019431 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019433 if (set_prevcount)
19434 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019435 vimvars[VV_COUNT].vv_nr = count;
19436 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437}
19438
19439/*
19440 * Set string v: variable to a copy of "val".
19441 */
19442 void
19443set_vim_var_string(idx, val, len)
19444 int idx;
19445 char_u *val;
19446 int len; /* length of "val" to use or -1 (whole string) */
19447{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019448 /* Need to do this (at least) once, since we can't initialize a union.
19449 * Will always be invoked when "v:progname" is set. */
19450 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19451
Bram Moolenaare9a41262005-01-15 22:18:47 +000019452 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019454 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019456 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019458 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459}
19460
19461/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019462 * Set List v: variable to "val".
19463 */
19464 void
19465set_vim_var_list(idx, val)
19466 int idx;
19467 list_T *val;
19468{
19469 list_unref(vimvars[idx].vv_list);
19470 vimvars[idx].vv_list = val;
19471 if (val != NULL)
19472 ++val->lv_refcount;
19473}
19474
19475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 * Set v:register if needed.
19477 */
19478 void
19479set_reg_var(c)
19480 int c;
19481{
19482 char_u regname;
19483
19484 if (c == 0 || c == ' ')
19485 regname = '"';
19486 else
19487 regname = c;
19488 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019489 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 set_vim_var_string(VV_REG, &regname, 1);
19491}
19492
19493/*
19494 * Get or set v:exception. If "oldval" == NULL, return the current value.
19495 * Otherwise, restore the value to "oldval" and return NULL.
19496 * Must always be called in pairs to save and restore v:exception! Does not
19497 * take care of memory allocations.
19498 */
19499 char_u *
19500v_exception(oldval)
19501 char_u *oldval;
19502{
19503 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019504 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505
Bram Moolenaare9a41262005-01-15 22:18:47 +000019506 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507 return NULL;
19508}
19509
19510/*
19511 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19512 * Otherwise, restore the value to "oldval" and return NULL.
19513 * Must always be called in pairs to save and restore v:throwpoint! Does not
19514 * take care of memory allocations.
19515 */
19516 char_u *
19517v_throwpoint(oldval)
19518 char_u *oldval;
19519{
19520 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019521 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522
Bram Moolenaare9a41262005-01-15 22:18:47 +000019523 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 return NULL;
19525}
19526
19527#if defined(FEAT_AUTOCMD) || defined(PROTO)
19528/*
19529 * Set v:cmdarg.
19530 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19531 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19532 * Must always be called in pairs!
19533 */
19534 char_u *
19535set_cmdarg(eap, oldarg)
19536 exarg_T *eap;
19537 char_u *oldarg;
19538{
19539 char_u *oldval;
19540 char_u *newval;
19541 unsigned len;
19542
Bram Moolenaare9a41262005-01-15 22:18:47 +000019543 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019544 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019546 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019547 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019548 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 }
19550
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019551 if (eap->force_bin == FORCE_BIN)
19552 len = 6;
19553 else if (eap->force_bin == FORCE_NOBIN)
19554 len = 8;
19555 else
19556 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019557
19558 if (eap->read_edit)
19559 len += 7;
19560
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019561 if (eap->force_ff != 0)
19562 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19563# ifdef FEAT_MBYTE
19564 if (eap->force_enc != 0)
19565 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019566 if (eap->bad_char != 0)
19567 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019568# endif
19569
19570 newval = alloc(len + 1);
19571 if (newval == NULL)
19572 return NULL;
19573
19574 if (eap->force_bin == FORCE_BIN)
19575 sprintf((char *)newval, " ++bin");
19576 else if (eap->force_bin == FORCE_NOBIN)
19577 sprintf((char *)newval, " ++nobin");
19578 else
19579 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019580
19581 if (eap->read_edit)
19582 STRCAT(newval, " ++edit");
19583
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019584 if (eap->force_ff != 0)
19585 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19586 eap->cmd + eap->force_ff);
19587# ifdef FEAT_MBYTE
19588 if (eap->force_enc != 0)
19589 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19590 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019591 if (eap->bad_char == BAD_KEEP)
19592 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19593 else if (eap->bad_char == BAD_DROP)
19594 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19595 else if (eap->bad_char != 0)
19596 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019597# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019598 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019599 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600}
19601#endif
19602
19603/*
19604 * Get the value of internal variable "name".
19605 * Return OK or FAIL.
19606 */
19607 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019608get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 char_u *name;
19610 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019611 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019612 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613{
19614 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 typval_T *tv = NULL;
19616 typval_T atv;
19617 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619
19620 /* truncate the name, so that we can use strcmp() */
19621 cc = name[len];
19622 name[len] = NUL;
19623
19624 /*
19625 * Check for "b:changedtick".
19626 */
19627 if (STRCMP(name, "b:changedtick") == 0)
19628 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019629 atv.v_type = VAR_NUMBER;
19630 atv.vval.v_number = curbuf->b_changedtick;
19631 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 }
19633
19634 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 * Check for user-defined variables.
19636 */
19637 else
19638 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019639 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 }
19643
Bram Moolenaare9a41262005-01-15 22:18:47 +000019644 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019646 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019647 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 ret = FAIL;
19649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019650 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019651 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652
19653 name[len] = cc;
19654
19655 return ret;
19656}
19657
19658/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019659 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19660 * Also handle function call with Funcref variable: func(expr)
19661 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19662 */
19663 static int
19664handle_subscript(arg, rettv, evaluate, verbose)
19665 char_u **arg;
19666 typval_T *rettv;
19667 int evaluate; /* do more than finding the end */
19668 int verbose; /* give error messages */
19669{
19670 int ret = OK;
19671 dict_T *selfdict = NULL;
19672 char_u *s;
19673 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019674 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019675
19676 while (ret == OK
19677 && (**arg == '['
19678 || (**arg == '.' && rettv->v_type == VAR_DICT)
19679 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19680 && !vim_iswhite(*(*arg - 1)))
19681 {
19682 if (**arg == '(')
19683 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019684 /* need to copy the funcref so that we can clear rettv */
19685 functv = *rettv;
19686 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019687
19688 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019689 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019690 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019691 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19692 &len, evaluate, selfdict);
19693
19694 /* Clear the funcref afterwards, so that deleting it while
19695 * evaluating the arguments is possible (see test55). */
19696 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019697
19698 /* Stop the expression evaluation when immediately aborting on
19699 * error, or when an interrupt occurred or an exception was thrown
19700 * but not caught. */
19701 if (aborting())
19702 {
19703 if (ret == OK)
19704 clear_tv(rettv);
19705 ret = FAIL;
19706 }
19707 dict_unref(selfdict);
19708 selfdict = NULL;
19709 }
19710 else /* **arg == '[' || **arg == '.' */
19711 {
19712 dict_unref(selfdict);
19713 if (rettv->v_type == VAR_DICT)
19714 {
19715 selfdict = rettv->vval.v_dict;
19716 if (selfdict != NULL)
19717 ++selfdict->dv_refcount;
19718 }
19719 else
19720 selfdict = NULL;
19721 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19722 {
19723 clear_tv(rettv);
19724 ret = FAIL;
19725 }
19726 }
19727 }
19728 dict_unref(selfdict);
19729 return ret;
19730}
19731
19732/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019733 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019734 * value).
19735 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019736 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019737alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019738{
Bram Moolenaar33570922005-01-25 22:26:29 +000019739 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019740}
19741
19742/*
19743 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 * The string "s" must have been allocated, it is consumed.
19745 * Return NULL for out of memory, the variable otherwise.
19746 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019748alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 char_u *s;
19750{
Bram Moolenaar33570922005-01-25 22:26:29 +000019751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019753 rettv = alloc_tv();
19754 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 rettv->v_type = VAR_STRING;
19757 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 }
19759 else
19760 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019761 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762}
19763
19764/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019765 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019767 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019769 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770{
19771 if (varp != NULL)
19772 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019773 switch (varp->v_type)
19774 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019775 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019776 func_unref(varp->vval.v_string);
19777 /*FALLTHROUGH*/
19778 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019779 vim_free(varp->vval.v_string);
19780 break;
19781 case VAR_LIST:
19782 list_unref(varp->vval.v_list);
19783 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019784 case VAR_DICT:
19785 dict_unref(varp->vval.v_dict);
19786 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019787 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019788#ifdef FEAT_FLOAT
19789 case VAR_FLOAT:
19790#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019791 case VAR_UNKNOWN:
19792 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019793 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019794 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019795 break;
19796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 vim_free(varp);
19798 }
19799}
19800
19801/*
19802 * Free the memory for a variable value and set the value to NULL or 0.
19803 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019804 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807{
19808 if (varp != NULL)
19809 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019810 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019812 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019813 func_unref(varp->vval.v_string);
19814 /*FALLTHROUGH*/
19815 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019816 vim_free(varp->vval.v_string);
19817 varp->vval.v_string = NULL;
19818 break;
19819 case VAR_LIST:
19820 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019821 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019822 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019823 case VAR_DICT:
19824 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019825 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019826 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019827 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019828 varp->vval.v_number = 0;
19829 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019830#ifdef FEAT_FLOAT
19831 case VAR_FLOAT:
19832 varp->vval.v_float = 0.0;
19833 break;
19834#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835 case VAR_UNKNOWN:
19836 break;
19837 default:
19838 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019840 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 }
19842}
19843
19844/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019845 * Set the value of a variable to NULL without freeing items.
19846 */
19847 static void
19848init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019850{
19851 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019852 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019853}
19854
19855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 * Get the number value of a variable.
19857 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019858 * For incompatible types, return 0.
19859 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19860 * caller of incompatible types: it sets *denote to TRUE if "denote"
19861 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 */
19863 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019864get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019865 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019867 int error = FALSE;
19868
19869 return get_tv_number_chk(varp, &error); /* return 0L on error */
19870}
19871
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019872 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019873get_tv_number_chk(varp, denote)
19874 typval_T *varp;
19875 int *denote;
19876{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019877 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019879 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019881 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019882 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019883#ifdef FEAT_FLOAT
19884 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019885 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019886 break;
19887#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019888 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019889 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019890 break;
19891 case VAR_STRING:
19892 if (varp->vval.v_string != NULL)
19893 vim_str2nr(varp->vval.v_string, NULL, NULL,
19894 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019895 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019896 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019897 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019898 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019899 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019900 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019901 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019902 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019903 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019906 if (denote == NULL) /* useful for values that must be unsigned */
19907 n = -1;
19908 else
19909 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019910 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911}
19912
19913/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019914 * Get the lnum from the first argument.
19915 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019916 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 */
19918 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019919get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019920 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921{
Bram Moolenaar33570922005-01-25 22:26:29 +000019922 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 linenr_T lnum;
19924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019925 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 if (lnum == 0) /* no valid number, try using line() */
19927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019928 rettv.v_type = VAR_NUMBER;
19929 f_line(argvars, &rettv);
19930 lnum = rettv.vval.v_number;
19931 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 }
19933 return lnum;
19934}
19935
19936/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019937 * Get the lnum from the first argument.
19938 * Also accepts "$", then "buf" is used.
19939 * Returns 0 on error.
19940 */
19941 static linenr_T
19942get_tv_lnum_buf(argvars, buf)
19943 typval_T *argvars;
19944 buf_T *buf;
19945{
19946 if (argvars[0].v_type == VAR_STRING
19947 && argvars[0].vval.v_string != NULL
19948 && argvars[0].vval.v_string[0] == '$'
19949 && buf != NULL)
19950 return buf->b_ml.ml_line_count;
19951 return get_tv_number_chk(&argvars[0], NULL);
19952}
19953
19954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 * Get the string value of a variable.
19956 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019957 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19958 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 * If the String variable has never been set, return an empty string.
19960 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019961 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19962 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 */
19964 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019965get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967{
19968 static char_u mybuf[NUMBUFLEN];
19969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019970 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971}
19972
19973 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019975 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019976 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019978 char_u *res = get_tv_string_buf_chk(varp, buf);
19979
19980 return res != NULL ? res : (char_u *)"";
19981}
19982
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019983 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019984get_tv_string_chk(varp)
19985 typval_T *varp;
19986{
19987 static char_u mybuf[NUMBUFLEN];
19988
19989 return get_tv_string_buf_chk(varp, mybuf);
19990}
19991
19992 static char_u *
19993get_tv_string_buf_chk(varp, buf)
19994 typval_T *varp;
19995 char_u *buf;
19996{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019997 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019999 case VAR_NUMBER:
20000 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20001 return buf;
20002 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020003 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020004 break;
20005 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020006 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020007 break;
20008 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020009 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020010 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020011#ifdef FEAT_FLOAT
20012 case VAR_FLOAT:
20013 EMSG(_("E806: using Float as a String"));
20014 break;
20015#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020016 case VAR_STRING:
20017 if (varp->vval.v_string != NULL)
20018 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020019 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020020 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020021 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020022 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020024 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025}
20026
20027/*
20028 * Find variable "name" in the list of variables.
20029 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020030 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020031 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020032 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020035find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020037 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041
Bram Moolenaara7043832005-01-21 11:56:39 +000020042 ht = find_var_ht(name, &varname);
20043 if (htp != NULL)
20044 *htp = ht;
20045 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020047 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048}
20049
20050/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020051 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020052 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020055find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020056 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020057 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020058 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020059 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020060{
Bram Moolenaar33570922005-01-25 22:26:29 +000020061 hashitem_T *hi;
20062
20063 if (*varname == NUL)
20064 {
20065 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020066 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020067 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020068 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020069 case 'g': return &globvars_var;
20070 case 'v': return &vimvars_var;
20071 case 'b': return &curbuf->b_bufvar;
20072 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020073#ifdef FEAT_WINDOWS
20074 case 't': return &curtab->tp_winvar;
20075#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020076 case 'l': return current_funccal == NULL
20077 ? NULL : &current_funccal->l_vars_var;
20078 case 'a': return current_funccal == NULL
20079 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020080 }
20081 return NULL;
20082 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020083
20084 hi = hash_find(ht, varname);
20085 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020086 {
20087 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020088 * worked find the variable again. Don't auto-load a script if it was
20089 * loaded already, otherwise it would be loaded every time when
20090 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020091 if (ht == &globvarht && !writing)
20092 {
20093 /* Note: script_autoload() may make "hi" invalid. It must either
20094 * be obtained again or not used. */
20095 if (!script_autoload(varname, FALSE) || aborting())
20096 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020097 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020098 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020099 if (HASHITEM_EMPTY(hi))
20100 return NULL;
20101 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020102 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020103}
20104
20105/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020106 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020107 * Set "varname" to the start of name without ':'.
20108 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020110find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 char_u *name;
20112 char_u **varname;
20113{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020114 hashitem_T *hi;
20115
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 if (name[1] != ':')
20117 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020118 /* The name must not start with a colon or #. */
20119 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 return NULL;
20121 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020122
20123 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020124 hi = hash_find(&compat_hashtab, name);
20125 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020126 return &compat_hashtab;
20127
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020129 return &globvarht; /* global variable */
20130 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131 }
20132 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020133 if (*name == 'g') /* global variable */
20134 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020135 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20136 */
20137 if (vim_strchr(name + 2, ':') != NULL
20138 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020139 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020141 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020143 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020144#ifdef FEAT_WINDOWS
20145 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020146 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020147#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 if (*name == 'v') /* v: variable */
20149 return &vimvarht;
20150 if (*name == 'a' && current_funccal != NULL) /* function argument */
20151 return &current_funccal->l_avars.dv_hashtab;
20152 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20153 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 if (*name == 's' /* script variable */
20155 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20156 return &SCRIPT_VARS(current_SID);
20157 return NULL;
20158}
20159
20160/*
20161 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020162 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 * Returns NULL when it doesn't exist.
20164 */
20165 char_u *
20166get_var_value(name)
20167 char_u *name;
20168{
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170
Bram Moolenaara7043832005-01-21 11:56:39 +000020171 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172 if (v == NULL)
20173 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020174 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175}
20176
20177/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020178 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 * sourcing this script and when executing functions defined in the script.
20180 */
20181 void
20182new_script_vars(id)
20183 scid_T id;
20184{
Bram Moolenaara7043832005-01-21 11:56:39 +000020185 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 hashtab_T *ht;
20187 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020188
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20190 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020191 /* Re-allocating ga_data means that an ht_array pointing to
20192 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020193 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020194 for (i = 1; i <= ga_scripts.ga_len; ++i)
20195 {
20196 ht = &SCRIPT_VARS(i);
20197 if (ht->ht_mask == HT_INIT_SIZE - 1)
20198 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020199 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020200 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020201 }
20202
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 while (ga_scripts.ga_len < id)
20204 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020205 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020206 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020207 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209 }
20210 }
20211}
20212
20213/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020214 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20215 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 */
20217 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020218init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020219 dict_T *dict;
20220 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020221 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222{
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020224 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020225 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020226 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020227 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 dict_var->di_tv.vval.v_dict = dict;
20229 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020230 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020231 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20232 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233}
20234
20235/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020236 * Unreference a dictionary initialized by init_var_dict().
20237 */
20238 void
20239unref_var_dict(dict)
20240 dict_T *dict;
20241{
20242 /* Now the dict needs to be freed if no one else is using it, go back to
20243 * normal reference counting. */
20244 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20245 dict_unref(dict);
20246}
20247
20248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020250 * Frees all allocated variables and the value they contain.
20251 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 */
20253 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020254vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020255 hashtab_T *ht;
20256{
20257 vars_clear_ext(ht, TRUE);
20258}
20259
20260/*
20261 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20262 */
20263 static void
20264vars_clear_ext(ht, free_val)
20265 hashtab_T *ht;
20266 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267{
Bram Moolenaara7043832005-01-21 11:56:39 +000020268 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 hashitem_T *hi;
20270 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271
Bram Moolenaar33570922005-01-25 22:26:29 +000020272 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020273 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020274 for (hi = ht->ht_array; todo > 0; ++hi)
20275 {
20276 if (!HASHITEM_EMPTY(hi))
20277 {
20278 --todo;
20279
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020281 * ht_array might change then. hash_clear() takes care of it
20282 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 v = HI2DI(hi);
20284 if (free_val)
20285 clear_tv(&v->di_tv);
20286 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20287 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020288 }
20289 }
20290 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020291 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292}
20293
Bram Moolenaara7043832005-01-21 11:56:39 +000020294/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020295 * Delete a variable from hashtab "ht" at item "hi".
20296 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020299delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 hashtab_T *ht;
20301 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302{
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020304
20305 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020306 clear_tv(&di->di_tv);
20307 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308}
20309
20310/*
20311 * List the value of one internal variable.
20312 */
20313 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020314list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020317 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020319 char_u *tofree;
20320 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020321 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020322
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020323 current_copyID += COPYID_INC;
20324 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020325 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020326 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020327 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328}
20329
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020331list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 char_u *prefix;
20333 char_u *name;
20334 int type;
20335 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020336 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337{
Bram Moolenaar31859182007-08-14 20:41:13 +000020338 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20339 msg_start();
20340 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 if (name != NULL) /* "a:" vars don't have a name stored */
20342 msg_puts(name);
20343 msg_putchar(' ');
20344 msg_advance(22);
20345 if (type == VAR_NUMBER)
20346 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020347 else if (type == VAR_FUNC)
20348 msg_putchar('*');
20349 else if (type == VAR_LIST)
20350 {
20351 msg_putchar('[');
20352 if (*string == '[')
20353 ++string;
20354 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020355 else if (type == VAR_DICT)
20356 {
20357 msg_putchar('{');
20358 if (*string == '{')
20359 ++string;
20360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 else
20362 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020363
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020365
20366 if (type == VAR_FUNC)
20367 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020368 if (*first)
20369 {
20370 msg_clr_eos();
20371 *first = FALSE;
20372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373}
20374
20375/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020376 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 * If the variable already exists, the value is updated.
20378 * Otherwise the variable is created.
20379 */
20380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020384 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385{
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020388 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020390 ht = find_var_ht(name, &varname);
20391 if (ht == NULL || *varname == NUL)
20392 {
20393 EMSG2(_(e_illvar), name);
20394 return;
20395 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020396 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020397
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020398 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20399 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020400
Bram Moolenaar33570922005-01-25 22:26:29 +000020401 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020404 if (var_check_ro(v->di_flags, name)
20405 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020406 return;
20407 if (v->di_tv.v_type != tv->v_type
20408 && !((v->di_tv.v_type == VAR_STRING
20409 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020411 || tv->v_type == VAR_NUMBER))
20412#ifdef FEAT_FLOAT
20413 && !((v->di_tv.v_type == VAR_NUMBER
20414 || v->di_tv.v_type == VAR_FLOAT)
20415 && (tv->v_type == VAR_NUMBER
20416 || tv->v_type == VAR_FLOAT))
20417#endif
20418 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020419 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020420 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020421 return;
20422 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020423
20424 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020425 * Handle setting internal v: variables separately: we don't change
20426 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020427 */
20428 if (ht == &vimvarht)
20429 {
20430 if (v->di_tv.v_type == VAR_STRING)
20431 {
20432 vim_free(v->di_tv.vval.v_string);
20433 if (copy || tv->v_type != VAR_STRING)
20434 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20435 else
20436 {
20437 /* Take over the string to avoid an extra alloc/free. */
20438 v->di_tv.vval.v_string = tv->vval.v_string;
20439 tv->vval.v_string = NULL;
20440 }
20441 }
20442 else if (v->di_tv.v_type != VAR_NUMBER)
20443 EMSG2(_(e_intern2), "set_var()");
20444 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020445 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020446 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020447 if (STRCMP(varname, "searchforward") == 0)
20448 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20449 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020450 return;
20451 }
20452
20453 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 }
20455 else /* add a new variable */
20456 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020457 /* Can't add "v:" variable. */
20458 if (ht == &vimvarht)
20459 {
20460 EMSG2(_(e_illvar), name);
20461 return;
20462 }
20463
Bram Moolenaar92124a32005-06-17 22:03:40 +000020464 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020465 if (!valid_varname(varname))
20466 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020467
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020468 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20469 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020470 if (v == NULL)
20471 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020472 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020475 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 return;
20477 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020478 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020480
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020481 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020483 else
20484 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020486 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020487 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489}
20490
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020491/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020492 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 * Also give an error message.
20494 */
20495 static int
20496var_check_ro(flags, name)
20497 int flags;
20498 char_u *name;
20499{
20500 if (flags & DI_FLAGS_RO)
20501 {
20502 EMSG2(_(e_readonlyvar), name);
20503 return TRUE;
20504 }
20505 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20506 {
20507 EMSG2(_(e_readonlysbx), name);
20508 return TRUE;
20509 }
20510 return FALSE;
20511}
20512
20513/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020514 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20515 * Also give an error message.
20516 */
20517 static int
20518var_check_fixed(flags, name)
20519 int flags;
20520 char_u *name;
20521{
20522 if (flags & DI_FLAGS_FIX)
20523 {
20524 EMSG2(_("E795: Cannot delete variable %s"), name);
20525 return TRUE;
20526 }
20527 return FALSE;
20528}
20529
20530/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020531 * Check if a funcref is assigned to a valid variable name.
20532 * Return TRUE and give an error if not.
20533 */
20534 static int
20535var_check_func_name(name, new_var)
20536 char_u *name; /* points to start of variable name */
20537 int new_var; /* TRUE when creating the variable */
20538{
20539 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20540 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20541 ? name[2] : name[0]))
20542 {
20543 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20544 name);
20545 return TRUE;
20546 }
20547 /* Don't allow hiding a function. When "v" is not NULL we might be
20548 * assigning another function to the same var, the type is checked
20549 * below. */
20550 if (new_var && function_exists(name))
20551 {
20552 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20553 name);
20554 return TRUE;
20555 }
20556 return FALSE;
20557}
20558
20559/*
20560 * Check if a variable name is valid.
20561 * Return FALSE and give an error if not.
20562 */
20563 static int
20564valid_varname(varname)
20565 char_u *varname;
20566{
20567 char_u *p;
20568
20569 for (p = varname; *p != NUL; ++p)
20570 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20571 && *p != AUTOLOAD_CHAR)
20572 {
20573 EMSG2(_(e_illvar), varname);
20574 return FALSE;
20575 }
20576 return TRUE;
20577}
20578
20579/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020580 * Return TRUE if typeval "tv" is set to be locked (immutable).
20581 * Also give an error message, using "name".
20582 */
20583 static int
20584tv_check_lock(lock, name)
20585 int lock;
20586 char_u *name;
20587{
20588 if (lock & VAR_LOCKED)
20589 {
20590 EMSG2(_("E741: Value is locked: %s"),
20591 name == NULL ? (char_u *)_("Unknown") : name);
20592 return TRUE;
20593 }
20594 if (lock & VAR_FIXED)
20595 {
20596 EMSG2(_("E742: Cannot change value of %s"),
20597 name == NULL ? (char_u *)_("Unknown") : name);
20598 return TRUE;
20599 }
20600 return FALSE;
20601}
20602
20603/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020604 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020605 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020606 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020607 * It is OK for "from" and "to" to point to the same item. This is used to
20608 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020609 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020610 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020612 typval_T *from;
20613 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020615 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020616 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020617 switch (from->v_type)
20618 {
20619 case VAR_NUMBER:
20620 to->vval.v_number = from->vval.v_number;
20621 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020622#ifdef FEAT_FLOAT
20623 case VAR_FLOAT:
20624 to->vval.v_float = from->vval.v_float;
20625 break;
20626#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020627 case VAR_STRING:
20628 case VAR_FUNC:
20629 if (from->vval.v_string == NULL)
20630 to->vval.v_string = NULL;
20631 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020633 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020634 if (from->v_type == VAR_FUNC)
20635 func_ref(to->vval.v_string);
20636 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020637 break;
20638 case VAR_LIST:
20639 if (from->vval.v_list == NULL)
20640 to->vval.v_list = NULL;
20641 else
20642 {
20643 to->vval.v_list = from->vval.v_list;
20644 ++to->vval.v_list->lv_refcount;
20645 }
20646 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020647 case VAR_DICT:
20648 if (from->vval.v_dict == NULL)
20649 to->vval.v_dict = NULL;
20650 else
20651 {
20652 to->vval.v_dict = from->vval.v_dict;
20653 ++to->vval.v_dict->dv_refcount;
20654 }
20655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020656 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020658 break;
20659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660}
20661
20662/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020663 * Make a copy of an item.
20664 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020665 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20666 * reference to an already copied list/dict can be used.
20667 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020668 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020669 static int
20670item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020671 typval_T *from;
20672 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020673 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020674 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020675{
20676 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020677 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020678
Bram Moolenaar33570922005-01-25 22:26:29 +000020679 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020680 {
20681 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020682 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020683 }
20684 ++recurse;
20685
20686 switch (from->v_type)
20687 {
20688 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020689#ifdef FEAT_FLOAT
20690 case VAR_FLOAT:
20691#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020692 case VAR_STRING:
20693 case VAR_FUNC:
20694 copy_tv(from, to);
20695 break;
20696 case VAR_LIST:
20697 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020698 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020699 if (from->vval.v_list == NULL)
20700 to->vval.v_list = NULL;
20701 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20702 {
20703 /* use the copy made earlier */
20704 to->vval.v_list = from->vval.v_list->lv_copylist;
20705 ++to->vval.v_list->lv_refcount;
20706 }
20707 else
20708 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20709 if (to->vval.v_list == NULL)
20710 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020711 break;
20712 case VAR_DICT:
20713 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020714 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020715 if (from->vval.v_dict == NULL)
20716 to->vval.v_dict = NULL;
20717 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20718 {
20719 /* use the copy made earlier */
20720 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20721 ++to->vval.v_dict->dv_refcount;
20722 }
20723 else
20724 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20725 if (to->vval.v_dict == NULL)
20726 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020727 break;
20728 default:
20729 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020730 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020731 }
20732 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020733 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020734}
20735
20736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 * ":echo expr1 ..." print each argument separated with a space, add a
20738 * newline at the end.
20739 * ":echon expr1 ..." print each argument plain.
20740 */
20741 void
20742ex_echo(eap)
20743 exarg_T *eap;
20744{
20745 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020746 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020747 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 char_u *p;
20749 int needclr = TRUE;
20750 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020751 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752
20753 if (eap->skip)
20754 ++emsg_skip;
20755 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20756 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020757 /* If eval1() causes an error message the text from the command may
20758 * still need to be cleared. E.g., "echo 22,44". */
20759 need_clr_eos = needclr;
20760
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020762 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 {
20764 /*
20765 * Report the invalid expression unless the expression evaluation
20766 * has been cancelled due to an aborting error, an interrupt, or an
20767 * exception.
20768 */
20769 if (!aborting())
20770 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020771 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 break;
20773 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020774 need_clr_eos = FALSE;
20775
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 if (!eap->skip)
20777 {
20778 if (atstart)
20779 {
20780 atstart = FALSE;
20781 /* Call msg_start() after eval1(), evaluating the expression
20782 * may cause a message to appear. */
20783 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020784 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020785 /* Mark the saved text as finishing the line, so that what
20786 * follows is displayed on a new line when scrolling back
20787 * at the more prompt. */
20788 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 }
20792 else if (eap->cmdidx == CMD_echo)
20793 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020794 current_copyID += COPYID_INC;
20795 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020796 if (p != NULL)
20797 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020799 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020801 if (*p != TAB && needclr)
20802 {
20803 /* remove any text still there from the command */
20804 msg_clr_eos();
20805 needclr = FALSE;
20806 }
20807 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 }
20809 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020810 {
20811#ifdef FEAT_MBYTE
20812 if (has_mbyte)
20813 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020814 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020815
20816 (void)msg_outtrans_len_attr(p, i, echo_attr);
20817 p += i - 1;
20818 }
20819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020821 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020824 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020826 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827 arg = skipwhite(arg);
20828 }
20829 eap->nextcmd = check_nextcmd(arg);
20830
20831 if (eap->skip)
20832 --emsg_skip;
20833 else
20834 {
20835 /* remove text that may still be there from the command */
20836 if (needclr)
20837 msg_clr_eos();
20838 if (eap->cmdidx == CMD_echo)
20839 msg_end();
20840 }
20841}
20842
20843/*
20844 * ":echohl {name}".
20845 */
20846 void
20847ex_echohl(eap)
20848 exarg_T *eap;
20849{
20850 int id;
20851
20852 id = syn_name2id(eap->arg);
20853 if (id == 0)
20854 echo_attr = 0;
20855 else
20856 echo_attr = syn_id2attr(id);
20857}
20858
20859/*
20860 * ":execute expr1 ..." execute the result of an expression.
20861 * ":echomsg expr1 ..." Print a message
20862 * ":echoerr expr1 ..." Print an error
20863 * Each gets spaces around each argument and a newline at the end for
20864 * echo commands
20865 */
20866 void
20867ex_execute(eap)
20868 exarg_T *eap;
20869{
20870 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020871 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 int ret = OK;
20873 char_u *p;
20874 garray_T ga;
20875 int len;
20876 int save_did_emsg;
20877
20878 ga_init2(&ga, 1, 80);
20879
20880 if (eap->skip)
20881 ++emsg_skip;
20882 while (*arg != NUL && *arg != '|' && *arg != '\n')
20883 {
20884 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020885 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 {
20887 /*
20888 * Report the invalid expression unless the expression evaluation
20889 * has been cancelled due to an aborting error, an interrupt, or an
20890 * exception.
20891 */
20892 if (!aborting())
20893 EMSG2(_(e_invexpr2), p);
20894 ret = FAIL;
20895 break;
20896 }
20897
20898 if (!eap->skip)
20899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020900 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 len = (int)STRLEN(p);
20902 if (ga_grow(&ga, len + 2) == FAIL)
20903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020904 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 ret = FAIL;
20906 break;
20907 }
20908 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 ga.ga_len += len;
20912 }
20913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 arg = skipwhite(arg);
20916 }
20917
20918 if (ret != FAIL && ga.ga_data != NULL)
20919 {
20920 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020923 out_flush();
20924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925 else if (eap->cmdidx == CMD_echoerr)
20926 {
20927 /* We don't want to abort following commands, restore did_emsg. */
20928 save_did_emsg = did_emsg;
20929 EMSG((char_u *)ga.ga_data);
20930 if (!force_abort)
20931 did_emsg = save_did_emsg;
20932 }
20933 else if (eap->cmdidx == CMD_execute)
20934 do_cmdline((char_u *)ga.ga_data,
20935 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20936 }
20937
20938 ga_clear(&ga);
20939
20940 if (eap->skip)
20941 --emsg_skip;
20942
20943 eap->nextcmd = check_nextcmd(arg);
20944}
20945
20946/*
20947 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20948 * "arg" points to the "&" or '+' when called, to "option" when returning.
20949 * Returns NULL when no option name found. Otherwise pointer to the char
20950 * after the option name.
20951 */
20952 static char_u *
20953find_option_end(arg, opt_flags)
20954 char_u **arg;
20955 int *opt_flags;
20956{
20957 char_u *p = *arg;
20958
20959 ++p;
20960 if (*p == 'g' && p[1] == ':')
20961 {
20962 *opt_flags = OPT_GLOBAL;
20963 p += 2;
20964 }
20965 else if (*p == 'l' && p[1] == ':')
20966 {
20967 *opt_flags = OPT_LOCAL;
20968 p += 2;
20969 }
20970 else
20971 *opt_flags = 0;
20972
20973 if (!ASCII_ISALPHA(*p))
20974 return NULL;
20975 *arg = p;
20976
20977 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20978 p += 4; /* termcap option */
20979 else
20980 while (ASCII_ISALPHA(*p))
20981 ++p;
20982 return p;
20983}
20984
20985/*
20986 * ":function"
20987 */
20988 void
20989ex_function(eap)
20990 exarg_T *eap;
20991{
20992 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020993 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 int j;
20995 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 char_u *name = NULL;
20998 char_u *p;
20999 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021000 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 garray_T newargs;
21002 garray_T newlines;
21003 int varargs = FALSE;
21004 int mustend = FALSE;
21005 int flags = 0;
21006 ufunc_T *fp;
21007 int indent;
21008 int nesting;
21009 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021010 dictitem_T *v;
21011 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021012 static int func_nr = 0; /* number for nameless function */
21013 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021014 hashtab_T *ht;
21015 int todo;
21016 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021017 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018
21019 /*
21020 * ":function" without argument: list functions.
21021 */
21022 if (ends_excmd(*eap->arg))
21023 {
21024 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021025 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021026 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021027 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021028 {
21029 if (!HASHITEM_EMPTY(hi))
21030 {
21031 --todo;
21032 fp = HI2UF(hi);
21033 if (!isdigit(*fp->uf_name))
21034 list_func_head(fp, FALSE);
21035 }
21036 }
21037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 eap->nextcmd = check_nextcmd(eap->arg);
21039 return;
21040 }
21041
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021042 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021043 * ":function /pat": list functions matching pattern.
21044 */
21045 if (*eap->arg == '/')
21046 {
21047 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21048 if (!eap->skip)
21049 {
21050 regmatch_T regmatch;
21051
21052 c = *p;
21053 *p = NUL;
21054 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21055 *p = c;
21056 if (regmatch.regprog != NULL)
21057 {
21058 regmatch.rm_ic = p_ic;
21059
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021060 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021061 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21062 {
21063 if (!HASHITEM_EMPTY(hi))
21064 {
21065 --todo;
21066 fp = HI2UF(hi);
21067 if (!isdigit(*fp->uf_name)
21068 && vim_regexec(&regmatch, fp->uf_name, 0))
21069 list_func_head(fp, FALSE);
21070 }
21071 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021072 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021073 }
21074 }
21075 if (*p == '/')
21076 ++p;
21077 eap->nextcmd = check_nextcmd(p);
21078 return;
21079 }
21080
21081 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021082 * Get the function name. There are these situations:
21083 * func normal function name
21084 * "name" == func, "fudi.fd_dict" == NULL
21085 * dict.func new dictionary entry
21086 * "name" == NULL, "fudi.fd_dict" set,
21087 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21088 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021089 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021090 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21091 * dict.func existing dict entry that's not a Funcref
21092 * "name" == NULL, "fudi.fd_dict" set,
21093 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021096 name = trans_function_name(&p, eap->skip, 0, &fudi);
21097 paren = (vim_strchr(p, '(') != NULL);
21098 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 {
21100 /*
21101 * Return on an invalid expression in braces, unless the expression
21102 * evaluation has been cancelled due to an aborting error, an
21103 * interrupt, or an exception.
21104 */
21105 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021106 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021107 if (!eap->skip && fudi.fd_newkey != NULL)
21108 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021109 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 else
21113 eap->skip = TRUE;
21114 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021115
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 /* An error in a function call during evaluation of an expression in magic
21117 * braces should not cause the function not to be defined. */
21118 saved_did_emsg = did_emsg;
21119 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120
21121 /*
21122 * ":function func" with only function name: list function.
21123 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021124 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 {
21126 if (!ends_excmd(*skipwhite(p)))
21127 {
21128 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021129 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 }
21131 eap->nextcmd = check_nextcmd(p);
21132 if (eap->nextcmd != NULL)
21133 *p = NUL;
21134 if (!eap->skip && !got_int)
21135 {
21136 fp = find_func(name);
21137 if (fp != NULL)
21138 {
21139 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021140 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021142 if (FUNCLINE(fp, j) == NULL)
21143 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 msg_putchar('\n');
21145 msg_outnum((long)(j + 1));
21146 if (j < 9)
21147 msg_putchar(' ');
21148 if (j < 99)
21149 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021150 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 out_flush(); /* show a line at a time */
21152 ui_breakcheck();
21153 }
21154 if (!got_int)
21155 {
21156 msg_putchar('\n');
21157 msg_puts((char_u *)" endfunction");
21158 }
21159 }
21160 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021161 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021163 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164 }
21165
21166 /*
21167 * ":function name(arg1, arg2)" Define function.
21168 */
21169 p = skipwhite(p);
21170 if (*p != '(')
21171 {
21172 if (!eap->skip)
21173 {
21174 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021175 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 }
21177 /* attempt to continue by skipping some text */
21178 if (vim_strchr(p, '(') != NULL)
21179 p = vim_strchr(p, '(');
21180 }
21181 p = skipwhite(p + 1);
21182
21183 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21184 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21185
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021186 if (!eap->skip)
21187 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021188 /* Check the name of the function. Unless it's a dictionary function
21189 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021190 if (name != NULL)
21191 arg = name;
21192 else
21193 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021194 if (arg != NULL && (fudi.fd_di == NULL
21195 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021196 {
21197 if (*arg == K_SPECIAL)
21198 j = 3;
21199 else
21200 j = 0;
21201 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21202 : eval_isnamec(arg[j])))
21203 ++j;
21204 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021205 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021206 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021207 /* Disallow using the g: dict. */
21208 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21209 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021210 }
21211
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 /*
21213 * Isolate the arguments: "arg1, arg2, ...)"
21214 */
21215 while (*p != ')')
21216 {
21217 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21218 {
21219 varargs = TRUE;
21220 p += 3;
21221 mustend = TRUE;
21222 }
21223 else
21224 {
21225 arg = p;
21226 while (ASCII_ISALNUM(*p) || *p == '_')
21227 ++p;
21228 if (arg == p || isdigit(*arg)
21229 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21230 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21231 {
21232 if (!eap->skip)
21233 EMSG2(_("E125: Illegal argument: %s"), arg);
21234 break;
21235 }
21236 if (ga_grow(&newargs, 1) == FAIL)
21237 goto erret;
21238 c = *p;
21239 *p = NUL;
21240 arg = vim_strsave(arg);
21241 if (arg == NULL)
21242 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021243
21244 /* Check for duplicate argument name. */
21245 for (i = 0; i < newargs.ga_len; ++i)
21246 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21247 {
21248 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21249 goto erret;
21250 }
21251
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21253 *p = c;
21254 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 if (*p == ',')
21256 ++p;
21257 else
21258 mustend = TRUE;
21259 }
21260 p = skipwhite(p);
21261 if (mustend && *p != ')')
21262 {
21263 if (!eap->skip)
21264 EMSG2(_(e_invarg2), eap->arg);
21265 break;
21266 }
21267 }
21268 ++p; /* skip the ')' */
21269
Bram Moolenaare9a41262005-01-15 22:18:47 +000021270 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 for (;;)
21272 {
21273 p = skipwhite(p);
21274 if (STRNCMP(p, "range", 5) == 0)
21275 {
21276 flags |= FC_RANGE;
21277 p += 5;
21278 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021279 else if (STRNCMP(p, "dict", 4) == 0)
21280 {
21281 flags |= FC_DICT;
21282 p += 4;
21283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 else if (STRNCMP(p, "abort", 5) == 0)
21285 {
21286 flags |= FC_ABORT;
21287 p += 5;
21288 }
21289 else
21290 break;
21291 }
21292
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021293 /* When there is a line break use what follows for the function body.
21294 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21295 if (*p == '\n')
21296 line_arg = p + 1;
21297 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 EMSG(_(e_trailing));
21299
21300 /*
21301 * Read the body of the function, until ":endfunction" is found.
21302 */
21303 if (KeyTyped)
21304 {
21305 /* Check if the function already exists, don't let the user type the
21306 * whole function before telling him it doesn't work! For a script we
21307 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021308 if (!eap->skip && !eap->forceit)
21309 {
21310 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21311 EMSG(_(e_funcdict));
21312 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021313 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021316 if (!eap->skip && did_emsg)
21317 goto erret;
21318
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 msg_putchar('\n'); /* don't overwrite the function name */
21320 cmdline_row = msg_row;
21321 }
21322
21323 indent = 2;
21324 nesting = 0;
21325 for (;;)
21326 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021327 if (KeyTyped)
21328 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021330 sourcing_lnum_off = sourcing_lnum;
21331
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021332 if (line_arg != NULL)
21333 {
21334 /* Use eap->arg, split up in parts by line breaks. */
21335 theline = line_arg;
21336 p = vim_strchr(theline, '\n');
21337 if (p == NULL)
21338 line_arg += STRLEN(line_arg);
21339 else
21340 {
21341 *p = NUL;
21342 line_arg = p + 1;
21343 }
21344 }
21345 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 theline = getcmdline(':', 0L, indent);
21347 else
21348 theline = eap->getline(':', eap->cookie, indent);
21349 if (KeyTyped)
21350 lines_left = Rows - 1;
21351 if (theline == NULL)
21352 {
21353 EMSG(_("E126: Missing :endfunction"));
21354 goto erret;
21355 }
21356
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021357 /* Detect line continuation: sourcing_lnum increased more than one. */
21358 if (sourcing_lnum > sourcing_lnum_off + 1)
21359 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21360 else
21361 sourcing_lnum_off = 0;
21362
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 if (skip_until != NULL)
21364 {
21365 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21366 * don't check for ":endfunc". */
21367 if (STRCMP(theline, skip_until) == 0)
21368 {
21369 vim_free(skip_until);
21370 skip_until = NULL;
21371 }
21372 }
21373 else
21374 {
21375 /* skip ':' and blanks*/
21376 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21377 ;
21378
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021379 /* Check for "endfunction". */
21380 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021382 if (line_arg == NULL)
21383 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 break;
21385 }
21386
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021387 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 * at "end". */
21389 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21390 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021391 else if (STRNCMP(p, "if", 2) == 0
21392 || STRNCMP(p, "wh", 2) == 0
21393 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 || STRNCMP(p, "try", 3) == 0)
21395 indent += 2;
21396
21397 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021398 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021400 if (*p == '!')
21401 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 p += eval_fname_script(p);
21403 if (ASCII_ISALPHA(*p))
21404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021405 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 if (*skipwhite(p) == '(')
21407 {
21408 ++nesting;
21409 indent += 2;
21410 }
21411 }
21412 }
21413
21414 /* Check for ":append" or ":insert". */
21415 p = skip_range(p, NULL);
21416 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21417 || (p[0] == 'i'
21418 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21419 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21420 skip_until = vim_strsave((char_u *)".");
21421
21422 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21423 arg = skipwhite(skiptowhite(p));
21424 if (arg[0] == '<' && arg[1] =='<'
21425 && ((p[0] == 'p' && p[1] == 'y'
21426 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21427 || (p[0] == 'p' && p[1] == 'e'
21428 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21429 || (p[0] == 't' && p[1] == 'c'
21430 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021431 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21432 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21434 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021435 || (p[0] == 'm' && p[1] == 'z'
21436 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 ))
21438 {
21439 /* ":python <<" continues until a dot, like ":append" */
21440 p = skipwhite(arg + 2);
21441 if (*p == NUL)
21442 skip_until = vim_strsave((char_u *)".");
21443 else
21444 skip_until = vim_strsave(p);
21445 }
21446 }
21447
21448 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021449 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021450 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021451 if (line_arg == NULL)
21452 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021454 }
21455
21456 /* Copy the line to newly allocated memory. get_one_sourceline()
21457 * allocates 250 bytes per line, this saves 80% on average. The cost
21458 * is an extra alloc/free. */
21459 p = vim_strsave(theline);
21460 if (p != NULL)
21461 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021462 if (line_arg == NULL)
21463 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021464 theline = p;
21465 }
21466
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021467 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21468
21469 /* Add NULL lines for continuation lines, so that the line count is
21470 * equal to the index in the growarray. */
21471 while (sourcing_lnum_off-- > 0)
21472 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021473
21474 /* Check for end of eap->arg. */
21475 if (line_arg != NULL && *line_arg == NUL)
21476 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 }
21478
21479 /* Don't define the function when skipping commands or when an error was
21480 * detected. */
21481 if (eap->skip || did_emsg)
21482 goto erret;
21483
21484 /*
21485 * If there are no errors, add the function
21486 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021487 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021488 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021489 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021490 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021491 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021492 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021493 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021494 goto erret;
21495 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021496
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021497 fp = find_func(name);
21498 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021500 if (!eap->forceit)
21501 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021502 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021503 goto erret;
21504 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021505 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021506 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021507 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021508 name);
21509 goto erret;
21510 }
21511 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021512 ga_clear_strings(&(fp->uf_args));
21513 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021514 vim_free(name);
21515 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 }
21518 else
21519 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021520 char numbuf[20];
21521
21522 fp = NULL;
21523 if (fudi.fd_newkey == NULL && !eap->forceit)
21524 {
21525 EMSG(_(e_funcdict));
21526 goto erret;
21527 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021528 if (fudi.fd_di == NULL)
21529 {
21530 /* Can't add a function to a locked dictionary */
21531 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21532 goto erret;
21533 }
21534 /* Can't change an existing function if it is locked */
21535 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21536 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021537
21538 /* Give the function a sequential number. Can only be used with a
21539 * Funcref! */
21540 vim_free(name);
21541 sprintf(numbuf, "%d", ++func_nr);
21542 name = vim_strsave((char_u *)numbuf);
21543 if (name == NULL)
21544 goto erret;
21545 }
21546
21547 if (fp == NULL)
21548 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021549 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021550 {
21551 int slen, plen;
21552 char_u *scriptname;
21553
21554 /* Check that the autoload name matches the script name. */
21555 j = FAIL;
21556 if (sourcing_name != NULL)
21557 {
21558 scriptname = autoload_name(name);
21559 if (scriptname != NULL)
21560 {
21561 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021562 plen = (int)STRLEN(p);
21563 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021564 if (slen > plen && fnamecmp(p,
21565 sourcing_name + slen - plen) == 0)
21566 j = OK;
21567 vim_free(scriptname);
21568 }
21569 }
21570 if (j == FAIL)
21571 {
21572 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21573 goto erret;
21574 }
21575 }
21576
21577 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 if (fp == NULL)
21579 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021580
21581 if (fudi.fd_dict != NULL)
21582 {
21583 if (fudi.fd_di == NULL)
21584 {
21585 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021586 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 if (fudi.fd_di == NULL)
21588 {
21589 vim_free(fp);
21590 goto erret;
21591 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021592 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21593 {
21594 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021595 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021596 goto erret;
21597 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021598 }
21599 else
21600 /* overwrite existing dict entry */
21601 clear_tv(&fudi.fd_di->di_tv);
21602 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021603 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021604 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021605 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021606
21607 /* behave like "dict" was used */
21608 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021609 }
21610
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021612 STRCPY(fp->uf_name, name);
21613 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021615 fp->uf_args = newargs;
21616 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021617#ifdef FEAT_PROFILE
21618 fp->uf_tml_count = NULL;
21619 fp->uf_tml_total = NULL;
21620 fp->uf_tml_self = NULL;
21621 fp->uf_profiling = FALSE;
21622 if (prof_def_func())
21623 func_do_profile(fp);
21624#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021625 fp->uf_varargs = varargs;
21626 fp->uf_flags = flags;
21627 fp->uf_calls = 0;
21628 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021629 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630
21631erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 ga_clear_strings(&newargs);
21633 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021634ret_free:
21635 vim_free(skip_until);
21636 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639}
21640
21641/*
21642 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021643 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021645 * flags:
21646 * TFN_INT: internal function name OK
21647 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 * Advances "pp" to just after the function name (if no error).
21649 */
21650 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 char_u **pp;
21653 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021654 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021655 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021657 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 char_u *start;
21659 char_u *end;
21660 int lead;
21661 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021664
21665 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021666 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021668
21669 /* Check for hard coded <SNR>: already translated function ID (from a user
21670 * command). */
21671 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21672 && (*pp)[2] == (int)KE_SNR)
21673 {
21674 *pp += 3;
21675 len = get_id_len(pp) + 3;
21676 return vim_strnsave(start, len);
21677 }
21678
21679 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21680 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021682 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021684
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021685 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21686 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021687 if (end == start)
21688 {
21689 if (!skip)
21690 EMSG(_("E129: Function name required"));
21691 goto theend;
21692 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021693 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021694 {
21695 /*
21696 * Report an invalid expression in braces, unless the expression
21697 * evaluation has been cancelled due to an aborting error, an
21698 * interrupt, or an exception.
21699 */
21700 if (!aborting())
21701 {
21702 if (end != NULL)
21703 EMSG2(_(e_invarg2), start);
21704 }
21705 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021706 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021707 goto theend;
21708 }
21709
21710 if (lv.ll_tv != NULL)
21711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021712 if (fdp != NULL)
21713 {
21714 fdp->fd_dict = lv.ll_dict;
21715 fdp->fd_newkey = lv.ll_newkey;
21716 lv.ll_newkey = NULL;
21717 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021719 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21720 {
21721 name = vim_strsave(lv.ll_tv->vval.v_string);
21722 *pp = end;
21723 }
21724 else
21725 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021726 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21727 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021728 EMSG(_(e_funcref));
21729 else
21730 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021731 name = NULL;
21732 }
21733 goto theend;
21734 }
21735
21736 if (lv.ll_name == NULL)
21737 {
21738 /* Error found, but continue after the function name. */
21739 *pp = end;
21740 goto theend;
21741 }
21742
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021743 /* Check if the name is a Funcref. If so, use the value. */
21744 if (lv.ll_exp_name != NULL)
21745 {
21746 len = (int)STRLEN(lv.ll_exp_name);
21747 name = deref_func_name(lv.ll_exp_name, &len);
21748 if (name == lv.ll_exp_name)
21749 name = NULL;
21750 }
21751 else
21752 {
21753 len = (int)(end - *pp);
21754 name = deref_func_name(*pp, &len);
21755 if (name == *pp)
21756 name = NULL;
21757 }
21758 if (name != NULL)
21759 {
21760 name = vim_strsave(name);
21761 *pp = end;
21762 goto theend;
21763 }
21764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021765 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021766 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021767 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021768 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21769 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21770 {
21771 /* When there was "s:" already or the name expanded to get a
21772 * leading "s:" then remove it. */
21773 lv.ll_name += 2;
21774 len -= 2;
21775 lead = 2;
21776 }
21777 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021778 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021779 {
21780 if (lead == 2) /* skip over "s:" */
21781 lv.ll_name += 2;
21782 len = (int)(end - lv.ll_name);
21783 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021784
21785 /*
21786 * Copy the function name to allocated memory.
21787 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21788 * Accept <SNR>123_name() outside a script.
21789 */
21790 if (skip)
21791 lead = 0; /* do nothing */
21792 else if (lead > 0)
21793 {
21794 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021795 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21796 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021797 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021798 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021799 if (current_SID <= 0)
21800 {
21801 EMSG(_(e_usingsid));
21802 goto theend;
21803 }
21804 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21805 lead += (int)STRLEN(sid_buf);
21806 }
21807 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021808 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021809 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021810 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021811 goto theend;
21812 }
21813 name = alloc((unsigned)(len + lead + 1));
21814 if (name != NULL)
21815 {
21816 if (lead > 0)
21817 {
21818 name[0] = K_SPECIAL;
21819 name[1] = KS_EXTRA;
21820 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021821 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021822 STRCPY(name + 3, sid_buf);
21823 }
21824 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21825 name[len + lead] = NUL;
21826 }
21827 *pp = end;
21828
21829theend:
21830 clear_lval(&lv);
21831 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832}
21833
21834/*
21835 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21836 * Return 2 if "p" starts with "s:".
21837 * Return 0 otherwise.
21838 */
21839 static int
21840eval_fname_script(p)
21841 char_u *p;
21842{
21843 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21844 || STRNICMP(p + 1, "SNR>", 4) == 0))
21845 return 5;
21846 if (p[0] == 's' && p[1] == ':')
21847 return 2;
21848 return 0;
21849}
21850
21851/*
21852 * Return TRUE if "p" starts with "<SID>" or "s:".
21853 * Only works if eval_fname_script() returned non-zero for "p"!
21854 */
21855 static int
21856eval_fname_sid(p)
21857 char_u *p;
21858{
21859 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21860}
21861
21862/*
21863 * List the head of the function: "name(arg1, arg2)".
21864 */
21865 static void
21866list_func_head(fp, indent)
21867 ufunc_T *fp;
21868 int indent;
21869{
21870 int j;
21871
21872 msg_start();
21873 if (indent)
21874 MSG_PUTS(" ");
21875 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021876 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 {
21878 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021879 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 }
21881 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021882 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021884 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 {
21886 if (j)
21887 MSG_PUTS(", ");
21888 msg_puts(FUNCARG(fp, j));
21889 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021890 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 {
21892 if (j)
21893 MSG_PUTS(", ");
21894 MSG_PUTS("...");
21895 }
21896 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021897 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021898 if (p_verbose > 0)
21899 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900}
21901
21902/*
21903 * Find a function by name, return pointer to it in ufuncs.
21904 * Return NULL for unknown function.
21905 */
21906 static ufunc_T *
21907find_func(name)
21908 char_u *name;
21909{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021910 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021912 hi = hash_find(&func_hashtab, name);
21913 if (!HASHITEM_EMPTY(hi))
21914 return HI2UF(hi);
21915 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916}
21917
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021918#if defined(EXITFREE) || defined(PROTO)
21919 void
21920free_all_functions()
21921{
21922 hashitem_T *hi;
21923
21924 /* Need to start all over every time, because func_free() may change the
21925 * hash table. */
21926 while (func_hashtab.ht_used > 0)
21927 for (hi = func_hashtab.ht_array; ; ++hi)
21928 if (!HASHITEM_EMPTY(hi))
21929 {
21930 func_free(HI2UF(hi));
21931 break;
21932 }
21933}
21934#endif
21935
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021936/*
21937 * Return TRUE if a function "name" exists.
21938 */
21939 static int
21940function_exists(name)
21941 char_u *name;
21942{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021943 char_u *nm = name;
21944 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021945 int n = FALSE;
21946
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021947 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021948 nm = skipwhite(nm);
21949
21950 /* Only accept "funcname", "funcname ", "funcname (..." and
21951 * "funcname(...", not "funcname!...". */
21952 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021953 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021954 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021955 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021956 else
21957 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021958 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021959 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021960 return n;
21961}
21962
Bram Moolenaara1544c02013-05-30 12:35:52 +020021963 char_u *
21964get_expanded_name(name, check)
21965 char_u *name;
21966 int check;
21967{
21968 char_u *nm = name;
21969 char_u *p;
21970
21971 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
21972
21973 if (p != NULL && *nm == NUL)
21974 {
21975 if (!check)
21976 return p;
21977 else if (builtin_function(p))
21978 {
21979 if (find_internal_func(p) >= 0)
21980 return p;
21981 }
21982 else
21983 if (find_func(p) != NULL)
21984 return p;
21985 }
21986 vim_free(p);
21987 return NULL;
21988}
21989
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021990/*
21991 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021992 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021993 */
21994 static int
21995builtin_function(name)
21996 char_u *name;
21997{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021998 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21999 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022000}
22001
Bram Moolenaar05159a02005-02-26 23:04:13 +000022002#if defined(FEAT_PROFILE) || defined(PROTO)
22003/*
22004 * Start profiling function "fp".
22005 */
22006 static void
22007func_do_profile(fp)
22008 ufunc_T *fp;
22009{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022010 int len = fp->uf_lines.ga_len;
22011
22012 if (len == 0)
22013 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022014 fp->uf_tm_count = 0;
22015 profile_zero(&fp->uf_tm_self);
22016 profile_zero(&fp->uf_tm_total);
22017 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022018 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022019 if (fp->uf_tml_total == NULL)
22020 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022021 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022022 if (fp->uf_tml_self == NULL)
22023 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022024 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022025 fp->uf_tml_idx = -1;
22026 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22027 || fp->uf_tml_self == NULL)
22028 return; /* out of memory */
22029
22030 fp->uf_profiling = TRUE;
22031}
22032
22033/*
22034 * Dump the profiling results for all functions in file "fd".
22035 */
22036 void
22037func_dump_profile(fd)
22038 FILE *fd;
22039{
22040 hashitem_T *hi;
22041 int todo;
22042 ufunc_T *fp;
22043 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022044 ufunc_T **sorttab;
22045 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022046
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022047 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022048 if (todo == 0)
22049 return; /* nothing to dump */
22050
Bram Moolenaar73830342005-02-28 22:48:19 +000022051 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22052
Bram Moolenaar05159a02005-02-26 23:04:13 +000022053 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22054 {
22055 if (!HASHITEM_EMPTY(hi))
22056 {
22057 --todo;
22058 fp = HI2UF(hi);
22059 if (fp->uf_profiling)
22060 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022061 if (sorttab != NULL)
22062 sorttab[st_len++] = fp;
22063
Bram Moolenaar05159a02005-02-26 23:04:13 +000022064 if (fp->uf_name[0] == K_SPECIAL)
22065 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22066 else
22067 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22068 if (fp->uf_tm_count == 1)
22069 fprintf(fd, "Called 1 time\n");
22070 else
22071 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22072 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22073 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22074 fprintf(fd, "\n");
22075 fprintf(fd, "count total (s) self (s)\n");
22076
22077 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22078 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022079 if (FUNCLINE(fp, i) == NULL)
22080 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022081 prof_func_line(fd, fp->uf_tml_count[i],
22082 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022083 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22084 }
22085 fprintf(fd, "\n");
22086 }
22087 }
22088 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022089
22090 if (sorttab != NULL && st_len > 0)
22091 {
22092 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22093 prof_total_cmp);
22094 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22095 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22096 prof_self_cmp);
22097 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22098 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022099
22100 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022101}
Bram Moolenaar73830342005-02-28 22:48:19 +000022102
22103 static void
22104prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22105 FILE *fd;
22106 ufunc_T **sorttab;
22107 int st_len;
22108 char *title;
22109 int prefer_self; /* when equal print only self time */
22110{
22111 int i;
22112 ufunc_T *fp;
22113
22114 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22115 fprintf(fd, "count total (s) self (s) function\n");
22116 for (i = 0; i < 20 && i < st_len; ++i)
22117 {
22118 fp = sorttab[i];
22119 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22120 prefer_self);
22121 if (fp->uf_name[0] == K_SPECIAL)
22122 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22123 else
22124 fprintf(fd, " %s()\n", fp->uf_name);
22125 }
22126 fprintf(fd, "\n");
22127}
22128
22129/*
22130 * Print the count and times for one function or function line.
22131 */
22132 static void
22133prof_func_line(fd, count, total, self, prefer_self)
22134 FILE *fd;
22135 int count;
22136 proftime_T *total;
22137 proftime_T *self;
22138 int prefer_self; /* when equal print only self time */
22139{
22140 if (count > 0)
22141 {
22142 fprintf(fd, "%5d ", count);
22143 if (prefer_self && profile_equal(total, self))
22144 fprintf(fd, " ");
22145 else
22146 fprintf(fd, "%s ", profile_msg(total));
22147 if (!prefer_self && profile_equal(total, self))
22148 fprintf(fd, " ");
22149 else
22150 fprintf(fd, "%s ", profile_msg(self));
22151 }
22152 else
22153 fprintf(fd, " ");
22154}
22155
22156/*
22157 * Compare function for total time sorting.
22158 */
22159 static int
22160#ifdef __BORLANDC__
22161_RTLENTRYF
22162#endif
22163prof_total_cmp(s1, s2)
22164 const void *s1;
22165 const void *s2;
22166{
22167 ufunc_T *p1, *p2;
22168
22169 p1 = *(ufunc_T **)s1;
22170 p2 = *(ufunc_T **)s2;
22171 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22172}
22173
22174/*
22175 * Compare function for self time sorting.
22176 */
22177 static int
22178#ifdef __BORLANDC__
22179_RTLENTRYF
22180#endif
22181prof_self_cmp(s1, s2)
22182 const void *s1;
22183 const void *s2;
22184{
22185 ufunc_T *p1, *p2;
22186
22187 p1 = *(ufunc_T **)s1;
22188 p2 = *(ufunc_T **)s2;
22189 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22190}
22191
Bram Moolenaar05159a02005-02-26 23:04:13 +000022192#endif
22193
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022194/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022195 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022196 * Return TRUE if a package was loaded.
22197 */
Bram Moolenaara1544c02013-05-30 12:35:52 +020022198 int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022199script_autoload(name, reload)
22200 char_u *name;
22201 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022202{
22203 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022204 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022205 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022206 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022207
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022208 /* Return quickly when autoload disabled. */
22209 if (no_autoload)
22210 return FALSE;
22211
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022212 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022213 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022214 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022215 return FALSE;
22216
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022217 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022218
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022219 /* Find the name in the list of previously loaded package names. Skip
22220 * "autoload/", it's always the same. */
22221 for (i = 0; i < ga_loaded.ga_len; ++i)
22222 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22223 break;
22224 if (!reload && i < ga_loaded.ga_len)
22225 ret = FALSE; /* was loaded already */
22226 else
22227 {
22228 /* Remember the name if it wasn't loaded already. */
22229 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22230 {
22231 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22232 tofree = NULL;
22233 }
22234
22235 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022236 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022237 ret = TRUE;
22238 }
22239
22240 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022241 return ret;
22242}
22243
22244/*
22245 * Return the autoload script name for a function or variable name.
22246 * Returns NULL when out of memory.
22247 */
22248 static char_u *
22249autoload_name(name)
22250 char_u *name;
22251{
22252 char_u *p;
22253 char_u *scriptname;
22254
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022255 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022256 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22257 if (scriptname == NULL)
22258 return FALSE;
22259 STRCPY(scriptname, "autoload/");
22260 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022261 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022262 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022263 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022264 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022265 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022266}
22267
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22269
22270/*
22271 * Function given to ExpandGeneric() to obtain the list of user defined
22272 * function names.
22273 */
22274 char_u *
22275get_user_func_name(xp, idx)
22276 expand_T *xp;
22277 int idx;
22278{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022279 static long_u done;
22280 static hashitem_T *hi;
22281 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282
22283 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022285 done = 0;
22286 hi = func_hashtab.ht_array;
22287 }
22288 if (done < func_hashtab.ht_used)
22289 {
22290 if (done++ > 0)
22291 ++hi;
22292 while (HASHITEM_EMPTY(hi))
22293 ++hi;
22294 fp = HI2UF(hi);
22295
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022296 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022297 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022299 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22300 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301
22302 cat_func_name(IObuff, fp);
22303 if (xp->xp_context != EXPAND_USER_FUNC)
22304 {
22305 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022306 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 STRCAT(IObuff, ")");
22308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 return IObuff;
22310 }
22311 return NULL;
22312}
22313
22314#endif /* FEAT_CMDL_COMPL */
22315
22316/*
22317 * Copy the function name of "fp" to buffer "buf".
22318 * "buf" must be able to hold the function name plus three bytes.
22319 * Takes care of script-local function names.
22320 */
22321 static void
22322cat_func_name(buf, fp)
22323 char_u *buf;
22324 ufunc_T *fp;
22325{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022326 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 {
22328 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022329 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 }
22331 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022332 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333}
22334
22335/*
22336 * ":delfunction {name}"
22337 */
22338 void
22339ex_delfunction(eap)
22340 exarg_T *eap;
22341{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022342 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 char_u *p;
22344 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022345 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346
22347 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 name = trans_function_name(&p, eap->skip, 0, &fudi);
22349 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022351 {
22352 if (fudi.fd_dict != NULL && !eap->skip)
22353 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 if (!ends_excmd(*skipwhite(p)))
22357 {
22358 vim_free(name);
22359 EMSG(_(e_trailing));
22360 return;
22361 }
22362 eap->nextcmd = check_nextcmd(p);
22363 if (eap->nextcmd != NULL)
22364 *p = NUL;
22365
22366 if (!eap->skip)
22367 fp = find_func(name);
22368 vim_free(name);
22369
22370 if (!eap->skip)
22371 {
22372 if (fp == NULL)
22373 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022374 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 return;
22376 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022377 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 {
22379 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22380 return;
22381 }
22382
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022383 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 /* Delete the dict item that refers to the function, it will
22386 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022387 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022389 else
22390 func_free(fp);
22391 }
22392}
22393
22394/*
22395 * Free a function and remove it from the list of functions.
22396 */
22397 static void
22398func_free(fp)
22399 ufunc_T *fp;
22400{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022401 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022402
22403 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022404 ga_clear_strings(&(fp->uf_args));
22405 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022406#ifdef FEAT_PROFILE
22407 vim_free(fp->uf_tml_count);
22408 vim_free(fp->uf_tml_total);
22409 vim_free(fp->uf_tml_self);
22410#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022411
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022412 /* remove the function from the function hashtable */
22413 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22414 if (HASHITEM_EMPTY(hi))
22415 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022416 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022417 hash_remove(&func_hashtab, hi);
22418
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022419 vim_free(fp);
22420}
22421
22422/*
22423 * Unreference a Function: decrement the reference count and free it when it
22424 * becomes zero. Only for numbered functions.
22425 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022426 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022427func_unref(name)
22428 char_u *name;
22429{
22430 ufunc_T *fp;
22431
22432 if (name != NULL && isdigit(*name))
22433 {
22434 fp = find_func(name);
22435 if (fp == NULL)
22436 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022437 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022438 {
22439 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022440 * when "uf_calls" becomes zero. */
22441 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022442 func_free(fp);
22443 }
22444 }
22445}
22446
22447/*
22448 * Count a reference to a Function.
22449 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022450 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022451func_ref(name)
22452 char_u *name;
22453{
22454 ufunc_T *fp;
22455
22456 if (name != NULL && isdigit(*name))
22457 {
22458 fp = find_func(name);
22459 if (fp == NULL)
22460 EMSG2(_(e_intern2), "func_ref()");
22461 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022462 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 }
22464}
22465
22466/*
22467 * Call a user function.
22468 */
22469 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022470call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 ufunc_T *fp; /* pointer to function */
22472 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022473 typval_T *argvars; /* arguments */
22474 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 linenr_T firstline; /* first line of range */
22476 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022477 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478{
Bram Moolenaar33570922005-01-25 22:26:29 +000022479 char_u *save_sourcing_name;
22480 linenr_T save_sourcing_lnum;
22481 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022482 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022483 int save_did_emsg;
22484 static int depth = 0;
22485 dictitem_T *v;
22486 int fixvar_idx = 0; /* index in fixvar[] */
22487 int i;
22488 int ai;
22489 char_u numbuf[NUMBUFLEN];
22490 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022491#ifdef FEAT_PROFILE
22492 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022493 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495
22496 /* If depth of calling is getting too high, don't execute the function */
22497 if (depth >= p_mfd)
22498 {
22499 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022500 rettv->v_type = VAR_NUMBER;
22501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 return;
22503 }
22504 ++depth;
22505
22506 line_breakcheck(); /* check for CTRL-C hit */
22507
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022508 fc = (funccall_T *)alloc(sizeof(funccall_T));
22509 fc->caller = current_funccal;
22510 current_funccal = fc;
22511 fc->func = fp;
22512 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022513 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022514 fc->linenr = 0;
22515 fc->returned = FALSE;
22516 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022518 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22519 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520
Bram Moolenaar33570922005-01-25 22:26:29 +000022521 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022522 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022523 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22524 * each argument variable and saves a lot of time.
22525 */
22526 /*
22527 * Init l: variables.
22528 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022529 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022530 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022531 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022532 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22533 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022534 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022535 name = v->di_key;
22536 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022538 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022540 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022541 v->di_tv.vval.v_dict = selfdict;
22542 ++selfdict->dv_refcount;
22543 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022544
Bram Moolenaar33570922005-01-25 22:26:29 +000022545 /*
22546 * Init a: variables.
22547 * Set a:0 to "argcount".
22548 * Set a:000 to a list with room for the "..." arguments.
22549 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022550 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022551 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022552 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022553 /* Use "name" to avoid a warning from some compiler that checks the
22554 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022555 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022556 name = v->di_key;
22557 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022558 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022559 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022561 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022562 v->di_tv.vval.v_list = &fc->l_varlist;
22563 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22564 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22565 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022566
22567 /*
22568 * Set a:firstline to "firstline" and a:lastline to "lastline".
22569 * Set a:name to named arguments.
22570 * Set a:N to the "..." arguments.
22571 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022572 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022574 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 (varnumber_T)lastline);
22576 for (i = 0; i < argcount; ++i)
22577 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022578 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 if (ai < 0)
22580 /* named argument a:name */
22581 name = FUNCARG(fp, i);
22582 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022583 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022584 /* "..." argument a:1, a:2, etc. */
22585 sprintf((char *)numbuf, "%d", ai + 1);
22586 name = numbuf;
22587 }
22588 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22589 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022590 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022591 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22592 }
22593 else
22594 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022595 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22596 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022597 if (v == NULL)
22598 break;
22599 v->di_flags = DI_FLAGS_RO;
22600 }
22601 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022602 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022603
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022604 /* Note: the values are copied directly to avoid alloc/free.
22605 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022607 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022608
22609 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22610 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022611 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22612 fc->l_listitems[ai].li_tv = argvars[i];
22613 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022614 }
22615 }
22616
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 /* Don't redraw while executing the function. */
22618 ++RedrawingDisabled;
22619 save_sourcing_name = sourcing_name;
22620 save_sourcing_lnum = sourcing_lnum;
22621 sourcing_lnum = 1;
22622 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022623 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 if (sourcing_name != NULL)
22625 {
22626 if (save_sourcing_name != NULL
22627 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22628 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22629 else
22630 STRCPY(sourcing_name, "function ");
22631 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22632
22633 if (p_verbose >= 12)
22634 {
22635 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022636 verbose_enter_scroll();
22637
Bram Moolenaar555b2802005-05-19 21:08:39 +000022638 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 if (p_verbose >= 14)
22640 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022642 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022643 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022644 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645
22646 msg_puts((char_u *)"(");
22647 for (i = 0; i < argcount; ++i)
22648 {
22649 if (i > 0)
22650 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022651 if (argvars[i].v_type == VAR_NUMBER)
22652 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 else
22654 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022655 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22656 if (s != NULL)
22657 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022658 if (vim_strsize(s) > MSG_BUF_CLEN)
22659 {
22660 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22661 s = buf;
22662 }
22663 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022664 vim_free(tofree);
22665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 }
22667 }
22668 msg_puts((char_u *)")");
22669 }
22670 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022671
22672 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 --no_wait_return;
22674 }
22675 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022676#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022677 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022678 {
22679 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22680 func_do_profile(fp);
22681 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022682 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022683 {
22684 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022685 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022686 profile_zero(&fp->uf_tm_children);
22687 }
22688 script_prof_save(&wait_start);
22689 }
22690#endif
22691
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022693 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 save_did_emsg = did_emsg;
22695 did_emsg = FALSE;
22696
22697 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022698 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22700
22701 --RedrawingDisabled;
22702
22703 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022704 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022706 clear_tv(rettv);
22707 rettv->v_type = VAR_NUMBER;
22708 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 }
22710
Bram Moolenaar05159a02005-02-26 23:04:13 +000022711#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022712 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022713 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022714 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022715 profile_end(&call_start);
22716 profile_sub_wait(&wait_start, &call_start);
22717 profile_add(&fp->uf_tm_total, &call_start);
22718 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022719 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022720 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022721 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22722 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022723 }
22724 }
22725#endif
22726
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 /* when being verbose, mention the return value */
22728 if (p_verbose >= 12)
22729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022731 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022734 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022735 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022736 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022737 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022740 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022741 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022742 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022743 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022744
Bram Moolenaar555b2802005-05-19 21:08:39 +000022745 /* The value may be very long. Skip the middle part, so that we
22746 * have some idea how it starts and ends. smsg() would always
22747 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022748 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022749 if (s != NULL)
22750 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022751 if (vim_strsize(s) > MSG_BUF_CLEN)
22752 {
22753 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22754 s = buf;
22755 }
22756 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022757 vim_free(tofree);
22758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 }
22760 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022761
22762 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 --no_wait_return;
22764 }
22765
22766 vim_free(sourcing_name);
22767 sourcing_name = save_sourcing_name;
22768 sourcing_lnum = save_sourcing_lnum;
22769 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022770#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022771 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022772 script_prof_restore(&wait_start);
22773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774
22775 if (p_verbose >= 12 && sourcing_name != NULL)
22776 {
22777 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022778 verbose_enter_scroll();
22779
Bram Moolenaar555b2802005-05-19 21:08:39 +000022780 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022782
22783 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784 --no_wait_return;
22785 }
22786
22787 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022788 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022790
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022791 /* If the a:000 list and the l: and a: dicts are not referenced we can
22792 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022793 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22794 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22795 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22796 {
22797 free_funccal(fc, FALSE);
22798 }
22799 else
22800 {
22801 hashitem_T *hi;
22802 listitem_T *li;
22803 int todo;
22804
22805 /* "fc" is still in use. This can happen when returning "a:000" or
22806 * assigning "l:" to a global variable.
22807 * Link "fc" in the list for garbage collection later. */
22808 fc->caller = previous_funccal;
22809 previous_funccal = fc;
22810
22811 /* Make a copy of the a: variables, since we didn't do that above. */
22812 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22813 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22814 {
22815 if (!HASHITEM_EMPTY(hi))
22816 {
22817 --todo;
22818 v = HI2DI(hi);
22819 copy_tv(&v->di_tv, &v->di_tv);
22820 }
22821 }
22822
22823 /* Make a copy of the a:000 items, since we didn't do that above. */
22824 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22825 copy_tv(&li->li_tv, &li->li_tv);
22826 }
22827}
22828
22829/*
22830 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022831 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022832 */
22833 static int
22834can_free_funccal(fc, copyID)
22835 funccall_T *fc;
22836 int copyID;
22837{
22838 return (fc->l_varlist.lv_copyID != copyID
22839 && fc->l_vars.dv_copyID != copyID
22840 && fc->l_avars.dv_copyID != copyID);
22841}
22842
22843/*
22844 * Free "fc" and what it contains.
22845 */
22846 static void
22847free_funccal(fc, free_val)
22848 funccall_T *fc;
22849 int free_val; /* a: vars were allocated */
22850{
22851 listitem_T *li;
22852
22853 /* The a: variables typevals may not have been allocated, only free the
22854 * allocated variables. */
22855 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22856
22857 /* free all l: variables */
22858 vars_clear(&fc->l_vars.dv_hashtab);
22859
22860 /* Free the a:000 variables if they were allocated. */
22861 if (free_val)
22862 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22863 clear_tv(&li->li_tv);
22864
22865 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866}
22867
22868/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022869 * Add a number variable "name" to dict "dp" with value "nr".
22870 */
22871 static void
22872add_nr_var(dp, v, name, nr)
22873 dict_T *dp;
22874 dictitem_T *v;
22875 char *name;
22876 varnumber_T nr;
22877{
22878 STRCPY(v->di_key, name);
22879 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22880 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22881 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022882 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022883 v->di_tv.vval.v_number = nr;
22884}
22885
22886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 * ":return [expr]"
22888 */
22889 void
22890ex_return(eap)
22891 exarg_T *eap;
22892{
22893 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022894 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 int returning = FALSE;
22896
22897 if (current_funccal == NULL)
22898 {
22899 EMSG(_("E133: :return not inside a function"));
22900 return;
22901 }
22902
22903 if (eap->skip)
22904 ++emsg_skip;
22905
22906 eap->nextcmd = NULL;
22907 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022908 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 {
22910 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022911 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 }
22915 /* It's safer to return also on error. */
22916 else if (!eap->skip)
22917 {
22918 /*
22919 * Return unless the expression evaluation has been cancelled due to an
22920 * aborting error, an interrupt, or an exception.
22921 */
22922 if (!aborting())
22923 returning = do_return(eap, FALSE, TRUE, NULL);
22924 }
22925
22926 /* When skipping or the return gets pending, advance to the next command
22927 * in this line (!returning). Otherwise, ignore the rest of the line.
22928 * Following lines will be ignored by get_func_line(). */
22929 if (returning)
22930 eap->nextcmd = NULL;
22931 else if (eap->nextcmd == NULL) /* no argument */
22932 eap->nextcmd = check_nextcmd(arg);
22933
22934 if (eap->skip)
22935 --emsg_skip;
22936}
22937
22938/*
22939 * Return from a function. Possibly makes the return pending. Also called
22940 * for a pending return at the ":endtry" or after returning from an extra
22941 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022942 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022943 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 * FALSE when the return gets pending.
22945 */
22946 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022947do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 exarg_T *eap;
22949 int reanimate;
22950 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022951 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952{
22953 int idx;
22954 struct condstack *cstack = eap->cstack;
22955
22956 if (reanimate)
22957 /* Undo the return. */
22958 current_funccal->returned = FALSE;
22959
22960 /*
22961 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22962 * not in its finally clause (which then is to be executed next) is found.
22963 * In this case, make the ":return" pending for execution at the ":endtry".
22964 * Otherwise, return normally.
22965 */
22966 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22967 if (idx >= 0)
22968 {
22969 cstack->cs_pending[idx] = CSTP_RETURN;
22970
22971 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022972 /* A pending return again gets pending. "rettv" points to an
22973 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022975 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 else
22977 {
22978 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022979 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022981 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022983 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 {
22985 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022986 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022987 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 else
22989 EMSG(_(e_outofmem));
22990 }
22991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022992 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993
22994 if (reanimate)
22995 {
22996 /* The pending return value could be overwritten by a ":return"
22997 * without argument in a finally clause; reset the default
22998 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022999 current_funccal->rettv->v_type = VAR_NUMBER;
23000 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 }
23002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023003 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 }
23005 else
23006 {
23007 current_funccal->returned = TRUE;
23008
23009 /* If the return is carried out now, store the return value. For
23010 * a return immediately after reanimation, the value is already
23011 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023012 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023014 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023017 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 }
23019 }
23020
23021 return idx < 0;
23022}
23023
23024/*
23025 * Free the variable with a pending return value.
23026 */
23027 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023028discard_pending_return(rettv)
23029 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030{
Bram Moolenaar33570922005-01-25 22:26:29 +000023031 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032}
23033
23034/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023035 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036 * is an allocated string. Used by report_pending() for verbose messages.
23037 */
23038 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023039get_return_cmd(rettv)
23040 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023042 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023043 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023044 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023046 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023047 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023048 if (s == NULL)
23049 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023050
23051 STRCPY(IObuff, ":return ");
23052 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23053 if (STRLEN(s) + 8 >= IOSIZE)
23054 STRCPY(IObuff + IOSIZE - 4, "...");
23055 vim_free(tofree);
23056 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057}
23058
23059/*
23060 * Get next function line.
23061 * Called by do_cmdline() to get the next line.
23062 * Returns allocated string, or NULL for end of function.
23063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 char_u *
23065get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023066 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023068 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069{
Bram Moolenaar33570922005-01-25 22:26:29 +000023070 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023071 ufunc_T *fp = fcp->func;
23072 char_u *retval;
23073 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074
23075 /* If breakpoints have been added/deleted need to check for it. */
23076 if (fcp->dbg_tick != debug_tick)
23077 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023078 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 sourcing_lnum);
23080 fcp->dbg_tick = debug_tick;
23081 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023082#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023083 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023084 func_line_end(cookie);
23085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086
Bram Moolenaar05159a02005-02-26 23:04:13 +000023087 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023088 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23089 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 retval = NULL;
23091 else
23092 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023093 /* Skip NULL lines (continuation lines). */
23094 while (fcp->linenr < gap->ga_len
23095 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23096 ++fcp->linenr;
23097 if (fcp->linenr >= gap->ga_len)
23098 retval = NULL;
23099 else
23100 {
23101 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23102 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023103#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023104 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023105 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023106#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 }
23109
23110 /* Did we encounter a breakpoint? */
23111 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23112 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023113 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023115 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 sourcing_lnum);
23117 fcp->dbg_tick = debug_tick;
23118 }
23119
23120 return retval;
23121}
23122
Bram Moolenaar05159a02005-02-26 23:04:13 +000023123#if defined(FEAT_PROFILE) || defined(PROTO)
23124/*
23125 * Called when starting to read a function line.
23126 * "sourcing_lnum" must be correct!
23127 * When skipping lines it may not actually be executed, but we won't find out
23128 * until later and we need to store the time now.
23129 */
23130 void
23131func_line_start(cookie)
23132 void *cookie;
23133{
23134 funccall_T *fcp = (funccall_T *)cookie;
23135 ufunc_T *fp = fcp->func;
23136
23137 if (fp->uf_profiling && sourcing_lnum >= 1
23138 && sourcing_lnum <= fp->uf_lines.ga_len)
23139 {
23140 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023141 /* Skip continuation lines. */
23142 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23143 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023144 fp->uf_tml_execed = FALSE;
23145 profile_start(&fp->uf_tml_start);
23146 profile_zero(&fp->uf_tml_children);
23147 profile_get_wait(&fp->uf_tml_wait);
23148 }
23149}
23150
23151/*
23152 * Called when actually executing a function line.
23153 */
23154 void
23155func_line_exec(cookie)
23156 void *cookie;
23157{
23158 funccall_T *fcp = (funccall_T *)cookie;
23159 ufunc_T *fp = fcp->func;
23160
23161 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23162 fp->uf_tml_execed = TRUE;
23163}
23164
23165/*
23166 * Called when done with a function line.
23167 */
23168 void
23169func_line_end(cookie)
23170 void *cookie;
23171{
23172 funccall_T *fcp = (funccall_T *)cookie;
23173 ufunc_T *fp = fcp->func;
23174
23175 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23176 {
23177 if (fp->uf_tml_execed)
23178 {
23179 ++fp->uf_tml_count[fp->uf_tml_idx];
23180 profile_end(&fp->uf_tml_start);
23181 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023182 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023183 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23184 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023185 }
23186 fp->uf_tml_idx = -1;
23187 }
23188}
23189#endif
23190
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191/*
23192 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023193 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 */
23195 int
23196func_has_ended(cookie)
23197 void *cookie;
23198{
Bram Moolenaar33570922005-01-25 22:26:29 +000023199 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023200
23201 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23202 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023203 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 || fcp->returned);
23205}
23206
23207/*
23208 * return TRUE if cookie indicates a function which "abort"s on errors.
23209 */
23210 int
23211func_has_abort(cookie)
23212 void *cookie;
23213{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023214 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215}
23216
23217#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23218typedef enum
23219{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023220 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23221 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23222 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223} var_flavour_T;
23224
23225static var_flavour_T var_flavour __ARGS((char_u *varname));
23226
23227 static var_flavour_T
23228var_flavour(varname)
23229 char_u *varname;
23230{
23231 char_u *p = varname;
23232
23233 if (ASCII_ISUPPER(*p))
23234 {
23235 while (*(++p))
23236 if (ASCII_ISLOWER(*p))
23237 return VAR_FLAVOUR_SESSION;
23238 return VAR_FLAVOUR_VIMINFO;
23239 }
23240 else
23241 return VAR_FLAVOUR_DEFAULT;
23242}
23243#endif
23244
23245#if defined(FEAT_VIMINFO) || defined(PROTO)
23246/*
23247 * Restore global vars that start with a capital from the viminfo file
23248 */
23249 int
23250read_viminfo_varlist(virp, writing)
23251 vir_T *virp;
23252 int writing;
23253{
23254 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023255 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023256 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257
23258 if (!writing && (find_viminfo_parameter('!') != NULL))
23259 {
23260 tab = vim_strchr(virp->vir_line + 1, '\t');
23261 if (tab != NULL)
23262 {
23263 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023264 switch (*tab)
23265 {
23266 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023267#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023268 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023269#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023270 case 'D': type = VAR_DICT; break;
23271 case 'L': type = VAR_LIST; break;
23272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273
23274 tab = vim_strchr(tab, '\t');
23275 if (tab != NULL)
23276 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023277 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023278 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023279 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023281#ifdef FEAT_FLOAT
23282 else if (type == VAR_FLOAT)
23283 (void)string2float(tab + 1, &tv.vval.v_float);
23284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023286 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023287 if (type == VAR_DICT || type == VAR_LIST)
23288 {
23289 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23290
23291 if (etv == NULL)
23292 /* Failed to parse back the dict or list, use it as a
23293 * string. */
23294 tv.v_type = VAR_STRING;
23295 else
23296 {
23297 vim_free(tv.vval.v_string);
23298 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023299 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023300 }
23301 }
23302
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023303 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023304
23305 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023306 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023307 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23308 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 }
23310 }
23311 }
23312
23313 return viminfo_readline(virp);
23314}
23315
23316/*
23317 * Write global vars that start with a capital to the viminfo file
23318 */
23319 void
23320write_viminfo_varlist(fp)
23321 FILE *fp;
23322{
Bram Moolenaar33570922005-01-25 22:26:29 +000023323 hashitem_T *hi;
23324 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023325 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023326 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023327 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023328 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023329 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330
23331 if (find_viminfo_parameter('!') == NULL)
23332 return;
23333
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023334 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023335
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023336 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023339 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023341 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023342 this_var = HI2DI(hi);
23343 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023344 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023345 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023346 {
23347 case VAR_STRING: s = "STR"; break;
23348 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023349#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023350 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023351#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023352 case VAR_DICT: s = "DIC"; break;
23353 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023354 default: continue;
23355 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023356 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023357 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023358 if (p != NULL)
23359 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023360 vim_free(tofree);
23361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 }
23363 }
23364}
23365#endif
23366
23367#if defined(FEAT_SESSION) || defined(PROTO)
23368 int
23369store_session_globals(fd)
23370 FILE *fd;
23371{
Bram Moolenaar33570922005-01-25 22:26:29 +000023372 hashitem_T *hi;
23373 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023374 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375 char_u *p, *t;
23376
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023377 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023378 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023380 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023382 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023383 this_var = HI2DI(hi);
23384 if ((this_var->di_tv.v_type == VAR_NUMBER
23385 || this_var->di_tv.v_type == VAR_STRING)
23386 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023387 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023388 /* Escape special characters with a backslash. Turn a LF and
23389 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023390 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023391 (char_u *)"\\\"\n\r");
23392 if (p == NULL) /* out of memory */
23393 break;
23394 for (t = p; *t != NUL; ++t)
23395 if (*t == '\n')
23396 *t = 'n';
23397 else if (*t == '\r')
23398 *t = 'r';
23399 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023400 this_var->di_key,
23401 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23402 : ' ',
23403 p,
23404 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23405 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023406 || put_eol(fd) == FAIL)
23407 {
23408 vim_free(p);
23409 return FAIL;
23410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 vim_free(p);
23412 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023413#ifdef FEAT_FLOAT
23414 else if (this_var->di_tv.v_type == VAR_FLOAT
23415 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23416 {
23417 float_T f = this_var->di_tv.vval.v_float;
23418 int sign = ' ';
23419
23420 if (f < 0)
23421 {
23422 f = -f;
23423 sign = '-';
23424 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023425 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023426 this_var->di_key, sign, f) < 0)
23427 || put_eol(fd) == FAIL)
23428 return FAIL;
23429 }
23430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431 }
23432 }
23433 return OK;
23434}
23435#endif
23436
Bram Moolenaar661b1822005-07-28 22:36:45 +000023437/*
23438 * Display script name where an item was last set.
23439 * Should only be invoked when 'verbose' is non-zero.
23440 */
23441 void
23442last_set_msg(scriptID)
23443 scid_T scriptID;
23444{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023445 char_u *p;
23446
Bram Moolenaar661b1822005-07-28 22:36:45 +000023447 if (scriptID != 0)
23448 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023449 p = home_replace_save(NULL, get_scriptname(scriptID));
23450 if (p != NULL)
23451 {
23452 verbose_enter();
23453 MSG_PUTS(_("\n\tLast set from "));
23454 MSG_PUTS(p);
23455 vim_free(p);
23456 verbose_leave();
23457 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023458 }
23459}
23460
Bram Moolenaard812df62008-11-09 12:46:09 +000023461/*
23462 * List v:oldfiles in a nice way.
23463 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023464 void
23465ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023466 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023467{
23468 list_T *l = vimvars[VV_OLDFILES].vv_list;
23469 listitem_T *li;
23470 int nr = 0;
23471
23472 if (l == NULL)
23473 msg((char_u *)_("No old files"));
23474 else
23475 {
23476 msg_start();
23477 msg_scroll = TRUE;
23478 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23479 {
23480 msg_outnum((long)++nr);
23481 MSG_PUTS(": ");
23482 msg_outtrans(get_tv_string(&li->li_tv));
23483 msg_putchar('\n');
23484 out_flush(); /* output one line at a time */
23485 ui_breakcheck();
23486 }
23487 /* Assume "got_int" was set to truncate the listing. */
23488 got_int = FALSE;
23489
23490#ifdef FEAT_BROWSE_CMD
23491 if (cmdmod.browse)
23492 {
23493 quit_more = FALSE;
23494 nr = prompt_for_number(FALSE);
23495 msg_starthere();
23496 if (nr > 0)
23497 {
23498 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23499 (long)nr);
23500
23501 if (p != NULL)
23502 {
23503 p = expand_env_save(p);
23504 eap->arg = p;
23505 eap->cmdidx = CMD_edit;
23506 cmdmod.browse = FALSE;
23507 do_exedit(eap, NULL);
23508 vim_free(p);
23509 }
23510 }
23511 }
23512#endif
23513 }
23514}
23515
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516#endif /* FEAT_EVAL */
23517
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023519#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520
23521#ifdef WIN3264
23522/*
23523 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23524 */
23525static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23526static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23527static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23528
23529/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023530 * Get the short path (8.3) for the filename in "fnamep".
23531 * Only works for a valid file name.
23532 * When the path gets longer "fnamep" is changed and the allocated buffer
23533 * is put in "bufp".
23534 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23535 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 */
23537 static int
23538get_short_pathname(fnamep, bufp, fnamelen)
23539 char_u **fnamep;
23540 char_u **bufp;
23541 int *fnamelen;
23542{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023543 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 char_u *newbuf;
23545
23546 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 l = GetShortPathName(*fnamep, *fnamep, len);
23548 if (l > len - 1)
23549 {
23550 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023551 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 newbuf = vim_strnsave(*fnamep, l);
23553 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023554 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555
23556 vim_free(*bufp);
23557 *fnamep = *bufp = newbuf;
23558
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023559 /* Really should always succeed, as the buffer is big enough. */
23560 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 }
23562
23563 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023564 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565}
23566
23567/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023568 * Get the short path (8.3) for the filename in "fname". The converted
23569 * path is returned in "bufp".
23570 *
23571 * Some of the directories specified in "fname" may not exist. This function
23572 * will shorten the existing directories at the beginning of the path and then
23573 * append the remaining non-existing path.
23574 *
23575 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023576 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023577 * bufp - Pointer to an allocated buffer for the filename.
23578 * fnamelen - Length of the filename pointed to by fname
23579 *
23580 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 */
23582 static int
23583shortpath_for_invalid_fname(fname, bufp, fnamelen)
23584 char_u **fname;
23585 char_u **bufp;
23586 int *fnamelen;
23587{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023588 char_u *short_fname, *save_fname, *pbuf_unused;
23589 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023591 int old_len, len;
23592 int new_len, sfx_len;
23593 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594
23595 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023596 old_len = *fnamelen;
23597 save_fname = vim_strnsave(*fname, old_len);
23598 pbuf_unused = NULL;
23599 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023601 endp = save_fname + old_len - 1; /* Find the end of the copy */
23602 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023604 /*
23605 * Try shortening the supplied path till it succeeds by removing one
23606 * directory at a time from the tail of the path.
23607 */
23608 len = 0;
23609 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023611 /* go back one path-separator */
23612 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23613 --endp;
23614 if (endp <= save_fname)
23615 break; /* processed the complete path */
23616
23617 /*
23618 * Replace the path separator with a NUL and try to shorten the
23619 * resulting path.
23620 */
23621 ch = *endp;
23622 *endp = 0;
23623 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023624 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023625 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23626 {
23627 retval = FAIL;
23628 goto theend;
23629 }
23630 *endp = ch; /* preserve the string */
23631
23632 if (len > 0)
23633 break; /* successfully shortened the path */
23634
23635 /* failed to shorten the path. Skip the path separator */
23636 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 }
23638
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023639 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023641 /*
23642 * Succeeded in shortening the path. Now concatenate the shortened
23643 * path with the remaining path at the tail.
23644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023646 /* Compute the length of the new path. */
23647 sfx_len = (int)(save_endp - endp) + 1;
23648 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023650 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023652 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023654 /* There is not enough space in the currently allocated string,
23655 * copy it to a buffer big enough. */
23656 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023658 {
23659 retval = FAIL;
23660 goto theend;
23661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662 }
23663 else
23664 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023665 /* Transfer short_fname to the main buffer (it's big enough),
23666 * unless get_short_pathname() did its work in-place. */
23667 *fname = *bufp = save_fname;
23668 if (short_fname != save_fname)
23669 vim_strncpy(save_fname, short_fname, len);
23670 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023672
23673 /* concat the not-shortened part of the path */
23674 vim_strncpy(*fname + len, endp, sfx_len);
23675 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023677
23678theend:
23679 vim_free(pbuf_unused);
23680 vim_free(save_fname);
23681
23682 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683}
23684
23685/*
23686 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023687 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023688 */
23689 static int
23690shortpath_for_partial(fnamep, bufp, fnamelen)
23691 char_u **fnamep;
23692 char_u **bufp;
23693 int *fnamelen;
23694{
23695 int sepcount, len, tflen;
23696 char_u *p;
23697 char_u *pbuf, *tfname;
23698 int hasTilde;
23699
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023700 /* Count up the path separators from the RHS.. so we know which part
23701 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023703 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 if (vim_ispathsep(*p))
23705 ++sepcount;
23706
23707 /* Need full path first (use expand_env() to remove a "~/") */
23708 hasTilde = (**fnamep == '~');
23709 if (hasTilde)
23710 pbuf = tfname = expand_env_save(*fnamep);
23711 else
23712 pbuf = tfname = FullName_save(*fnamep, FALSE);
23713
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023714 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023716 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23717 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718
23719 if (len == 0)
23720 {
23721 /* Don't have a valid filename, so shorten the rest of the
23722 * path if we can. This CAN give us invalid 8.3 filenames, but
23723 * there's not a lot of point in guessing what it might be.
23724 */
23725 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023726 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23727 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 }
23729
23730 /* Count the paths backward to find the beginning of the desired string. */
23731 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023732 {
23733#ifdef FEAT_MBYTE
23734 if (has_mbyte)
23735 p -= mb_head_off(tfname, p);
23736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 if (vim_ispathsep(*p))
23738 {
23739 if (sepcount == 0 || (hasTilde && sepcount == 1))
23740 break;
23741 else
23742 sepcount --;
23743 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 if (hasTilde)
23746 {
23747 --p;
23748 if (p >= tfname)
23749 *p = '~';
23750 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023751 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 }
23753 else
23754 ++p;
23755
23756 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23757 vim_free(*bufp);
23758 *fnamelen = (int)STRLEN(p);
23759 *bufp = pbuf;
23760 *fnamep = p;
23761
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023762 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763}
23764#endif /* WIN3264 */
23765
23766/*
23767 * Adjust a filename, according to a string of modifiers.
23768 * *fnamep must be NUL terminated when called. When returning, the length is
23769 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023770 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 * When there is an error, *fnamep is set to NULL.
23772 */
23773 int
23774modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23775 char_u *src; /* string with modifiers */
23776 int *usedlen; /* characters after src that are used */
23777 char_u **fnamep; /* file name so far */
23778 char_u **bufp; /* buffer for allocated file name or NULL */
23779 int *fnamelen; /* length of fnamep */
23780{
23781 int valid = 0;
23782 char_u *tail;
23783 char_u *s, *p, *pbuf;
23784 char_u dirname[MAXPATHL];
23785 int c;
23786 int has_fullname = 0;
23787#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023788 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023789 int has_shortname = 0;
23790#endif
23791
23792repeat:
23793 /* ":p" - full path/file_name */
23794 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23795 {
23796 has_fullname = 1;
23797
23798 valid |= VALID_PATH;
23799 *usedlen += 2;
23800
23801 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23802 if ((*fnamep)[0] == '~'
23803#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23804 && ((*fnamep)[1] == '/'
23805# ifdef BACKSLASH_IN_FILENAME
23806 || (*fnamep)[1] == '\\'
23807# endif
23808 || (*fnamep)[1] == NUL)
23809
23810#endif
23811 )
23812 {
23813 *fnamep = expand_env_save(*fnamep);
23814 vim_free(*bufp); /* free any allocated file name */
23815 *bufp = *fnamep;
23816 if (*fnamep == NULL)
23817 return -1;
23818 }
23819
23820 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023821 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 {
23823 if (vim_ispathsep(*p)
23824 && p[1] == '.'
23825 && (p[2] == NUL
23826 || vim_ispathsep(p[2])
23827 || (p[2] == '.'
23828 && (p[3] == NUL || vim_ispathsep(p[3])))))
23829 break;
23830 }
23831
23832 /* FullName_save() is slow, don't use it when not needed. */
23833 if (*p != NUL || !vim_isAbsName(*fnamep))
23834 {
23835 *fnamep = FullName_save(*fnamep, *p != NUL);
23836 vim_free(*bufp); /* free any allocated file name */
23837 *bufp = *fnamep;
23838 if (*fnamep == NULL)
23839 return -1;
23840 }
23841
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023842#ifdef WIN3264
23843# if _WIN32_WINNT >= 0x0500
23844 if (vim_strchr(*fnamep, '~') != NULL)
23845 {
23846 /* Expand 8.3 filename to full path. Needed to make sure the same
23847 * file does not have two different names.
23848 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23849 p = alloc(_MAX_PATH + 1);
23850 if (p != NULL)
23851 {
23852 if (GetLongPathName(*fnamep, p, MAXPATHL))
23853 {
23854 vim_free(*bufp);
23855 *bufp = *fnamep = p;
23856 }
23857 else
23858 vim_free(p);
23859 }
23860 }
23861# endif
23862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 /* Append a path separator to a directory. */
23864 if (mch_isdir(*fnamep))
23865 {
23866 /* Make room for one or two extra characters. */
23867 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23868 vim_free(*bufp); /* free any allocated file name */
23869 *bufp = *fnamep;
23870 if (*fnamep == NULL)
23871 return -1;
23872 add_pathsep(*fnamep);
23873 }
23874 }
23875
23876 /* ":." - path relative to the current directory */
23877 /* ":~" - path relative to the home directory */
23878 /* ":8" - shortname path - postponed till after */
23879 while (src[*usedlen] == ':'
23880 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23881 {
23882 *usedlen += 2;
23883 if (c == '8')
23884 {
23885#ifdef WIN3264
23886 has_shortname = 1; /* Postpone this. */
23887#endif
23888 continue;
23889 }
23890 pbuf = NULL;
23891 /* Need full path first (use expand_env() to remove a "~/") */
23892 if (!has_fullname)
23893 {
23894 if (c == '.' && **fnamep == '~')
23895 p = pbuf = expand_env_save(*fnamep);
23896 else
23897 p = pbuf = FullName_save(*fnamep, FALSE);
23898 }
23899 else
23900 p = *fnamep;
23901
23902 has_fullname = 0;
23903
23904 if (p != NULL)
23905 {
23906 if (c == '.')
23907 {
23908 mch_dirname(dirname, MAXPATHL);
23909 s = shorten_fname(p, dirname);
23910 if (s != NULL)
23911 {
23912 *fnamep = s;
23913 if (pbuf != NULL)
23914 {
23915 vim_free(*bufp); /* free any allocated file name */
23916 *bufp = pbuf;
23917 pbuf = NULL;
23918 }
23919 }
23920 }
23921 else
23922 {
23923 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23924 /* Only replace it when it starts with '~' */
23925 if (*dirname == '~')
23926 {
23927 s = vim_strsave(dirname);
23928 if (s != NULL)
23929 {
23930 *fnamep = s;
23931 vim_free(*bufp);
23932 *bufp = s;
23933 }
23934 }
23935 }
23936 vim_free(pbuf);
23937 }
23938 }
23939
23940 tail = gettail(*fnamep);
23941 *fnamelen = (int)STRLEN(*fnamep);
23942
23943 /* ":h" - head, remove "/file_name", can be repeated */
23944 /* Don't remove the first "/" or "c:\" */
23945 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23946 {
23947 valid |= VALID_HEAD;
23948 *usedlen += 2;
23949 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023950 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023951 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 *fnamelen = (int)(tail - *fnamep);
23953#ifdef VMS
23954 if (*fnamelen > 0)
23955 *fnamelen += 1; /* the path separator is part of the path */
23956#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023957 if (*fnamelen == 0)
23958 {
23959 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23960 p = vim_strsave((char_u *)".");
23961 if (p == NULL)
23962 return -1;
23963 vim_free(*bufp);
23964 *bufp = *fnamep = tail = p;
23965 *fnamelen = 1;
23966 }
23967 else
23968 {
23969 while (tail > s && !after_pathsep(s, tail))
23970 mb_ptr_back(*fnamep, tail);
23971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 }
23973
23974 /* ":8" - shortname */
23975 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23976 {
23977 *usedlen += 2;
23978#ifdef WIN3264
23979 has_shortname = 1;
23980#endif
23981 }
23982
23983#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023984 /*
23985 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 */
23987 if (has_shortname)
23988 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023989 /* Copy the string if it is shortened by :h and when it wasn't copied
23990 * yet, because we are going to change it in place. Avoids changing
23991 * the buffer name for "%:8". */
23992 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 {
23994 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023995 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 return -1;
23997 vim_free(*bufp);
23998 *bufp = *fnamep = p;
23999 }
24000
24001 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024002 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 if (!has_fullname && !vim_isAbsName(*fnamep))
24004 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024005 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 return -1;
24007 }
24008 else
24009 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024010 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011
Bram Moolenaardc935552011-08-17 15:23:23 +020024012 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024014 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 return -1;
24016
24017 if (l == 0)
24018 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024019 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024021 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 return -1;
24023 }
24024 *fnamelen = l;
24025 }
24026 }
24027#endif /* WIN3264 */
24028
24029 /* ":t" - tail, just the basename */
24030 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24031 {
24032 *usedlen += 2;
24033 *fnamelen -= (int)(tail - *fnamep);
24034 *fnamep = tail;
24035 }
24036
24037 /* ":e" - extension, can be repeated */
24038 /* ":r" - root, without extension, can be repeated */
24039 while (src[*usedlen] == ':'
24040 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24041 {
24042 /* find a '.' in the tail:
24043 * - for second :e: before the current fname
24044 * - otherwise: The last '.'
24045 */
24046 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24047 s = *fnamep - 2;
24048 else
24049 s = *fnamep + *fnamelen - 1;
24050 for ( ; s > tail; --s)
24051 if (s[0] == '.')
24052 break;
24053 if (src[*usedlen + 1] == 'e') /* :e */
24054 {
24055 if (s > tail)
24056 {
24057 *fnamelen += (int)(*fnamep - (s + 1));
24058 *fnamep = s + 1;
24059#ifdef VMS
24060 /* cut version from the extension */
24061 s = *fnamep + *fnamelen - 1;
24062 for ( ; s > *fnamep; --s)
24063 if (s[0] == ';')
24064 break;
24065 if (s > *fnamep)
24066 *fnamelen = s - *fnamep;
24067#endif
24068 }
24069 else if (*fnamep <= tail)
24070 *fnamelen = 0;
24071 }
24072 else /* :r */
24073 {
24074 if (s > tail) /* remove one extension */
24075 *fnamelen = (int)(s - *fnamep);
24076 }
24077 *usedlen += 2;
24078 }
24079
24080 /* ":s?pat?foo?" - substitute */
24081 /* ":gs?pat?foo?" - global substitute */
24082 if (src[*usedlen] == ':'
24083 && (src[*usedlen + 1] == 's'
24084 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24085 {
24086 char_u *str;
24087 char_u *pat;
24088 char_u *sub;
24089 int sep;
24090 char_u *flags;
24091 int didit = FALSE;
24092
24093 flags = (char_u *)"";
24094 s = src + *usedlen + 2;
24095 if (src[*usedlen + 1] == 'g')
24096 {
24097 flags = (char_u *)"g";
24098 ++s;
24099 }
24100
24101 sep = *s++;
24102 if (sep)
24103 {
24104 /* find end of pattern */
24105 p = vim_strchr(s, sep);
24106 if (p != NULL)
24107 {
24108 pat = vim_strnsave(s, (int)(p - s));
24109 if (pat != NULL)
24110 {
24111 s = p + 1;
24112 /* find end of substitution */
24113 p = vim_strchr(s, sep);
24114 if (p != NULL)
24115 {
24116 sub = vim_strnsave(s, (int)(p - s));
24117 str = vim_strnsave(*fnamep, *fnamelen);
24118 if (sub != NULL && str != NULL)
24119 {
24120 *usedlen = (int)(p + 1 - src);
24121 s = do_string_sub(str, pat, sub, flags);
24122 if (s != NULL)
24123 {
24124 *fnamep = s;
24125 *fnamelen = (int)STRLEN(s);
24126 vim_free(*bufp);
24127 *bufp = s;
24128 didit = TRUE;
24129 }
24130 }
24131 vim_free(sub);
24132 vim_free(str);
24133 }
24134 vim_free(pat);
24135 }
24136 }
24137 /* after using ":s", repeat all the modifiers */
24138 if (didit)
24139 goto repeat;
24140 }
24141 }
24142
24143 return valid;
24144}
24145
24146/*
24147 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24148 * "flags" can be "g" to do a global substitute.
24149 * Returns an allocated string, NULL for error.
24150 */
24151 char_u *
24152do_string_sub(str, pat, sub, flags)
24153 char_u *str;
24154 char_u *pat;
24155 char_u *sub;
24156 char_u *flags;
24157{
24158 int sublen;
24159 regmatch_T regmatch;
24160 int i;
24161 int do_all;
24162 char_u *tail;
24163 garray_T ga;
24164 char_u *ret;
24165 char_u *save_cpo;
24166
24167 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24168 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024169 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170
24171 ga_init2(&ga, 1, 200);
24172
24173 do_all = (flags[0] == 'g');
24174
24175 regmatch.rm_ic = p_ic;
24176 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24177 if (regmatch.regprog != NULL)
24178 {
24179 tail = str;
24180 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24181 {
24182 /*
24183 * Get some space for a temporary buffer to do the substitution
24184 * into. It will contain:
24185 * - The text up to where the match is.
24186 * - The substituted text.
24187 * - The text after the match.
24188 */
24189 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24190 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24191 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24192 {
24193 ga_clear(&ga);
24194 break;
24195 }
24196
24197 /* copy the text up to where the match is */
24198 i = (int)(regmatch.startp[0] - tail);
24199 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24200 /* add the substituted text */
24201 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24202 + ga.ga_len + i, TRUE, TRUE, FALSE);
24203 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 /* avoid getting stuck on a match with an empty string */
24205 if (tail == regmatch.endp[0])
24206 {
24207 if (*tail == NUL)
24208 break;
24209 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24210 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 }
24212 else
24213 {
24214 tail = regmatch.endp[0];
24215 if (*tail == NUL)
24216 break;
24217 }
24218 if (!do_all)
24219 break;
24220 }
24221
24222 if (ga.ga_data != NULL)
24223 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24224
24225 vim_free(regmatch.regprog);
24226 }
24227
24228 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24229 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024230 if (p_cpo == empty_option)
24231 p_cpo = save_cpo;
24232 else
24233 /* Darn, evaluating {sub} expression changed the value. */
24234 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235
24236 return ret;
24237}
24238
24239#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */