blob: 7250556fc22b1c504cff682c45ae80666f74dde1 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200125/* When using exists() don't auto-load a script. */
126static int no_autoload = FALSE;
127
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
158 * Structure to hold info for a user function.
159 */
160typedef struct ufunc ufunc_T;
161
162struct ufunc
163{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000164 int uf_varargs; /* variable nr of arguments */
165 int uf_flags;
166 int uf_calls; /* nr of active calls */
167 garray_T uf_args; /* arguments */
168 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169#ifdef FEAT_PROFILE
170 int uf_profiling; /* TRUE when func is being profiled */
171 /* profiling the function as a whole */
172 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000173 proftime_T uf_tm_total; /* time spent in function + children */
174 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175 proftime_T uf_tm_children; /* time spent in children this call */
176 /* profiling the function per line */
177 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T *uf_tml_total; /* time spent in a line + children */
179 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tml_start; /* start time for current line */
181 proftime_T uf_tml_children; /* time spent in children for this line */
182 proftime_T uf_tml_wait; /* start wait time for current line */
183 int uf_tml_idx; /* index of line being timed; -1 if none */
184 int uf_tml_execed; /* line being timed was executed */
185#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000186 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000188 int uf_refcount; /* for numbered function: reference count */
189 char_u uf_name[1]; /* name of function (actually longer); can
190 start with <SNR>123_ (<SNR> is K_SPECIAL
191 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192};
193
194/* function flags */
195#define FC_ABORT 1 /* abort function on error */
196#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000197#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000200 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000202static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000204/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000205static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
206
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207/* list heads for garbage collection */
208static dict_T *first_dict = NULL; /* list of all dicts */
209static list_T *first_list = NULL; /* list of all lists */
210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000211/* From user function to hashitem and back. */
212static ufunc_T dumuf;
213#define UF2HIKEY(fp) ((fp)->uf_name)
214#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
215#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
216
217#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
218#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaar33570922005-01-25 22:26:29 +0000220#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
221#define VAR_SHORT_LEN 20 /* short variable name length */
222#define FIXVAR_CNT 12 /* number of fixed variables */
223
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000225typedef struct funccall_S funccall_T;
226
227struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 ufunc_T *func; /* function being called */
230 int linenr; /* next line to be executed */
231 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000232 struct /* fixed variables for arguments */
233 {
234 dictitem_T var; /* variable (without room for name) */
235 char_u room[VAR_SHORT_LEN]; /* room for the name */
236 } fixvar[FIXVAR_CNT];
237 dict_T l_vars; /* l: local function variables */
238 dictitem_T l_vars_var; /* variable for l: scope */
239 dict_T l_avars; /* a: argument variables */
240 dictitem_T l_avars_var; /* variable for a: scope */
241 list_T l_varlist; /* list for a:000 */
242 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
243 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 linenr_T breakpoint; /* next line with breakpoint or zero */
245 int dbg_tick; /* debug_tick when breakpoint was set */
246 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000247#ifdef FEAT_PROFILE
248 proftime_T prof_child; /* time spent in a child */
249#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000250 funccall_T *caller; /* calling function or NULL */
251};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
253/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000254 * Info used by a ":for" loop.
255 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000256typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257{
258 int fi_semicolon; /* TRUE if ending in '; var]' */
259 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260 listwatch_T fi_lw; /* keep an eye on the item used. */
261 list_T *fi_list; /* list being used */
262} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000265 * Struct used by trans_function_name()
266 */
267typedef struct
268{
Bram Moolenaar33570922005-01-25 22:26:29 +0000269 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000270 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 dictitem_T *fd_di; /* Dictionary item used */
272} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273
Bram Moolenaara7043832005-01-21 11:56:39 +0000274
275/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 * Array to hold the value of v: variables.
277 * The value is in a dictitem, so that it can also be used in the v: scope.
278 * The reason to use this table anyway is for very quick access to the
279 * variables with the VV_ defines.
280 */
281#include "version.h"
282
283/* values for vv_flags: */
284#define VV_COMPAT 1 /* compatible, also used without "v:" */
285#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000286#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000288#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000289
290static struct vimvar
291{
292 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000293 dictitem_T vv_di; /* value and name for key */
294 char vv_filler[16]; /* space for LONGEST name below!!! */
295 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
296} vimvars[VV_LEN] =
297{
298 /*
299 * The order here must match the VV_ defines in vim.h!
300 * Initializing a union does not work, leave tv.vval empty to get zero's.
301 */
302 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("count1", VAR_NUMBER), VV_RO},
304 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
305 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
306 {VV_NAME("warningmsg", VAR_STRING), 0},
307 {VV_NAME("statusmsg", VAR_STRING), 0},
308 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
310 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
312 {VV_NAME("termresponse", VAR_STRING), VV_RO},
313 {VV_NAME("fname", VAR_STRING), VV_RO},
314 {VV_NAME("lang", VAR_STRING), VV_RO},
315 {VV_NAME("lc_time", VAR_STRING), VV_RO},
316 {VV_NAME("ctype", VAR_STRING), VV_RO},
317 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
319 {VV_NAME("fname_in", VAR_STRING), VV_RO},
320 {VV_NAME("fname_out", VAR_STRING), VV_RO},
321 {VV_NAME("fname_new", VAR_STRING), VV_RO},
322 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
323 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
324 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
325 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
327 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("progname", VAR_STRING), VV_RO},
329 {VV_NAME("servername", VAR_STRING), VV_RO},
330 {VV_NAME("dying", VAR_NUMBER), VV_RO},
331 {VV_NAME("exception", VAR_STRING), VV_RO},
332 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
333 {VV_NAME("register", VAR_STRING), VV_RO},
334 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
335 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000336 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
337 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000338 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000339 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
340 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000341 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000346 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000347 {VV_NAME("swapname", VAR_STRING), VV_RO},
348 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000349 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200350 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000351 {VV_NAME("mouse_win", VAR_NUMBER), 0},
352 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
353 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000354 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000355 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000356 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200357 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000358};
359
360/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361#define vv_type vv_di.di_tv.v_type
362#define vv_nr vv_di.di_tv.vval.v_number
363#define vv_float vv_di.di_tv.vval.v_float
364#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000365#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000367
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200368static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000369#define vimvarht vimvardict.dv_hashtab
370
Bram Moolenaara40058a2005-07-11 22:42:07 +0000371static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
372static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000373static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
374static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
375static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000376static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
377static void list_glob_vars __ARGS((int *first));
378static void list_buf_vars __ARGS((int *first));
379static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000380#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000381static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_vim_vars __ARGS((int *first));
384static void list_script_vars __ARGS((int *first));
385static void list_func_vars __ARGS((int *first));
386static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
388static int check_changedtick __ARGS((char_u *arg));
389static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
390static void clear_lval __ARGS((lval_T *lp));
391static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
392static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
394static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
395static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
396static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
397static void item_lock __ARGS((typval_T *tv, int deep, int lock));
398static int tv_islocked __ARGS((typval_T *tv));
399
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
401static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000406static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
407static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000409static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000414static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100416static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
417static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
418static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000419static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000421static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
423static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000424static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000425static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100426static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000427static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000428static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200429static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
431static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
437static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000439#ifdef FEAT_FLOAT
440static int string2float __ARGS((char_u *text, float_T *value));
441#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
443static int find_internal_func __ARGS((char_u *name));
444static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
445static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200446static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000447static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000448static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000450#ifdef FEAT_FLOAT
451static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200452static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100455static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200463static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
478#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000479static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000482static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000484#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000485static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000486static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
488#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#ifdef FEAT_FLOAT
492static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200493static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
498static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200509#ifdef FEAT_FLOAT
510static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000514static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#ifdef FEAT_FLOAT
521static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000524#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000525static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000534static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000536static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000542static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000550static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000551static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000552static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000553static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200556static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000557static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000565static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000579static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100584static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000586static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000598#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200599static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000600static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
601#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200602#ifdef FEAT_LUA
603static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000609static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000610static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000611static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000613static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000617#ifdef vim_mkdir
618static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100621#ifdef FEAT_MZSCHEME
622static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100626static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000628#ifdef FEAT_FLOAT
629static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000632static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000633static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200634#ifdef FEAT_PYTHON3
635static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
637#ifdef FEAT_PYTHON
638static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000642static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
655static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100657static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000660static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000662static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000669static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000670static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000671static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000672static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200674static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000675static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100677#ifdef FEAT_CRYPT
678static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
679#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000680static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200681static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000683#ifdef FEAT_FLOAT
684static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200685static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000688static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000689static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
693static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
695#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000696static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200697static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698#ifdef HAVE_STRFTIME
699static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
701static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200707static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000714static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200715static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000717static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000718static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000719static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000720static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000721static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000723static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200724#ifdef FEAT_FLOAT
725static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
727#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#ifdef FEAT_FLOAT
732static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200735static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200736static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000737static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100740static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000747static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000750static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100751static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000753static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000754static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static int get_env_len __ARGS((char_u **arg));
756static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000758static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
759#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
760#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
761 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000762static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
766static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static typval_T *alloc_tv __ARGS((void));
768static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void init_tv __ARGS((typval_T *varp));
770static long get_tv_number __ARGS((typval_T *varp));
771static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000772static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static char_u *get_tv_string __ARGS((typval_T *varp));
774static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000775static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200777static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
779static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
780static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000781static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
782static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
784static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000785static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200786static int var_check_func_name __ARGS((char_u *name, int new_var));
787static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000788static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000789static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
791static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
792static int eval_fname_script __ARGS((char_u *p));
793static int eval_fname_sid __ARGS((char_u *p));
794static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static ufunc_T *find_func __ARGS((char_u *name));
796static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000797static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798#ifdef FEAT_PROFILE
799static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000800static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
801static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
802static int
803# ifdef __BORLANDC__
804 _RTLENTRYF
805# endif
806 prof_total_cmp __ARGS((const void *s1, const void *s2));
807static int
808# ifdef __BORLANDC__
809 _RTLENTRYF
810# endif
811 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200813static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000814static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000815static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000817static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000818static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
819static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000821static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
822static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000823static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000824static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200827
828#ifdef EBCDIC
829static int compare_func_name __ARGS((const void *s1, const void *s2));
830static void sortFunctions __ARGS(());
831#endif
832
Bram Moolenaar33570922005-01-25 22:26:29 +0000833/*
834 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000835 */
836 void
837eval_init()
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 int i;
840 struct vimvar *p;
841
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200842 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
843 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200844 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000845 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000847
848 for (i = 0; i < VV_LEN; ++i)
849 {
850 p = &vimvars[i];
851 STRCPY(p->vv_di.di_key, p->vv_name);
852 if (p->vv_flags & VV_RO)
853 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
854 else if (p->vv_flags & VV_RO_SBX)
855 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
856 else
857 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000858
859 /* add to v: scope dict, unless the value is not always available */
860 if (p->vv_type != VAR_UNKNOWN)
861 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000863 /* add to compat scope dict */
864 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000866 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200867 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200868
869#ifdef EBCDIC
870 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100871 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200872 */
873 sortFunctions();
874#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000875}
876
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000877#if defined(EXITFREE) || defined(PROTO)
878 void
879eval_clear()
880{
881 int i;
882 struct vimvar *p;
883
884 for (i = 0; i < VV_LEN; ++i)
885 {
886 p = &vimvars[i];
887 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000888 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000889 vim_free(p->vv_str);
890 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000891 }
892 else if (p->vv_di.di_tv.v_type == VAR_LIST)
893 {
894 list_unref(p->vv_list);
895 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000896 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000897 }
898 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000899 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000900 hash_clear(&compat_hashtab);
901
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100903# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200904 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100905# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000906
907 /* global variables */
908 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000909
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000910 /* autoloaded script names */
911 ga_clear_strings(&ga_loaded);
912
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200913 /* script-local variables */
914 for (i = 1; i <= ga_scripts.ga_len; ++i)
915 {
916 vars_clear(&SCRIPT_VARS(i));
917 vim_free(SCRIPT_SV(i));
918 }
919 ga_clear(&ga_scripts);
920
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000921 /* unreferenced lists and dicts */
922 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000923
924 /* functions */
925 free_all_functions();
926 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000927}
928#endif
929
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 * Return the name of the executed function.
932 */
933 char_u *
934func_name(cookie)
935 void *cookie;
936{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000937 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938}
939
940/*
941 * Return the address holding the next breakpoint line for a funccall cookie.
942 */
943 linenr_T *
944func_breakpoint(cookie)
945 void *cookie;
946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/*
951 * Return the address holding the debug tick for a funccall cookie.
952 */
953 int *
954func_dbg_tick(cookie)
955 void *cookie;
956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958}
959
960/*
961 * Return the nesting level for a funccall cookie.
962 */
963 int
964func_level(cookie)
965 void *cookie;
966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000971funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000973/* pointer to list of previously used funccal, still around because some
974 * item in it is still being used. */
975funccall_T *previous_funccal = NULL;
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977/*
978 * Return TRUE when a function was ended by a ":return" command.
979 */
980 int
981current_func_returned()
982{
983 return current_funccal->returned;
984}
985
986
987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 * Set an internal variable to a string value. Creates the variable if it does
989 * not already exist.
990 */
991 void
992set_internal_string_var(name, value)
993 char_u *name;
994 char_u *value;
995{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000996 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000997 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
999 val = vim_strsave(value);
1000 if (val != NULL)
1001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001003 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008 }
1009}
1010
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001012static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013static char_u *redir_endp = NULL;
1014static char_u *redir_varname = NULL;
1015
1016/*
1017 * Start recording command output to a variable
1018 * Returns OK if successfully completed the setup. FAIL otherwise.
1019 */
1020 int
1021var_redir_start(name, append)
1022 char_u *name;
1023 int append; /* append to an existing variable */
1024{
1025 int save_emsg;
1026 int err;
1027 typval_T tv;
1028
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001029 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001030 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 {
1032 EMSG(_(e_invarg));
1033 return FAIL;
1034 }
1035
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001036 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037 redir_varname = vim_strsave(name);
1038 if (redir_varname == NULL)
1039 return FAIL;
1040
1041 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1042 if (redir_lval == NULL)
1043 {
1044 var_redir_stop();
1045 return FAIL;
1046 }
1047
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001048 /* The output is stored in growarray "redir_ga" until redirection ends. */
1049 ga_init2(&redir_ga, (int)sizeof(char), 500);
1050
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001052 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1053 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1055 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001056 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (redir_endp != NULL && *redir_endp != NUL)
1058 /* Trailing characters are present after the variable name */
1059 EMSG(_(e_trailing));
1060 else
1061 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001062 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 var_redir_stop();
1064 return FAIL;
1065 }
1066
1067 /* check if we can write to the variable: set it to or append an empty
1068 * string */
1069 save_emsg = did_emsg;
1070 did_emsg = FALSE;
1071 tv.v_type = VAR_STRING;
1072 tv.vval.v_string = (char_u *)"";
1073 if (append)
1074 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1075 else
1076 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001077 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001079 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 if (err)
1081 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001082 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 var_redir_stop();
1084 return FAIL;
1085 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086
1087 return OK;
1088}
1089
1090/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091 * Append "value[value_len]" to the variable set by var_redir_start().
1092 * The actual appending is postponed until redirection ends, because the value
1093 * appended may in fact be the string we write to, changing it may cause freed
1094 * memory to be used:
1095 * :redir => foo
1096 * :let foo
1097 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 */
1099 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001104 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105
1106 if (redir_lval == NULL)
1107 return;
1108
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 {
1116 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 }
1119 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121}
1122
1123/*
1124 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 */
1127 void
1128var_redir_stop()
1129{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 typval_T tv;
1131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 if (redir_lval != NULL)
1133 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 /* If there was no error: assign the text to the variable. */
1135 if (redir_endp != NULL)
1136 {
1137 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1138 tv.v_type = VAR_STRING;
1139 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001140 /* Call get_lval() again, if it's inside a Dict or List it may
1141 * have changed. */
1142 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1143 FALSE, FALSE, FALSE, FNE_CHECK_START);
1144 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1145 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1146 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* free the collected output */
1150 vim_free(redir_ga.ga_data);
1151 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 vim_free(redir_lval);
1154 redir_lval = NULL;
1155 }
1156 vim_free(redir_varname);
1157 redir_varname = NULL;
1158}
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160# if defined(FEAT_MBYTE) || defined(PROTO)
1161 int
1162eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1163 char_u *enc_from;
1164 char_u *enc_to;
1165 char_u *fname_from;
1166 char_u *fname_to;
1167{
1168 int err = FALSE;
1169
1170 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1171 set_vim_var_string(VV_CC_TO, enc_to, -1);
1172 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1173 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1174 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1175 err = TRUE;
1176 set_vim_var_string(VV_CC_FROM, NULL, -1);
1177 set_vim_var_string(VV_CC_TO, NULL, -1);
1178 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1179 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1180
1181 if (err)
1182 return FAIL;
1183 return OK;
1184}
1185# endif
1186
1187# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1188 int
1189eval_printexpr(fname, args)
1190 char_u *fname;
1191 char_u *args;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_FNAME_IN, fname, -1);
1196 set_vim_var_string(VV_CMDARG, args, -1);
1197 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1198 err = TRUE;
1199 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1200 set_vim_var_string(VV_CMDARG, NULL, -1);
1201
1202 if (err)
1203 {
1204 mch_remove(fname);
1205 return FAIL;
1206 }
1207 return OK;
1208}
1209# endif
1210
1211# if defined(FEAT_DIFF) || defined(PROTO)
1212 void
1213eval_diff(origfile, newfile, outfile)
1214 char_u *origfile;
1215 char_u *newfile;
1216 char_u *outfile;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1221 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1222 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1223 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1226 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1227}
1228
1229 void
1230eval_patch(origfile, difffile, outfile)
1231 char_u *origfile;
1232 char_u *difffile;
1233 char_u *outfile;
1234{
1235 int err;
1236
1237 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1238 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1239 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1240 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1241 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1242 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1243 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1244}
1245# endif
1246
1247/*
1248 * Top level evaluation function, returning a boolean.
1249 * Sets "error" to TRUE if there was an error.
1250 * Return TRUE or FALSE.
1251 */
1252 int
1253eval_to_bool(arg, error, nextcmd, skip)
1254 char_u *arg;
1255 int *error;
1256 char_u **nextcmd;
1257 int skip; /* only parse, don't execute */
1258{
Bram Moolenaar33570922005-01-25 22:26:29 +00001259 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 int retval = FALSE;
1261
1262 if (skip)
1263 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001264 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 else
1267 {
1268 *error = FALSE;
1269 if (!skip)
1270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001271 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 }
1275 if (skip)
1276 --emsg_skip;
1277
1278 return retval;
1279}
1280
1281/*
1282 * Top level evaluation function, returning a string. If "skip" is TRUE,
1283 * only parsing to "nextcmd" is done, without reporting errors. Return
1284 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1285 */
1286 char_u *
1287eval_to_string_skip(arg, nextcmd, skip)
1288 char_u *arg;
1289 char_u **nextcmd;
1290 int skip; /* only parse, don't execute */
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 char_u *retval;
1294
1295 if (skip)
1296 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 retval = NULL;
1299 else
1300 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 retval = vim_strsave(get_tv_string(&tv));
1302 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 if (skip)
1305 --emsg_skip;
1306
1307 return retval;
1308}
1309
1310/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001311 * Skip over an expression at "*pp".
1312 * Return FAIL for an error, OK otherwise.
1313 */
1314 int
1315skip_expr(pp)
1316 char_u **pp;
1317{
Bram Moolenaar33570922005-01-25 22:26:29 +00001318 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319
1320 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001322}
1323
1324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001326 * When "convert" is TRUE convert a List into a sequence of lines and convert
1327 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * Return pointer to allocated memory, or NULL for failure.
1329 */
1330 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 char_u *arg;
1333 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001338 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001339#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 retval = NULL;
1345 else
1346 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001347 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001348 {
1349 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001350 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001351 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001352 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001353 if (tv.vval.v_list->lv_len > 0)
1354 ga_append(&ga, NL);
1355 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 ga_append(&ga, NUL);
1357 retval = (char_u *)ga.ga_data;
1358 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359#ifdef FEAT_FLOAT
1360 else if (convert && tv.v_type == VAR_FLOAT)
1361 {
1362 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1363 retval = vim_strsave(numbuf);
1364 }
1365#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 else
1367 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370
1371 return retval;
1372}
1373
1374/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001375 * Call eval_to_string() without using current local variables and using
1376 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 */
1378 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 char_u *arg;
1381 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 char_u *retval;
1385 void *save_funccalp;
1386
1387 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 if (use_sandbox)
1389 ++sandbox;
1390 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 --sandbox;
1394 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 restore_funccal(save_funccalp);
1396 return retval;
1397}
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399/*
1400 * Top level evaluation function, returning a number.
1401 * Evaluates "expr" silently.
1402 * Returns -1 for an error.
1403 */
1404 int
1405eval_to_number(expr)
1406 char_u *expr;
1407{
Bram Moolenaar33570922005-01-25 22:26:29 +00001408 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001410 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412 ++emsg_off;
1413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 retval = -1;
1416 else
1417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001418 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421 --emsg_off;
1422
1423 return retval;
1424}
1425
Bram Moolenaara40058a2005-07-11 22:42:07 +00001426/*
1427 * Prepare v: variable "idx" to be used.
1428 * Save the current typeval in "save_tv".
1429 * When not used yet add the variable to the v: hashtable.
1430 */
1431 static void
1432prepare_vimvar(idx, save_tv)
1433 int idx;
1434 typval_T *save_tv;
1435{
1436 *save_tv = vimvars[idx].vv_tv;
1437 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1438 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1439}
1440
1441/*
1442 * Restore v: variable "idx" to typeval "save_tv".
1443 * When no longer defined, remove the variable from the v: hashtable.
1444 */
1445 static void
1446restore_vimvar(idx, save_tv)
1447 int idx;
1448 typval_T *save_tv;
1449{
1450 hashitem_T *hi;
1451
Bram Moolenaara40058a2005-07-11 22:42:07 +00001452 vimvars[idx].vv_tv = *save_tv;
1453 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1454 {
1455 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1456 if (HASHITEM_EMPTY(hi))
1457 EMSG2(_(e_intern2), "restore_vimvar()");
1458 else
1459 hash_remove(&vimvarht, hi);
1460 }
1461}
1462
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001463#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001464/*
1465 * Evaluate an expression to a list with suggestions.
1466 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001467 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468 */
1469 list_T *
1470eval_spell_expr(badword, expr)
1471 char_u *badword;
1472 char_u *expr;
1473{
1474 typval_T save_val;
1475 typval_T rettv;
1476 list_T *list = NULL;
1477 char_u *p = skipwhite(expr);
1478
1479 /* Set "v:val" to the bad word. */
1480 prepare_vimvar(VV_VAL, &save_val);
1481 vimvars[VV_VAL].vv_type = VAR_STRING;
1482 vimvars[VV_VAL].vv_str = badword;
1483 if (p_verbose == 0)
1484 ++emsg_off;
1485
1486 if (eval1(&p, &rettv, TRUE) == OK)
1487 {
1488 if (rettv.v_type != VAR_LIST)
1489 clear_tv(&rettv);
1490 else
1491 list = rettv.vval.v_list;
1492 }
1493
1494 if (p_verbose == 0)
1495 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001496 restore_vimvar(VV_VAL, &save_val);
1497
1498 return list;
1499}
1500
1501/*
1502 * "list" is supposed to contain two items: a word and a number. Return the
1503 * word in "pp" and the number as the return value.
1504 * Return -1 if anything isn't right.
1505 * Used to get the good word and score from the eval_spell_expr() result.
1506 */
1507 int
1508get_spellword(list, pp)
1509 list_T *list;
1510 char_u **pp;
1511{
1512 listitem_T *li;
1513
1514 li = list->lv_first;
1515 if (li == NULL)
1516 return -1;
1517 *pp = get_tv_string(&li->li_tv);
1518
1519 li = li->li_next;
1520 if (li == NULL)
1521 return -1;
1522 return get_tv_number(&li->li_tv);
1523}
1524#endif
1525
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001526/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001527 * Top level evaluation function.
1528 * Returns an allocated typval_T with the result.
1529 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530 */
1531 typval_T *
1532eval_expr(arg, nextcmd)
1533 char_u *arg;
1534 char_u **nextcmd;
1535{
1536 typval_T *tv;
1537
1538 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540 {
1541 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 }
1544
1545 return tv;
1546}
1547
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001550 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551 * Uses argv[argc] for the function arguments. Only Number and String
1552 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001555 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001556call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 char_u *func;
1558 int argc;
1559 char_u **argv;
1560 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001561 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 long n;
1566 int len;
1567 int i;
1568 int doesrange;
1569 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001572 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 for (i = 0; i < argc; i++)
1577 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001578 /* Pass a NULL or empty argument as an empty string */
1579 if (argv[i] == NULL || *argv[i] == NUL)
1580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001581 argvars[i].v_type = VAR_STRING;
1582 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 continue;
1584 }
1585
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 if (str_arg_only)
1587 len = 0;
1588 else
1589 /* Recognize a number argument, the others must be strings. */
1590 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (len != 0 && len == (int)STRLEN(argv[i]))
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_NUMBER;
1594 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 else
1597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001598 argvars[i].v_type = VAR_STRING;
1599 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 }
1602
1603 if (safe)
1604 {
1605 save_funccalp = save_funccal();
1606 ++sandbox;
1607 }
1608
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001609 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1610 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (safe)
1614 {
1615 --sandbox;
1616 restore_funccal(save_funccalp);
1617 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 vim_free(argvars);
1619
1620 if (ret == FAIL)
1621 clear_tv(rettv);
1622
1623 return ret;
1624}
1625
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001626/*
1627 * Call vimL function "func" and return the result as a number.
1628 * Returns -1 when calling the function fails.
1629 * Uses argv[argc] for the function arguments.
1630 */
1631 long
1632call_func_retnr(func, argc, argv, safe)
1633 char_u *func;
1634 int argc;
1635 char_u **argv;
1636 int safe; /* use the sandbox */
1637{
1638 typval_T rettv;
1639 long retval;
1640
1641 /* All arguments are passed as strings, no conversion to number. */
1642 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1643 return -1;
1644
1645 retval = get_tv_number_chk(&rettv, NULL);
1646 clear_tv(&rettv);
1647 return retval;
1648}
1649
1650#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1651 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1652
Bram Moolenaar4f688582007-07-24 12:34:30 +00001653# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001655 * Call vimL function "func" and return the result as a string.
1656 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 * Uses argv[argc] for the function arguments.
1658 */
1659 void *
1660call_func_retstr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001669 /* All arguments are passed as strings, no conversion to number. */
1670 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 return NULL;
1672
1673 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 return retval;
1676}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001682 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 */
1684 void *
1685call_func_retlist(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
1692
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001693 /* All arguments are passed as strings, no conversion to number. */
1694 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 return NULL;
1696
1697 if (rettv.v_type != VAR_LIST)
1698 {
1699 clear_tv(&rettv);
1700 return NULL;
1701 }
1702
1703 return rettv.vval.v_list;
1704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#endif
1706
1707/*
1708 * Save the current function call pointer, and set it to NULL.
1709 * Used when executing autocommands and for ":source".
1710 */
1711 void *
1712save_funccal()
1713{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 current_funccal = NULL;
1717 return (void *)fc;
1718}
1719
1720 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001721restore_funccal(vfc)
1722 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724 funccall_T *fc = (funccall_T *)vfc;
1725
1726 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
1728
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729#if defined(FEAT_PROFILE) || defined(PROTO)
1730/*
1731 * Prepare profiling for entering a child or something else that is not
1732 * counted for the script/function itself.
1733 * Should always be called in pair with prof_child_exit().
1734 */
1735 void
1736prof_child_enter(tm)
1737 proftime_T *tm; /* place to store waittime */
1738{
1739 funccall_T *fc = current_funccal;
1740
1741 if (fc != NULL && fc->func->uf_profiling)
1742 profile_start(&fc->prof_child);
1743 script_prof_save(tm);
1744}
1745
1746/*
1747 * Take care of time spent in a child.
1748 * Should always be called after prof_child_enter().
1749 */
1750 void
1751prof_child_exit(tm)
1752 proftime_T *tm; /* where waittime was stored */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 {
1758 profile_end(&fc->prof_child);
1759 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1760 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1761 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1762 }
1763 script_prof_restore(tm);
1764}
1765#endif
1766
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef FEAT_FOLDING
1769/*
1770 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1771 * it in "*cp". Doesn't give error messages.
1772 */
1773 int
1774eval_foldexpr(arg, cp)
1775 char_u *arg;
1776 int *cp;
1777{
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 int retval;
1780 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001781 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1782 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001785 if (use_sandbox)
1786 ++sandbox;
1787 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = 0;
1791 else
1792 {
1793 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (tv.v_type == VAR_NUMBER)
1795 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001796 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 retval = 0;
1798 else
1799 {
1800 /* If the result is a string, check if there is a non-digit before
1801 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (!VIM_ISDIGIT(*s) && *s != '-')
1804 *cp = *s++;
1805 retval = atol((char *)s);
1806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 --sandbox;
1812 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 return retval;
1815}
1816#endif
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 * ":let" list all variable values
1820 * ":let var1 var2" list variable values
1821 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 * ":let var += expr" assignment command.
1823 * ":let var -= expr" assignment command.
1824 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827 void
1828ex_let(eap)
1829 exarg_T *eap;
1830{
1831 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 int var_count = 0;
1836 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001838 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001839 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 argend = skip_var_list(arg, &var_count, &semicolon);
1842 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001844 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1845 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001849 /*
1850 * ":let" without "=": list variables
1851 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (*arg == '[')
1853 EMSG(_(e_invarg));
1854 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_glob_vars(&first);
1861 list_buf_vars(&first);
1862 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001865#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_script_vars(&first);
1867 list_func_vars(&first);
1868 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 eap->nextcmd = check_nextcmd(arg);
1871 }
1872 else
1873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 op[0] = '=';
1875 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001876 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 {
1878 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1879 op[0] = expr[-1]; /* +=, -= or .= */
1880 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (eap->skip)
1884 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (eap->skip)
1887 {
1888 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 --emsg_skip;
1891 }
1892 else if (i != FAIL)
1893 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 }
1899}
1900
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901/*
1902 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1903 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 * When "nextchars" is not NULL it points to a string with characters that
1905 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1906 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 * Returns OK or FAIL;
1908 */
1909 static int
1910ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1911 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 int copy; /* copy values from "tv", don't move */
1914 int semicolon; /* from skip_var_list() */
1915 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917{
1918 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 listitem_T *item;
1922 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923
1924 if (*arg != '[')
1925 {
1926 /*
1927 * ":let var = expr" or ":for var in list"
1928 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 return FAIL;
1931 return OK;
1932 }
1933
1934 /*
1935 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1936 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001937 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 {
1939 EMSG(_(e_listreq));
1940 return FAIL;
1941 }
1942
1943 i = list_len(l);
1944 if (semicolon == 0 && var_count < i)
1945 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001946 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 return FAIL;
1948 }
1949 if (var_count - semicolon > i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954
1955 item = l->lv_first;
1956 while (*arg != ']')
1957 {
1958 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 item = item->li_next;
1961 if (arg == NULL)
1962 return FAIL;
1963
1964 arg = skipwhite(arg);
1965 if (*arg == ';')
1966 {
1967 /* Put the rest of the list (may be empty) in the var after ';'.
1968 * Create a new list for this. */
1969 l = list_alloc();
1970 if (l == NULL)
1971 return FAIL;
1972 while (item != NULL)
1973 {
1974 list_append_tv(l, &item->li_tv);
1975 item = item->li_next;
1976 }
1977
1978 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001979 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 ltv.vval.v_list = l;
1981 l->lv_refcount = 1;
1982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1984 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 clear_tv(&ltv);
1986 if (arg == NULL)
1987 return FAIL;
1988 break;
1989 }
1990 else if (*arg != ',' && *arg != ']')
1991 {
1992 EMSG2(_(e_intern2), "ex_let_vars()");
1993 return FAIL;
1994 }
1995 }
1996
1997 return OK;
1998}
1999
2000/*
2001 * Skip over assignable variable "var" or list of variables "[var, var]".
2002 * Used for ":let varvar = expr" and ":for varvar in expr".
2003 * For "[var, var]" increment "*var_count" for each variable.
2004 * for "[var, var; var]" set "semicolon".
2005 * Return NULL for an error.
2006 */
2007 static char_u *
2008skip_var_list(arg, var_count, semicolon)
2009 char_u *arg;
2010 int *var_count;
2011 int *semicolon;
2012{
2013 char_u *p, *s;
2014
2015 if (*arg == '[')
2016 {
2017 /* "[var, var]": find the matching ']'. */
2018 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002019 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 {
2021 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2022 s = skip_var_one(p);
2023 if (s == p)
2024 {
2025 EMSG2(_(e_invarg2), p);
2026 return NULL;
2027 }
2028 ++*var_count;
2029
2030 p = skipwhite(s);
2031 if (*p == ']')
2032 break;
2033 else if (*p == ';')
2034 {
2035 if (*semicolon == 1)
2036 {
2037 EMSG(_("Double ; in list of variables"));
2038 return NULL;
2039 }
2040 *semicolon = 1;
2041 }
2042 else if (*p != ',')
2043 {
2044 EMSG2(_(e_invarg2), p);
2045 return NULL;
2046 }
2047 }
2048 return p + 1;
2049 }
2050 else
2051 return skip_var_one(arg);
2052}
2053
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002055 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002056 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 static char_u *
2059skip_var_one(arg)
2060 char_u *arg;
2061{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 if (*arg == '@' && arg[1] != NUL)
2063 return arg + 2;
2064 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2065 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066}
2067
Bram Moolenaara7043832005-01-21 11:56:39 +00002068/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 * List variables for hashtab "ht" with prefix "prefix".
2070 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078{
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashitem_T *hi;
2080 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 int todo;
2082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2085 {
2086 if (!HASHITEM_EMPTY(hi))
2087 {
2088 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 di = HI2DI(hi);
2090 if (empty || di->di_tv.v_type != VAR_STRING
2091 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 }
2094 }
2095}
2096
2097/*
2098 * List global variables.
2099 */
2100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_glob_vars(first)
2102 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105}
2106
2107/*
2108 * List buffer variables.
2109 */
2110 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111list_buf_vars(first)
2112 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002113{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114 char_u numbuf[NUMBUFLEN];
2115
Bram Moolenaar429fa852013-04-15 12:27:36 +02002116 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118
2119 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2121 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
2124/*
2125 * List window variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_win_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002131 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135#ifdef FEAT_WINDOWS
2136/*
2137 * List tab page variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_tab_vars(first)
2141 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002142{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145}
2146#endif
2147
Bram Moolenaara7043832005-01-21 11:56:39 +00002148/*
2149 * List Vim variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_vim_vars(first)
2153 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156}
2157
2158/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002159 * List script-local variables, if there is a script.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_script_vars(first)
2163 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164{
2165 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2167 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168}
2169
2170/*
2171 * List function variables, if there is a function.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_func_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_funccal != NULL)
2178 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 * List variables in "arg".
2184 */
2185 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 exarg_T *eap;
2188 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
2191 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 char_u *name_start;
2195 char_u *arg_subsc;
2196 char_u *tofree;
2197 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198
2199 while (!ends_excmd(*arg) && !got_int)
2200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002203 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2205 {
2206 emsg_severe = TRUE;
2207 EMSG(_(e_trailing));
2208 break;
2209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 /* get_name_len() takes care of expanding curly braces */
2214 name_start = name = arg;
2215 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2216 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* This is mainly to keep test 49 working: when expanding
2219 * curly braces fails overrule the exception error message. */
2220 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 emsg_severe = TRUE;
2223 EMSG2(_(e_invarg2), arg);
2224 break;
2225 }
2226 error = TRUE;
2227 }
2228 else
2229 {
2230 if (tofree != NULL)
2231 name = tofree;
2232 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 else
2235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* handle d.key, l[idx], f(expr) */
2237 arg_subsc = arg;
2238 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 'g': list_glob_vars(first); break;
2247 case 'b': list_buf_vars(first); break;
2248 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'v': list_vim_vars(first); break;
2253 case 's': list_script_vars(first); break;
2254 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 default:
2256 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 }
2259 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 {
2261 char_u numbuf[NUMBUFLEN];
2262 char_u *tf;
2263 int c;
2264 char_u *s;
2265
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002266 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 c = *arg;
2268 *arg = NUL;
2269 list_one_var_a((char_u *)"",
2270 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 tv.v_type,
2272 s == NULL ? (char_u *)"" : s,
2273 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 *arg = c;
2275 vim_free(tf);
2276 }
2277 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281
2282 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284
2285 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
2287
2288 return arg;
2289}
2290
2291/*
2292 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2293 * Returns a pointer to the char just after the var name.
2294 * Returns NULL if there is an error.
2295 */
2296 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002299 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002301 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303{
2304 int c1;
2305 char_u *name;
2306 char_u *p;
2307 char_u *arg_end = NULL;
2308 int len;
2309 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311
2312 /*
2313 * ":let $VAR = expr": Set environment variable.
2314 */
2315 if (*arg == '$')
2316 {
2317 /* Find the end of the name. */
2318 ++arg;
2319 name = arg;
2320 len = get_env_len(&arg);
2321 if (len == 0)
2322 EMSG2(_(e_invarg2), name - 1);
2323 else
2324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 if (op != NULL && (*op == '+' || *op == '-'))
2326 EMSG2(_(e_letwrong), op);
2327 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002330 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
2332 c1 = name[len];
2333 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334 p = get_tv_string_chk(tv);
2335 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 {
2337 int mustfree = FALSE;
2338 char_u *s = vim_getenv(name, &mustfree);
2339
2340 if (s != NULL)
2341 {
2342 p = tofree = concat_str(s, p);
2343 if (mustfree)
2344 vim_free(s);
2345 }
2346 }
2347 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 if (STRICMP(name, "HOME") == 0)
2351 init_homedir();
2352 else if (didset_vim && STRICMP(name, "VIM") == 0)
2353 didset_vim = FALSE;
2354 else if (didset_vimruntime
2355 && STRICMP(name, "VIMRUNTIME") == 0)
2356 didset_vimruntime = FALSE;
2357 arg_end = arg;
2358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362 }
2363 }
2364
2365 /*
2366 * ":let &option = expr": Set option value.
2367 * ":let &l:option = expr": Set local option value.
2368 * ":let &g:option = expr": Set global option value.
2369 */
2370 else if (*arg == '&')
2371 {
2372 /* Find the end of the name. */
2373 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 if (p == NULL || (endchars != NULL
2375 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 EMSG(_(e_letunexp));
2377 else
2378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 long n;
2380 int opt_type;
2381 long numval;
2382 char_u *stringval = NULL;
2383 char_u *s;
2384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 c1 = *p;
2386 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387
2388 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 s = get_tv_string_chk(tv); /* != NULL if number or string */
2390 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 opt_type = get_option_value(arg, &numval,
2393 &stringval, opt_flags);
2394 if ((opt_type == 1 && *op == '.')
2395 || (opt_type == 0 && *op != '.'))
2396 EMSG2(_(e_letwrong), op);
2397 else
2398 {
2399 if (opt_type == 1) /* number */
2400 {
2401 if (*op == '+')
2402 n = numval + n;
2403 else
2404 n = numval - n;
2405 }
2406 else if (opt_type == 0 && stringval != NULL) /* string */
2407 {
2408 s = concat_str(stringval, s);
2409 vim_free(stringval);
2410 stringval = s;
2411 }
2412 }
2413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 if (s != NULL)
2415 {
2416 set_option_value(arg, n, s, opt_flags);
2417 arg_end = p;
2418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 }
2422 }
2423
2424 /*
2425 * ":let @r = expr": Set register contents.
2426 */
2427 else if (*arg == '@')
2428 {
2429 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 if (op != NULL && (*op == '+' || *op == '-'))
2431 EMSG2(_(e_letwrong), op);
2432 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002437 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 char_u *s;
2439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 p = get_tv_string_chk(tv);
2441 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002443 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 if (s != NULL)
2445 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002446 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 arg_end = arg + 1;
2454 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
2457 }
2458
2459 /*
2460 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002465 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480
2481 else
2482 EMSG2(_(e_invarg2), arg);
2483
2484 return arg_end;
2485}
2486
2487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2489 */
2490 static int
2491check_changedtick(arg)
2492 char_u *arg;
2493{
2494 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2495 {
2496 EMSG2(_(e_readonlyvar), arg);
2497 return TRUE;
2498 }
2499 return FALSE;
2500}
2501
2502/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * Get an lval: variable, Dict item or List item that can be assigned a value
2504 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2505 * "name.key", "name.key[expr]" etc.
2506 * Indexing only works if "name" is an existing List or Dictionary.
2507 * "name" points to the start of the name.
2508 * If "rettv" is not NULL it points to the value to be assigned.
2509 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2510 * wrong; must end in space or cmd separator.
2511 *
2512 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002513 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 */
2516 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 typval_T *rettv;
2520 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 int unlet;
2522 int skip;
2523 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 char_u *expr_start, *expr_end;
2528 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 dictitem_T *v;
2530 typval_T var1;
2531 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540
2541 if (skip)
2542 {
2543 /* When skipping just find the end of the name. */
2544 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 }
2547
2548 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (expr_start != NULL)
2551 {
2552 /* Don't expand the name when we already know there is an error. */
2553 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2554 && *p != '[' && *p != '.')
2555 {
2556 EMSG(_(e_trailing));
2557 return NULL;
2558 }
2559
2560 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2561 if (lp->ll_exp_name == NULL)
2562 {
2563 /* Report an invalid expression in braces, unless the
2564 * expression evaluation has been cancelled due to an
2565 * aborting error, an interrupt, or an exception. */
2566 if (!aborting() && !quiet)
2567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002568 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 EMSG2(_(e_invarg2), name);
2570 return NULL;
2571 }
2572 }
2573 lp->ll_name = lp->ll_exp_name;
2574 }
2575 else
2576 lp->ll_name = name;
2577
2578 /* Without [idx] or .key we are done. */
2579 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2580 return p;
2581
2582 cc = *p;
2583 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002584 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (v == NULL && !quiet)
2586 EMSG2(_(e_undefvar), lp->ll_name);
2587 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 if (v == NULL)
2589 return NULL;
2590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 /*
2592 * Loop until no more [idx] or .key is following.
2593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2598 && !(lp->ll_tv->v_type == VAR_DICT
2599 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_("E689: Can only index a List or Dictionary"));
2603 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E708: [:] must come last"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 len = -1;
2613 if (*p == '.')
2614 {
2615 key = p + 1;
2616 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2617 ;
2618 if (len == 0)
2619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!quiet)
2621 EMSG(_(e_emptykey));
2622 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 }
2624 p = key + len;
2625 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 else
2627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 if (*p == ':')
2631 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 empty1 = FALSE;
2635 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 if (get_tv_string_chk(&var1) == NULL)
2638 {
2639 /* not a number or string */
2640 clear_tv(&var1);
2641 return NULL;
2642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 }
2644
2645 /* Optionally get the second index [ :expr]. */
2646 if (*p == ':')
2647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 if (!empty1)
2653 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (rettv != NULL && (rettv->v_type != VAR_LIST
2657 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (!empty1)
2662 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = skipwhite(p + 1);
2666 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 else
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 if (!empty1)
2674 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002677 if (get_tv_string_chk(&var2) == NULL)
2678 {
2679 /* not a number or string */
2680 if (!empty1)
2681 clear_tv(&var1);
2682 clear_tv(&var2);
2683 return NULL;
2684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (!quiet)
2694 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (!empty1)
2696 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Skip to past ']'. */
2703 ++p;
2704 }
2705
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
2708 if (len == -1)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (*key == NUL)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 lp->ll_list = NULL;
2721 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002722 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002723
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002724 /* When assigning to a scope dictionary check that a function and
2725 * variable name is valid (only variable name unless it is l: or
2726 * g: dictionary). Disallow overwriting a builtin function. */
2727 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002729 int prevval;
2730 int wrong;
2731
2732 if (len != -1)
2733 {
2734 prevval = key[len];
2735 key[len] = NUL;
2736 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002737 else
2738 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002739 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2740 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002741 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002742 || !valid_varname(key);
2743 if (len != -1)
2744 key[len] = prevval;
2745 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 return NULL;
2747 }
2748
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 /* Can't add "v:" variable. */
2752 if (lp->ll_dict == &vimvardict)
2753 {
2754 EMSG2(_(e_illvar), name);
2755 return NULL;
2756 }
2757
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002758 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 if (len == -1)
2764 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 }
2767 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (len == -1)
2772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 p = NULL;
2775 break;
2776 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 /* existing variable, need to check if it can be changed */
2778 else if (var_check_ro(lp->ll_di->di_flags, name))
2779 return NULL;
2780
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 if (len == -1)
2782 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 }
2785 else
2786 {
2787 /*
2788 * Get the number and item for the only or first index of the List.
2789 */
2790 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 else
2793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002794 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 clear_tv(&var1);
2796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002797 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 lp->ll_list = lp->ll_tv->vval.v_list;
2799 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2800 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002802 if (lp->ll_n1 < 0)
2803 {
2804 lp->ll_n1 = 0;
2805 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2806 }
2807 }
2808 if (lp->ll_li == NULL)
2809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002812 if (!quiet)
2813 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816
2817 /*
2818 * May need to find the item or absolute index for the second
2819 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 * When no index given: "lp->ll_empty2" is TRUE.
2821 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002825 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002831 {
2832 if (!quiet)
2833 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002835 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 }
2838
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2840 if (lp->ll_n1 < 0)
2841 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2842 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002843 {
2844 if (!quiet)
2845 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002848 }
2849
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002852 }
2853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 return p;
2855}
2856
2857/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002858 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 */
2860 static void
2861clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863{
2864 vim_free(lp->ll_exp_name);
2865 vim_free(lp->ll_newkey);
2866}
2867
2868/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002869 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 */
2873 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002874set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880{
2881 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 listitem_T *ri;
2883 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884
2885 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 cc = *endp;
2890 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891 if (op != NULL && *op != '=')
2892 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002893 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002896 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002897 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002898 {
2899 if (tv_op(&tv, rettv, op) == OK)
2900 set_var(lp->ll_name, &tv, FALSE);
2901 clear_tv(&tv);
2902 }
2903 }
2904 else
2905 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002909 else if (tv_check_lock(lp->ll_newkey == NULL
2910 ? lp->ll_tv->v_lock
2911 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2912 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 else if (lp->ll_range)
2914 {
2915 /*
2916 * Assign the List values to the list items.
2917 */
2918 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002919 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 if (op != NULL && *op != '=')
2921 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2922 else
2923 {
2924 clear_tv(&lp->ll_li->li_tv);
2925 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2926 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 ri = ri->li_next;
2928 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2929 break;
2930 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002933 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 ri = NULL;
2936 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002937 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002938 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 lp->ll_li = lp->ll_li->li_next;
2940 ++lp->ll_n1;
2941 }
2942 if (ri != NULL)
2943 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 else if (lp->ll_empty2
2945 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 : lp->ll_n1 != lp->ll_n2)
2947 EMSG(_("E711: List value has not enough items"));
2948 }
2949 else
2950 {
2951 /*
2952 * Assign to a List or Dictionary item.
2953 */
2954 if (lp->ll_newkey != NULL)
2955 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 if (op != NULL && *op != '=')
2957 {
2958 EMSG2(_(e_letwrong), op);
2959 return;
2960 }
2961
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002963 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 if (di == NULL)
2965 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002966 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2967 {
2968 vim_free(di);
2969 return;
2970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002972 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 else if (op != NULL && *op != '=')
2974 {
2975 tv_op(lp->ll_tv, rettv, op);
2976 return;
2977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002980
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 /*
2982 * Assign the value to the variable or list item.
2983 */
2984 if (copy)
2985 copy_tv(rettv, lp->ll_tv);
2986 else
2987 {
2988 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002989 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002991 }
2992 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002993}
2994
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002995/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2997 * Returns OK or FAIL.
2998 */
2999 static int
3000tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003001 typval_T *tv1;
3002 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 char_u *op;
3004{
3005 long n;
3006 char_u numbuf[NUMBUFLEN];
3007 char_u *s;
3008
3009 /* Can't do anything with a Funcref or a Dict on the right. */
3010 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3011 {
3012 switch (tv1->v_type)
3013 {
3014 case VAR_DICT:
3015 case VAR_FUNC:
3016 break;
3017
3018 case VAR_LIST:
3019 if (*op != '+' || tv2->v_type != VAR_LIST)
3020 break;
3021 /* List += List */
3022 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3023 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3024 return OK;
3025
3026 case VAR_NUMBER:
3027 case VAR_STRING:
3028 if (tv2->v_type == VAR_LIST)
3029 break;
3030 if (*op == '+' || *op == '-')
3031 {
3032 /* nr += nr or nr -= nr*/
3033 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034#ifdef FEAT_FLOAT
3035 if (tv2->v_type == VAR_FLOAT)
3036 {
3037 float_T f = n;
3038
3039 if (*op == '+')
3040 f += tv2->vval.v_float;
3041 else
3042 f -= tv2->vval.v_float;
3043 clear_tv(tv1);
3044 tv1->v_type = VAR_FLOAT;
3045 tv1->vval.v_float = f;
3046 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003047 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048#endif
3049 {
3050 if (*op == '+')
3051 n += get_tv_number(tv2);
3052 else
3053 n -= get_tv_number(tv2);
3054 clear_tv(tv1);
3055 tv1->v_type = VAR_NUMBER;
3056 tv1->vval.v_number = n;
3057 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 }
3059 else
3060 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003061 if (tv2->v_type == VAR_FLOAT)
3062 break;
3063
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 /* str .= str */
3065 s = get_tv_string(tv1);
3066 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3067 clear_tv(tv1);
3068 tv1->v_type = VAR_STRING;
3069 tv1->vval.v_string = s;
3070 }
3071 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003072
3073#ifdef FEAT_FLOAT
3074 case VAR_FLOAT:
3075 {
3076 float_T f;
3077
3078 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3079 && tv2->v_type != VAR_NUMBER
3080 && tv2->v_type != VAR_STRING))
3081 break;
3082 if (tv2->v_type == VAR_FLOAT)
3083 f = tv2->vval.v_float;
3084 else
3085 f = get_tv_number(tv2);
3086 if (*op == '+')
3087 tv1->vval.v_float += f;
3088 else
3089 tv1->vval.v_float -= f;
3090 }
3091 return OK;
3092#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 }
3094 }
3095
3096 EMSG2(_(e_letwrong), op);
3097 return FAIL;
3098}
3099
3100/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003101 * Add a watcher to a list.
3102 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003103 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003105 list_T *l;
3106 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107{
3108 lw->lw_next = l->lv_watch;
3109 l->lv_watch = lw;
3110}
3111
3112/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003113 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 * No warning when it isn't found...
3115 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003116 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 list_T *l;
3119 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120{
Bram Moolenaar33570922005-01-25 22:26:29 +00003121 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122
3123 lwp = &l->lv_watch;
3124 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3125 {
3126 if (lw == lwrem)
3127 {
3128 *lwp = lw->lw_next;
3129 break;
3130 }
3131 lwp = &lw->lw_next;
3132 }
3133}
3134
3135/*
3136 * Just before removing an item from a list: advance watchers to the next
3137 * item.
3138 */
3139 static void
3140list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 list_T *l;
3142 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143{
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145
3146 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3147 if (lw->lw_item == item)
3148 lw->lw_item = item->li_next;
3149}
3150
3151/*
3152 * Evaluate the expression used in a ":for var in expr" command.
3153 * "arg" points to "var".
3154 * Set "*errp" to TRUE for an error, FALSE otherwise;
3155 * Return a pointer that holds the info. Null when there is an error.
3156 */
3157 void *
3158eval_for_line(arg, errp, nextcmdp, skip)
3159 char_u *arg;
3160 int *errp;
3161 char_u **nextcmdp;
3162 int skip;
3163{
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003165 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003166 typval_T tv;
3167 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168
3169 *errp = TRUE; /* default: there is an error */
3170
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 if (fi == NULL)
3173 return NULL;
3174
3175 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3176 if (expr == NULL)
3177 return fi;
3178
3179 expr = skipwhite(expr);
3180 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3181 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003182 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 return fi;
3184 }
3185
3186 if (skip)
3187 ++emsg_skip;
3188 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3189 {
3190 *errp = FALSE;
3191 if (!skip)
3192 {
3193 l = tv.vval.v_list;
3194 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003195 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003197 clear_tv(&tv);
3198 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 else
3200 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003201 /* No need to increment the refcount, it's already set for the
3202 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203 fi->fi_list = l;
3204 list_add_watch(l, &fi->fi_lw);
3205 fi->fi_lw.lw_item = l->lv_first;
3206 }
3207 }
3208 }
3209 if (skip)
3210 --emsg_skip;
3211
3212 return fi;
3213}
3214
3215/*
3216 * Use the first item in a ":for" list. Advance to the next.
3217 * Assign the values to the variable (list). "arg" points to the first one.
3218 * Return TRUE when a valid item was found, FALSE when at end of list or
3219 * something wrong.
3220 */
3221 int
3222next_for_item(fi_void, arg)
3223 void *fi_void;
3224 char_u *arg;
3225{
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229
3230 item = fi->fi_lw.lw_item;
3231 if (item == NULL)
3232 result = FALSE;
3233 else
3234 {
3235 fi->fi_lw.lw_item = item->li_next;
3236 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3237 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3238 }
3239 return result;
3240}
3241
3242/*
3243 * Free the structure used to store info used by ":for".
3244 */
3245 void
3246free_for_info(fi_void)
3247 void *fi_void;
3248{
Bram Moolenaar33570922005-01-25 22:26:29 +00003249 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003251 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003252 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003254 list_unref(fi->fi_list);
3255 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 vim_free(fi);
3257}
3258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3260
3261 void
3262set_context_for_expression(xp, arg, cmdidx)
3263 expand_T *xp;
3264 char_u *arg;
3265 cmdidx_T cmdidx;
3266{
3267 int got_eq = FALSE;
3268 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 if (cmdidx == CMD_let)
3272 {
3273 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003274 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 {
3276 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003277 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 {
3279 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 if (vim_iswhite(*p))
3282 break;
3283 }
3284 return;
3285 }
3286 }
3287 else
3288 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3289 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 while ((xp->xp_pattern = vim_strpbrk(arg,
3291 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3292 {
3293 c = *xp->xp_pattern;
3294 if (c == '&')
3295 {
3296 c = xp->xp_pattern[1];
3297 if (c == '&')
3298 {
3299 ++xp->xp_pattern;
3300 xp->xp_context = cmdidx != CMD_let || got_eq
3301 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3302 }
3303 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003306 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3307 xp->xp_pattern += 2;
3308
3309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311 else if (c == '$')
3312 {
3313 /* environment variable */
3314 xp->xp_context = EXPAND_ENV_VARS;
3315 }
3316 else if (c == '=')
3317 {
3318 got_eq = TRUE;
3319 xp->xp_context = EXPAND_EXPRESSION;
3320 }
3321 else if (c == '<'
3322 && xp->xp_context == EXPAND_FUNCTIONS
3323 && vim_strchr(xp->xp_pattern, '(') == NULL)
3324 {
3325 /* Function name can start with "<SNR>" */
3326 break;
3327 }
3328 else if (cmdidx != CMD_let || got_eq)
3329 {
3330 if (c == '"') /* string */
3331 {
3332 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3333 if (c == '\\' && xp->xp_pattern[1] != NUL)
3334 ++xp->xp_pattern;
3335 xp->xp_context = EXPAND_NOTHING;
3336 }
3337 else if (c == '\'') /* literal string */
3338 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003339 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3341 /* skip */ ;
3342 xp->xp_context = EXPAND_NOTHING;
3343 }
3344 else if (c == '|')
3345 {
3346 if (xp->xp_pattern[1] == '|')
3347 {
3348 ++xp->xp_pattern;
3349 xp->xp_context = EXPAND_EXPRESSION;
3350 }
3351 else
3352 xp->xp_context = EXPAND_COMMANDS;
3353 }
3354 else
3355 xp->xp_context = EXPAND_EXPRESSION;
3356 }
3357 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 /* Doesn't look like something valid, expand as an expression
3359 * anyway. */
3360 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 arg = xp->xp_pattern;
3362 if (*arg != NUL)
3363 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3364 /* skip */ ;
3365 }
3366 xp->xp_pattern = arg;
3367}
3368
3369#endif /* FEAT_CMDL_COMPL */
3370
3371/*
3372 * ":1,25call func(arg1, arg2)" function call.
3373 */
3374 void
3375ex_call(eap)
3376 exarg_T *eap;
3377{
3378 char_u *arg = eap->arg;
3379 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003381 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 linenr_T lnum;
3385 int doesrange;
3386 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003387 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003389 if (eap->skip)
3390 {
3391 /* trans_function_name() doesn't work well when skipping, use eval0()
3392 * instead to skip to any following command, e.g. for:
3393 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003394 ++emsg_skip;
3395 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3396 clear_tv(&rettv);
3397 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003398 return;
3399 }
3400
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003401 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003402 if (fudi.fd_newkey != NULL)
3403 {
3404 /* Still need to give an error message for missing key. */
3405 EMSG2(_(e_dictkey), fudi.fd_newkey);
3406 vim_free(fudi.fd_newkey);
3407 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003408 if (tofree == NULL)
3409 return;
3410
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003411 /* Increase refcount on dictionary, it could get deleted when evaluating
3412 * the arguments. */
3413 if (fudi.fd_dict != NULL)
3414 ++fudi.fd_dict->dv_refcount;
3415
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003416 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003417 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003418 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaar532c7802005-01-27 14:44:31 +00003420 /* Skip white space to allow ":call func ()". Not good, but required for
3421 * backward compatibility. */
3422 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003423 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 if (*startarg != '(')
3426 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003427 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 goto end;
3429 }
3430
3431 /*
3432 * When skipping, evaluate the function once, to find the end of the
3433 * arguments.
3434 * When the function takes a range, this is discovered after the first
3435 * call, and the loop is broken.
3436 */
3437 if (eap->skip)
3438 {
3439 ++emsg_skip;
3440 lnum = eap->line2; /* do it once, also with an invalid range */
3441 }
3442 else
3443 lnum = eap->line1;
3444 for ( ; lnum <= eap->line2; ++lnum)
3445 {
3446 if (!eap->skip && eap->addr_count > 0)
3447 {
3448 curwin->w_cursor.lnum = lnum;
3449 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003450#ifdef FEAT_VIRTUALEDIT
3451 curwin->w_cursor.coladd = 0;
3452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003455 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003456 eap->line1, eap->line2, &doesrange,
3457 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 failed = TRUE;
3460 break;
3461 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003462
3463 /* Handle a function returning a Funcref, Dictionary or List. */
3464 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3465 {
3466 failed = TRUE;
3467 break;
3468 }
3469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003470 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (doesrange || eap->skip)
3472 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003475 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003477 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (aborting())
3479 break;
3480 }
3481 if (eap->skip)
3482 --emsg_skip;
3483
3484 if (!failed)
3485 {
3486 /* Check for trailing illegal characters and a following command. */
3487 if (!ends_excmd(*arg))
3488 {
3489 emsg_severe = TRUE;
3490 EMSG(_(e_trailing));
3491 }
3492 else
3493 eap->nextcmd = check_nextcmd(arg);
3494 }
3495
3496end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003497 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003498 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499}
3500
3501/*
3502 * ":unlet[!] var1 ... " command.
3503 */
3504 void
3505ex_unlet(eap)
3506 exarg_T *eap;
3507{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003508 ex_unletlock(eap, eap->arg, 0);
3509}
3510
3511/*
3512 * ":lockvar" and ":unlockvar" commands
3513 */
3514 void
3515ex_lockvar(eap)
3516 exarg_T *eap;
3517{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003519 int deep = 2;
3520
3521 if (eap->forceit)
3522 deep = -1;
3523 else if (vim_isdigit(*arg))
3524 {
3525 deep = getdigits(&arg);
3526 arg = skipwhite(arg);
3527 }
3528
3529 ex_unletlock(eap, arg, deep);
3530}
3531
3532/*
3533 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3534 */
3535 static void
3536ex_unletlock(eap, argstart, deep)
3537 exarg_T *eap;
3538 char_u *argstart;
3539 int deep;
3540{
3541 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003544 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 do
3547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003548 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003549 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3550 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003551 if (lv.ll_name == NULL)
3552 error = TRUE; /* error but continue parsing */
3553 if (name_end == NULL || (!vim_iswhite(*name_end)
3554 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 if (name_end != NULL)
3557 {
3558 emsg_severe = TRUE;
3559 EMSG(_(e_trailing));
3560 }
3561 if (!(eap->skip || error))
3562 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 break;
3564 }
3565
3566 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003567 {
3568 if (eap->cmdidx == CMD_unlet)
3569 {
3570 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3571 error = TRUE;
3572 }
3573 else
3574 {
3575 if (do_lock_var(&lv, name_end, deep,
3576 eap->cmdidx == CMD_lockvar) == FAIL)
3577 error = TRUE;
3578 }
3579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003581 if (!eap->skip)
3582 clear_lval(&lv);
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 arg = skipwhite(name_end);
3585 } while (!ends_excmd(*arg));
3586
3587 eap->nextcmd = check_nextcmd(arg);
3588}
3589
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003592 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003593 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 int forceit;
3595{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596 int ret = OK;
3597 int cc;
3598
3599 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 cc = *name_end;
3602 *name_end = NUL;
3603
3604 /* Normal name or expanded name. */
3605 if (check_changedtick(lp->ll_name))
3606 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3612 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 else if (lp->ll_range)
3614 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616
3617 /* Delete a range of List items. */
3618 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3619 {
3620 li = lp->ll_li->li_next;
3621 listitem_remove(lp->ll_list, lp->ll_li);
3622 lp->ll_li = li;
3623 ++lp->ll_n1;
3624 }
3625 }
3626 else
3627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 /* unlet a List item. */
3630 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003633 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 }
3635
3636 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003637}
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639/*
3640 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
3643 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003644do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 hashtab_T *ht;
3649 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003650 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003651 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 ht = find_var_ht(name, &varname);
3654 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003656 hi = hash_find(ht, varname);
3657 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003658 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003659 di = HI2DI(hi);
3660 if (var_check_fixed(di->di_flags, name)
3661 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 return FAIL;
3663 delete_var(ht, hi);
3664 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 if (forceit)
3668 return OK;
3669 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 return FAIL;
3671}
3672
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673/*
3674 * Lock or unlock variable indicated by "lp".
3675 * "deep" is the levels to go (-1 for unlimited);
3676 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3677 */
3678 static int
3679do_lock_var(lp, name_end, deep, lock)
3680 lval_T *lp;
3681 char_u *name_end;
3682 int deep;
3683 int lock;
3684{
3685 int ret = OK;
3686 int cc;
3687 dictitem_T *di;
3688
3689 if (deep == 0) /* nothing to do */
3690 return OK;
3691
3692 if (lp->ll_tv == NULL)
3693 {
3694 cc = *name_end;
3695 *name_end = NUL;
3696
3697 /* Normal name or expanded name. */
3698 if (check_changedtick(lp->ll_name))
3699 ret = FAIL;
3700 else
3701 {
3702 di = find_var(lp->ll_name, NULL);
3703 if (di == NULL)
3704 ret = FAIL;
3705 else
3706 {
3707 if (lock)
3708 di->di_flags |= DI_FLAGS_LOCK;
3709 else
3710 di->di_flags &= ~DI_FLAGS_LOCK;
3711 item_lock(&di->di_tv, deep, lock);
3712 }
3713 }
3714 *name_end = cc;
3715 }
3716 else if (lp->ll_range)
3717 {
3718 listitem_T *li = lp->ll_li;
3719
3720 /* (un)lock a range of List items. */
3721 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3722 {
3723 item_lock(&li->li_tv, deep, lock);
3724 li = li->li_next;
3725 ++lp->ll_n1;
3726 }
3727 }
3728 else if (lp->ll_list != NULL)
3729 /* (un)lock a List item. */
3730 item_lock(&lp->ll_li->li_tv, deep, lock);
3731 else
3732 /* un(lock) a Dictionary item. */
3733 item_lock(&lp->ll_di->di_tv, deep, lock);
3734
3735 return ret;
3736}
3737
3738/*
3739 * Lock or unlock an item. "deep" is nr of levels to go.
3740 */
3741 static void
3742item_lock(tv, deep, lock)
3743 typval_T *tv;
3744 int deep;
3745 int lock;
3746{
3747 static int recurse = 0;
3748 list_T *l;
3749 listitem_T *li;
3750 dict_T *d;
3751 hashitem_T *hi;
3752 int todo;
3753
3754 if (recurse >= DICT_MAXNEST)
3755 {
3756 EMSG(_("E743: variable nested too deep for (un)lock"));
3757 return;
3758 }
3759 if (deep == 0)
3760 return;
3761 ++recurse;
3762
3763 /* lock/unlock the item itself */
3764 if (lock)
3765 tv->v_lock |= VAR_LOCKED;
3766 else
3767 tv->v_lock &= ~VAR_LOCKED;
3768
3769 switch (tv->v_type)
3770 {
3771 case VAR_LIST:
3772 if ((l = tv->vval.v_list) != NULL)
3773 {
3774 if (lock)
3775 l->lv_lock |= VAR_LOCKED;
3776 else
3777 l->lv_lock &= ~VAR_LOCKED;
3778 if (deep < 0 || deep > 1)
3779 /* recursive: lock/unlock the items the List contains */
3780 for (li = l->lv_first; li != NULL; li = li->li_next)
3781 item_lock(&li->li_tv, deep - 1, lock);
3782 }
3783 break;
3784 case VAR_DICT:
3785 if ((d = tv->vval.v_dict) != NULL)
3786 {
3787 if (lock)
3788 d->dv_lock |= VAR_LOCKED;
3789 else
3790 d->dv_lock &= ~VAR_LOCKED;
3791 if (deep < 0 || deep > 1)
3792 {
3793 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003794 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003795 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3796 {
3797 if (!HASHITEM_EMPTY(hi))
3798 {
3799 --todo;
3800 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3801 }
3802 }
3803 }
3804 }
3805 }
3806 --recurse;
3807}
3808
Bram Moolenaara40058a2005-07-11 22:42:07 +00003809/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003810 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3811 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003812 */
3813 static int
3814tv_islocked(tv)
3815 typval_T *tv;
3816{
3817 return (tv->v_lock & VAR_LOCKED)
3818 || (tv->v_type == VAR_LIST
3819 && tv->vval.v_list != NULL
3820 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3821 || (tv->v_type == VAR_DICT
3822 && tv->vval.v_dict != NULL
3823 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3824}
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3827/*
3828 * Delete all "menutrans_" variables.
3829 */
3830 void
3831del_menutrans_vars()
3832{
Bram Moolenaar33570922005-01-25 22:26:29 +00003833 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003834 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
Bram Moolenaar33570922005-01-25 22:26:29 +00003836 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003837 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003839 {
3840 if (!HASHITEM_EMPTY(hi))
3841 {
3842 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3844 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003845 }
3846 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848}
3849#endif
3850
3851#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3852
3853/*
3854 * Local string buffer for the next two functions to store a variable name
3855 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3856 * get_user_var_name().
3857 */
3858
3859static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3860
3861static char_u *varnamebuf = NULL;
3862static int varnamebuflen = 0;
3863
3864/*
3865 * Function to concatenate a prefix and a variable name.
3866 */
3867 static char_u *
3868cat_prefix_varname(prefix, name)
3869 int prefix;
3870 char_u *name;
3871{
3872 int len;
3873
3874 len = (int)STRLEN(name) + 3;
3875 if (len > varnamebuflen)
3876 {
3877 vim_free(varnamebuf);
3878 len += 10; /* some additional space */
3879 varnamebuf = alloc(len);
3880 if (varnamebuf == NULL)
3881 {
3882 varnamebuflen = 0;
3883 return NULL;
3884 }
3885 varnamebuflen = len;
3886 }
3887 *varnamebuf = prefix;
3888 varnamebuf[1] = ':';
3889 STRCPY(varnamebuf + 2, name);
3890 return varnamebuf;
3891}
3892
3893/*
3894 * Function given to ExpandGeneric() to obtain the list of user defined
3895 * (global/buffer/window/built-in) variable names.
3896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 char_u *
3898get_user_var_name(xp, idx)
3899 expand_T *xp;
3900 int idx;
3901{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003902 static long_u gdone;
3903 static long_u bdone;
3904 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003905#ifdef FEAT_WINDOWS
3906 static long_u tdone;
3907#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003908 static int vidx;
3909 static hashitem_T *hi;
3910 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003913 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003914 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003915#ifdef FEAT_WINDOWS
3916 tdone = 0;
3917#endif
3918 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003919
3920 /* Global variables */
3921 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003923 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003924 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 else
3926 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003927 while (HASHITEM_EMPTY(hi))
3928 ++hi;
3929 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3930 return cat_prefix_varname('g', hi->hi_key);
3931 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003933
3934 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003935 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003940 else
3941 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 while (HASHITEM_EMPTY(hi))
3943 ++hi;
3944 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 return (char_u *)"b:changedtick";
3950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951
3952 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003953 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003956 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003958 else
3959 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003960 while (HASHITEM_EMPTY(hi))
3961 ++hi;
3962 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003964
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003965#ifdef FEAT_WINDOWS
3966 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003967 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003968 if (tdone < ht->ht_used)
3969 {
3970 if (tdone++ == 0)
3971 hi = ht->ht_array;
3972 else
3973 ++hi;
3974 while (HASHITEM_EMPTY(hi))
3975 ++hi;
3976 return cat_prefix_varname('t', hi->hi_key);
3977 }
3978#endif
3979
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 /* v: variables */
3981 if (vidx < VV_LEN)
3982 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 vim_free(varnamebuf);
3985 varnamebuf = NULL;
3986 varnamebuflen = 0;
3987 return NULL;
3988}
3989
3990#endif /* FEAT_CMDL_COMPL */
3991
3992/*
3993 * types for expressions.
3994 */
3995typedef enum
3996{
3997 TYPE_UNKNOWN = 0
3998 , TYPE_EQUAL /* == */
3999 , TYPE_NEQUAL /* != */
4000 , TYPE_GREATER /* > */
4001 , TYPE_GEQUAL /* >= */
4002 , TYPE_SMALLER /* < */
4003 , TYPE_SEQUAL /* <= */
4004 , TYPE_MATCH /* =~ */
4005 , TYPE_NOMATCH /* !~ */
4006} exptype_T;
4007
4008/*
4009 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4012 */
4013
4014/*
4015 * Handle zero level expression.
4016 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004017 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004018 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * Return OK or FAIL.
4020 */
4021 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 char_u **nextcmd;
4026 int evaluate;
4027{
4028 int ret;
4029 char_u *p;
4030
4031 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (ret == FAIL || !ends_excmd(*p))
4034 {
4035 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 /*
4038 * Report the invalid expression unless the expression evaluation has
4039 * been cancelled due to an aborting error, an interrupt, or an
4040 * exception.
4041 */
4042 if (!aborting())
4043 EMSG2(_(e_invexpr2), arg);
4044 ret = FAIL;
4045 }
4046 if (nextcmd != NULL)
4047 *nextcmd = check_nextcmd(p);
4048
4049 return ret;
4050}
4051
4052/*
4053 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004054 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 *
4056 * "arg" must point to the first non-white of the expression.
4057 * "arg" is advanced to the next non-white after the recognized expression.
4058 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004059 * Note: "rettv.v_lock" is not set.
4060 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 * Return OK or FAIL.
4062 */
4063 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 int evaluate;
4068{
4069 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072 /*
4073 * Get the first variable.
4074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004075 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 return FAIL;
4077
4078 if ((*arg)[0] == '?')
4079 {
4080 result = FALSE;
4081 if (evaluate)
4082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083 int error = FALSE;
4084
4085 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 if (error)
4089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091
4092 /*
4093 * Get the second variable.
4094 */
4095 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 return FAIL;
4098
4099 /*
4100 * Check for the ":".
4101 */
4102 if ((*arg)[0] != ':')
4103 {
4104 EMSG(_("E109: Missing ':' after '?'"));
4105 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 return FAIL;
4108 }
4109
4110 /*
4111 * Get the third variable.
4112 */
4113 *arg = skipwhite(*arg + 1);
4114 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4115 {
4116 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 return FAIL;
4119 }
4120 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123
4124 return OK;
4125}
4126
4127/*
4128 * Handle first level expression:
4129 * expr2 || expr2 || expr2 logical OR
4130 *
4131 * "arg" must point to the first non-white of the expression.
4132 * "arg" is advanced to the next non-white after the recognized expression.
4133 *
4134 * Return OK or FAIL.
4135 */
4136 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int evaluate;
4141{
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 long result;
4144 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004145 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 /*
4148 * Get the first variable.
4149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 return FAIL;
4152
4153 /*
4154 * Repeat until there is no following "||".
4155 */
4156 first = TRUE;
4157 result = FALSE;
4158 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4159 {
4160 if (evaluate && first)
4161 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 if (error)
4166 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 first = FALSE;
4168 }
4169
4170 /*
4171 * Get the second variable.
4172 */
4173 *arg = skipwhite(*arg + 2);
4174 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4175 return FAIL;
4176
4177 /*
4178 * Compute the result.
4179 */
4180 if (evaluate && !result)
4181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (error)
4186 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 if (evaluate)
4189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 rettv->v_type = VAR_NUMBER;
4191 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 }
4194
4195 return OK;
4196}
4197
4198/*
4199 * Handle second level expression:
4200 * expr3 && expr3 && expr3 logical AND
4201 *
4202 * "arg" must point to the first non-white of the expression.
4203 * "arg" is advanced to the next non-white after the recognized expression.
4204 *
4205 * Return OK or FAIL.
4206 */
4207 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 int evaluate;
4212{
Bram Moolenaar33570922005-01-25 22:26:29 +00004213 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 long result;
4215 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218 /*
4219 * Get the first variable.
4220 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return FAIL;
4223
4224 /*
4225 * Repeat until there is no following "&&".
4226 */
4227 first = TRUE;
4228 result = TRUE;
4229 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4230 {
4231 if (evaluate && first)
4232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 if (error)
4237 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 first = FALSE;
4239 }
4240
4241 /*
4242 * Get the second variable.
4243 */
4244 *arg = skipwhite(*arg + 2);
4245 if (eval4(arg, &var2, evaluate && result) == FAIL)
4246 return FAIL;
4247
4248 /*
4249 * Compute the result.
4250 */
4251 if (evaluate && result)
4252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (error)
4257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 if (evaluate)
4260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 rettv->v_type = VAR_NUMBER;
4262 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 }
4265
4266 return OK;
4267}
4268
4269/*
4270 * Handle third level expression:
4271 * var1 == var2
4272 * var1 =~ var2
4273 * var1 != var2
4274 * var1 !~ var2
4275 * var1 > var2
4276 * var1 >= var2
4277 * var1 < var2
4278 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004279 * var1 is var2
4280 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 *
4282 * "arg" must point to the first non-white of the expression.
4283 * "arg" is advanced to the next non-white after the recognized expression.
4284 *
4285 * Return OK or FAIL.
4286 */
4287 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 int evaluate;
4292{
Bram Moolenaar33570922005-01-25 22:26:29 +00004293 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 char_u *p;
4295 int i;
4296 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004297 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int len = 2;
4299 long n1, n2;
4300 char_u *s1, *s2;
4301 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4302 regmatch_T regmatch;
4303 int ic;
4304 char_u *save_cpo;
4305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 p = *arg;
4313 switch (p[0])
4314 {
4315 case '=': if (p[1] == '=')
4316 type = TYPE_EQUAL;
4317 else if (p[1] == '~')
4318 type = TYPE_MATCH;
4319 break;
4320 case '!': if (p[1] == '=')
4321 type = TYPE_NEQUAL;
4322 else if (p[1] == '~')
4323 type = TYPE_NOMATCH;
4324 break;
4325 case '>': if (p[1] != '=')
4326 {
4327 type = TYPE_GREATER;
4328 len = 1;
4329 }
4330 else
4331 type = TYPE_GEQUAL;
4332 break;
4333 case '<': if (p[1] != '=')
4334 {
4335 type = TYPE_SMALLER;
4336 len = 1;
4337 }
4338 else
4339 type = TYPE_SEQUAL;
4340 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004341 case 'i': if (p[1] == 's')
4342 {
4343 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4344 len = 5;
4345 if (!vim_isIDc(p[len]))
4346 {
4347 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4348 type_is = TRUE;
4349 }
4350 }
4351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353
4354 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004355 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 */
4357 if (type != TYPE_UNKNOWN)
4358 {
4359 /* extra question mark appended: ignore case */
4360 if (p[len] == '?')
4361 {
4362 ic = TRUE;
4363 ++len;
4364 }
4365 /* extra '#' appended: match case */
4366 else if (p[len] == '#')
4367 {
4368 ic = FALSE;
4369 ++len;
4370 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004371 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 else
4373 ic = p_ic;
4374
4375 /*
4376 * Get the second variable.
4377 */
4378 *arg = skipwhite(p + len);
4379 if (eval5(arg, &var2, evaluate) == FAIL)
4380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004381 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 return FAIL;
4383 }
4384
4385 if (evaluate)
4386 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 if (type_is && rettv->v_type != var2.v_type)
4388 {
4389 /* For "is" a different type always means FALSE, for "notis"
4390 * it means TRUE. */
4391 n1 = (type == TYPE_NEQUAL);
4392 }
4393 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4394 {
4395 if (type_is)
4396 {
4397 n1 = (rettv->v_type == var2.v_type
4398 && rettv->vval.v_list == var2.vval.v_list);
4399 if (type == TYPE_NEQUAL)
4400 n1 = !n1;
4401 }
4402 else if (rettv->v_type != var2.v_type
4403 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4404 {
4405 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004406 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004407 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004408 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004409 clear_tv(rettv);
4410 clear_tv(&var2);
4411 return FAIL;
4412 }
4413 else
4414 {
4415 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004416 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4417 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004418 if (type == TYPE_NEQUAL)
4419 n1 = !n1;
4420 }
4421 }
4422
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004423 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4424 {
4425 if (type_is)
4426 {
4427 n1 = (rettv->v_type == var2.v_type
4428 && rettv->vval.v_dict == var2.vval.v_dict);
4429 if (type == TYPE_NEQUAL)
4430 n1 = !n1;
4431 }
4432 else if (rettv->v_type != var2.v_type
4433 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4434 {
4435 if (rettv->v_type != var2.v_type)
4436 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4437 else
4438 EMSG(_("E736: Invalid operation for Dictionary"));
4439 clear_tv(rettv);
4440 clear_tv(&var2);
4441 return FAIL;
4442 }
4443 else
4444 {
4445 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004446 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4447 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004448 if (type == TYPE_NEQUAL)
4449 n1 = !n1;
4450 }
4451 }
4452
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004453 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4454 {
4455 if (rettv->v_type != var2.v_type
4456 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4457 {
4458 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004459 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004460 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004461 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004462 clear_tv(rettv);
4463 clear_tv(&var2);
4464 return FAIL;
4465 }
4466 else
4467 {
4468 /* Compare two Funcrefs for being equal or unequal. */
4469 if (rettv->vval.v_string == NULL
4470 || var2.vval.v_string == NULL)
4471 n1 = FALSE;
4472 else
4473 n1 = STRCMP(rettv->vval.v_string,
4474 var2.vval.v_string) == 0;
4475 if (type == TYPE_NEQUAL)
4476 n1 = !n1;
4477 }
4478 }
4479
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004480#ifdef FEAT_FLOAT
4481 /*
4482 * If one of the two variables is a float, compare as a float.
4483 * When using "=~" or "!~", always compare as string.
4484 */
4485 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4486 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4487 {
4488 float_T f1, f2;
4489
4490 if (rettv->v_type == VAR_FLOAT)
4491 f1 = rettv->vval.v_float;
4492 else
4493 f1 = get_tv_number(rettv);
4494 if (var2.v_type == VAR_FLOAT)
4495 f2 = var2.vval.v_float;
4496 else
4497 f2 = get_tv_number(&var2);
4498 n1 = FALSE;
4499 switch (type)
4500 {
4501 case TYPE_EQUAL: n1 = (f1 == f2); break;
4502 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4503 case TYPE_GREATER: n1 = (f1 > f2); break;
4504 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4505 case TYPE_SMALLER: n1 = (f1 < f2); break;
4506 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4507 case TYPE_UNKNOWN:
4508 case TYPE_MATCH:
4509 case TYPE_NOMATCH: break; /* avoid gcc warning */
4510 }
4511 }
4512#endif
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /*
4515 * If one of the two variables is a number, compare as a number.
4516 * When using "=~" or "!~", always compare as string.
4517 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004518 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004521 n1 = get_tv_number(rettv);
4522 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 switch (type)
4524 {
4525 case TYPE_EQUAL: n1 = (n1 == n2); break;
4526 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4527 case TYPE_GREATER: n1 = (n1 > n2); break;
4528 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4529 case TYPE_SMALLER: n1 = (n1 < n2); break;
4530 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4531 case TYPE_UNKNOWN:
4532 case TYPE_MATCH:
4533 case TYPE_NOMATCH: break; /* avoid gcc warning */
4534 }
4535 }
4536 else
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 s1 = get_tv_string_buf(rettv, buf1);
4539 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4541 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4542 else
4543 i = 0;
4544 n1 = FALSE;
4545 switch (type)
4546 {
4547 case TYPE_EQUAL: n1 = (i == 0); break;
4548 case TYPE_NEQUAL: n1 = (i != 0); break;
4549 case TYPE_GREATER: n1 = (i > 0); break;
4550 case TYPE_GEQUAL: n1 = (i >= 0); break;
4551 case TYPE_SMALLER: n1 = (i < 0); break;
4552 case TYPE_SEQUAL: n1 = (i <= 0); break;
4553
4554 case TYPE_MATCH:
4555 case TYPE_NOMATCH:
4556 /* avoid 'l' flag in 'cpoptions' */
4557 save_cpo = p_cpo;
4558 p_cpo = (char_u *)"";
4559 regmatch.regprog = vim_regcomp(s2,
4560 RE_MAGIC + RE_STRING);
4561 regmatch.rm_ic = ic;
4562 if (regmatch.regprog != NULL)
4563 {
4564 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004565 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (type == TYPE_NOMATCH)
4567 n1 = !n1;
4568 }
4569 p_cpo = save_cpo;
4570 break;
4571
4572 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4573 }
4574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 rettv->v_type = VAR_NUMBER;
4578 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 }
4581
4582 return OK;
4583}
4584
4585/*
4586 * Handle fourth level expression:
4587 * + number addition
4588 * - number subtraction
4589 * . string concatenation
4590 *
4591 * "arg" must point to the first non-white of the expression.
4592 * "arg" is advanced to the next non-white after the recognized expression.
4593 *
4594 * Return OK or FAIL.
4595 */
4596 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 int evaluate;
4601{
Bram Moolenaar33570922005-01-25 22:26:29 +00004602 typval_T var2;
4603 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 int op;
4605 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004606#ifdef FEAT_FLOAT
4607 float_T f1 = 0, f2 = 0;
4608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 char_u *s1, *s2;
4610 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4611 char_u *p;
4612
4613 /*
4614 * Get the first variable.
4615 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004616 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 return FAIL;
4618
4619 /*
4620 * Repeat computing, until no '+', '-' or '.' is following.
4621 */
4622 for (;;)
4623 {
4624 op = **arg;
4625 if (op != '+' && op != '-' && op != '.')
4626 break;
4627
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004628 if ((op != '+' || rettv->v_type != VAR_LIST)
4629#ifdef FEAT_FLOAT
4630 && (op == '.' || rettv->v_type != VAR_FLOAT)
4631#endif
4632 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004633 {
4634 /* For "list + ...", an illegal use of the first operand as
4635 * a number cannot be determined before evaluating the 2nd
4636 * operand: if this is also a list, all is ok.
4637 * For "something . ...", "something - ..." or "non-list + ...",
4638 * we know that the first operand needs to be a string or number
4639 * without evaluating the 2nd operand. So check before to avoid
4640 * side effects after an error. */
4641 if (evaluate && get_tv_string_chk(rettv) == NULL)
4642 {
4643 clear_tv(rettv);
4644 return FAIL;
4645 }
4646 }
4647
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 /*
4649 * Get the second variable.
4650 */
4651 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004652 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 return FAIL;
4656 }
4657
4658 if (evaluate)
4659 {
4660 /*
4661 * Compute the result.
4662 */
4663 if (op == '.')
4664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004665 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4666 s2 = get_tv_string_buf_chk(&var2, buf2);
4667 if (s2 == NULL) /* type error ? */
4668 {
4669 clear_tv(rettv);
4670 clear_tv(&var2);
4671 return FAIL;
4672 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004673 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
4675 rettv->v_type = VAR_STRING;
4676 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004678 else if (op == '+' && rettv->v_type == VAR_LIST
4679 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004680 {
4681 /* concatenate Lists */
4682 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4683 &var3) == FAIL)
4684 {
4685 clear_tv(rettv);
4686 clear_tv(&var2);
4687 return FAIL;
4688 }
4689 clear_tv(rettv);
4690 *rettv = var3;
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 else
4693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004694 int error = FALSE;
4695
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004699 f1 = rettv->vval.v_float;
4700 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702 else
4703#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705 n1 = get_tv_number_chk(rettv, &error);
4706 if (error)
4707 {
4708 /* This can only happen for "list + non-list". For
4709 * "non-list + ..." or "something - ...", we returned
4710 * before evaluating the 2nd operand. */
4711 clear_tv(rettv);
4712 return FAIL;
4713 }
4714#ifdef FEAT_FLOAT
4715 if (var2.v_type == VAR_FLOAT)
4716 f1 = n1;
4717#endif
4718 }
4719#ifdef FEAT_FLOAT
4720 if (var2.v_type == VAR_FLOAT)
4721 {
4722 f2 = var2.vval.v_float;
4723 n2 = 0;
4724 }
4725 else
4726#endif
4727 {
4728 n2 = get_tv_number_chk(&var2, &error);
4729 if (error)
4730 {
4731 clear_tv(rettv);
4732 clear_tv(&var2);
4733 return FAIL;
4734 }
4735#ifdef FEAT_FLOAT
4736 if (rettv->v_type == VAR_FLOAT)
4737 f2 = n2;
4738#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004740 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004741
4742#ifdef FEAT_FLOAT
4743 /* If there is a float on either side the result is a float. */
4744 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4745 {
4746 if (op == '+')
4747 f1 = f1 + f2;
4748 else
4749 f1 = f1 - f2;
4750 rettv->v_type = VAR_FLOAT;
4751 rettv->vval.v_float = f1;
4752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004754#endif
4755 {
4756 if (op == '+')
4757 n1 = n1 + n2;
4758 else
4759 n1 = n1 - n2;
4760 rettv->v_type = VAR_NUMBER;
4761 rettv->vval.v_number = n1;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 }
4767 return OK;
4768}
4769
4770/*
4771 * Handle fifth level expression:
4772 * * number multiplication
4773 * / number division
4774 * % number modulo
4775 *
4776 * "arg" must point to the first non-white of the expression.
4777 * "arg" is advanced to the next non-white after the recognized expression.
4778 *
4779 * Return OK or FAIL.
4780 */
4781 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004782eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004786 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
Bram Moolenaar33570922005-01-25 22:26:29 +00004788 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 int op;
4790 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791#ifdef FEAT_FLOAT
4792 int use_float = FALSE;
4793 float_T f1 = 0, f2;
4794#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797 /*
4798 * Get the first variable.
4799 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004800 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 return FAIL;
4802
4803 /*
4804 * Repeat computing, until no '*', '/' or '%' is following.
4805 */
4806 for (;;)
4807 {
4808 op = **arg;
4809 if (op != '*' && op != '/' && op != '%')
4810 break;
4811
4812 if (evaluate)
4813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814#ifdef FEAT_FLOAT
4815 if (rettv->v_type == VAR_FLOAT)
4816 {
4817 f1 = rettv->vval.v_float;
4818 use_float = TRUE;
4819 n1 = 0;
4820 }
4821 else
4822#endif
4823 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 if (error)
4826 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828 else
4829 n1 = 0;
4830
4831 /*
4832 * Get the second variable.
4833 */
4834 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004835 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 return FAIL;
4837
4838 if (evaluate)
4839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840#ifdef FEAT_FLOAT
4841 if (var2.v_type == VAR_FLOAT)
4842 {
4843 if (!use_float)
4844 {
4845 f1 = n1;
4846 use_float = TRUE;
4847 }
4848 f2 = var2.vval.v_float;
4849 n2 = 0;
4850 }
4851 else
4852#endif
4853 {
4854 n2 = get_tv_number_chk(&var2, &error);
4855 clear_tv(&var2);
4856 if (error)
4857 return FAIL;
4858#ifdef FEAT_FLOAT
4859 if (use_float)
4860 f2 = n2;
4861#endif
4862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
4864 /*
4865 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868#ifdef FEAT_FLOAT
4869 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871 if (op == '*')
4872 f1 = f1 * f2;
4873 else if (op == '/')
4874 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004875# ifdef VMS
4876 /* VMS crashes on divide by zero, work around it */
4877 if (f2 == 0.0)
4878 {
4879 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004880 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004881 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004882 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004884 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004885 }
4886 else
4887 f1 = f1 / f2;
4888# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889 /* We rely on the floating point library to handle divide
4890 * by zero to result in "inf" and not a crash. */
4891 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004892# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004896 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 return FAIL;
4898 }
4899 rettv->v_type = VAR_FLOAT;
4900 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 if (op == '*')
4906 n1 = n1 * n2;
4907 else if (op == '/')
4908 {
4909 if (n2 == 0) /* give an error message? */
4910 {
4911 if (n1 == 0)
4912 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4913 else if (n1 < 0)
4914 n1 = -0x7fffffffL;
4915 else
4916 n1 = 0x7fffffffL;
4917 }
4918 else
4919 n1 = n1 / n2;
4920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 {
4923 if (n2 == 0) /* give an error message? */
4924 n1 = 0;
4925 else
4926 n1 = n1 % n2;
4927 }
4928 rettv->v_type = VAR_NUMBER;
4929 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 }
4933
4934 return OK;
4935}
4936
4937/*
4938 * Handle sixth level expression:
4939 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004940 * "string" string constant
4941 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 * &option-name option value
4943 * @r register contents
4944 * identifier variable value
4945 * function() function call
4946 * $VAR environment variable
4947 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004948 * [expr, expr] List
4949 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 *
4951 * Also handle:
4952 * ! in front logical NOT
4953 * - in front unary minus
4954 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 * trailing [] subscript in String or List
4956 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 *
4958 * "arg" must point to the first non-white of the expression.
4959 * "arg" is advanced to the next non-white after the recognized expression.
4960 *
4961 * Return OK or FAIL.
4962 */
4963 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004964eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004968 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 long n;
4971 int len;
4972 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 char_u *start_leader, *end_leader;
4974 int ret = OK;
4975 char_u *alias;
4976
4977 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004979 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004981 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 /*
4984 * Skip '!' and '-' characters. They are handled later.
4985 */
4986 start_leader = *arg;
4987 while (**arg == '!' || **arg == '-' || **arg == '+')
4988 *arg = skipwhite(*arg + 1);
4989 end_leader = *arg;
4990
4991 switch (**arg)
4992 {
4993 /*
4994 * Number constant.
4995 */
4996 case '0':
4997 case '1':
4998 case '2':
4999 case '3':
5000 case '4':
5001 case '5':
5002 case '6':
5003 case '7':
5004 case '8':
5005 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 {
5007#ifdef FEAT_FLOAT
5008 char_u *p = skipdigits(*arg + 1);
5009 int get_float = FALSE;
5010
5011 /* We accept a float when the format matches
5012 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005013 * strict to avoid backwards compatibility problems.
5014 * Don't look for a float after the "." operator, so that
5015 * ":let vers = 1.2.3" doesn't fail. */
5016 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 get_float = TRUE;
5019 p = skipdigits(p + 2);
5020 if (*p == 'e' || *p == 'E')
5021 {
5022 ++p;
5023 if (*p == '-' || *p == '+')
5024 ++p;
5025 if (!vim_isdigit(*p))
5026 get_float = FALSE;
5027 else
5028 p = skipdigits(p + 1);
5029 }
5030 if (ASCII_ISALPHA(*p) || *p == '.')
5031 get_float = FALSE;
5032 }
5033 if (get_float)
5034 {
5035 float_T f;
5036
5037 *arg += string2float(*arg, &f);
5038 if (evaluate)
5039 {
5040 rettv->v_type = VAR_FLOAT;
5041 rettv->vval.v_float = f;
5042 }
5043 }
5044 else
5045#endif
5046 {
5047 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5048 *arg += len;
5049 if (evaluate)
5050 {
5051 rettv->v_type = VAR_NUMBER;
5052 rettv->vval.v_number = n;
5053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
5058 /*
5059 * String constant: "string".
5060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 break;
5063
5064 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068 break;
5069
5070 /*
5071 * List: [expr, expr]
5072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 break;
5075
5076 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 * Dictionary: {key: val, key: val}
5078 */
5079 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5080 break;
5081
5082 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005083 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005085 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087
5088 /*
5089 * Environment variable: $VAR.
5090 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
5093
5094 /*
5095 * Register contents: @r.
5096 */
5097 case '@': ++*arg;
5098 if (evaluate)
5099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005101 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103 if (**arg != NUL)
5104 ++*arg;
5105 break;
5106
5107 /*
5108 * nested expression: (expression).
5109 */
5110 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (**arg == ')')
5113 ++*arg;
5114 else if (ret == OK)
5115 {
5116 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 ret = FAIL;
5119 }
5120 break;
5121
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 break;
5124 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005125
5126 if (ret == NOTDONE)
5127 {
5128 /*
5129 * Must be a variable or function name.
5130 * Can also be a curly-braces kind of name: {expr}.
5131 */
5132 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005133 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 if (alias != NULL)
5135 s = alias;
5136
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005137 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 ret = FAIL;
5139 else
5140 {
5141 if (**arg == '(') /* recursive! */
5142 {
5143 /* If "s" is the name of a variable of type VAR_FUNC
5144 * use its contents. */
5145 s = deref_func_name(s, &len);
5146
5147 /* Invoke the function. */
5148 ret = get_func_tv(s, len, rettv, arg,
5149 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005150 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005151
5152 /* If evaluate is FALSE rettv->v_type was not set in
5153 * get_func_tv, but it's needed in handle_subscript() to parse
5154 * what follows. So set it here. */
5155 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5156 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005157 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005158 rettv->v_type = VAR_FUNC;
5159 }
5160
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 /* Stop the expression evaluation when immediately
5162 * aborting on error, or when an interrupt occurred or
5163 * an exception was thrown but not caught. */
5164 if (aborting())
5165 {
5166 if (ret == OK)
5167 clear_tv(rettv);
5168 ret = FAIL;
5169 }
5170 }
5171 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005172 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005173 else
5174 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005176 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 }
5178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 *arg = skipwhite(*arg);
5180
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5182 * expr(expr). */
5183 if (ret == OK)
5184 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186 /*
5187 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5188 */
5189 if (ret == OK && evaluate && end_leader > start_leader)
5190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005191 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005192 int val = 0;
5193#ifdef FEAT_FLOAT
5194 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005195
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 if (rettv->v_type == VAR_FLOAT)
5197 f = rettv->vval.v_float;
5198 else
5199#endif
5200 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 clear_tv(rettv);
5204 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 else
5207 {
5208 while (end_leader > start_leader)
5209 {
5210 --end_leader;
5211 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 {
5213#ifdef FEAT_FLOAT
5214 if (rettv->v_type == VAR_FLOAT)
5215 f = !f;
5216 else
5217#endif
5218 val = !val;
5219 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005221 {
5222#ifdef FEAT_FLOAT
5223 if (rettv->v_type == VAR_FLOAT)
5224 f = -f;
5225 else
5226#endif
5227 val = -val;
5228 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 {
5233 clear_tv(rettv);
5234 rettv->vval.v_float = f;
5235 }
5236 else
5237#endif
5238 {
5239 clear_tv(rettv);
5240 rettv->v_type = VAR_NUMBER;
5241 rettv->vval.v_number = val;
5242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245
5246 return ret;
5247}
5248
5249/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005250 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5251 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5253 */
5254 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005255eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260{
5261 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 long len = -1;
5265 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005269 if (rettv->v_type == VAR_FUNC
5270#ifdef FEAT_FLOAT
5271 || rettv->v_type == VAR_FLOAT
5272#endif
5273 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 if (verbose)
5276 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 return FAIL;
5278 }
5279
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 /*
5283 * dict.name
5284 */
5285 key = *arg + 1;
5286 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5287 ;
5288 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 /*
5295 * something[idx]
5296 *
5297 * Get the (first) variable from inside the [].
5298 */
5299 *arg = skipwhite(*arg + 1);
5300 if (**arg == ':')
5301 empty1 = TRUE;
5302 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5303 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5305 {
5306 /* not a number or string */
5307 clear_tv(&var1);
5308 return FAIL;
5309 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310
5311 /*
5312 * Get the second variable from inside the [:].
5313 */
5314 if (**arg == ':')
5315 {
5316 range = TRUE;
5317 *arg = skipwhite(*arg + 1);
5318 if (**arg == ']')
5319 empty2 = TRUE;
5320 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 if (!empty1)
5323 clear_tv(&var1);
5324 return FAIL;
5325 }
5326 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5327 {
5328 /* not a number or string */
5329 if (!empty1)
5330 clear_tv(&var1);
5331 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332 return FAIL;
5333 }
5334 }
5335
5336 /* Check for the ']'. */
5337 if (**arg != ']')
5338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005339 if (verbose)
5340 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 clear_tv(&var1);
5342 if (range)
5343 clear_tv(&var2);
5344 return FAIL;
5345 }
5346 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 }
5348
5349 if (evaluate)
5350 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 n1 = 0;
5352 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 n1 = get_tv_number(&var1);
5355 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357 if (range)
5358 {
5359 if (empty2)
5360 n2 = -1;
5361 else
5362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 n2 = get_tv_number(&var2);
5364 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 }
5366 }
5367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 {
5370 case VAR_NUMBER:
5371 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 len = (long)STRLEN(s);
5374 if (range)
5375 {
5376 /* The resulting variable is a substring. If the indexes
5377 * are out of range the result is empty. */
5378 if (n1 < 0)
5379 {
5380 n1 = len + n1;
5381 if (n1 < 0)
5382 n1 = 0;
5383 }
5384 if (n2 < 0)
5385 n2 = len + n2;
5386 else if (n2 >= len)
5387 n2 = len;
5388 if (n1 >= len || n2 < 0 || n1 > n2)
5389 s = NULL;
5390 else
5391 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5392 }
5393 else
5394 {
5395 /* The resulting variable is a string of a single
5396 * character. If the index is too big or negative the
5397 * result is empty. */
5398 if (n1 >= len || n1 < 0)
5399 s = NULL;
5400 else
5401 s = vim_strnsave(s + n1, 1);
5402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 clear_tv(rettv);
5404 rettv->v_type = VAR_STRING;
5405 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 break;
5407
5408 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 if (n1 < 0)
5411 n1 = len + n1;
5412 if (!empty1 && (n1 < 0 || n1 >= len))
5413 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005414 /* For a range we allow invalid values and return an empty
5415 * list. A list index out of range is an error. */
5416 if (!range)
5417 {
5418 if (verbose)
5419 EMSGN(_(e_listidx), n1);
5420 return FAIL;
5421 }
5422 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 }
5424 if (range)
5425 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 list_T *l;
5427 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428
5429 if (n2 < 0)
5430 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005431 else if (n2 >= len)
5432 n2 = len - 1;
5433 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005434 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 l = list_alloc();
5436 if (l == NULL)
5437 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 n1 <= n2; ++n1)
5440 {
5441 if (list_append_tv(l, &item->li_tv) == FAIL)
5442 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005443 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 return FAIL;
5445 }
5446 item = item->li_next;
5447 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 clear_tv(rettv);
5449 rettv->v_type = VAR_LIST;
5450 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005451 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453 else
5454 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005455 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 clear_tv(rettv);
5457 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460
5461 case VAR_DICT:
5462 if (range)
5463 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005464 if (verbose)
5465 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 if (len == -1)
5467 clear_tv(&var1);
5468 return FAIL;
5469 }
5470 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005471 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472
5473 if (len == -1)
5474 {
5475 key = get_tv_string(&var1);
5476 if (*key == NUL)
5477 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005478 if (verbose)
5479 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 clear_tv(&var1);
5481 return FAIL;
5482 }
5483 }
5484
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005485 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005487 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005488 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 if (len == -1)
5490 clear_tv(&var1);
5491 if (item == NULL)
5492 return FAIL;
5493
5494 copy_tv(&item->di_tv, &var1);
5495 clear_tv(rettv);
5496 *rettv = var1;
5497 }
5498 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 }
5500 }
5501
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 return OK;
5503}
5504
5505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * Get an option value.
5507 * "arg" points to the '&' or '+' before the option name.
5508 * "arg" is advanced to character after the option name.
5509 * Return OK or FAIL.
5510 */
5511 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005514 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 int evaluate;
5516{
5517 char_u *option_end;
5518 long numval;
5519 char_u *stringval;
5520 int opt_type;
5521 int c;
5522 int working = (**arg == '+'); /* has("+option") */
5523 int ret = OK;
5524 int opt_flags;
5525
5526 /*
5527 * Isolate the option name and find its value.
5528 */
5529 option_end = find_option_end(arg, &opt_flags);
5530 if (option_end == NULL)
5531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 EMSG2(_("E112: Option name missing: %s"), *arg);
5534 return FAIL;
5535 }
5536
5537 if (!evaluate)
5538 {
5539 *arg = option_end;
5540 return OK;
5541 }
5542
5543 c = *option_end;
5544 *option_end = NUL;
5545 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 if (opt_type == -3) /* invalid name */
5549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 EMSG2(_("E113: Unknown option: %s"), *arg);
5552 ret = FAIL;
5553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
5556 if (opt_type == -2) /* hidden string option */
5557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 rettv->v_type = VAR_STRING;
5559 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561 else if (opt_type == -1) /* hidden number option */
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 rettv->v_type = VAR_NUMBER;
5564 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566 else if (opt_type == 1) /* number option */
5567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 rettv->v_type = VAR_NUMBER;
5569 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 else /* string option */
5572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 rettv->v_type = VAR_STRING;
5574 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576 }
5577 else if (working && (opt_type == -2 || opt_type == -1))
5578 ret = FAIL;
5579
5580 *option_end = c; /* put back for error messages */
5581 *arg = option_end;
5582
5583 return ret;
5584}
5585
5586/*
5587 * Allocate a variable for a string constant.
5588 * Return OK or FAIL.
5589 */
5590 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 int evaluate;
5595{
5596 char_u *p;
5597 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 int extra = 0;
5599
5600 /*
5601 * Find the end of the string, skipping backslashed characters.
5602 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005603 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
5605 if (*p == '\\' && p[1] != NUL)
5606 {
5607 ++p;
5608 /* A "\<x>" form occupies at least 4 characters, and produces up
5609 * to 6 characters: reserve space for 2 extra */
5610 if (*p == '<')
5611 extra += 2;
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614
5615 if (*p != '"')
5616 {
5617 EMSG2(_("E114: Missing quote: %s"), *arg);
5618 return FAIL;
5619 }
5620
5621 /* If only parsing, set *arg and return here */
5622 if (!evaluate)
5623 {
5624 *arg = p + 1;
5625 return OK;
5626 }
5627
5628 /*
5629 * Copy the string into allocated memory, handling backslashed
5630 * characters.
5631 */
5632 name = alloc((unsigned)(p - *arg + extra));
5633 if (name == NULL)
5634 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 rettv->v_type = VAR_STRING;
5636 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
5640 if (*p == '\\')
5641 {
5642 switch (*++p)
5643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 case 'b': *name++ = BS; ++p; break;
5645 case 'e': *name++ = ESC; ++p; break;
5646 case 'f': *name++ = FF; ++p; break;
5647 case 'n': *name++ = NL; ++p; break;
5648 case 'r': *name++ = CAR; ++p; break;
5649 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 case 'X': /* hex: "\x1", "\x12" */
5652 case 'x':
5653 case 'u': /* Unicode: "\u0023" */
5654 case 'U':
5655 if (vim_isxdigit(p[1]))
5656 {
5657 int n, nr;
5658 int c = toupper(*p);
5659
5660 if (c == 'X')
5661 n = 2;
5662 else
5663 n = 4;
5664 nr = 0;
5665 while (--n >= 0 && vim_isxdigit(p[1]))
5666 {
5667 ++p;
5668 nr = (nr << 4) + hex2nr(*p);
5669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_MBYTE
5672 /* For "\u" store the number according to
5673 * 'encoding'. */
5674 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 else
5677#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 break;
5681
5682 /* octal: "\1", "\12", "\123" */
5683 case '0':
5684 case '1':
5685 case '2':
5686 case '3':
5687 case '4':
5688 case '5':
5689 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 case '7': *name = *p++ - '0';
5691 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693 *name = (*name << 3) + *p++ - '0';
5694 if (*p >= '0' && *p <= '7')
5695 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 break;
5699
5700 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 if (extra != 0)
5703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 break;
5706 }
5707 /* FALLTHROUGH */
5708
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 break;
5711 }
5712 }
5713 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 *arg = p + 1;
5719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 return OK;
5721}
5722
5723/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * Return OK or FAIL.
5726 */
5727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 int evaluate;
5732{
5733 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005734 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005735 int reduce = 0;
5736
5737 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 */
5740 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 break;
5746 ++reduce;
5747 ++p;
5748 }
5749 }
5750
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 return FAIL;
5755 }
5756
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 if (!evaluate)
5759 {
5760 *arg = p + 1;
5761 return OK;
5762 }
5763
5764 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 */
5767 str = alloc((unsigned)((p - *arg) - reduce));
5768 if (str == NULL)
5769 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 rettv->v_type = VAR_STRING;
5771 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 break;
5779 ++p;
5780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 *arg = p + 1;
5785
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 return OK;
5787}
5788
5789/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 * Allocate a variable for a List and fill it from "*arg".
5791 * Return OK or FAIL.
5792 */
5793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005794get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005796 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005797 int evaluate;
5798{
Bram Moolenaar33570922005-01-25 22:26:29 +00005799 list_T *l = NULL;
5800 typval_T tv;
5801 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802
5803 if (evaluate)
5804 {
5805 l = list_alloc();
5806 if (l == NULL)
5807 return FAIL;
5808 }
5809
5810 *arg = skipwhite(*arg + 1);
5811 while (**arg != ']' && **arg != NUL)
5812 {
5813 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5814 goto failret;
5815 if (evaluate)
5816 {
5817 item = listitem_alloc();
5818 if (item != NULL)
5819 {
5820 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005821 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 list_append(l, item);
5823 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005824 else
5825 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 }
5827
5828 if (**arg == ']')
5829 break;
5830 if (**arg != ',')
5831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 goto failret;
5834 }
5835 *arg = skipwhite(*arg + 1);
5836 }
5837
5838 if (**arg != ']')
5839 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841failret:
5842 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005843 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 return FAIL;
5845 }
5846
5847 *arg = skipwhite(*arg + 1);
5848 if (evaluate)
5849 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005850 rettv->v_type = VAR_LIST;
5851 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 ++l->lv_refcount;
5853 }
5854
5855 return OK;
5856}
5857
5858/*
5859 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005860 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005862 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863list_alloc()
5864{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005865 list_T *l;
5866
5867 l = (list_T *)alloc_clear(sizeof(list_T));
5868 if (l != NULL)
5869 {
5870 /* Prepend the list to the list of lists for garbage collection. */
5871 if (first_list != NULL)
5872 first_list->lv_used_prev = l;
5873 l->lv_used_prev = NULL;
5874 l->lv_used_next = first_list;
5875 first_list = l;
5876 }
5877 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878}
5879
5880/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005881 * Allocate an empty list for a return value.
5882 * Returns OK or FAIL.
5883 */
5884 static int
5885rettv_list_alloc(rettv)
5886 typval_T *rettv;
5887{
5888 list_T *l = list_alloc();
5889
5890 if (l == NULL)
5891 return FAIL;
5892
5893 rettv->vval.v_list = l;
5894 rettv->v_type = VAR_LIST;
5895 ++l->lv_refcount;
5896 return OK;
5897}
5898
5899/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005900 * Unreference a list: decrement the reference count and free it when it
5901 * becomes zero.
5902 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005903 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005907 if (l != NULL && --l->lv_refcount <= 0)
5908 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909}
5910
5911/*
5912 * Free a list, including all items it points to.
5913 * Ignores the reference count.
5914 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005915 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005916list_free(l, recurse)
5917 list_T *l;
5918 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919{
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005922 /* Remove the list from the list of lists for garbage collection. */
5923 if (l->lv_used_prev == NULL)
5924 first_list = l->lv_used_next;
5925 else
5926 l->lv_used_prev->lv_used_next = l->lv_used_next;
5927 if (l->lv_used_next != NULL)
5928 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5929
Bram Moolenaard9fba312005-06-26 22:34:35 +00005930 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005932 /* Remove the item before deleting it. */
5933 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005934 if (recurse || (item->li_tv.v_type != VAR_LIST
5935 && item->li_tv.v_type != VAR_DICT))
5936 clear_tv(&item->li_tv);
5937 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 }
5939 vim_free(l);
5940}
5941
5942/*
5943 * Allocate a list item.
5944 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005945 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946listitem_alloc()
5947{
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949}
5950
5951/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005952 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005954 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 vim_free(item);
5960}
5961
5962/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005963 * Remove a list item from a List and free it. Also clears the value.
5964 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005965 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005966listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005967 list_T *l;
5968 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005969{
5970 list_remove(l, item, item);
5971 listitem_free(item);
5972}
5973
5974/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 * Get the number of items in a list.
5976 */
5977 static long
5978list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 if (l == NULL)
5982 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005983 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984}
5985
5986/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005987 * Return TRUE when two lists have exactly the same values.
5988 */
5989 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005990list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005991 list_T *l1;
5992 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005993 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005994 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995{
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005998 if (l1 == NULL || l2 == NULL)
5999 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006000 if (l1 == l2)
6001 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006002 if (list_len(l1) != list_len(l2))
6003 return FALSE;
6004
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005 for (item1 = l1->lv_first, item2 = l2->lv_first;
6006 item1 != NULL && item2 != NULL;
6007 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006008 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 return FALSE;
6010 return item1 == NULL && item2 == NULL;
6011}
6012
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006013#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6014 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006015/*
6016 * Return the dictitem that an entry in a hashtable points to.
6017 */
6018 dictitem_T *
6019dict_lookup(hi)
6020 hashitem_T *hi;
6021{
6022 return HI2DI(hi);
6023}
6024#endif
6025
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006027 * Return TRUE when two dictionaries have exactly the same key/values.
6028 */
6029 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006030dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 dict_T *d1;
6032 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006034 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 hashitem_T *hi;
6037 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006040 if (d1 == NULL || d2 == NULL)
6041 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006042 if (d1 == d2)
6043 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044 if (dict_len(d1) != dict_len(d2))
6045 return FALSE;
6046
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006047 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006050 if (!HASHITEM_EMPTY(hi))
6051 {
6052 item2 = dict_find(d2, hi->hi_key, -1);
6053 if (item2 == NULL)
6054 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006056 return FALSE;
6057 --todo;
6058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006059 }
6060 return TRUE;
6061}
6062
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006063static int tv_equal_recurse_limit;
6064
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006066 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006067 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006068 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006069 */
6070 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006071tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 typval_T *tv1;
6073 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006074 int ic; /* ignore case */
6075 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076{
6077 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006078 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006080 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006081
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006082 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006083 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006085 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 * recursiveness to a limit. We guess they are equal then.
6087 * A fixed limit has the problem of still taking an awful long time.
6088 * Reduce the limit every time running into it. That should work fine for
6089 * deeply linked structures that are not recursively linked and catch
6090 * recursiveness quickly. */
6091 if (!recursive)
6092 tv_equal_recurse_limit = 1000;
6093 if (recursive_cnt >= tv_equal_recurse_limit)
6094 {
6095 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006096 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006097 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098
6099 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006101 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 ++recursive_cnt;
6103 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6104 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006105 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006106
6107 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108 ++recursive_cnt;
6109 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6110 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112
6113 case VAR_FUNC:
6114 return (tv1->vval.v_string != NULL
6115 && tv2->vval.v_string != NULL
6116 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6117
6118 case VAR_NUMBER:
6119 return tv1->vval.v_number == tv2->vval.v_number;
6120
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006121#ifdef FEAT_FLOAT
6122 case VAR_FLOAT:
6123 return tv1->vval.v_float == tv2->vval.v_float;
6124#endif
6125
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006126 case VAR_STRING:
6127 s1 = get_tv_string_buf(tv1, buf1);
6128 s2 = get_tv_string_buf(tv2, buf2);
6129 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006131
6132 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 return TRUE;
6134}
6135
6136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 * Locate item with index "n" in list "l" and return it.
6138 * A negative index is counted from the end; -1 is the last item.
6139 * Returns NULL when "n" is out of range.
6140 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006141 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006143 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144 long n;
6145{
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 long idx;
6148
6149 if (l == NULL)
6150 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006151
6152 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006154 n = l->lv_len + n;
6155
6156 /* Check for index out of range. */
6157 if (n < 0 || n >= l->lv_len)
6158 return NULL;
6159
6160 /* When there is a cached index may start search from there. */
6161 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163 if (n < l->lv_idx / 2)
6164 {
6165 /* closest to the start of the list */
6166 item = l->lv_first;
6167 idx = 0;
6168 }
6169 else if (n > (l->lv_idx + l->lv_len) / 2)
6170 {
6171 /* closest to the end of the list */
6172 item = l->lv_last;
6173 idx = l->lv_len - 1;
6174 }
6175 else
6176 {
6177 /* closest to the cached index */
6178 item = l->lv_idx_item;
6179 idx = l->lv_idx;
6180 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 }
6182 else
6183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 if (n < l->lv_len / 2)
6185 {
6186 /* closest to the start of the list */
6187 item = l->lv_first;
6188 idx = 0;
6189 }
6190 else
6191 {
6192 /* closest to the end of the list */
6193 item = l->lv_last;
6194 idx = l->lv_len - 1;
6195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006197
6198 while (n > idx)
6199 {
6200 /* search forward */
6201 item = item->li_next;
6202 ++idx;
6203 }
6204 while (n < idx)
6205 {
6206 /* search backward */
6207 item = item->li_prev;
6208 --idx;
6209 }
6210
6211 /* cache the used index */
6212 l->lv_idx = idx;
6213 l->lv_idx_item = item;
6214
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006215 return item;
6216}
6217
6218/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006219 * Get list item "l[idx]" as a number.
6220 */
6221 static long
6222list_find_nr(l, idx, errorp)
6223 list_T *l;
6224 long idx;
6225 int *errorp; /* set to TRUE when something wrong */
6226{
6227 listitem_T *li;
6228
6229 li = list_find(l, idx);
6230 if (li == NULL)
6231 {
6232 if (errorp != NULL)
6233 *errorp = TRUE;
6234 return -1L;
6235 }
6236 return get_tv_number_chk(&li->li_tv, errorp);
6237}
6238
6239/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006240 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6241 */
6242 char_u *
6243list_find_str(l, idx)
6244 list_T *l;
6245 long idx;
6246{
6247 listitem_T *li;
6248
6249 li = list_find(l, idx - 1);
6250 if (li == NULL)
6251 {
6252 EMSGN(_(e_listidx), idx);
6253 return NULL;
6254 }
6255 return get_tv_string(&li->li_tv);
6256}
6257
6258/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006259 * Locate "item" list "l" and return its index.
6260 * Returns -1 when "item" is not in the list.
6261 */
6262 static long
6263list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 list_T *l;
6265 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006266{
6267 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006269
6270 if (l == NULL)
6271 return -1;
6272 idx = 0;
6273 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6274 ++idx;
6275 if (li == NULL)
6276 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006277 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006278}
6279
6280/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 * Append item "item" to the end of list "l".
6282 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287{
6288 if (l->lv_last == NULL)
6289 {
6290 /* empty list */
6291 l->lv_first = item;
6292 l->lv_last = item;
6293 item->li_prev = NULL;
6294 }
6295 else
6296 {
6297 l->lv_last->li_next = item;
6298 item->li_prev = l->lv_last;
6299 l->lv_last = item;
6300 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 item->li_next = NULL;
6303}
6304
6305/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006309 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006311 list_T *l;
6312 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006314 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315
Bram Moolenaar05159a02005-02-26 23:04:13 +00006316 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006318 copy_tv(tv, &li->li_tv);
6319 list_append(l, li);
6320 return OK;
6321}
6322
6323/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006324 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 * Return FAIL when out of memory.
6326 */
6327 int
6328list_append_dict(list, dict)
6329 list_T *list;
6330 dict_T *dict;
6331{
6332 listitem_T *li = listitem_alloc();
6333
6334 if (li == NULL)
6335 return FAIL;
6336 li->li_tv.v_type = VAR_DICT;
6337 li->li_tv.v_lock = 0;
6338 li->li_tv.vval.v_dict = dict;
6339 list_append(list, li);
6340 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341 return OK;
6342}
6343
6344/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006345 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006346 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006347 * Returns FAIL when out of memory.
6348 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006349 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006350list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006351 list_T *l;
6352 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006353 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006354{
6355 listitem_T *li = listitem_alloc();
6356
6357 if (li == NULL)
6358 return FAIL;
6359 list_append(l, li);
6360 li->li_tv.v_type = VAR_STRING;
6361 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006362 if (str == NULL)
6363 li->li_tv.vval.v_string = NULL;
6364 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006365 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366 return FAIL;
6367 return OK;
6368}
6369
6370/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006371 * Append "n" to list "l".
6372 * Returns FAIL when out of memory.
6373 */
6374 static int
6375list_append_number(l, n)
6376 list_T *l;
6377 varnumber_T n;
6378{
6379 listitem_T *li;
6380
6381 li = listitem_alloc();
6382 if (li == NULL)
6383 return FAIL;
6384 li->li_tv.v_type = VAR_NUMBER;
6385 li->li_tv.v_lock = 0;
6386 li->li_tv.vval.v_number = n;
6387 list_append(l, li);
6388 return OK;
6389}
6390
6391/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006393 * If "item" is NULL append at the end.
6394 * Return FAIL when out of memory.
6395 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006396 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *l;
6399 typval_T *tv;
6400 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401{
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403
6404 if (ni == NULL)
6405 return FAIL;
6406 copy_tv(tv, &ni->li_tv);
6407 if (item == NULL)
6408 /* Append new item at end of list. */
6409 list_append(l, ni);
6410 else
6411 {
6412 /* Insert new item before existing item. */
6413 ni->li_prev = item->li_prev;
6414 ni->li_next = item;
6415 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006416 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006418 ++l->lv_idx;
6419 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006421 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 l->lv_idx_item = NULL;
6424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006426 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 }
6428 return OK;
6429}
6430
6431/*
6432 * Extend "l1" with "l2".
6433 * If "bef" is NULL append at the end, otherwise insert before this item.
6434 * Returns FAIL when out of memory.
6435 */
6436 static int
6437list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 list_T *l1;
6439 list_T *l2;
6440 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441{
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006443 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006445 /* We also quit the loop when we have inserted the original item count of
6446 * the list, avoid a hang when we extend a list with itself. */
6447 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6449 return FAIL;
6450 return OK;
6451}
6452
6453/*
6454 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6455 * Return FAIL when out of memory.
6456 */
6457 static int
6458list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 list_T *l1;
6460 list_T *l2;
6461 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462{
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006465 if (l1 == NULL || l2 == NULL)
6466 return FAIL;
6467
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 if (l == NULL)
6471 return FAIL;
6472 tv->v_type = VAR_LIST;
6473 tv->vval.v_list = l;
6474
6475 /* append all items from the second list */
6476 return list_extend(l, l2, NULL);
6477}
6478
6479/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006480 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006482 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * Returns NULL when out of memory.
6484 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006489 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490{
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 list_T *copy;
6492 listitem_T *item;
6493 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494
6495 if (orig == NULL)
6496 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497
6498 copy = list_alloc();
6499 if (copy != NULL)
6500 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006501 if (copyID != 0)
6502 {
6503 /* Do this before adding the items, because one of the items may
6504 * refer back to this list. */
6505 orig->lv_copyID = copyID;
6506 orig->lv_copylist = copy;
6507 }
6508 for (item = orig->lv_first; item != NULL && !got_int;
6509 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 {
6511 ni = listitem_alloc();
6512 if (ni == NULL)
6513 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006514 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 {
6516 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6517 {
6518 vim_free(ni);
6519 break;
6520 }
6521 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006523 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 list_append(copy, ni);
6525 }
6526 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006527 if (item != NULL)
6528 {
6529 list_unref(copy);
6530 copy = NULL;
6531 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 }
6533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 return copy;
6535}
6536
6537/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006539 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006541 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006542list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 list_T *l;
6544 listitem_T *item;
6545 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546{
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 /* notify watchers */
6550 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006551 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 list_fix_watch(l, ip);
6554 if (ip == item2)
6555 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557
6558 if (item2->li_next == NULL)
6559 l->lv_last = item->li_prev;
6560 else
6561 item2->li_next->li_prev = item->li_prev;
6562 if (item->li_prev == NULL)
6563 l->lv_first = item2->li_next;
6564 else
6565 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006566 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567}
6568
6569/*
6570 * Return an allocated string with the string representation of a list.
6571 * May return NULL.
6572 */
6573 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006574list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006576 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577{
6578 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579
6580 if (tv->vval.v_list == NULL)
6581 return NULL;
6582 ga_init2(&ga, (int)sizeof(char), 80);
6583 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006584 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 {
6586 vim_free(ga.ga_data);
6587 return NULL;
6588 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 ga_append(&ga, ']');
6590 ga_append(&ga, NUL);
6591 return (char_u *)ga.ga_data;
6592}
6593
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006594typedef struct join_S {
6595 char_u *s;
6596 char_u *tofree;
6597} join_T;
6598
6599 static int
6600list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6601 garray_T *gap; /* to store the result in */
6602 list_T *l;
6603 char_u *sep;
6604 int echo_style;
6605 int copyID;
6606 garray_T *join_gap; /* to keep each list item string */
6607{
6608 int i;
6609 join_T *p;
6610 int len;
6611 int sumlen = 0;
6612 int first = TRUE;
6613 char_u *tofree;
6614 char_u numbuf[NUMBUFLEN];
6615 listitem_T *item;
6616 char_u *s;
6617
6618 /* Stringify each item in the list. */
6619 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6620 {
6621 if (echo_style)
6622 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6623 else
6624 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6625 if (s == NULL)
6626 return FAIL;
6627
6628 len = (int)STRLEN(s);
6629 sumlen += len;
6630
6631 ga_grow(join_gap, 1);
6632 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6633 if (tofree != NULL || s != numbuf)
6634 {
6635 p->s = s;
6636 p->tofree = tofree;
6637 }
6638 else
6639 {
6640 p->s = vim_strnsave(s, len);
6641 p->tofree = p->s;
6642 }
6643
6644 line_breakcheck();
6645 }
6646
6647 /* Allocate result buffer with its total size, avoid re-allocation and
6648 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6649 if (join_gap->ga_len >= 2)
6650 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6651 if (ga_grow(gap, sumlen + 2) == FAIL)
6652 return FAIL;
6653
6654 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6655 {
6656 if (first)
6657 first = FALSE;
6658 else
6659 ga_concat(gap, sep);
6660 p = ((join_T *)join_gap->ga_data) + i;
6661
6662 if (p->s != NULL)
6663 ga_concat(gap, p->s);
6664 line_breakcheck();
6665 }
6666
6667 return OK;
6668}
6669
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006671 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006672 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006673 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006674 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006676list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006677 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006678 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006679 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006680 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006681 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006682{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006683 garray_T join_ga;
6684 int retval;
6685 join_T *p;
6686 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006688 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6689 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6690
6691 /* Dispose each item in join_ga. */
6692 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694 p = (join_T *)join_ga.ga_data;
6695 for (i = 0; i < join_ga.ga_len; ++i)
6696 {
6697 vim_free(p->tofree);
6698 ++p;
6699 }
6700 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006702
6703 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704}
6705
6706/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006707 * Garbage collection for lists and dictionaries.
6708 *
6709 * We use reference counts to be able to free most items right away when they
6710 * are no longer used. But for composite items it's possible that it becomes
6711 * unused while the reference count is > 0: When there is a recursive
6712 * reference. Example:
6713 * :let l = [1, 2, 3]
6714 * :let d = {9: l}
6715 * :let l[1] = d
6716 *
6717 * Since this is quite unusual we handle this with garbage collection: every
6718 * once in a while find out which lists and dicts are not referenced from any
6719 * variable.
6720 *
6721 * Here is a good reference text about garbage collection (refers to Python
6722 * but it applies to all reference-counting mechanisms):
6723 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006724 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006725
6726/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006727 * Do garbage collection for lists and dicts.
6728 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006729 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006730 int
6731garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006732{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006733 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006734 buf_T *buf;
6735 win_T *wp;
6736 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006737 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006738 int did_free;
6739 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006740#ifdef FEAT_WINDOWS
6741 tabpage_T *tp;
6742#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006744 /* Only do this once. */
6745 want_garbage_collect = FALSE;
6746 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006747 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006748
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006749 /* We advance by two because we add one for items referenced through
6750 * previous_funccal. */
6751 current_copyID += COPYID_INC;
6752 copyID = current_copyID;
6753
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006754 /*
6755 * 1. Go through all accessible variables and mark all lists and dicts
6756 * with copyID.
6757 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006758
6759 /* Don't free variables in the previous_funccal list unless they are only
6760 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006761 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006762 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6763 {
6764 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6765 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6766 }
6767
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006768 /* script-local variables */
6769 for (i = 1; i <= ga_scripts.ga_len; ++i)
6770 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6771
6772 /* buffer-local variables */
6773 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006774 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775
6776 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006777 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006778 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006779#ifdef FEAT_AUTOCMD
6780 if (aucmd_win != NULL)
6781 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6782#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006784#ifdef FEAT_WINDOWS
6785 /* tabpage-local variables */
6786 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006787 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006788#endif
6789
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006790 /* global variables */
6791 set_ref_in_ht(&globvarht, copyID);
6792
6793 /* function-local variables */
6794 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6795 {
6796 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6797 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6798 }
6799
Bram Moolenaard812df62008-11-09 12:46:09 +00006800 /* v: vars */
6801 set_ref_in_ht(&vimvarht, copyID);
6802
Bram Moolenaar1dced572012-04-05 16:54:08 +02006803#ifdef FEAT_LUA
6804 set_ref_in_lua(copyID);
6805#endif
6806
Bram Moolenaardb913952012-06-29 12:54:53 +02006807#ifdef FEAT_PYTHON
6808 set_ref_in_python(copyID);
6809#endif
6810
6811#ifdef FEAT_PYTHON3
6812 set_ref_in_python3(copyID);
6813#endif
6814
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006815 /*
6816 * 2. Free lists and dictionaries that are not referenced.
6817 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006818 did_free = free_unref_items(copyID);
6819
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006820 /*
6821 * 3. Check if any funccal can be freed now.
6822 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006823 for (pfc = &previous_funccal; *pfc != NULL; )
6824 {
6825 if (can_free_funccal(*pfc, copyID))
6826 {
6827 fc = *pfc;
6828 *pfc = fc->caller;
6829 free_funccal(fc, TRUE);
6830 did_free = TRUE;
6831 did_free_funccal = TRUE;
6832 }
6833 else
6834 pfc = &(*pfc)->caller;
6835 }
6836 if (did_free_funccal)
6837 /* When a funccal was freed some more items might be garbage
6838 * collected, so run again. */
6839 (void)garbage_collect();
6840
6841 return did_free;
6842}
6843
6844/*
6845 * Free lists and dictionaries that are no longer referenced.
6846 */
6847 static int
6848free_unref_items(copyID)
6849 int copyID;
6850{
6851 dict_T *dd;
6852 list_T *ll;
6853 int did_free = FALSE;
6854
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006856 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 */
6858 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006861 /* Free the Dictionary and ordinary items it contains, but don't
6862 * recurse into Lists and Dictionaries, they will be in the list
6863 * of dicts or list of lists. */
6864 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 did_free = TRUE;
6866
6867 /* restart, next dict may also have been freed */
6868 dd = first_dict;
6869 }
6870 else
6871 dd = dd->dv_used_next;
6872
6873 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 * Go through the list of lists and free items without the copyID.
6875 * But don't free a list that has a watcher (used in a for loop), these
6876 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 */
6878 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6880 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006882 /* Free the List and ordinary items it contains, but don't recurse
6883 * into Lists and Dictionaries, they will be in the list of dicts
6884 * or list of lists. */
6885 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 did_free = TRUE;
6887
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006888 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 ll = first_list;
6890 }
6891 else
6892 ll = ll->lv_used_next;
6893
6894 return did_free;
6895}
6896
6897/*
6898 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6899 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006900 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901set_ref_in_ht(ht, copyID)
6902 hashtab_T *ht;
6903 int copyID;
6904{
6905 int todo;
6906 hashitem_T *hi;
6907
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006908 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 for (hi = ht->ht_array; todo > 0; ++hi)
6910 if (!HASHITEM_EMPTY(hi))
6911 {
6912 --todo;
6913 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6914 }
6915}
6916
6917/*
6918 * Mark all lists and dicts referenced through list "l" with "copyID".
6919 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006920 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921set_ref_in_list(l, copyID)
6922 list_T *l;
6923 int copyID;
6924{
6925 listitem_T *li;
6926
6927 for (li = l->lv_first; li != NULL; li = li->li_next)
6928 set_ref_in_item(&li->li_tv, copyID);
6929}
6930
6931/*
6932 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6933 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006934 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935set_ref_in_item(tv, copyID)
6936 typval_T *tv;
6937 int copyID;
6938{
6939 dict_T *dd;
6940 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006941
6942 switch (tv->v_type)
6943 {
6944 case VAR_DICT:
6945 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006946 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006947 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006948 /* Didn't see this dict yet. */
6949 dd->dv_copyID = copyID;
6950 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953
6954 case VAR_LIST:
6955 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006956 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006957 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 /* Didn't see this list yet. */
6959 ll->lv_copyID = copyID;
6960 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965}
6966
6967/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006968 * Allocate an empty header for a dictionary.
6969 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006970 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006971dict_alloc()
6972{
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974
Bram Moolenaar33570922005-01-25 22:26:29 +00006975 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006976 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006977 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006978 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 if (first_dict != NULL)
6980 first_dict->dv_used_prev = d;
6981 d->dv_used_next = first_dict;
6982 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006983 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006987 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006989 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006990 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006991 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006992}
6993
6994/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006995 * Allocate an empty dict for a return value.
6996 * Returns OK or FAIL.
6997 */
6998 static int
6999rettv_dict_alloc(rettv)
7000 typval_T *rettv;
7001{
7002 dict_T *d = dict_alloc();
7003
7004 if (d == NULL)
7005 return FAIL;
7006
7007 rettv->vval.v_dict = d;
7008 rettv->v_type = VAR_DICT;
7009 ++d->dv_refcount;
7010 return OK;
7011}
7012
7013
7014/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015 * Unreference a Dictionary: decrement the reference count and free it when it
7016 * becomes zero.
7017 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007018 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007020 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007022 if (d != NULL && --d->dv_refcount <= 0)
7023 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024}
7025
7026/*
7027 * Free a Dictionary, including all items it contains.
7028 * Ignores the reference count.
7029 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007030 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007031dict_free(d, recurse)
7032 dict_T *d;
7033 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007035 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007037 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 /* Remove the dict from the list of dicts for garbage collection. */
7040 if (d->dv_used_prev == NULL)
7041 first_dict = d->dv_used_next;
7042 else
7043 d->dv_used_prev->dv_used_next = d->dv_used_next;
7044 if (d->dv_used_next != NULL)
7045 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7046
7047 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007048 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007049 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007052 if (!HASHITEM_EMPTY(hi))
7053 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007054 /* Remove the item before deleting it, just in case there is
7055 * something recursive causing trouble. */
7056 di = HI2DI(hi);
7057 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007058 if (recurse || (di->di_tv.v_type != VAR_LIST
7059 && di->di_tv.v_type != VAR_DICT))
7060 clear_tv(&di->di_tv);
7061 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 --todo;
7063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007065 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066 vim_free(d);
7067}
7068
7069/*
7070 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 * The "key" is copied to the new item.
7072 * Note that the value of the item "di_tv" still needs to be initialized!
7073 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007075 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007076dictitem_alloc(key)
7077 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078{
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007081 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007083 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007084 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007085 di->di_flags = 0;
7086 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007087 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088}
7089
7090/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007091 * Make a copy of a Dictionary item.
7092 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096{
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007098
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007099 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7100 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007101 if (di != NULL)
7102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007103 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007104 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105 copy_tv(&org->di_tv, &di->di_tv);
7106 }
7107 return di;
7108}
7109
7110/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007111 * Remove item "item" from Dictionary "dict" and free it.
7112 */
7113 static void
7114dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 dict_T *dict;
7116 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117{
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 if (HASHITEM_EMPTY(hi))
7122 EMSG2(_(e_intern2), "dictitem_remove()");
7123 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125 dictitem_free(item);
7126}
7127
7128/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007129 * Free a dict item. Also clears the value.
7130 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007131 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135 clear_tv(&item->di_tv);
7136 vim_free(item);
7137}
7138
7139/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7141 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007142 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 * Returns NULL when out of memory.
7144 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007146dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007149 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150{
Bram Moolenaar33570922005-01-25 22:26:29 +00007151 dict_T *copy;
7152 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007153 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155
7156 if (orig == NULL)
7157 return NULL;
7158
7159 copy = dict_alloc();
7160 if (copy != NULL)
7161 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007162 if (copyID != 0)
7163 {
7164 orig->dv_copyID = copyID;
7165 orig->dv_copydict = copy;
7166 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007167 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007168 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007172 --todo;
7173
7174 di = dictitem_alloc(hi->hi_key);
7175 if (di == NULL)
7176 break;
7177 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 {
7179 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7180 copyID) == FAIL)
7181 {
7182 vim_free(di);
7183 break;
7184 }
7185 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 else
7187 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7188 if (dict_add(copy, di) == FAIL)
7189 {
7190 dictitem_free(di);
7191 break;
7192 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007193 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195
Bram Moolenaare9a41262005-01-15 22:18:47 +00007196 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 if (todo > 0)
7198 {
7199 dict_unref(copy);
7200 copy = NULL;
7201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 }
7203
7204 return copy;
7205}
7206
7207/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007209 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007211 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 dict_T *d;
7214 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215{
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217}
7218
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007220 * Add a number or string entry to dictionary "d".
7221 * When "str" is NULL use number "nr", otherwise use "str".
7222 * Returns FAIL when out of memory and when key already exists.
7223 */
7224 int
7225dict_add_nr_str(d, key, nr, str)
7226 dict_T *d;
7227 char *key;
7228 long nr;
7229 char_u *str;
7230{
7231 dictitem_T *item;
7232
7233 item = dictitem_alloc((char_u *)key);
7234 if (item == NULL)
7235 return FAIL;
7236 item->di_tv.v_lock = 0;
7237 if (str == NULL)
7238 {
7239 item->di_tv.v_type = VAR_NUMBER;
7240 item->di_tv.vval.v_number = nr;
7241 }
7242 else
7243 {
7244 item->di_tv.v_type = VAR_STRING;
7245 item->di_tv.vval.v_string = vim_strsave(str);
7246 }
7247 if (dict_add(d, item) == FAIL)
7248 {
7249 dictitem_free(item);
7250 return FAIL;
7251 }
7252 return OK;
7253}
7254
7255/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007256 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007257 * Returns FAIL when out of memory and when key already exists.
7258 */
7259 int
7260dict_add_list(d, key, list)
7261 dict_T *d;
7262 char *key;
7263 list_T *list;
7264{
7265 dictitem_T *item;
7266
7267 item = dictitem_alloc((char_u *)key);
7268 if (item == NULL)
7269 return FAIL;
7270 item->di_tv.v_lock = 0;
7271 item->di_tv.v_type = VAR_LIST;
7272 item->di_tv.vval.v_list = list;
7273 if (dict_add(d, item) == FAIL)
7274 {
7275 dictitem_free(item);
7276 return FAIL;
7277 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007278 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007279 return OK;
7280}
7281
7282/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283 * Get the number of items in a Dictionary.
7284 */
7285 static long
7286dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007288{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289 if (d == NULL)
7290 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007291 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292}
7293
7294/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295 * Find item "key[len]" in Dictionary "d".
7296 * If "len" is negative use strlen(key).
7297 * Returns NULL when not found.
7298 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007299 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 char_u *key;
7303 int len;
7304{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305#define AKEYLEN 200
7306 char_u buf[AKEYLEN];
7307 char_u *akey;
7308 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 if (len < 0)
7312 akey = key;
7313 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007314 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 tofree = akey = vim_strnsave(key, len);
7316 if (akey == NULL)
7317 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007318 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 else
7320 {
7321 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007322 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 akey = buf;
7324 }
7325
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 vim_free(tofree);
7328 if (HASHITEM_EMPTY(hi))
7329 return NULL;
7330 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331}
7332
7333/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007334 * Get a string item from a dictionary.
7335 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007336 * Returns NULL if the entry doesn't exist or out of memory.
7337 */
7338 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007339get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007340 dict_T *d;
7341 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007342 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007343{
7344 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007345 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007346
7347 di = dict_find(d, key, -1);
7348 if (di == NULL)
7349 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007350 s = get_tv_string(&di->di_tv);
7351 if (save && s != NULL)
7352 s = vim_strsave(s);
7353 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007354}
7355
7356/*
7357 * Get a number item from a dictionary.
7358 * Returns 0 if the entry doesn't exist or out of memory.
7359 */
7360 long
7361get_dict_number(d, key)
7362 dict_T *d;
7363 char_u *key;
7364{
7365 dictitem_T *di;
7366
7367 di = dict_find(d, key, -1);
7368 if (di == NULL)
7369 return 0;
7370 return get_tv_number(&di->di_tv);
7371}
7372
7373/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007374 * Return an allocated string with the string representation of a Dictionary.
7375 * May return NULL.
7376 */
7377 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007378dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007380 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007381{
7382 garray_T ga;
7383 int first = TRUE;
7384 char_u *tofree;
7385 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392 return NULL;
7393 ga_init2(&ga, (int)sizeof(char), 80);
7394 ga_append(&ga, '{');
7395
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007396 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 --todo;
7402
7403 if (first)
7404 first = FALSE;
7405 else
7406 ga_concat(&ga, (char_u *)", ");
7407
7408 tofree = string_quote(hi->hi_key, FALSE);
7409 if (tofree != NULL)
7410 {
7411 ga_concat(&ga, tofree);
7412 vim_free(tofree);
7413 }
7414 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007415 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007416 if (s != NULL)
7417 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007419 if (s == NULL)
7420 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007423 if (todo > 0)
7424 {
7425 vim_free(ga.ga_data);
7426 return NULL;
7427 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428
7429 ga_append(&ga, '}');
7430 ga_append(&ga, NUL);
7431 return (char_u *)ga.ga_data;
7432}
7433
7434/*
7435 * Allocate a variable for a Dictionary and fill it from "*arg".
7436 * Return OK or FAIL. Returns NOTDONE for {expr}.
7437 */
7438 static int
7439get_dict_tv(arg, rettv, evaluate)
7440 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 int evaluate;
7443{
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 dict_T *d = NULL;
7445 typval_T tvkey;
7446 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007447 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451
7452 /*
7453 * First check if it's not a curly-braces thing: {expr}.
7454 * Must do this without evaluating, otherwise a function may be called
7455 * twice. Unfortunately this means we need to call eval1() twice for the
7456 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 if (*start != '}')
7460 {
7461 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7462 return FAIL;
7463 if (*start == '}')
7464 return NOTDONE;
7465 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007466
7467 if (evaluate)
7468 {
7469 d = dict_alloc();
7470 if (d == NULL)
7471 return FAIL;
7472 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007473 tvkey.v_type = VAR_UNKNOWN;
7474 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475
7476 *arg = skipwhite(*arg + 1);
7477 while (**arg != '}' && **arg != NUL)
7478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 goto failret;
7481 if (**arg != ':')
7482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007483 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007484 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 goto failret;
7486 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007487 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007489 key = get_tv_string_buf_chk(&tvkey, buf);
7490 if (key == NULL || *key == NUL)
7491 {
7492 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7493 if (key != NULL)
7494 EMSG(_(e_emptykey));
7495 clear_tv(&tvkey);
7496 goto failret;
7497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499
7500 *arg = skipwhite(*arg + 1);
7501 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7502 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007503 if (evaluate)
7504 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 goto failret;
7506 }
7507 if (evaluate)
7508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 if (item != NULL)
7511 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007512 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 clear_tv(&tv);
7515 goto failret;
7516 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 item = dictitem_alloc(key);
7518 clear_tv(&tvkey);
7519 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007522 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 if (dict_add(d, item) == FAIL)
7524 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 }
7526 }
7527
7528 if (**arg == '}')
7529 break;
7530 if (**arg != ',')
7531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007532 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 goto failret;
7534 }
7535 *arg = skipwhite(*arg + 1);
7536 }
7537
7538 if (**arg != '}')
7539 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007540 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541failret:
7542 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007543 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 return FAIL;
7545 }
7546
7547 *arg = skipwhite(*arg + 1);
7548 if (evaluate)
7549 {
7550 rettv->v_type = VAR_DICT;
7551 rettv->vval.v_dict = d;
7552 ++d->dv_refcount;
7553 }
7554
7555 return OK;
7556}
7557
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007559 * Return a string with the string representation of a variable.
7560 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007561 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007562 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007563 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007564 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 */
7566 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007570 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007571 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007572{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573 static int recurse = 0;
7574 char_u *r = NULL;
7575
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007577 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007578 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 *tofree = NULL;
7580 return NULL;
7581 }
7582 ++recurse;
7583
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007584 switch (tv->v_type)
7585 {
7586 case VAR_FUNC:
7587 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588 r = tv->vval.v_string;
7589 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007591 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592 if (tv->vval.v_list == NULL)
7593 {
7594 *tofree = NULL;
7595 r = NULL;
7596 }
7597 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7598 {
7599 *tofree = NULL;
7600 r = (char_u *)"[...]";
7601 }
7602 else
7603 {
7604 tv->vval.v_list->lv_copyID = copyID;
7605 *tofree = list2string(tv, copyID);
7606 r = *tofree;
7607 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007608 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611 if (tv->vval.v_dict == NULL)
7612 {
7613 *tofree = NULL;
7614 r = NULL;
7615 }
7616 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7617 {
7618 *tofree = NULL;
7619 r = (char_u *)"{...}";
7620 }
7621 else
7622 {
7623 tv->vval.v_dict->dv_copyID = copyID;
7624 *tofree = dict2string(tv, copyID);
7625 r = *tofree;
7626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007627 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629 case VAR_STRING:
7630 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007631 *tofree = NULL;
7632 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007634
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007635#ifdef FEAT_FLOAT
7636 case VAR_FLOAT:
7637 *tofree = NULL;
7638 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7639 r = numbuf;
7640 break;
7641#endif
7642
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007644 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007646 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647
7648 --recurse;
7649 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007650}
7651
7652/*
7653 * Return a string with the string representation of a variable.
7654 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7655 * "numbuf" is used for a number.
7656 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007657 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 */
7659 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007660tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007662 char_u **tofree;
7663 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007665{
7666 switch (tv->v_type)
7667 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007668 case VAR_FUNC:
7669 *tofree = string_quote(tv->vval.v_string, TRUE);
7670 return *tofree;
7671 case VAR_STRING:
7672 *tofree = string_quote(tv->vval.v_string, FALSE);
7673 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007674#ifdef FEAT_FLOAT
7675 case VAR_FLOAT:
7676 *tofree = NULL;
7677 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7678 return numbuf;
7679#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007680 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007681 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007683 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007687 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007688}
7689
7690/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 * Return string "str" in ' quotes, doubling ' characters.
7692 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007694 */
7695 static char_u *
7696string_quote(str, function)
7697 char_u *str;
7698 int function;
7699{
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701 char_u *p, *r, *s;
7702
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 len = (function ? 13 : 3);
7704 if (str != NULL)
7705 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007706 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 for (p = str; *p != NUL; mb_ptr_adv(p))
7708 if (*p == '\'')
7709 ++len;
7710 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 s = r = alloc(len);
7712 if (r != NULL)
7713 {
7714 if (function)
7715 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 r += 10;
7718 }
7719 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 if (str != NULL)
7722 for (p = str; *p != NUL; )
7723 {
7724 if (*p == '\'')
7725 *r++ = '\'';
7726 MB_COPY_CHAR(p, r);
7727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 if (function)
7730 *r++ = ')';
7731 *r++ = NUL;
7732 }
7733 return s;
7734}
7735
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007736#ifdef FEAT_FLOAT
7737/*
7738 * Convert the string "text" to a floating point number.
7739 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7740 * this always uses a decimal point.
7741 * Returns the length of the text that was consumed.
7742 */
7743 static int
7744string2float(text, value)
7745 char_u *text;
7746 float_T *value; /* result stored here */
7747{
7748 char *s = (char *)text;
7749 float_T f;
7750
7751 f = strtod(s, &s);
7752 *value = f;
7753 return (int)((char_u *)s - text);
7754}
7755#endif
7756
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 * Get the value of an environment variable.
7759 * "arg" is pointing to the '$'. It is advanced to after the name.
7760 * If the environment variable was not set, silently assume it is empty.
7761 * Always return OK.
7762 */
7763 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 int evaluate;
7768{
7769 char_u *string = NULL;
7770 int len;
7771 int cc;
7772 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007773 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 ++*arg;
7776 name = *arg;
7777 len = get_env_len(arg);
7778 if (evaluate)
7779 {
7780 if (len != 0)
7781 {
7782 cc = name[len];
7783 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007784 /* first try vim_getenv(), fast for normal environment vars */
7785 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007787 {
7788 if (!mustfree)
7789 string = vim_strsave(string);
7790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 else
7792 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007793 if (mustfree)
7794 vim_free(string);
7795
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 /* next try expanding things like $VIM and ${HOME} */
7797 string = expand_env_save(name - 1);
7798 if (string != NULL && *string == '$')
7799 {
7800 vim_free(string);
7801 string = NULL;
7802 }
7803 }
7804 name[len] = cc;
7805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806 rettv->v_type = VAR_STRING;
7807 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809
7810 return OK;
7811}
7812
7813/*
7814 * Array with names and number of arguments of all internal functions
7815 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7816 */
7817static struct fst
7818{
7819 char *f_name; /* function name */
7820 char f_min_argc; /* minimal number of arguments */
7821 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007822 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007823 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824} functions[] =
7825{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007826#ifdef FEAT_FLOAT
7827 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007828 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007829#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007830 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007831 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {"append", 2, 2, f_append},
7833 {"argc", 0, 0, f_argc},
7834 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007835 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007839 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007842 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"bufexists", 1, 1, f_bufexists},
7844 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7845 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7846 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7847 {"buflisted", 1, 1, f_buflisted},
7848 {"bufloaded", 1, 1, f_bufloaded},
7849 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007850 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {"bufwinnr", 1, 1, f_bufwinnr},
7852 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007853 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007855#ifdef FEAT_FLOAT
7856 {"ceil", 1, 1, f_ceil},
7857#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007858 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007859 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007861 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007863#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007864 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007865 {"complete_add", 1, 1, f_complete_add},
7866 {"complete_check", 0, 0, f_complete_check},
7867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#ifdef FEAT_FLOAT
7871 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007872 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007874 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007876 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007877 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"delete", 1, 1, f_delete},
7879 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007880 {"diff_filler", 1, 1, f_diff_filler},
7881 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007882 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"eventhandler", 0, 0, f_eventhandler},
7886 {"executable", 1, 1, f_executable},
7887 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007888#ifdef FEAT_FLOAT
7889 {"exp", 1, 1, f_exp},
7890#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007891 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007892 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007893 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7895 {"filereadable", 1, 1, f_filereadable},
7896 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007898 {"finddir", 1, 3, f_finddir},
7899 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007900#ifdef FEAT_FLOAT
7901 {"float2nr", 1, 1, f_float2nr},
7902 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007903 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007905 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"fnamemodify", 2, 2, f_fnamemodify},
7907 {"foldclosed", 1, 1, f_foldclosed},
7908 {"foldclosedend", 1, 1, f_foldclosedend},
7909 {"foldlevel", 1, 1, f_foldlevel},
7910 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007911 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007914 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007915 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007916 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007917 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"getchar", 0, 1, f_getchar},
7919 {"getcharmod", 0, 0, f_getcharmod},
7920 {"getcmdline", 0, 0, f_getcmdline},
7921 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007922 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007924 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007925 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"getfsize", 1, 1, f_getfsize},
7927 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007928 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007929 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007930 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007931 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007932 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007933 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007934 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007935 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007937 {"gettabvar", 2, 3, f_gettabvar},
7938 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"getwinposx", 0, 0, f_getwinposx},
7940 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007941 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007942 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007943 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007945 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007946 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007947 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7949 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7950 {"histadd", 2, 2, f_histadd},
7951 {"histdel", 1, 2, f_histdel},
7952 {"histget", 1, 2, f_histget},
7953 {"histnr", 1, 1, f_histnr},
7954 {"hlID", 1, 1, f_hlID},
7955 {"hlexists", 1, 1, f_hlexists},
7956 {"hostname", 0, 0, f_hostname},
7957 {"iconv", 3, 3, f_iconv},
7958 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007960 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007962 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"inputrestore", 0, 0, f_inputrestore},
7964 {"inputsave", 0, 0, f_inputsave},
7965 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007966 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007967 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007969 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007974 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"libcall", 3, 3, f_libcall},
7976 {"libcallnr", 3, 3, f_libcallnr},
7977 {"line", 1, 1, f_line},
7978 {"line2byte", 1, 1, f_line2byte},
7979 {"lispindent", 1, 1, f_lispindent},
7980 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007981#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007982 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007983 {"log10", 1, 1, f_log10},
7984#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007985#ifdef FEAT_LUA
7986 {"luaeval", 1, 2, f_luaeval},
7987#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007988 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007989 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007990 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007991 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007992 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007993 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007994 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007995 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007996 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007997 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007998 {"max", 1, 1, f_max},
7999 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008000#ifdef vim_mkdir
8001 {"mkdir", 1, 3, f_mkdir},
8002#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008003 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008004#ifdef FEAT_MZSCHEME
8005 {"mzeval", 1, 1, f_mzeval},
8006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008008 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008009 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008010 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008011#ifdef FEAT_FLOAT
8012 {"pow", 2, 2, f_pow},
8013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008015 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008016 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008017#ifdef FEAT_PYTHON3
8018 {"py3eval", 1, 1, f_py3eval},
8019#endif
8020#ifdef FEAT_PYTHON
8021 {"pyeval", 1, 1, f_pyeval},
8022#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008024 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008025 {"reltime", 0, 2, f_reltime},
8026 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 {"remote_expr", 2, 3, f_remote_expr},
8028 {"remote_foreground", 1, 1, f_remote_foreground},
8029 {"remote_peek", 1, 2, f_remote_peek},
8030 {"remote_read", 1, 1, f_remote_read},
8031 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008032 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008034 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008036 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008037#ifdef FEAT_FLOAT
8038 {"round", 1, 1, f_round},
8039#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008040 {"screencol", 0, 0, f_screencol},
8041 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008042 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008043 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008044 {"searchpair", 3, 7, f_searchpair},
8045 {"searchpairpos", 3, 7, f_searchpairpos},
8046 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {"server2client", 2, 2, f_server2client},
8048 {"serverlist", 0, 0, f_serverlist},
8049 {"setbufvar", 3, 3, f_setbufvar},
8050 {"setcmdpos", 1, 1, f_setcmdpos},
8051 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008052 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008053 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008054 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008055 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008057 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008058 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008060#ifdef FEAT_CRYPT
8061 {"sha256", 1, 1, f_sha256},
8062#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008063 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008064 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066#ifdef FEAT_FLOAT
8067 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008068 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008069#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008070 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008071 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008072 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008073 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008074 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"sqrt", 1, 1, f_sqrt},
8077 {"str2float", 1, 1, f_str2float},
8078#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008079 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008080 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008081 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082#ifdef HAVE_STRFTIME
8083 {"strftime", 1, 2, f_strftime},
8084#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008086 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"strlen", 1, 1, f_strlen},
8088 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008089 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008091 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"submatch", 1, 1, f_submatch},
8093 {"substitute", 4, 4, f_substitute},
8094 {"synID", 3, 3, f_synID},
8095 {"synIDattr", 2, 3, f_synIDattr},
8096 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008097 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008098 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008099 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008100 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008101 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008102 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008103 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008104 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105#ifdef FEAT_FLOAT
8106 {"tan", 1, 1, f_tan},
8107 {"tanh", 1, 1, f_tanh},
8108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008110 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"tolower", 1, 1, f_tolower},
8112 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008113 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#ifdef FEAT_FLOAT
8115 {"trunc", 1, 1, f_trunc},
8116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008118 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008119 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008120 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"virtcol", 1, 1, f_virtcol},
8122 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008123 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"winbufnr", 1, 1, f_winbufnr},
8125 {"wincol", 0, 0, f_wincol},
8126 {"winheight", 1, 1, f_winheight},
8127 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008128 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008130 {"winrestview", 1, 1, f_winrestview},
8131 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008133 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008134 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135};
8136
8137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8138
8139/*
8140 * Function given to ExpandGeneric() to obtain the list of internal
8141 * or user defined function names.
8142 */
8143 char_u *
8144get_function_name(xp, idx)
8145 expand_T *xp;
8146 int idx;
8147{
8148 static int intidx = -1;
8149 char_u *name;
8150
8151 if (idx == 0)
8152 intidx = -1;
8153 if (intidx < 0)
8154 {
8155 name = get_user_func_name(xp, idx);
8156 if (name != NULL)
8157 return name;
8158 }
8159 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8160 {
8161 STRCPY(IObuff, functions[intidx].f_name);
8162 STRCAT(IObuff, "(");
8163 if (functions[intidx].f_max_argc == 0)
8164 STRCAT(IObuff, ")");
8165 return IObuff;
8166 }
8167
8168 return NULL;
8169}
8170
8171/*
8172 * Function given to ExpandGeneric() to obtain the list of internal or
8173 * user defined variable or function names.
8174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 char_u *
8176get_expr_name(xp, idx)
8177 expand_T *xp;
8178 int idx;
8179{
8180 static int intidx = -1;
8181 char_u *name;
8182
8183 if (idx == 0)
8184 intidx = -1;
8185 if (intidx < 0)
8186 {
8187 name = get_function_name(xp, idx);
8188 if (name != NULL)
8189 return name;
8190 }
8191 return get_user_var_name(xp, ++intidx);
8192}
8193
8194#endif /* FEAT_CMDL_COMPL */
8195
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008196#if defined(EBCDIC) || defined(PROTO)
8197/*
8198 * Compare struct fst by function name.
8199 */
8200 static int
8201compare_func_name(s1, s2)
8202 const void *s1;
8203 const void *s2;
8204{
8205 struct fst *p1 = (struct fst *)s1;
8206 struct fst *p2 = (struct fst *)s2;
8207
8208 return STRCMP(p1->f_name, p2->f_name);
8209}
8210
8211/*
8212 * Sort the function table by function name.
8213 * The sorting of the table above is ASCII dependant.
8214 * On machines using EBCDIC we have to sort it.
8215 */
8216 static void
8217sortFunctions()
8218{
8219 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8220
8221 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8222}
8223#endif
8224
8225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226/*
8227 * Find internal function in table above.
8228 * Return index, or -1 if not found
8229 */
8230 static int
8231find_internal_func(name)
8232 char_u *name; /* name of the function */
8233{
8234 int first = 0;
8235 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8236 int cmp;
8237 int x;
8238
8239 /*
8240 * Find the function name in the table. Binary search.
8241 */
8242 while (first <= last)
8243 {
8244 x = first + ((unsigned)(last - first) >> 1);
8245 cmp = STRCMP(name, functions[x].f_name);
8246 if (cmp < 0)
8247 last = x - 1;
8248 else if (cmp > 0)
8249 first = x + 1;
8250 else
8251 return x;
8252 }
8253 return -1;
8254}
8255
8256/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008257 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8258 * name it contains, otherwise return "name".
8259 */
8260 static char_u *
8261deref_func_name(name, lenp)
8262 char_u *name;
8263 int *lenp;
8264{
Bram Moolenaar33570922005-01-25 22:26:29 +00008265 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008266 int cc;
8267
8268 cc = name[*lenp];
8269 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008270 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008273 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 {
8276 *lenp = 0;
8277 return (char_u *)""; /* just in case */
8278 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008279 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008280 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 }
8282
8283 return name;
8284}
8285
8286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 * Allocate a variable for the result of a function.
8288 * Return OK or FAIL.
8289 */
8290 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008291get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8292 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 char_u *name; /* name of the function */
8294 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 char_u **arg; /* argument, pointing to the '(' */
8297 linenr_T firstline; /* first line of range */
8298 linenr_T lastline; /* last line of range */
8299 int *doesrange; /* return: function handled range */
8300 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 char_u *argp;
8304 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008305 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 int argcount = 0; /* number of arguments found */
8307
8308 /*
8309 * Get the arguments.
8310 */
8311 argp = *arg;
8312 while (argcount < MAX_FUNC_ARGS)
8313 {
8314 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8315 if (*argp == ')' || *argp == ',' || *argp == NUL)
8316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8318 {
8319 ret = FAIL;
8320 break;
8321 }
8322 ++argcount;
8323 if (*argp != ',')
8324 break;
8325 }
8326 if (*argp == ')')
8327 ++argp;
8328 else
8329 ret = FAIL;
8330
8331 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008332 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 {
8336 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008337 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008339 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
8345 *arg = skipwhite(argp);
8346 return ret;
8347}
8348
8349
8350/*
8351 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008352 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008353 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 */
8355 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008356call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008357 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008358 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008362 typval_T *argvars; /* vars for arguments, must have "argcount"
8363 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 linenr_T firstline; /* first line of range */
8365 linenr_T lastline; /* last line of range */
8366 int *doesrange; /* return: function handled range */
8367 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371#define ERROR_UNKNOWN 0
8372#define ERROR_TOOMANY 1
8373#define ERROR_TOOFEW 2
8374#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008375#define ERROR_DICT 4
8376#define ERROR_NONE 5
8377#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 int error = ERROR_NONE;
8379 int i;
8380 int llen;
8381 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#define FLEN_FIXED 40
8383 char_u fname_buf[FLEN_FIXED + 1];
8384 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008385 char_u *name;
8386
8387 /* Make a copy of the name, if it comes from a funcref variable it could
8388 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008389 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008390 if (name == NULL)
8391 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392
8393 /*
8394 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8395 * Change <SNR>123_name() to K_SNR 123_name().
8396 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 llen = eval_fname_script(name);
8399 if (llen > 0)
8400 {
8401 fname_buf[0] = K_SPECIAL;
8402 fname_buf[1] = KS_EXTRA;
8403 fname_buf[2] = (int)KE_SNR;
8404 i = 3;
8405 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8406 {
8407 if (current_SID <= 0)
8408 error = ERROR_SCRIPT;
8409 else
8410 {
8411 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8412 i = (int)STRLEN(fname_buf);
8413 }
8414 }
8415 if (i + STRLEN(name + llen) < FLEN_FIXED)
8416 {
8417 STRCPY(fname_buf + i, name + llen);
8418 fname = fname_buf;
8419 }
8420 else
8421 {
8422 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8423 if (fname == NULL)
8424 error = ERROR_OTHER;
8425 else
8426 {
8427 mch_memmove(fname, fname_buf, (size_t)i);
8428 STRCPY(fname + i, name + llen);
8429 }
8430 }
8431 }
8432 else
8433 fname = name;
8434
8435 *doesrange = FALSE;
8436
8437
8438 /* execute the function if no errors detected and executing */
8439 if (evaluate && error == ERROR_NONE)
8440 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008441 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8442 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 error = ERROR_UNKNOWN;
8444
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008445 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
8447 /*
8448 * User defined function.
8449 */
8450 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008453 /* Trigger FuncUndefined event, may load the function. */
8454 if (fp == NULL
8455 && apply_autocmds(EVENT_FUNCUNDEFINED,
8456 fname, fname, TRUE, NULL)
8457 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008459 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 fp = find_func(fname);
8461 }
8462#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008464 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 {
8466 /* loaded a package, search for the function again */
8467 fp = find_func(fname);
8468 }
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (fp != NULL)
8471 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008472 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008474 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008478 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008479 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 else
8481 {
8482 /*
8483 * Call the user function.
8484 * Save and restore search patterns, script variables and
8485 * redo buffer.
8486 */
8487 save_search_patterns();
8488 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008492 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8493 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8494 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008495 /* Function was unreferenced while being used, free it
8496 * now. */
8497 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 restoreRedobuff();
8499 restore_search_patterns();
8500 error = ERROR_NONE;
8501 }
8502 }
8503 }
8504 else
8505 {
8506 /*
8507 * Find the function name in the table, call its implementation.
8508 */
8509 i = find_internal_func(fname);
8510 if (i >= 0)
8511 {
8512 if (argcount < functions[i].f_min_argc)
8513 error = ERROR_TOOFEW;
8514 else if (argcount > functions[i].f_max_argc)
8515 error = ERROR_TOOMANY;
8516 else
8517 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 error = ERROR_NONE;
8521 }
8522 }
8523 }
8524 /*
8525 * The function call (or "FuncUndefined" autocommand sequence) might
8526 * have been aborted by an error, an interrupt, or an explicitly thrown
8527 * exception that has not been caught so far. This situation can be
8528 * tested for by calling aborting(). For an error in an internal
8529 * function or for the "E132" error in call_user_func(), however, the
8530 * throw point at which the "force_abort" flag (temporarily reset by
8531 * emsg()) is normally updated has not been reached yet. We need to
8532 * update that flag first to make aborting() reliable.
8533 */
8534 update_force_abort();
8535 }
8536 if (error == ERROR_NONE)
8537 ret = OK;
8538
8539 /*
8540 * Report an error unless the argument evaluation or function call has been
8541 * cancelled due to an aborting error, an interrupt, or an exception.
8542 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008543 if (!aborting())
8544 {
8545 switch (error)
8546 {
8547 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008549 break;
8550 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008551 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008552 break;
8553 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008554 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 name);
8556 break;
8557 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008558 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 name);
8560 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008562 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008563 name);
8564 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008565 }
8566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (fname != name && fname != fname_buf)
8569 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008570 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
8572 return ret;
8573}
8574
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008575/*
8576 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008577 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008578 */
8579 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008580emsg_funcname(ermsg, name)
8581 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008582 char_u *name;
8583{
8584 char_u *p;
8585
8586 if (*name == K_SPECIAL)
8587 p = concat_str((char_u *)"<SNR>", name + 3);
8588 else
8589 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008590 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008591 if (p != name)
8592 vim_free(p);
8593}
8594
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008595/*
8596 * Return TRUE for a non-zero Number and a non-empty String.
8597 */
8598 static int
8599non_zero_arg(argvars)
8600 typval_T *argvars;
8601{
8602 return ((argvars[0].v_type == VAR_NUMBER
8603 && argvars[0].vval.v_number != 0)
8604 || (argvars[0].v_type == VAR_STRING
8605 && argvars[0].vval.v_string != NULL
8606 && *argvars[0].vval.v_string != NUL));
8607}
8608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609/*********************************************
8610 * Implementation of the built-in functions
8611 */
8612
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008613#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008614static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8615
8616/*
8617 * Get the float value of "argvars[0]" into "f".
8618 * Returns FAIL when the argument is not a Number or Float.
8619 */
8620 static int
8621get_float_arg(argvars, f)
8622 typval_T *argvars;
8623 float_T *f;
8624{
8625 if (argvars[0].v_type == VAR_FLOAT)
8626 {
8627 *f = argvars[0].vval.v_float;
8628 return OK;
8629 }
8630 if (argvars[0].v_type == VAR_NUMBER)
8631 {
8632 *f = (float_T)argvars[0].vval.v_number;
8633 return OK;
8634 }
8635 EMSG(_("E808: Number or Float required"));
8636 return FAIL;
8637}
8638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008639/*
8640 * "abs(expr)" function
8641 */
8642 static void
8643f_abs(argvars, rettv)
8644 typval_T *argvars;
8645 typval_T *rettv;
8646{
8647 if (argvars[0].v_type == VAR_FLOAT)
8648 {
8649 rettv->v_type = VAR_FLOAT;
8650 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8651 }
8652 else
8653 {
8654 varnumber_T n;
8655 int error = FALSE;
8656
8657 n = get_tv_number_chk(&argvars[0], &error);
8658 if (error)
8659 rettv->vval.v_number = -1;
8660 else if (n > 0)
8661 rettv->vval.v_number = n;
8662 else
8663 rettv->vval.v_number = -n;
8664 }
8665}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008666
8667/*
8668 * "acos()" function
8669 */
8670 static void
8671f_acos(argvars, rettv)
8672 typval_T *argvars;
8673 typval_T *rettv;
8674{
8675 float_T f;
8676
8677 rettv->v_type = VAR_FLOAT;
8678 if (get_float_arg(argvars, &f) == OK)
8679 rettv->vval.v_float = acos(f);
8680 else
8681 rettv->vval.v_float = 0.0;
8682}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683#endif
8684
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008686 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 */
8688 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008689f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 typval_T *argvars;
8691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008696 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008698 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008699 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008700 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008702 }
8703 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008704 EMSG(_(e_listreq));
8705}
8706
8707/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008708 * "and(expr, expr)" function
8709 */
8710 static void
8711f_and(argvars, rettv)
8712 typval_T *argvars;
8713 typval_T *rettv;
8714{
8715 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8716 & get_tv_number_chk(&argvars[1], NULL);
8717}
8718
8719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 * "append(lnum, string/list)" function
8721 */
8722 static void
8723f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008726{
8727 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 list_T *l = NULL;
8730 listitem_T *li = NULL;
8731 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 long added = 0;
8733
Bram Moolenaar0d660222005-01-07 21:51:51 +00008734 lnum = get_tv_lnum(argvars);
8735 if (lnum >= 0
8736 && lnum <= curbuf->b_ml.ml_line_count
8737 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008738 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008739 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008740 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008741 l = argvars[1].vval.v_list;
8742 if (l == NULL)
8743 return;
8744 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008745 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746 for (;;)
8747 {
8748 if (l == NULL)
8749 tv = &argvars[1]; /* append a string */
8750 else if (li == NULL)
8751 break; /* end of list */
8752 else
8753 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008754 line = get_tv_string_chk(tv);
8755 if (line == NULL) /* type error */
8756 {
8757 rettv->vval.v_number = 1; /* Failed */
8758 break;
8759 }
8760 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008761 ++added;
8762 if (l == NULL)
8763 break;
8764 li = li->li_next;
8765 }
8766
8767 appended_lines_mark(lnum, added);
8768 if (curwin->w_cursor.lnum > lnum)
8769 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 else
8772 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773}
8774
8775/*
8776 * "argc()" function
8777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008779f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784}
8785
8786/*
8787 * "argidx()" function
8788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795}
8796
8797/*
8798 * "argv(nr)" function
8799 */
8800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 typval_T *argvars;
8803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 int idx;
8806
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008807 if (argvars[0].v_type != VAR_UNKNOWN)
8808 {
8809 idx = get_tv_number_chk(&argvars[0], NULL);
8810 if (idx >= 0 && idx < ARGCOUNT)
8811 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8812 else
8813 rettv->vval.v_string = NULL;
8814 rettv->v_type = VAR_STRING;
8815 }
8816 else if (rettv_list_alloc(rettv) == OK)
8817 for (idx = 0; idx < ARGCOUNT; ++idx)
8818 list_append_string(rettv->vval.v_list,
8819 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820}
8821
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008822#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008823/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008824 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008825 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008826 static void
8827f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008828 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008829 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008830{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008831 float_T f;
8832
8833 rettv->v_type = VAR_FLOAT;
8834 if (get_float_arg(argvars, &f) == OK)
8835 rettv->vval.v_float = asin(f);
8836 else
8837 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008838}
8839
8840/*
8841 * "atan()" function
8842 */
8843 static void
8844f_atan(argvars, rettv)
8845 typval_T *argvars;
8846 typval_T *rettv;
8847{
8848 float_T f;
8849
8850 rettv->v_type = VAR_FLOAT;
8851 if (get_float_arg(argvars, &f) == OK)
8852 rettv->vval.v_float = atan(f);
8853 else
8854 rettv->vval.v_float = 0.0;
8855}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008856
8857/*
8858 * "atan2()" function
8859 */
8860 static void
8861f_atan2(argvars, rettv)
8862 typval_T *argvars;
8863 typval_T *rettv;
8864{
8865 float_T fx, fy;
8866
8867 rettv->v_type = VAR_FLOAT;
8868 if (get_float_arg(argvars, &fx) == OK
8869 && get_float_arg(&argvars[1], &fy) == OK)
8870 rettv->vval.v_float = atan2(fx, fy);
8871 else
8872 rettv->vval.v_float = 0.0;
8873}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008874#endif
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876/*
8877 * "browse(save, title, initdir, default)" function
8878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008881 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883{
8884#ifdef FEAT_BROWSE
8885 int save;
8886 char_u *title;
8887 char_u *initdir;
8888 char_u *defname;
8889 char_u buf[NUMBUFLEN];
8890 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 save = get_tv_number_chk(&argvars[0], &error);
8894 title = get_tv_string_chk(&argvars[1]);
8895 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8896 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008898 if (error || title == NULL || initdir == NULL || defname == NULL)
8899 rettv->vval.v_string = NULL;
8900 else
8901 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008902 do_browse(save ? BROWSE_SAVE : 0,
8903 title, defname, NULL, initdir, NULL, curbuf);
8904#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008906#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008908}
8909
8910/*
8911 * "browsedir(title, initdir)" function
8912 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008917{
8918#ifdef FEAT_BROWSE
8919 char_u *title;
8920 char_u *initdir;
8921 char_u buf[NUMBUFLEN];
8922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 title = get_tv_string_chk(&argvars[0]);
8924 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008926 if (title == NULL || initdir == NULL)
8927 rettv->vval.v_string = NULL;
8928 else
8929 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008930 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935}
8936
Bram Moolenaar33570922005-01-25 22:26:29 +00008937static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939/*
8940 * Find a buffer by number or exact name.
8941 */
8942 static buf_T *
8943find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
8946 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008948 if (avar->v_type == VAR_NUMBER)
8949 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008950 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008952 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008953 if (buf == NULL)
8954 {
8955 /* No full path name match, try a match with a URL or a "nofile"
8956 * buffer, these don't use the full path. */
8957 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8958 if (buf->b_fname != NULL
8959 && (path_with_url(buf->b_fname)
8960#ifdef FEAT_QUICKFIX
8961 || bt_nofile(buf)
8962#endif
8963 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008964 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008965 break;
8966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
8968 return buf;
8969}
8970
8971/*
8972 * "bufexists(expr)" function
8973 */
8974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *argvars;
8977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
8981
8982/*
8983 * "buflisted(expr)" function
8984 */
8985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 buf_T *buf;
8991
8992 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994}
8995
8996/*
8997 * "bufloaded(expr)" function
8998 */
8999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 buf_T *buf;
9005
9006 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008}
9009
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009010static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012/*
9013 * Get buffer by number or pattern.
9014 */
9015 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009016get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009018 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 int save_magic;
9022 char_u *save_cpo;
9023 buf_T *buf;
9024
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 if (tv->v_type == VAR_NUMBER)
9026 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009027 if (tv->v_type != VAR_STRING)
9028 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 if (name == NULL || *name == NUL)
9030 return curbuf;
9031 if (name[0] == '$' && name[1] == NUL)
9032 return lastbuf;
9033
9034 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9035 save_magic = p_magic;
9036 p_magic = TRUE;
9037 save_cpo = p_cpo;
9038 p_cpo = (char_u *)"";
9039
9040 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009041 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
9043 p_magic = save_magic;
9044 p_cpo = save_cpo;
9045
9046 /* If not found, try expanding the name, like done for bufexists(). */
9047 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049
9050 return buf;
9051}
9052
9053/*
9054 * "bufname(expr)" function
9055 */
9056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009058 typval_T *argvars;
9059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
9061 buf_T *buf;
9062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009063 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009065 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 --emsg_off;
9072}
9073
9074/*
9075 * "bufnr(expr)" function
9076 */
9077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 typval_T *argvars;
9080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009083 int error = FALSE;
9084 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009086 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009088 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009089 --emsg_off;
9090
9091 /* If the buffer isn't found and the second argument is not zero create a
9092 * new buffer. */
9093 if (buf == NULL
9094 && argvars[1].v_type != VAR_UNKNOWN
9095 && get_tv_number_chk(&argvars[1], &error) != 0
9096 && !error
9097 && (name = get_tv_string_chk(&argvars[0])) != NULL
9098 && !error)
9099 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9100
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105}
9106
9107/*
9108 * "bufwinnr(nr)" function
9109 */
9110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 typval_T *argvars;
9113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114{
9115#ifdef FEAT_WINDOWS
9116 win_T *wp;
9117 int winnr = 0;
9118#endif
9119 buf_T *buf;
9120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009123 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124#ifdef FEAT_WINDOWS
9125 for (wp = firstwin; wp; wp = wp->w_next)
9126 {
9127 ++winnr;
9128 if (wp->w_buffer == buf)
9129 break;
9130 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134#endif
9135 --emsg_off;
9136}
9137
9138/*
9139 * "byte2line(byte)" function
9140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009143 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009147 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148#else
9149 long boff = 0;
9150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 (linenr_T)0, &boff);
9157#endif
9158}
9159
9160/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009161 * "byteidx()" function
9162 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 typval_T *argvars;
9166 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009167{
9168#ifdef FEAT_MBYTE
9169 char_u *t;
9170#endif
9171 char_u *str;
9172 long idx;
9173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174 str = get_tv_string_chk(&argvars[0]);
9175 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009177 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009178 return;
9179
9180#ifdef FEAT_MBYTE
9181 t = str;
9182 for ( ; idx > 0; idx--)
9183 {
9184 if (*t == NUL) /* EOL reached */
9185 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009186 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009187 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009188 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009189#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009190 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009192#endif
9193}
9194
Bram Moolenaardb913952012-06-29 12:54:53 +02009195 int
9196func_call(name, args, selfdict, rettv)
9197 char_u *name;
9198 typval_T *args;
9199 dict_T *selfdict;
9200 typval_T *rettv;
9201{
9202 listitem_T *item;
9203 typval_T argv[MAX_FUNC_ARGS + 1];
9204 int argc = 0;
9205 int dummy;
9206 int r = 0;
9207
9208 for (item = args->vval.v_list->lv_first; item != NULL;
9209 item = item->li_next)
9210 {
9211 if (argc == MAX_FUNC_ARGS)
9212 {
9213 EMSG(_("E699: Too many arguments"));
9214 break;
9215 }
9216 /* Make a copy of each argument. This is needed to be able to set
9217 * v_lock to VAR_FIXED in the copy without changing the original list.
9218 */
9219 copy_tv(&item->li_tv, &argv[argc++]);
9220 }
9221
9222 if (item == NULL)
9223 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9224 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9225 &dummy, TRUE, selfdict);
9226
9227 /* Free the arguments. */
9228 while (argc > 0)
9229 clear_tv(&argv[--argc]);
9230
9231 return r;
9232}
9233
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009234/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009235 * "call(func, arglist)" function
9236 */
9237 static void
9238f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009239 typval_T *argvars;
9240 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009241{
9242 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009243 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009244
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009245 if (argvars[1].v_type != VAR_LIST)
9246 {
9247 EMSG(_(e_listreq));
9248 return;
9249 }
9250 if (argvars[1].vval.v_list == NULL)
9251 return;
9252
9253 if (argvars[0].v_type == VAR_FUNC)
9254 func = argvars[0].vval.v_string;
9255 else
9256 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 if (*func == NUL)
9258 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009259
Bram Moolenaare9a41262005-01-15 22:18:47 +00009260 if (argvars[2].v_type != VAR_UNKNOWN)
9261 {
9262 if (argvars[2].v_type != VAR_DICT)
9263 {
9264 EMSG(_(e_dictreq));
9265 return;
9266 }
9267 selfdict = argvars[2].vval.v_dict;
9268 }
9269
Bram Moolenaardb913952012-06-29 12:54:53 +02009270 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009271}
9272
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009273#ifdef FEAT_FLOAT
9274/*
9275 * "ceil({float})" function
9276 */
9277 static void
9278f_ceil(argvars, rettv)
9279 typval_T *argvars;
9280 typval_T *rettv;
9281{
9282 float_T f;
9283
9284 rettv->v_type = VAR_FLOAT;
9285 if (get_float_arg(argvars, &f) == OK)
9286 rettv->vval.v_float = ceil(f);
9287 else
9288 rettv->vval.v_float = 0.0;
9289}
9290#endif
9291
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009292/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009293 * "changenr()" function
9294 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009295 static void
9296f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009297 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009298 typval_T *rettv;
9299{
9300 rettv->vval.v_number = curbuf->b_u_seq_cur;
9301}
9302
9303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 * "char2nr(string)" function
9305 */
9306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009308 typval_T *argvars;
9309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311#ifdef FEAT_MBYTE
9312 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009313 {
9314 int utf8 = 0;
9315
9316 if (argvars[1].v_type != VAR_UNKNOWN)
9317 utf8 = get_tv_number_chk(&argvars[1], NULL);
9318
9319 if (utf8)
9320 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9321 else
9322 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 else
9325#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327}
9328
9329/*
9330 * "cindent(lnum)" function
9331 */
9332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009333f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009334 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336{
9337#ifdef FEAT_CINDENT
9338 pos_T pos;
9339 linenr_T lnum;
9340
9341 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9344 {
9345 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 curwin->w_cursor = pos;
9348 }
9349 else
9350#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352}
9353
9354/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009355 * "clearmatches()" function
9356 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009357 static void
9358f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009359 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009360 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009361{
9362#ifdef FEAT_SEARCH_EXTRA
9363 clear_matches(curwin);
9364#endif
9365}
9366
9367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 * "col(string)" function
9369 */
9370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 typval_T *argvars;
9373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375 colnr_T col = 0;
9376 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009377 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009379 fp = var2fpos(&argvars[0], FALSE, &fnum);
9380 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 {
9382 if (fp->col == MAXCOL)
9383 {
9384 /* '> can be MAXCOL, get the length of the line then */
9385 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009386 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 else
9388 col = MAXCOL;
9389 }
9390 else
9391 {
9392 col = fp->col + 1;
9393#ifdef FEAT_VIRTUALEDIT
9394 /* col(".") when the cursor is on the NUL at the end of the line
9395 * because of "coladd" can be seen as an extra column. */
9396 if (virtual_active() && fp == &curwin->w_cursor)
9397 {
9398 char_u *p = ml_get_cursor();
9399
9400 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9401 curwin->w_virtcol - curwin->w_cursor.coladd))
9402 {
9403# ifdef FEAT_MBYTE
9404 int l;
9405
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009406 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 col += l;
9408# else
9409 if (*p != NUL && p[1] == NUL)
9410 ++col;
9411# endif
9412 }
9413 }
9414#endif
9415 }
9416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418}
9419
Bram Moolenaar572cb562005-08-05 21:35:02 +00009420#if defined(FEAT_INS_EXPAND)
9421/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009422 * "complete()" function
9423 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009424 static void
9425f_complete(argvars, rettv)
9426 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009427 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009428{
9429 int startcol;
9430
9431 if ((State & INSERT) == 0)
9432 {
9433 EMSG(_("E785: complete() can only be used in Insert mode"));
9434 return;
9435 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009436
9437 /* Check for undo allowed here, because if something was already inserted
9438 * the line was already saved for undo and this check isn't done. */
9439 if (!undo_allowed())
9440 return;
9441
Bram Moolenaarade00832006-03-10 21:46:58 +00009442 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9443 {
9444 EMSG(_(e_invarg));
9445 return;
9446 }
9447
9448 startcol = get_tv_number_chk(&argvars[0], NULL);
9449 if (startcol <= 0)
9450 return;
9451
9452 set_completion(startcol - 1, argvars[1].vval.v_list);
9453}
9454
9455/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009456 * "complete_add()" function
9457 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009458 static void
9459f_complete_add(argvars, rettv)
9460 typval_T *argvars;
9461 typval_T *rettv;
9462{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009463 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009464}
9465
9466/*
9467 * "complete_check()" function
9468 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009469 static void
9470f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009471 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009472 typval_T *rettv;
9473{
9474 int saved = RedrawingDisabled;
9475
9476 RedrawingDisabled = 0;
9477 ins_compl_check_keys(0);
9478 rettv->vval.v_number = compl_interrupted;
9479 RedrawingDisabled = saved;
9480}
9481#endif
9482
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483/*
9484 * "confirm(message, buttons[, default [, type]])" function
9485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009488 typval_T *argvars UNUSED;
9489 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9492 char_u *message;
9493 char_u *buttons = NULL;
9494 char_u buf[NUMBUFLEN];
9495 char_u buf2[NUMBUFLEN];
9496 int def = 1;
9497 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009498 char_u *typestr;
9499 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009501 message = get_tv_string_chk(&argvars[0]);
9502 if (message == NULL)
9503 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009504 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9507 if (buttons == NULL)
9508 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009509 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009511 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9515 if (typestr == NULL)
9516 error = TRUE;
9517 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 switch (TOUPPER_ASC(*typestr))
9520 {
9521 case 'E': type = VIM_ERROR; break;
9522 case 'Q': type = VIM_QUESTION; break;
9523 case 'I': type = VIM_INFO; break;
9524 case 'W': type = VIM_WARNING; break;
9525 case 'G': type = VIM_GENERIC; break;
9526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 }
9528 }
9529 }
9530 }
9531
9532 if (buttons == NULL || *buttons == NUL)
9533 buttons = (char_u *)_("&Ok");
9534
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009535 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009536 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009537 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538#endif
9539}
9540
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009541/*
9542 * "copy()" function
9543 */
9544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 typval_T *argvars;
9547 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009548{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009549 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009552#ifdef FEAT_FLOAT
9553/*
9554 * "cos()" function
9555 */
9556 static void
9557f_cos(argvars, rettv)
9558 typval_T *argvars;
9559 typval_T *rettv;
9560{
9561 float_T f;
9562
9563 rettv->v_type = VAR_FLOAT;
9564 if (get_float_arg(argvars, &f) == OK)
9565 rettv->vval.v_float = cos(f);
9566 else
9567 rettv->vval.v_float = 0.0;
9568}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009569
9570/*
9571 * "cosh()" function
9572 */
9573 static void
9574f_cosh(argvars, rettv)
9575 typval_T *argvars;
9576 typval_T *rettv;
9577{
9578 float_T f;
9579
9580 rettv->v_type = VAR_FLOAT;
9581 if (get_float_arg(argvars, &f) == OK)
9582 rettv->vval.v_float = cosh(f);
9583 else
9584 rettv->vval.v_float = 0.0;
9585}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009586#endif
9587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009589 * "count()" function
9590 */
9591 static void
9592f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009593 typval_T *argvars;
9594 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009595{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009596 long n = 0;
9597 int ic = FALSE;
9598
Bram Moolenaare9a41262005-01-15 22:18:47 +00009599 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009601 listitem_T *li;
9602 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009603 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009604
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 if ((l = argvars[0].vval.v_list) != NULL)
9606 {
9607 li = l->lv_first;
9608 if (argvars[2].v_type != VAR_UNKNOWN)
9609 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009610 int error = FALSE;
9611
9612 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 if (argvars[3].v_type != VAR_UNKNOWN)
9614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009615 idx = get_tv_number_chk(&argvars[3], &error);
9616 if (!error)
9617 {
9618 li = list_find(l, idx);
9619 if (li == NULL)
9620 EMSGN(_(e_listidx), idx);
9621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 if (error)
9624 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 }
9626
9627 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009628 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 ++n;
9630 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009631 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 else if (argvars[0].v_type == VAR_DICT)
9633 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009634 int todo;
9635 dict_T *d;
9636 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009637
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009638 if ((d = argvars[0].vval.v_dict) != NULL)
9639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009640 int error = FALSE;
9641
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 if (argvars[2].v_type != VAR_UNKNOWN)
9643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009644 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 if (argvars[3].v_type != VAR_UNKNOWN)
9646 EMSG(_(e_invarg));
9647 }
9648
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009649 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009651 {
9652 if (!HASHITEM_EMPTY(hi))
9653 {
9654 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009655 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009656 ++n;
9657 }
9658 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009659 }
9660 }
9661 else
9662 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009663 rettv->vval.v_number = n;
9664}
9665
9666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9668 *
9669 * Checks the existence of a cscope connection.
9670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009673 typval_T *argvars UNUSED;
9674 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675{
9676#ifdef FEAT_CSCOPE
9677 int num = 0;
9678 char_u *dbpath = NULL;
9679 char_u *prepend = NULL;
9680 char_u buf[NUMBUFLEN];
9681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009682 if (argvars[0].v_type != VAR_UNKNOWN
9683 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009685 num = (int)get_tv_number(&argvars[0]);
9686 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009687 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 }
9690
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692#endif
9693}
9694
9695/*
9696 * "cursor(lnum, col)" function
9697 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009698 * Moves the cursor to the specified line and column.
9699 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009703 typval_T *argvars;
9704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009707#ifdef FEAT_VIRTUALEDIT
9708 long coladd = 0;
9709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009711 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009712 if (argvars[1].v_type == VAR_UNKNOWN)
9713 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009714 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009715
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009716 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009717 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009718 line = pos.lnum;
9719 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009720#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009721 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009722#endif
9723 }
9724 else
9725 {
9726 line = get_tv_lnum(argvars);
9727 col = get_tv_number_chk(&argvars[1], NULL);
9728#ifdef FEAT_VIRTUALEDIT
9729 if (argvars[2].v_type != VAR_UNKNOWN)
9730 coladd = get_tv_number_chk(&argvars[2], NULL);
9731#endif
9732 }
9733 if (line < 0 || col < 0
9734#ifdef FEAT_VIRTUALEDIT
9735 || coladd < 0
9736#endif
9737 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009738 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (line > 0)
9740 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (col > 0)
9742 curwin->w_cursor.col = col - 1;
9743#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009744 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745#endif
9746
9747 /* Make sure the cursor is in a valid position. */
9748 check_cursor();
9749#ifdef FEAT_MBYTE
9750 /* Correct cursor for multi-byte character. */
9751 if (has_mbyte)
9752 mb_adjust_cursor();
9753#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009754
9755 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009756 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009760 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 */
9762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 typval_T *argvars;
9765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009767 int noref = 0;
9768
9769 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009770 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009771 if (noref < 0 || noref > 1)
9772 EMSG(_(e_invarg));
9773 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009774 {
9775 current_copyID += COPYID_INC;
9776 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778}
9779
9780/*
9781 * "delete()" function
9782 */
9783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009785 typval_T *argvars;
9786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787{
9788 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794/*
9795 * "did_filetype()" function
9796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009799 typval_T *argvars UNUSED;
9800 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804#endif
9805}
9806
9807/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009808 * "diff_filler()" function
9809 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009812 typval_T *argvars UNUSED;
9813 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009814{
9815#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009817#endif
9818}
9819
9820/*
9821 * "diff_hlID()" function
9822 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009824f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009825 typval_T *argvars UNUSED;
9826 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009827{
9828#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009830 static linenr_T prev_lnum = 0;
9831 static int changedtick = 0;
9832 static int fnum = 0;
9833 static int change_start = 0;
9834 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009835 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009836 int filler_lines;
9837 int col;
9838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 if (lnum < 0) /* ignore type error in {lnum} arg */
9840 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009841 if (lnum != prev_lnum
9842 || changedtick != curbuf->b_changedtick
9843 || fnum != curbuf->b_fnum)
9844 {
9845 /* New line, buffer, change: need to get the values. */
9846 filler_lines = diff_check(curwin, lnum);
9847 if (filler_lines < 0)
9848 {
9849 if (filler_lines == -1)
9850 {
9851 change_start = MAXCOL;
9852 change_end = -1;
9853 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9854 hlID = HLF_ADD; /* added line */
9855 else
9856 hlID = HLF_CHD; /* changed line */
9857 }
9858 else
9859 hlID = HLF_ADD; /* added line */
9860 }
9861 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009862 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009863 prev_lnum = lnum;
9864 changedtick = curbuf->b_changedtick;
9865 fnum = curbuf->b_fnum;
9866 }
9867
9868 if (hlID == HLF_CHD || hlID == HLF_TXD)
9869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009870 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009871 if (col >= change_start && col <= change_end)
9872 hlID = HLF_TXD; /* changed text */
9873 else
9874 hlID = HLF_CHD; /* changed line */
9875 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009876 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009877#endif
9878}
9879
9880/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009881 * "empty({expr})" function
9882 */
9883 static void
9884f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009885 typval_T *argvars;
9886 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009887{
9888 int n;
9889
9890 switch (argvars[0].v_type)
9891 {
9892 case VAR_STRING:
9893 case VAR_FUNC:
9894 n = argvars[0].vval.v_string == NULL
9895 || *argvars[0].vval.v_string == NUL;
9896 break;
9897 case VAR_NUMBER:
9898 n = argvars[0].vval.v_number == 0;
9899 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009900#ifdef FEAT_FLOAT
9901 case VAR_FLOAT:
9902 n = argvars[0].vval.v_float == 0.0;
9903 break;
9904#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009905 case VAR_LIST:
9906 n = argvars[0].vval.v_list == NULL
9907 || argvars[0].vval.v_list->lv_first == NULL;
9908 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909 case VAR_DICT:
9910 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009913 default:
9914 EMSG2(_(e_intern2), "f_empty()");
9915 n = 0;
9916 }
9917
9918 rettv->vval.v_number = n;
9919}
9920
9921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 * "escape({string}, {chars})" function
9923 */
9924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009926 typval_T *argvars;
9927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
9929 char_u buf[NUMBUFLEN];
9930
Bram Moolenaar758711c2005-02-02 23:11:38 +00009931 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9932 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009933 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934}
9935
9936/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009937 * "eval()" function
9938 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009939 static void
9940f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009941 typval_T *argvars;
9942 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009943{
9944 char_u *s;
9945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 s = get_tv_string_chk(&argvars[0]);
9947 if (s != NULL)
9948 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009950 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9951 {
9952 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009953 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009955 else if (*s != NUL)
9956 EMSG(_(e_trailing));
9957}
9958
9959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 * "eventhandler()" function
9961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009964 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968}
9969
9970/*
9971 * "executable()" function
9972 */
9973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 typval_T *argvars;
9976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
9981/*
9982 * "exists()" function
9983 */
9984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989 char_u *p;
9990 char_u *name;
9991 int n = FALSE;
9992 int len = 0;
9993
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009994 no_autoload = TRUE;
9995
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 if (*p == '$') /* environment variable */
9998 {
9999 /* first try "normal" environment variables (fast) */
10000 if (mch_getenv(p + 1) != NULL)
10001 n = TRUE;
10002 else
10003 {
10004 /* try expanding things like $VIM and ${HOME} */
10005 p = expand_env_save(p);
10006 if (p != NULL && *p != '$')
10007 n = TRUE;
10008 vim_free(p);
10009 }
10010 }
10011 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010014 if (*skipwhite(p) != NUL)
10015 n = FALSE; /* trailing garbage */
10016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 else if (*p == '*') /* internal or user defined function */
10018 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010019 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021 else if (*p == ':')
10022 {
10023 n = cmd_exists(p + 1);
10024 }
10025 else if (*p == '#')
10026 {
10027#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010028 if (p[1] == '#')
10029 n = autocmd_supported(p + 2);
10030 else
10031 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032#endif
10033 }
10034 else /* internal variable */
10035 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010036 char_u *tofree;
10037 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010039 /* get_name_len() takes care of expanding curly braces */
10040 name = p;
10041 len = get_name_len(&p, &tofree, TRUE, FALSE);
10042 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010044 if (tofree != NULL)
10045 name = tofree;
10046 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10047 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010049 /* handle d.key, l[idx], f(expr) */
10050 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10051 if (n)
10052 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010055 if (*p != NUL)
10056 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010058 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
10060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010062
10063 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064}
10065
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010066#ifdef FEAT_FLOAT
10067/*
10068 * "exp()" function
10069 */
10070 static void
10071f_exp(argvars, rettv)
10072 typval_T *argvars;
10073 typval_T *rettv;
10074{
10075 float_T f;
10076
10077 rettv->v_type = VAR_FLOAT;
10078 if (get_float_arg(argvars, &f) == OK)
10079 rettv->vval.v_float = exp(f);
10080 else
10081 rettv->vval.v_float = 0.0;
10082}
10083#endif
10084
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085/*
10086 * "expand()" function
10087 */
10088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010090 typval_T *argvars;
10091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
10093 char_u *s;
10094 int len;
10095 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010096 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010099 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010102 if (argvars[1].v_type != VAR_UNKNOWN
10103 && argvars[2].v_type != VAR_UNKNOWN
10104 && get_tv_number_chk(&argvars[2], &error)
10105 && !error)
10106 {
10107 rettv->v_type = VAR_LIST;
10108 rettv->vval.v_list = NULL;
10109 }
10110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010111 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 if (*s == '%' || *s == '#' || *s == '<')
10113 {
10114 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010115 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010117 if (rettv->v_type == VAR_LIST)
10118 {
10119 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10120 list_append_string(rettv->vval.v_list, result, -1);
10121 else
10122 vim_free(result);
10123 }
10124 else
10125 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 }
10127 else
10128 {
10129 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010130 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 if (argvars[1].v_type != VAR_UNKNOWN
10132 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010133 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 if (!error)
10135 {
10136 ExpandInit(&xpc);
10137 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010138 if (p_wic)
10139 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010140 if (rettv->v_type == VAR_STRING)
10141 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10142 options, WILD_ALL);
10143 else if (rettv_list_alloc(rettv) != FAIL)
10144 {
10145 int i;
10146
10147 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10148 for (i = 0; i < xpc.xp_numfiles; i++)
10149 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10150 ExpandCleanup(&xpc);
10151 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152 }
10153 else
10154 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 }
10156}
10157
10158/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010159 * Go over all entries in "d2" and add them to "d1".
10160 * When "action" is "error" then a duplicate key is an error.
10161 * When "action" is "force" then a duplicate key is overwritten.
10162 * Otherwise duplicate keys are ignored ("action" is "keep").
10163 */
10164 void
10165dict_extend(d1, d2, action)
10166 dict_T *d1;
10167 dict_T *d2;
10168 char_u *action;
10169{
10170 dictitem_T *di1;
10171 hashitem_T *hi2;
10172 int todo;
10173
10174 todo = (int)d2->dv_hashtab.ht_used;
10175 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10176 {
10177 if (!HASHITEM_EMPTY(hi2))
10178 {
10179 --todo;
10180 di1 = dict_find(d1, hi2->hi_key, -1);
10181 if (d1->dv_scope != 0)
10182 {
10183 /* Disallow replacing a builtin function in l: and g:.
10184 * Check the key to be valid when adding to any
10185 * scope. */
10186 if (d1->dv_scope == VAR_DEF_SCOPE
10187 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10188 && var_check_func_name(hi2->hi_key,
10189 di1 == NULL))
10190 break;
10191 if (!valid_varname(hi2->hi_key))
10192 break;
10193 }
10194 if (di1 == NULL)
10195 {
10196 di1 = dictitem_copy(HI2DI(hi2));
10197 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10198 dictitem_free(di1);
10199 }
10200 else if (*action == 'e')
10201 {
10202 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10203 break;
10204 }
10205 else if (*action == 'f' && HI2DI(hi2) != di1)
10206 {
10207 clear_tv(&di1->di_tv);
10208 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10209 }
10210 }
10211 }
10212}
10213
10214/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010215 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010217 */
10218 static void
10219f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010220 typval_T *argvars;
10221 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010222{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010223 char *arg_errmsg = N_("extend() argument");
10224
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010227 list_T *l1, *l2;
10228 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010229 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010231
Bram Moolenaare9a41262005-01-15 22:18:47 +000010232 l1 = argvars[0].vval.v_list;
10233 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010234 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010235 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 {
10237 if (argvars[2].v_type != VAR_UNKNOWN)
10238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 before = get_tv_number_chk(&argvars[2], &error);
10240 if (error)
10241 return; /* type error; errmsg already given */
10242
Bram Moolenaar758711c2005-02-02 23:11:38 +000010243 if (before == l1->lv_len)
10244 item = NULL;
10245 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010246 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010247 item = list_find(l1, before);
10248 if (item == NULL)
10249 {
10250 EMSGN(_(e_listidx), before);
10251 return;
10252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010253 }
10254 }
10255 else
10256 item = NULL;
10257 list_extend(l1, l2, item);
10258
Bram Moolenaare9a41262005-01-15 22:18:47 +000010259 copy_tv(&argvars[0], rettv);
10260 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010261 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10263 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010264 dict_T *d1, *d2;
10265 char_u *action;
10266 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267
10268 d1 = argvars[0].vval.v_dict;
10269 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010270 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010271 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 {
10273 /* Check the third argument. */
10274 if (argvars[2].v_type != VAR_UNKNOWN)
10275 {
10276 static char *(av[]) = {"keep", "force", "error"};
10277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 action = get_tv_string_chk(&argvars[2]);
10279 if (action == NULL)
10280 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 for (i = 0; i < 3; ++i)
10282 if (STRCMP(action, av[i]) == 0)
10283 break;
10284 if (i == 3)
10285 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010286 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010287 return;
10288 }
10289 }
10290 else
10291 action = (char_u *)"force";
10292
Bram Moolenaara9922d62013-05-30 13:01:18 +020010293 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295 copy_tv(&argvars[0], rettv);
10296 }
10297 }
10298 else
10299 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010300}
10301
10302/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010303 * "feedkeys()" function
10304 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010305 static void
10306f_feedkeys(argvars, rettv)
10307 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010308 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010309{
10310 int remap = TRUE;
10311 char_u *keys, *flags;
10312 char_u nbuf[NUMBUFLEN];
10313 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010314 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010315
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010316 /* This is not allowed in the sandbox. If the commands would still be
10317 * executed in the sandbox it would be OK, but it probably happens later,
10318 * when "sandbox" is no longer set. */
10319 if (check_secure())
10320 return;
10321
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010322 keys = get_tv_string(&argvars[0]);
10323 if (*keys != NUL)
10324 {
10325 if (argvars[1].v_type != VAR_UNKNOWN)
10326 {
10327 flags = get_tv_string_buf(&argvars[1], nbuf);
10328 for ( ; *flags != NUL; ++flags)
10329 {
10330 switch (*flags)
10331 {
10332 case 'n': remap = FALSE; break;
10333 case 'm': remap = TRUE; break;
10334 case 't': typed = TRUE; break;
10335 }
10336 }
10337 }
10338
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010339 /* Need to escape K_SPECIAL and CSI before putting the string in the
10340 * typeahead buffer. */
10341 keys_esc = vim_strsave_escape_csi(keys);
10342 if (keys_esc != NULL)
10343 {
10344 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010345 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010346 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010347 if (vgetc_busy)
10348 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010349 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010350 }
10351}
10352
10353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 * "filereadable()" function
10355 */
10356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010357f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 typval_T *argvars;
10359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010361 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 char_u *p;
10363 int n;
10364
Bram Moolenaarc236c162008-07-13 17:41:49 +000010365#ifndef O_NONBLOCK
10366# define O_NONBLOCK 0
10367#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010369 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10370 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 {
10372 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010373 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 }
10375 else
10376 n = FALSE;
10377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379}
10380
10381/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010382 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 * rights to write into.
10384 */
10385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010387 typval_T *argvars;
10388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010390 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391}
10392
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010393static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010394
10395 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010396findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010398 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010399 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010400{
10401#ifdef FEAT_SEARCHPATH
10402 char_u *fname;
10403 char_u *fresult = NULL;
10404 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10405 char_u *p;
10406 char_u pathbuf[NUMBUFLEN];
10407 int count = 1;
10408 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010409 int error = FALSE;
10410#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010411
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010412 rettv->vval.v_string = NULL;
10413 rettv->v_type = VAR_STRING;
10414
10415#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010417
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010418 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10421 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010422 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 else
10424 {
10425 if (*p != NUL)
10426 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010429 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010431 }
10432
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010433 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10434 error = TRUE;
10435
10436 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 do
10439 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010440 if (rettv->v_type == VAR_STRING)
10441 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 fresult = find_file_in_path_option(first ? fname : NULL,
10443 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010444 0, first, path,
10445 find_what,
10446 curbuf->b_ffname,
10447 find_what == FINDFILE_DIR
10448 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010450
10451 if (fresult != NULL && rettv->v_type == VAR_LIST)
10452 list_append_string(rettv->vval.v_list, fresult, -1);
10453
10454 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010456
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010457 if (rettv->v_type == VAR_STRING)
10458 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010459#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010460}
10461
Bram Moolenaar33570922005-01-25 22:26:29 +000010462static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10463static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010464
10465/*
10466 * Implementation of map() and filter().
10467 */
10468 static void
10469filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010470 typval_T *argvars;
10471 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010472 int map;
10473{
10474 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010475 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 listitem_T *li, *nli;
10477 list_T *l = NULL;
10478 dictitem_T *di;
10479 hashtab_T *ht;
10480 hashitem_T *hi;
10481 dict_T *d = NULL;
10482 typval_T save_val;
10483 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010485 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010486 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10487 char *arg_errmsg = (map ? N_("map() argument")
10488 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010489 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010490 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010491
Bram Moolenaare9a41262005-01-15 22:18:47 +000010492 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010493 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010494 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010495 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010496 return;
10497 }
10498 else if (argvars[0].v_type == VAR_DICT)
10499 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010500 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010501 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010502 return;
10503 }
10504 else
10505 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010506 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 return;
10508 }
10509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 expr = get_tv_string_buf_chk(&argvars[1], buf);
10511 /* On type errors, the preceding call has already displayed an error
10512 * message. Avoid a misleading error message for an empty string that
10513 * was not passed as argument. */
10514 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 prepare_vimvar(VV_VAL, &save_val);
10517 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010518
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010519 /* We reset "did_emsg" to be able to detect whether an error
10520 * occurred during evaluation of the expression. */
10521 save_did_emsg = did_emsg;
10522 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010523
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010524 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 vimvars[VV_KEY].vv_type = VAR_STRING;
10528
10529 ht = &d->dv_hashtab;
10530 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010531 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 if (!HASHITEM_EMPTY(hi))
10535 {
10536 --todo;
10537 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010538 if (tv_check_lock(di->di_tv.v_lock,
10539 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 break;
10541 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010542 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010543 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 break;
10545 if (!map && rem)
10546 dictitem_remove(d, di);
10547 clear_tv(&vimvars[VV_KEY].vv_tv);
10548 }
10549 }
10550 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 }
10552 else
10553 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010554 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 for (li = l->lv_first; li != NULL; li = nli)
10557 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010558 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010559 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010561 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010562 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010563 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010564 break;
10565 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010566 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010567 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010568 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010569 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010570
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010571 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010573
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010574 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010575 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576
10577 copy_tv(&argvars[0], rettv);
10578}
10579
10580 static int
10581filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010582 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 char_u *expr;
10584 int map;
10585 int *remp;
10586{
Bram Moolenaar33570922005-01-25 22:26:29 +000010587 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010589 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 s = expr;
10593 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010594 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010595 if (*s != NUL) /* check for trailing chars after expr */
10596 {
10597 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010598 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 }
10600 if (map)
10601 {
10602 /* map(): replace the list item value */
10603 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010604 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 *tv = rettv;
10606 }
10607 else
10608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 int error = FALSE;
10610
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 /* On type error, nothing has been removed; return FAIL to stop the
10615 * loop. The error message was given by get_tv_number_chk(). */
10616 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010617 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010619 retval = OK;
10620theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010621 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010622 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010623}
10624
10625/*
10626 * "filter()" function
10627 */
10628 static void
10629f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *argvars;
10631 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010632{
10633 filter_map(argvars, rettv, FALSE);
10634}
10635
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010637 * "finddir({fname}[, {path}[, {count}]])" function
10638 */
10639 static void
10640f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 typval_T *argvars;
10642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010643{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010644 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010645}
10646
10647/*
10648 * "findfile({fname}[, {path}[, {count}]])" function
10649 */
10650 static void
10651f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 typval_T *argvars;
10653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010654{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010655 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010656}
10657
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010658#ifdef FEAT_FLOAT
10659/*
10660 * "float2nr({float})" function
10661 */
10662 static void
10663f_float2nr(argvars, rettv)
10664 typval_T *argvars;
10665 typval_T *rettv;
10666{
10667 float_T f;
10668
10669 if (get_float_arg(argvars, &f) == OK)
10670 {
10671 if (f < -0x7fffffff)
10672 rettv->vval.v_number = -0x7fffffff;
10673 else if (f > 0x7fffffff)
10674 rettv->vval.v_number = 0x7fffffff;
10675 else
10676 rettv->vval.v_number = (varnumber_T)f;
10677 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010678}
10679
10680/*
10681 * "floor({float})" function
10682 */
10683 static void
10684f_floor(argvars, rettv)
10685 typval_T *argvars;
10686 typval_T *rettv;
10687{
10688 float_T f;
10689
10690 rettv->v_type = VAR_FLOAT;
10691 if (get_float_arg(argvars, &f) == OK)
10692 rettv->vval.v_float = floor(f);
10693 else
10694 rettv->vval.v_float = 0.0;
10695}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010696
10697/*
10698 * "fmod()" function
10699 */
10700 static void
10701f_fmod(argvars, rettv)
10702 typval_T *argvars;
10703 typval_T *rettv;
10704{
10705 float_T fx, fy;
10706
10707 rettv->v_type = VAR_FLOAT;
10708 if (get_float_arg(argvars, &fx) == OK
10709 && get_float_arg(&argvars[1], &fy) == OK)
10710 rettv->vval.v_float = fmod(fx, fy);
10711 else
10712 rettv->vval.v_float = 0.0;
10713}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010714#endif
10715
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010717 * "fnameescape({string})" function
10718 */
10719 static void
10720f_fnameescape(argvars, rettv)
10721 typval_T *argvars;
10722 typval_T *rettv;
10723{
10724 rettv->vval.v_string = vim_strsave_fnameescape(
10725 get_tv_string(&argvars[0]), FALSE);
10726 rettv->v_type = VAR_STRING;
10727}
10728
10729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 * "fnamemodify({fname}, {mods})" function
10731 */
10732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *argvars;
10735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
10737 char_u *fname;
10738 char_u *mods;
10739 int usedlen = 0;
10740 int len;
10741 char_u *fbuf = NULL;
10742 char_u buf[NUMBUFLEN];
10743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 fname = get_tv_string_chk(&argvars[0]);
10745 mods = get_tv_string_buf_chk(&argvars[1], buf);
10746 if (fname == NULL || mods == NULL)
10747 fname = NULL;
10748 else
10749 {
10750 len = (int)STRLEN(fname);
10751 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 vim_free(fbuf);
10760}
10761
Bram Moolenaar33570922005-01-25 22:26:29 +000010762static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763
10764/*
10765 * "foldclosed()" function
10766 */
10767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010769 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010770 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010771 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772{
10773#ifdef FEAT_FOLDING
10774 linenr_T lnum;
10775 linenr_T first, last;
10776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10779 {
10780 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10781 {
10782 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010785 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 return;
10787 }
10788 }
10789#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791}
10792
10793/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010794 * "foldclosed()" function
10795 */
10796 static void
10797f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010798 typval_T *argvars;
10799 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800{
10801 foldclosed_both(argvars, rettv, FALSE);
10802}
10803
10804/*
10805 * "foldclosedend()" function
10806 */
10807 static void
10808f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010809 typval_T *argvars;
10810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811{
10812 foldclosed_both(argvars, rettv, TRUE);
10813}
10814
10815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 * "foldlevel()" function
10817 */
10818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010820 typval_T *argvars UNUSED;
10821 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
10823#ifdef FEAT_FOLDING
10824 linenr_T lnum;
10825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830}
10831
10832/*
10833 * "foldtext()" function
10834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010837 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839{
10840#ifdef FEAT_FOLDING
10841 linenr_T lnum;
10842 char_u *s;
10843 char_u *r;
10844 int len;
10845 char *txt;
10846#endif
10847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848 rettv->v_type = VAR_STRING;
10849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010851 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10852 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10853 <= curbuf->b_ml.ml_line_count
10854 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 {
10856 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010857 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10858 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 {
10860 if (!linewhite(lnum))
10861 break;
10862 ++lnum;
10863 }
10864
10865 /* Find interesting text in this line. */
10866 s = skipwhite(ml_get(lnum));
10867 /* skip C comment-start */
10868 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010871 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010872 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010873 {
10874 s = skipwhite(ml_get(lnum + 1));
10875 if (*s == '*')
10876 s = skipwhite(s + 1);
10877 }
10878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 txt = _("+-%s%3ld lines: ");
10880 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010881 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 + 20 /* for %3ld */
10883 + STRLEN(s))); /* concatenated */
10884 if (r != NULL)
10885 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010886 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10887 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10888 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 len = (int)STRLEN(r);
10890 STRCAT(r, s);
10891 /* remove 'foldmarker' and 'commentstring' */
10892 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 }
10895 }
10896#endif
10897}
10898
10899/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010900 * "foldtextresult(lnum)" function
10901 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010906{
10907#ifdef FEAT_FOLDING
10908 linenr_T lnum;
10909 char_u *text;
10910 char_u buf[51];
10911 foldinfo_T foldinfo;
10912 int fold_count;
10913#endif
10914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->v_type = VAR_STRING;
10916 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010917#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 /* treat illegal types and illegal string values for {lnum} the same */
10920 if (lnum < 0)
10921 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010922 fold_count = foldedCount(curwin, lnum, &foldinfo);
10923 if (fold_count > 0)
10924 {
10925 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10926 &foldinfo, buf);
10927 if (text == buf)
10928 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010930 }
10931#endif
10932}
10933
10934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 * "foreground()" function
10936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010939 typval_T *argvars UNUSED;
10940 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942#ifdef FEAT_GUI
10943 if (gui.in_use)
10944 gui_mch_set_foreground();
10945#else
10946# ifdef WIN32
10947 win32_set_foreground();
10948# endif
10949#endif
10950}
10951
10952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010953 * "function()" function
10954 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010956f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 typval_T *argvars;
10958 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010959{
10960 char_u *s;
Bram Moolenaara1544c02013-05-30 12:35:52 +020010961 char_u *name = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010964 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010965 EMSG2(_(e_invarg2), s);
Bram Moolenaara1544c02013-05-30 12:35:52 +020010966 /* Don't check an autoload name for existence here, but still expand it
10967 * checking for validity */
10968 else if ((name = get_expanded_name(s, vim_strchr(s, AUTOLOAD_CHAR) == NULL))
10969 == NULL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010970 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010971 else
10972 {
Bram Moolenaara1544c02013-05-30 12:35:52 +020010973 if (name == NULL)
10974 /* Autoload function, need to copy string */
10975 rettv->vval.v_string = vim_strsave(s);
10976 else
10977 /* Function found by get_expanded_name, string allocated by
10978 * trans_function_name: no need to copy */
10979 rettv->vval.v_string = name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010981 }
10982}
10983
10984/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010985 * "garbagecollect()" function
10986 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010987 static void
10988f_garbagecollect(argvars, rettv)
10989 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010990 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010991{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010992 /* This is postponed until we are back at the toplevel, because we may be
10993 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10994 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010995
10996 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10997 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010998}
10999
11000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011001 * "get()" function
11002 */
11003 static void
11004f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 typval_T *argvars;
11006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007{
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 listitem_T *li;
11009 list_T *l;
11010 dictitem_T *di;
11011 dict_T *d;
11012 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011013
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011015 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011018 int error = FALSE;
11019
11020 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11021 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011023 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011024 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011025 else if (argvars[0].v_type == VAR_DICT)
11026 {
11027 if ((d = argvars[0].vval.v_dict) != NULL)
11028 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011029 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011030 if (di != NULL)
11031 tv = &di->di_tv;
11032 }
11033 }
11034 else
11035 EMSG2(_(e_listdictarg), "get()");
11036
11037 if (tv == NULL)
11038 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011039 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011040 copy_tv(&argvars[2], rettv);
11041 }
11042 else
11043 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011044}
11045
Bram Moolenaar342337a2005-07-21 21:11:17 +000011046static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011047
11048/*
11049 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011050 * Return a range (from start to end) of lines in rettv from the specified
11051 * buffer.
11052 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011053 */
11054 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011055get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011056 buf_T *buf;
11057 linenr_T start;
11058 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011059 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011060 typval_T *rettv;
11061{
11062 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011063
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011064 if (retlist && rettv_list_alloc(rettv) == FAIL)
11065 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011066
11067 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11068 return;
11069
11070 if (!retlist)
11071 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011072 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11073 p = ml_get_buf(buf, start, FALSE);
11074 else
11075 p = (char_u *)"";
11076
11077 rettv->v_type = VAR_STRING;
11078 rettv->vval.v_string = vim_strsave(p);
11079 }
11080 else
11081 {
11082 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011083 return;
11084
11085 if (start < 1)
11086 start = 1;
11087 if (end > buf->b_ml.ml_line_count)
11088 end = buf->b_ml.ml_line_count;
11089 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011090 if (list_append_string(rettv->vval.v_list,
11091 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011092 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011093 }
11094}
11095
11096/*
11097 * "getbufline()" function
11098 */
11099 static void
11100f_getbufline(argvars, rettv)
11101 typval_T *argvars;
11102 typval_T *rettv;
11103{
11104 linenr_T lnum;
11105 linenr_T end;
11106 buf_T *buf;
11107
11108 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11109 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011110 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011111 --emsg_off;
11112
Bram Moolenaar661b1822005-07-28 22:36:45 +000011113 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011114 if (argvars[2].v_type == VAR_UNKNOWN)
11115 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011116 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011117 end = get_tv_lnum_buf(&argvars[2], buf);
11118
Bram Moolenaar342337a2005-07-21 21:11:17 +000011119 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011120}
11121
Bram Moolenaar0d660222005-01-07 21:51:51 +000011122/*
11123 * "getbufvar()" function
11124 */
11125 static void
11126f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011127 typval_T *argvars;
11128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011129{
11130 buf_T *buf;
11131 buf_T *save_curbuf;
11132 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011134 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11137 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011138 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011139 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011140
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011141 rettv->v_type = VAR_STRING;
11142 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143
11144 if (buf != NULL && varname != NULL)
11145 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011146 /* set curbuf to be our buf, temporarily */
11147 save_curbuf = curbuf;
11148 curbuf = buf;
11149
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011151 {
11152 if (get_option_tv(&varname, rettv, TRUE) == OK)
11153 done = TRUE;
11154 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011155 else if (STRCMP(varname, "changedtick") == 0)
11156 {
11157 rettv->v_type = VAR_NUMBER;
11158 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011159 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011160 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161 else
11162 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011163 /* Look up the variable. */
11164 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11165 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11166 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011167 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011168 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011169 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011170 done = TRUE;
11171 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011172 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011173
11174 /* restore previous notion of curbuf */
11175 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011176 }
11177
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011178 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11179 /* use the default value */
11180 copy_tv(&argvars[2], rettv);
11181
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182 --emsg_off;
11183}
11184
11185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 * "getchar()" function
11187 */
11188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *argvars;
11191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192{
11193 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011196 /* Position the cursor. Needed after a message that ends in a space. */
11197 windgoto(msg_row, msg_col);
11198
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 ++no_mapping;
11200 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011201 for (;;)
11202 {
11203 if (argvars[0].v_type == VAR_UNKNOWN)
11204 /* getchar(): blocking wait. */
11205 n = safe_vgetc();
11206 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11207 /* getchar(1): only check if char avail */
11208 n = vpeekc();
11209 else if (error || vpeekc() == NUL)
11210 /* illegal argument or getchar(0) and no char avail: return zero */
11211 n = 0;
11212 else
11213 /* getchar(0) and char avail: return char */
11214 n = safe_vgetc();
11215 if (n == K_IGNORE)
11216 continue;
11217 break;
11218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 --no_mapping;
11220 --allow_keys;
11221
Bram Moolenaar219b8702006-11-01 14:32:36 +000011222 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11223 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11224 vimvars[VV_MOUSE_COL].vv_nr = 0;
11225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 if (IS_SPECIAL(n) || mod_mask != 0)
11228 {
11229 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11230 int i = 0;
11231
11232 /* Turn a special key into three bytes, plus modifier. */
11233 if (mod_mask != 0)
11234 {
11235 temp[i++] = K_SPECIAL;
11236 temp[i++] = KS_MODIFIER;
11237 temp[i++] = mod_mask;
11238 }
11239 if (IS_SPECIAL(n))
11240 {
11241 temp[i++] = K_SPECIAL;
11242 temp[i++] = K_SECOND(n);
11243 temp[i++] = K_THIRD(n);
11244 }
11245#ifdef FEAT_MBYTE
11246 else if (has_mbyte)
11247 i += (*mb_char2bytes)(n, temp + i);
11248#endif
11249 else
11250 temp[i++] = n;
11251 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011252 rettv->v_type = VAR_STRING;
11253 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011254
11255#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011256 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011257 {
11258 int row = mouse_row;
11259 int col = mouse_col;
11260 win_T *win;
11261 linenr_T lnum;
11262# ifdef FEAT_WINDOWS
11263 win_T *wp;
11264# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011265 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011266
11267 if (row >= 0 && col >= 0)
11268 {
11269 /* Find the window at the mouse coordinates and compute the
11270 * text position. */
11271 win = mouse_find_win(&row, &col);
11272 (void)mouse_comp_pos(win, &row, &col, &lnum);
11273# ifdef FEAT_WINDOWS
11274 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011275 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011276# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011277 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011278 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11279 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11280 }
11281 }
11282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 }
11284}
11285
11286/*
11287 * "getcharmod()" function
11288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011291 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295}
11296
11297/*
11298 * "getcmdline()" function
11299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011302 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011305 rettv->v_type = VAR_STRING;
11306 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307}
11308
11309/*
11310 * "getcmdpos()" function
11311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011314 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318}
11319
11320/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011321 * "getcmdtype()" function
11322 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011323 static void
11324f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011325 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011326 typval_T *rettv;
11327{
11328 rettv->v_type = VAR_STRING;
11329 rettv->vval.v_string = alloc(2);
11330 if (rettv->vval.v_string != NULL)
11331 {
11332 rettv->vval.v_string[0] = get_cmdline_type();
11333 rettv->vval.v_string[1] = NUL;
11334 }
11335}
11336
11337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 * "getcwd()" function
11339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011342 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011345 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011348 rettv->vval.v_string = NULL;
11349 cwd = alloc(MAXPATHL);
11350 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011352 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11353 {
11354 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011356 if (rettv->vval.v_string != NULL)
11357 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011359 }
11360 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 }
11362}
11363
11364/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011365 * "getfontname()" function
11366 */
11367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011369 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 rettv->v_type = VAR_STRING;
11373 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011374#ifdef FEAT_GUI
11375 if (gui.in_use)
11376 {
11377 GuiFont font;
11378 char_u *name = NULL;
11379
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011380 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011381 {
11382 /* Get the "Normal" font. Either the name saved by
11383 * hl_set_font_name() or from the font ID. */
11384 font = gui.norm_font;
11385 name = hl_get_font_name();
11386 }
11387 else
11388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011390 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11391 return;
11392 font = gui_mch_get_font(name, FALSE);
11393 if (font == NOFONT)
11394 return; /* Invalid font name, return empty string. */
11395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011397 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011398 gui_mch_free_font(font);
11399 }
11400#endif
11401}
11402
11403/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011404 * "getfperm({fname})" function
11405 */
11406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011408 typval_T *argvars;
11409 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011410{
11411 char_u *fname;
11412 struct stat st;
11413 char_u *perm = NULL;
11414 char_u flags[] = "rwx";
11415 int i;
11416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011420 if (mch_stat((char *)fname, &st) >= 0)
11421 {
11422 perm = vim_strsave((char_u *)"---------");
11423 if (perm != NULL)
11424 {
11425 for (i = 0; i < 9; i++)
11426 {
11427 if (st.st_mode & (1 << (8 - i)))
11428 perm[i] = flags[i % 3];
11429 }
11430 }
11431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011432 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011433}
11434
11435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 * "getfsize({fname})" function
11437 */
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443 char_u *fname;
11444 struct stat st;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449
11450 if (mch_stat((char *)fname, &st) >= 0)
11451 {
11452 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011457
11458 /* non-perfect check for overflow */
11459 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11460 rettv->vval.v_number = -2;
11461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 }
11463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
11468 * "getftime({fname})" function
11469 */
11470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *argvars;
11473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474{
11475 char_u *fname;
11476 struct stat st;
11477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479
11480 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484}
11485
11486/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011487 * "getftype({fname})" function
11488 */
11489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011491 typval_T *argvars;
11492 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011493{
11494 char_u *fname;
11495 struct stat st;
11496 char_u *type = NULL;
11497 char *t;
11498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011502 if (mch_lstat((char *)fname, &st) >= 0)
11503 {
11504#ifdef S_ISREG
11505 if (S_ISREG(st.st_mode))
11506 t = "file";
11507 else if (S_ISDIR(st.st_mode))
11508 t = "dir";
11509# ifdef S_ISLNK
11510 else if (S_ISLNK(st.st_mode))
11511 t = "link";
11512# endif
11513# ifdef S_ISBLK
11514 else if (S_ISBLK(st.st_mode))
11515 t = "bdev";
11516# endif
11517# ifdef S_ISCHR
11518 else if (S_ISCHR(st.st_mode))
11519 t = "cdev";
11520# endif
11521# ifdef S_ISFIFO
11522 else if (S_ISFIFO(st.st_mode))
11523 t = "fifo";
11524# endif
11525# ifdef S_ISSOCK
11526 else if (S_ISSOCK(st.st_mode))
11527 t = "fifo";
11528# endif
11529 else
11530 t = "other";
11531#else
11532# ifdef S_IFMT
11533 switch (st.st_mode & S_IFMT)
11534 {
11535 case S_IFREG: t = "file"; break;
11536 case S_IFDIR: t = "dir"; break;
11537# ifdef S_IFLNK
11538 case S_IFLNK: t = "link"; break;
11539# endif
11540# ifdef S_IFBLK
11541 case S_IFBLK: t = "bdev"; break;
11542# endif
11543# ifdef S_IFCHR
11544 case S_IFCHR: t = "cdev"; break;
11545# endif
11546# ifdef S_IFIFO
11547 case S_IFIFO: t = "fifo"; break;
11548# endif
11549# ifdef S_IFSOCK
11550 case S_IFSOCK: t = "socket"; break;
11551# endif
11552 default: t = "other";
11553 }
11554# else
11555 if (mch_isdir(fname))
11556 t = "dir";
11557 else
11558 t = "file";
11559# endif
11560#endif
11561 type = vim_strsave((char_u *)t);
11562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011564}
11565
11566/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011567 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011568 */
11569 static void
11570f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *argvars;
11572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011573{
11574 linenr_T lnum;
11575 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011576 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577
11578 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011579 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011581 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011582 retlist = FALSE;
11583 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011584 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011585 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011586 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011587 retlist = TRUE;
11588 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011589
Bram Moolenaar342337a2005-07-21 21:11:17 +000011590 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011591}
11592
11593/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011594 * "getmatches()" function
11595 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011596 static void
11597f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011598 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011599 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011600{
11601#ifdef FEAT_SEARCH_EXTRA
11602 dict_T *dict;
11603 matchitem_T *cur = curwin->w_match_head;
11604
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011605 if (rettv_list_alloc(rettv) == OK)
11606 {
11607 while (cur != NULL)
11608 {
11609 dict = dict_alloc();
11610 if (dict == NULL)
11611 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011612 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11613 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11614 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11615 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11616 list_append_dict(rettv->vval.v_list, dict);
11617 cur = cur->next;
11618 }
11619 }
11620#endif
11621}
11622
11623/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011624 * "getpid()" function
11625 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011626 static void
11627f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011628 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011629 typval_T *rettv;
11630{
11631 rettv->vval.v_number = mch_get_pid();
11632}
11633
11634/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011635 * "getpos(string)" function
11636 */
11637 static void
11638f_getpos(argvars, rettv)
11639 typval_T *argvars;
11640 typval_T *rettv;
11641{
11642 pos_T *fp;
11643 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011644 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011645
11646 if (rettv_list_alloc(rettv) == OK)
11647 {
11648 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011649 fp = var2fpos(&argvars[0], TRUE, &fnum);
11650 if (fnum != -1)
11651 list_append_number(l, (varnumber_T)fnum);
11652 else
11653 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011654 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11655 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011656 list_append_number(l, (fp != NULL)
11657 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011658 : (varnumber_T)0);
11659 list_append_number(l,
11660#ifdef FEAT_VIRTUALEDIT
11661 (fp != NULL) ? (varnumber_T)fp->coladd :
11662#endif
11663 (varnumber_T)0);
11664 }
11665 else
11666 rettv->vval.v_number = FALSE;
11667}
11668
11669/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011670 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011671 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011672 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011673f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011674 typval_T *argvars UNUSED;
11675 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011676{
11677#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011678 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011679#endif
11680
Bram Moolenaar2641f772005-03-25 21:58:17 +000011681#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011682 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011683 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011684 wp = NULL;
11685 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11686 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011687 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011688 if (wp == NULL)
11689 return;
11690 }
11691
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011692 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011693 }
11694#endif
11695}
11696
11697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 * "getreg()" function
11699 */
11700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011702 typval_T *argvars;
11703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704{
11705 char_u *strregname;
11706 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011707 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011708 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011710 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 strregname = get_tv_string_chk(&argvars[0]);
11713 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011714 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011715 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 regname = (strregname == NULL ? '"' : *strregname);
11720 if (regname == 0)
11721 regname = '"';
11722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011723 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 rettv->vval.v_string = error ? NULL :
11725 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726}
11727
11728/*
11729 * "getregtype()" function
11730 */
11731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011732f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011733 typval_T *argvars;
11734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735{
11736 char_u *strregname;
11737 int regname;
11738 char_u buf[NUMBUFLEN + 2];
11739 long reglen = 0;
11740
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011741 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011742 {
11743 strregname = get_tv_string_chk(&argvars[0]);
11744 if (strregname == NULL) /* type error; errmsg already given */
11745 {
11746 rettv->v_type = VAR_STRING;
11747 rettv->vval.v_string = NULL;
11748 return;
11749 }
11750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 else
11752 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011753 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754
11755 regname = (strregname == NULL ? '"' : *strregname);
11756 if (regname == 0)
11757 regname = '"';
11758
11759 buf[0] = NUL;
11760 buf[1] = NUL;
11761 switch (get_reg_type(regname, &reglen))
11762 {
11763 case MLINE: buf[0] = 'V'; break;
11764 case MCHAR: buf[0] = 'v'; break;
11765#ifdef FEAT_VISUAL
11766 case MBLOCK:
11767 buf[0] = Ctrl_V;
11768 sprintf((char *)buf + 1, "%ld", reglen + 1);
11769 break;
11770#endif
11771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772 rettv->v_type = VAR_STRING;
11773 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774}
11775
11776/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011777 * "gettabvar()" function
11778 */
11779 static void
11780f_gettabvar(argvars, rettv)
11781 typval_T *argvars;
11782 typval_T *rettv;
11783{
11784 tabpage_T *tp;
11785 dictitem_T *v;
11786 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011787 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011788
11789 rettv->v_type = VAR_STRING;
11790 rettv->vval.v_string = NULL;
11791
11792 varname = get_tv_string_chk(&argvars[1]);
11793 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11794 if (tp != NULL && varname != NULL)
11795 {
11796 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011797 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011798 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011799 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011800 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011801 done = TRUE;
11802 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011803 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011804
11805 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011806 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011807}
11808
11809/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011810 * "gettabwinvar()" function
11811 */
11812 static void
11813f_gettabwinvar(argvars, rettv)
11814 typval_T *argvars;
11815 typval_T *rettv;
11816{
11817 getwinvar(argvars, rettv, 1);
11818}
11819
11820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 * "getwinposx()" function
11822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011824f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829#ifdef FEAT_GUI
11830 if (gui.in_use)
11831 {
11832 int x, y;
11833
11834 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 }
11837#endif
11838}
11839
11840/*
11841 * "getwinposy()" function
11842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011845 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011848 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849#ifdef FEAT_GUI
11850 if (gui.in_use)
11851 {
11852 int x, y;
11853
11854 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 }
11857#endif
11858}
11859
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011860/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011861 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011862 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011863 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011864find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011865 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011866 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011867{
11868#ifdef FEAT_WINDOWS
11869 win_T *wp;
11870#endif
11871 int nr;
11872
11873 nr = get_tv_number_chk(vp, NULL);
11874
11875#ifdef FEAT_WINDOWS
11876 if (nr < 0)
11877 return NULL;
11878 if (nr == 0)
11879 return curwin;
11880
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011881 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11882 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011883 if (--nr <= 0)
11884 break;
11885 return wp;
11886#else
11887 if (nr == 0 || nr == 1)
11888 return curwin;
11889 return NULL;
11890#endif
11891}
11892
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893/*
11894 * "getwinvar()" function
11895 */
11896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011898 typval_T *argvars;
11899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011901 getwinvar(argvars, rettv, 0);
11902}
11903
11904/*
11905 * getwinvar() and gettabwinvar()
11906 */
11907 static void
11908getwinvar(argvars, rettv, off)
11909 typval_T *argvars;
11910 typval_T *rettv;
11911 int off; /* 1 for gettabwinvar() */
11912{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 win_T *win, *oldcurwin;
11914 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 dictitem_T *v;
Bram Moolenaar105bc352013-05-17 16:03:57 +020011916 tabpage_T *tp, *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011917 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011919#ifdef FEAT_WINDOWS
11920 if (off == 1)
11921 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11922 else
11923 tp = curtab;
11924#endif
11925 win = find_win_by_nr(&argvars[off], tp);
11926 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011927 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011929 rettv->v_type = VAR_STRING;
11930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931
11932 if (win != NULL && varname != NULL)
11933 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011934 /* Set curwin to be our win, temporarily. Also set the tabpage,
11935 * otherwise the window is not valid. */
11936 switch_win(&oldcurwin, &oldtabpage, win, tp);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011937
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011939 {
11940 if (get_option_tv(&varname, rettv, 1) == OK)
11941 done = TRUE;
11942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 else
11944 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011945 /* Look up the variable. */
11946 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11947 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011949 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011950 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011951 done = TRUE;
11952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011954
11955 /* restore previous notion of curwin */
Bram Moolenaar105bc352013-05-17 16:03:57 +020011956 restore_win(oldcurwin, oldtabpage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957 }
11958
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011959 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11960 /* use the default return value */
11961 copy_tv(&argvars[off + 2], rettv);
11962
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963 --emsg_off;
11964}
11965
11966/*
11967 * "glob()" function
11968 */
11969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011971 typval_T *argvars;
11972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011974 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011978 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011979 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011981 if (argvars[1].v_type != VAR_UNKNOWN)
11982 {
11983 if (get_tv_number_chk(&argvars[1], &error))
11984 options |= WILD_KEEP_ALL;
11985 if (argvars[2].v_type != VAR_UNKNOWN
11986 && get_tv_number_chk(&argvars[2], &error))
11987 {
11988 rettv->v_type = VAR_LIST;
11989 rettv->vval.v_list = NULL;
11990 }
11991 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011992 if (!error)
11993 {
11994 ExpandInit(&xpc);
11995 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011996 if (p_wic)
11997 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011998 if (rettv->v_type == VAR_STRING)
11999 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012000 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012001 else if (rettv_list_alloc(rettv) != FAIL)
12002 {
12003 int i;
12004
12005 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12006 NULL, options, WILD_ALL_KEEP);
12007 for (i = 0; i < xpc.xp_numfiles; i++)
12008 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12009
12010 ExpandCleanup(&xpc);
12011 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012012 }
12013 else
12014 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015}
12016
12017/*
12018 * "globpath()" function
12019 */
12020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 typval_T *argvars;
12023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012025 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012028 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012030 /* When the optional second argument is non-zero, don't remove matches
12031 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12032 if (argvars[2].v_type != VAR_UNKNOWN
12033 && get_tv_number_chk(&argvars[2], &error))
12034 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012036 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012037 rettv->vval.v_string = NULL;
12038 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012039 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12040 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041}
12042
12043/*
12044 * "has()" function
12045 */
12046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012048 typval_T *argvars;
12049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050{
12051 int i;
12052 char_u *name;
12053 int n = FALSE;
12054 static char *(has_list[]) =
12055 {
12056#ifdef AMIGA
12057 "amiga",
12058# ifdef FEAT_ARP
12059 "arp",
12060# endif
12061#endif
12062#ifdef __BEOS__
12063 "beos",
12064#endif
12065#ifdef MSDOS
12066# ifdef DJGPP
12067 "dos32",
12068# else
12069 "dos16",
12070# endif
12071#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012072#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 "mac",
12074#endif
12075#if defined(MACOS_X_UNIX)
12076 "macunix",
12077#endif
12078#ifdef OS2
12079 "os2",
12080#endif
12081#ifdef __QNX__
12082 "qnx",
12083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084#ifdef UNIX
12085 "unix",
12086#endif
12087#ifdef VMS
12088 "vms",
12089#endif
12090#ifdef WIN16
12091 "win16",
12092#endif
12093#ifdef WIN32
12094 "win32",
12095#endif
12096#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12097 "win32unix",
12098#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012099#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 "win64",
12101#endif
12102#ifdef EBCDIC
12103 "ebcdic",
12104#endif
12105#ifndef CASE_INSENSITIVE_FILENAME
12106 "fname_case",
12107#endif
12108#ifdef FEAT_ARABIC
12109 "arabic",
12110#endif
12111#ifdef FEAT_AUTOCMD
12112 "autocmd",
12113#endif
12114#ifdef FEAT_BEVAL
12115 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012116# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12117 "balloon_multiline",
12118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119#endif
12120#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12121 "builtin_terms",
12122# ifdef ALL_BUILTIN_TCAPS
12123 "all_builtin_terms",
12124# endif
12125#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012126#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12127 || defined(FEAT_GUI_W32) \
12128 || defined(FEAT_GUI_MOTIF))
12129 "browsefilter",
12130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131#ifdef FEAT_BYTEOFF
12132 "byte_offset",
12133#endif
12134#ifdef FEAT_CINDENT
12135 "cindent",
12136#endif
12137#ifdef FEAT_CLIENTSERVER
12138 "clientserver",
12139#endif
12140#ifdef FEAT_CLIPBOARD
12141 "clipboard",
12142#endif
12143#ifdef FEAT_CMDL_COMPL
12144 "cmdline_compl",
12145#endif
12146#ifdef FEAT_CMDHIST
12147 "cmdline_hist",
12148#endif
12149#ifdef FEAT_COMMENTS
12150 "comments",
12151#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012152#ifdef FEAT_CONCEAL
12153 "conceal",
12154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155#ifdef FEAT_CRYPT
12156 "cryptv",
12157#endif
12158#ifdef FEAT_CSCOPE
12159 "cscope",
12160#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012161#ifdef FEAT_CURSORBIND
12162 "cursorbind",
12163#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012164#ifdef CURSOR_SHAPE
12165 "cursorshape",
12166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167#ifdef DEBUG
12168 "debug",
12169#endif
12170#ifdef FEAT_CON_DIALOG
12171 "dialog_con",
12172#endif
12173#ifdef FEAT_GUI_DIALOG
12174 "dialog_gui",
12175#endif
12176#ifdef FEAT_DIFF
12177 "diff",
12178#endif
12179#ifdef FEAT_DIGRAPHS
12180 "digraphs",
12181#endif
12182#ifdef FEAT_DND
12183 "dnd",
12184#endif
12185#ifdef FEAT_EMACS_TAGS
12186 "emacs_tags",
12187#endif
12188 "eval", /* always present, of course! */
12189#ifdef FEAT_EX_EXTRA
12190 "ex_extra",
12191#endif
12192#ifdef FEAT_SEARCH_EXTRA
12193 "extra_search",
12194#endif
12195#ifdef FEAT_FKMAP
12196 "farsi",
12197#endif
12198#ifdef FEAT_SEARCHPATH
12199 "file_in_path",
12200#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012201#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012202 "filterpipe",
12203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef FEAT_FIND_ID
12205 "find_in_path",
12206#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012207#ifdef FEAT_FLOAT
12208 "float",
12209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210#ifdef FEAT_FOLDING
12211 "folding",
12212#endif
12213#ifdef FEAT_FOOTER
12214 "footer",
12215#endif
12216#if !defined(USE_SYSTEM) && defined(UNIX)
12217 "fork",
12218#endif
12219#ifdef FEAT_GETTEXT
12220 "gettext",
12221#endif
12222#ifdef FEAT_GUI
12223 "gui",
12224#endif
12225#ifdef FEAT_GUI_ATHENA
12226# ifdef FEAT_GUI_NEXTAW
12227 "gui_neXtaw",
12228# else
12229 "gui_athena",
12230# endif
12231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232#ifdef FEAT_GUI_GTK
12233 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012236#ifdef FEAT_GUI_GNOME
12237 "gui_gnome",
12238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239#ifdef FEAT_GUI_MAC
12240 "gui_mac",
12241#endif
12242#ifdef FEAT_GUI_MOTIF
12243 "gui_motif",
12244#endif
12245#ifdef FEAT_GUI_PHOTON
12246 "gui_photon",
12247#endif
12248#ifdef FEAT_GUI_W16
12249 "gui_win16",
12250#endif
12251#ifdef FEAT_GUI_W32
12252 "gui_win32",
12253#endif
12254#ifdef FEAT_HANGULIN
12255 "hangul_input",
12256#endif
12257#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12258 "iconv",
12259#endif
12260#ifdef FEAT_INS_EXPAND
12261 "insert_expand",
12262#endif
12263#ifdef FEAT_JUMPLIST
12264 "jumplist",
12265#endif
12266#ifdef FEAT_KEYMAP
12267 "keymap",
12268#endif
12269#ifdef FEAT_LANGMAP
12270 "langmap",
12271#endif
12272#ifdef FEAT_LIBCALL
12273 "libcall",
12274#endif
12275#ifdef FEAT_LINEBREAK
12276 "linebreak",
12277#endif
12278#ifdef FEAT_LISP
12279 "lispindent",
12280#endif
12281#ifdef FEAT_LISTCMDS
12282 "listcmds",
12283#endif
12284#ifdef FEAT_LOCALMAP
12285 "localmap",
12286#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012287#ifdef FEAT_LUA
12288# ifndef DYNAMIC_LUA
12289 "lua",
12290# endif
12291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292#ifdef FEAT_MENU
12293 "menu",
12294#endif
12295#ifdef FEAT_SESSION
12296 "mksession",
12297#endif
12298#ifdef FEAT_MODIFY_FNAME
12299 "modify_fname",
12300#endif
12301#ifdef FEAT_MOUSE
12302 "mouse",
12303#endif
12304#ifdef FEAT_MOUSESHAPE
12305 "mouseshape",
12306#endif
12307#if defined(UNIX) || defined(VMS)
12308# ifdef FEAT_MOUSE_DEC
12309 "mouse_dec",
12310# endif
12311# ifdef FEAT_MOUSE_GPM
12312 "mouse_gpm",
12313# endif
12314# ifdef FEAT_MOUSE_JSB
12315 "mouse_jsbterm",
12316# endif
12317# ifdef FEAT_MOUSE_NET
12318 "mouse_netterm",
12319# endif
12320# ifdef FEAT_MOUSE_PTERM
12321 "mouse_pterm",
12322# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012323# ifdef FEAT_MOUSE_SGR
12324 "mouse_sgr",
12325# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012326# ifdef FEAT_SYSMOUSE
12327 "mouse_sysmouse",
12328# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012329# ifdef FEAT_MOUSE_URXVT
12330 "mouse_urxvt",
12331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332# ifdef FEAT_MOUSE_XTERM
12333 "mouse_xterm",
12334# endif
12335#endif
12336#ifdef FEAT_MBYTE
12337 "multi_byte",
12338#endif
12339#ifdef FEAT_MBYTE_IME
12340 "multi_byte_ime",
12341#endif
12342#ifdef FEAT_MULTI_LANG
12343 "multi_lang",
12344#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012345#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012346#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012347 "mzscheme",
12348#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350#ifdef FEAT_OLE
12351 "ole",
12352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353#ifdef FEAT_PATH_EXTRA
12354 "path_extra",
12355#endif
12356#ifdef FEAT_PERL
12357#ifndef DYNAMIC_PERL
12358 "perl",
12359#endif
12360#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012361#ifdef FEAT_PERSISTENT_UNDO
12362 "persistent_undo",
12363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364#ifdef FEAT_PYTHON
12365#ifndef DYNAMIC_PYTHON
12366 "python",
12367#endif
12368#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012369#ifdef FEAT_PYTHON3
12370#ifndef DYNAMIC_PYTHON3
12371 "python3",
12372#endif
12373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374#ifdef FEAT_POSTSCRIPT
12375 "postscript",
12376#endif
12377#ifdef FEAT_PRINTER
12378 "printer",
12379#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012380#ifdef FEAT_PROFILE
12381 "profile",
12382#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012383#ifdef FEAT_RELTIME
12384 "reltime",
12385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386#ifdef FEAT_QUICKFIX
12387 "quickfix",
12388#endif
12389#ifdef FEAT_RIGHTLEFT
12390 "rightleft",
12391#endif
12392#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12393 "ruby",
12394#endif
12395#ifdef FEAT_SCROLLBIND
12396 "scrollbind",
12397#endif
12398#ifdef FEAT_CMDL_INFO
12399 "showcmd",
12400 "cmdline_info",
12401#endif
12402#ifdef FEAT_SIGNS
12403 "signs",
12404#endif
12405#ifdef FEAT_SMARTINDENT
12406 "smartindent",
12407#endif
12408#ifdef FEAT_SNIFF
12409 "sniff",
12410#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012411#ifdef STARTUPTIME
12412 "startuptime",
12413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414#ifdef FEAT_STL_OPT
12415 "statusline",
12416#endif
12417#ifdef FEAT_SUN_WORKSHOP
12418 "sun_workshop",
12419#endif
12420#ifdef FEAT_NETBEANS_INTG
12421 "netbeans_intg",
12422#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012423#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012424 "spell",
12425#endif
12426#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 "syntax",
12428#endif
12429#if defined(USE_SYSTEM) || !defined(UNIX)
12430 "system",
12431#endif
12432#ifdef FEAT_TAG_BINS
12433 "tag_binary",
12434#endif
12435#ifdef FEAT_TAG_OLDSTATIC
12436 "tag_old_static",
12437#endif
12438#ifdef FEAT_TAG_ANYWHITE
12439 "tag_any_white",
12440#endif
12441#ifdef FEAT_TCL
12442# ifndef DYNAMIC_TCL
12443 "tcl",
12444# endif
12445#endif
12446#ifdef TERMINFO
12447 "terminfo",
12448#endif
12449#ifdef FEAT_TERMRESPONSE
12450 "termresponse",
12451#endif
12452#ifdef FEAT_TEXTOBJ
12453 "textobjects",
12454#endif
12455#ifdef HAVE_TGETENT
12456 "tgetent",
12457#endif
12458#ifdef FEAT_TITLE
12459 "title",
12460#endif
12461#ifdef FEAT_TOOLBAR
12462 "toolbar",
12463#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012464#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12465 "unnamedplus",
12466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467#ifdef FEAT_USR_CMDS
12468 "user-commands", /* was accidentally included in 5.4 */
12469 "user_commands",
12470#endif
12471#ifdef FEAT_VIMINFO
12472 "viminfo",
12473#endif
12474#ifdef FEAT_VERTSPLIT
12475 "vertsplit",
12476#endif
12477#ifdef FEAT_VIRTUALEDIT
12478 "virtualedit",
12479#endif
12480#ifdef FEAT_VISUAL
12481 "visual",
12482#endif
12483#ifdef FEAT_VISUALEXTRA
12484 "visualextra",
12485#endif
12486#ifdef FEAT_VREPLACE
12487 "vreplace",
12488#endif
12489#ifdef FEAT_WILDIGN
12490 "wildignore",
12491#endif
12492#ifdef FEAT_WILDMENU
12493 "wildmenu",
12494#endif
12495#ifdef FEAT_WINDOWS
12496 "windows",
12497#endif
12498#ifdef FEAT_WAK
12499 "winaltkeys",
12500#endif
12501#ifdef FEAT_WRITEBACKUP
12502 "writebackup",
12503#endif
12504#ifdef FEAT_XIM
12505 "xim",
12506#endif
12507#ifdef FEAT_XFONTSET
12508 "xfontset",
12509#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012510#ifdef FEAT_XPM_W32
12511 "xpm_w32",
12512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513#ifdef USE_XSMP
12514 "xsmp",
12515#endif
12516#ifdef USE_XSMP_INTERACT
12517 "xsmp_interact",
12518#endif
12519#ifdef FEAT_XCLIPBOARD
12520 "xterm_clipboard",
12521#endif
12522#ifdef FEAT_XTERM_SAVE
12523 "xterm_save",
12524#endif
12525#if defined(UNIX) && defined(FEAT_X11)
12526 "X11",
12527#endif
12528 NULL
12529 };
12530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 for (i = 0; has_list[i] != NULL; ++i)
12533 if (STRICMP(name, has_list[i]) == 0)
12534 {
12535 n = TRUE;
12536 break;
12537 }
12538
12539 if (n == FALSE)
12540 {
12541 if (STRNICMP(name, "patch", 5) == 0)
12542 n = has_patch(atoi((char *)name + 5));
12543 else if (STRICMP(name, "vim_starting") == 0)
12544 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012545#ifdef FEAT_MBYTE
12546 else if (STRICMP(name, "multi_byte_encoding") == 0)
12547 n = has_mbyte;
12548#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012549#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12550 else if (STRICMP(name, "balloon_multiline") == 0)
12551 n = multiline_balloon_available();
12552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553#ifdef DYNAMIC_TCL
12554 else if (STRICMP(name, "tcl") == 0)
12555 n = tcl_enabled(FALSE);
12556#endif
12557#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12558 else if (STRICMP(name, "iconv") == 0)
12559 n = iconv_enabled(FALSE);
12560#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012561#ifdef DYNAMIC_LUA
12562 else if (STRICMP(name, "lua") == 0)
12563 n = lua_enabled(FALSE);
12564#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012565#ifdef DYNAMIC_MZSCHEME
12566 else if (STRICMP(name, "mzscheme") == 0)
12567 n = mzscheme_enabled(FALSE);
12568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569#ifdef DYNAMIC_RUBY
12570 else if (STRICMP(name, "ruby") == 0)
12571 n = ruby_enabled(FALSE);
12572#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012573#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574#ifdef DYNAMIC_PYTHON
12575 else if (STRICMP(name, "python") == 0)
12576 n = python_enabled(FALSE);
12577#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012578#endif
12579#ifdef FEAT_PYTHON3
12580#ifdef DYNAMIC_PYTHON3
12581 else if (STRICMP(name, "python3") == 0)
12582 n = python3_enabled(FALSE);
12583#endif
12584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585#ifdef DYNAMIC_PERL
12586 else if (STRICMP(name, "perl") == 0)
12587 n = perl_enabled(FALSE);
12588#endif
12589#ifdef FEAT_GUI
12590 else if (STRICMP(name, "gui_running") == 0)
12591 n = (gui.in_use || gui.starting);
12592# ifdef FEAT_GUI_W32
12593 else if (STRICMP(name, "gui_win32s") == 0)
12594 n = gui_is_win32s();
12595# endif
12596# ifdef FEAT_BROWSE
12597 else if (STRICMP(name, "browse") == 0)
12598 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12599# endif
12600#endif
12601#ifdef FEAT_SYN_HL
12602 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012603 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604#endif
12605#if defined(WIN3264)
12606 else if (STRICMP(name, "win95") == 0)
12607 n = mch_windows95();
12608#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012609#ifdef FEAT_NETBEANS_INTG
12610 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012611 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 }
12614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012615 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616}
12617
12618/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012619 * "has_key()" function
12620 */
12621 static void
12622f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012623 typval_T *argvars;
12624 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012625{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012626 if (argvars[0].v_type != VAR_DICT)
12627 {
12628 EMSG(_(e_dictreq));
12629 return;
12630 }
12631 if (argvars[0].vval.v_dict == NULL)
12632 return;
12633
12634 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012635 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012636}
12637
12638/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012639 * "haslocaldir()" function
12640 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012641 static void
12642f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012643 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012644 typval_T *rettv;
12645{
12646 rettv->vval.v_number = (curwin->w_localdir != NULL);
12647}
12648
12649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 * "hasmapto()" function
12651 */
12652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012654 typval_T *argvars;
12655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656{
12657 char_u *name;
12658 char_u *mode;
12659 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012660 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012663 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 mode = (char_u *)"nvo";
12665 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012668 if (argvars[2].v_type != VAR_UNKNOWN)
12669 abbr = get_tv_number(&argvars[2]);
12670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671
Bram Moolenaar2c932302006-03-18 21:42:09 +000012672 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676}
12677
12678/*
12679 * "histadd()" function
12680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685{
12686#ifdef FEAT_CMDHIST
12687 int histype;
12688 char_u *str;
12689 char_u buf[NUMBUFLEN];
12690#endif
12691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 if (check_restricted() || check_secure())
12694 return;
12695#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012696 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12697 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 if (histype >= 0)
12699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 if (*str != NUL)
12702 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012703 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 return;
12707 }
12708 }
12709#endif
12710}
12711
12712/*
12713 * "histdel()" function
12714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012717 typval_T *argvars UNUSED;
12718 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719{
12720#ifdef FEAT_CMDHIST
12721 int n;
12722 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012725 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12726 if (str == NULL)
12727 n = 0;
12728 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012731 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012733 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 else
12736 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 get_tv_string_buf(&argvars[1], buf));
12739 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740#endif
12741}
12742
12743/*
12744 * "histget()" function
12745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012748 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750{
12751#ifdef FEAT_CMDHIST
12752 int type;
12753 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12757 if (str == NULL)
12758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 {
12761 type = get_histtype(str);
12762 if (argvars[1].v_type == VAR_UNKNOWN)
12763 idx = get_history_idx(type);
12764 else
12765 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12766 /* -1 on type error */
12767 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773}
12774
12775/*
12776 * "histnr()" function
12777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782{
12783 int i;
12784
12785#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786 char_u *history = get_tv_string_chk(&argvars[0]);
12787
12788 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 if (i >= HIST_CMD && i < HIST_COUNT)
12790 i = get_history_idx(i);
12791 else
12792#endif
12793 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795}
12796
12797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 * "highlightID(name)" function
12799 */
12800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012802 typval_T *argvars;
12803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806}
12807
12808/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012809 * "highlight_exists()" function
12810 */
12811 static void
12812f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012813 typval_T *argvars;
12814 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012815{
12816 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12817}
12818
12819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 * "hostname()" function
12821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012824 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
12827 char_u hostname[256];
12828
12829 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832}
12833
12834/*
12835 * iconv() function
12836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841{
12842#ifdef FEAT_MBYTE
12843 char_u buf1[NUMBUFLEN];
12844 char_u buf2[NUMBUFLEN];
12845 char_u *from, *to, *str;
12846 vimconv_T vimconv;
12847#endif
12848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->v_type = VAR_STRING;
12850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851
12852#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 str = get_tv_string(&argvars[0]);
12854 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12855 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 vimconv.vc_type = CONV_NONE;
12857 convert_setup(&vimconv, from, to);
12858
12859 /* If the encodings are equal, no conversion needed. */
12860 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864
12865 convert_setup(&vimconv, NULL, NULL);
12866 vim_free(from);
12867 vim_free(to);
12868#endif
12869}
12870
12871/*
12872 * "indent()" function
12873 */
12874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012876 typval_T *argvars;
12877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878{
12879 linenr_T lnum;
12880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886}
12887
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012888/*
12889 * "index()" function
12890 */
12891 static void
12892f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012893 typval_T *argvars;
12894 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012895{
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 list_T *l;
12897 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012898 long idx = 0;
12899 int ic = FALSE;
12900
12901 rettv->vval.v_number = -1;
12902 if (argvars[0].v_type != VAR_LIST)
12903 {
12904 EMSG(_(e_listreq));
12905 return;
12906 }
12907 l = argvars[0].vval.v_list;
12908 if (l != NULL)
12909 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012910 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012911 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012913 int error = FALSE;
12914
Bram Moolenaar758711c2005-02-02 23:11:38 +000012915 /* Start at specified item. Use the cached index that list_find()
12916 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012917 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012918 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012919 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012920 ic = get_tv_number_chk(&argvars[3], &error);
12921 if (error)
12922 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012923 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012924
Bram Moolenaar758711c2005-02-02 23:11:38 +000012925 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012926 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012927 {
12928 rettv->vval.v_number = idx;
12929 break;
12930 }
12931 }
12932}
12933
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934static int inputsecret_flag = 0;
12935
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012936static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12937
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012939 * This function is used by f_input() and f_inputdialog() functions. The third
12940 * argument to f_input() specifies the type of completion to use at the
12941 * prompt. The third argument to f_inputdialog() specifies the value to return
12942 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943 */
12944 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012945get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012946 typval_T *argvars;
12947 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012948 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012950 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 char_u *p = NULL;
12952 int c;
12953 char_u buf[NUMBUFLEN];
12954 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012955 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012956 int xp_type = EXPAND_NOTHING;
12957 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012960 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961
12962#ifdef NO_CONSOLE_INPUT
12963 /* While starting up, there is no place to enter text. */
12964 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966#endif
12967
12968 cmd_silent = FALSE; /* Want to see the prompt. */
12969 if (prompt != NULL)
12970 {
12971 /* Only the part of the message after the last NL is considered as
12972 * prompt for the command line */
12973 p = vim_strrchr(prompt, '\n');
12974 if (p == NULL)
12975 p = prompt;
12976 else
12977 {
12978 ++p;
12979 c = *p;
12980 *p = NUL;
12981 msg_start();
12982 msg_clr_eos();
12983 msg_puts_attr(prompt, echo_attr);
12984 msg_didout = FALSE;
12985 msg_starthere();
12986 *p = c;
12987 }
12988 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012990 if (argvars[1].v_type != VAR_UNKNOWN)
12991 {
12992 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12993 if (defstr != NULL)
12994 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012996 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012997 {
12998 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012999 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013000 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013001
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013002 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013003 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013004
Bram Moolenaar4463f292005-09-25 22:20:24 +000013005 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13006 if (xp_name == NULL)
13007 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013008
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013009 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013010
Bram Moolenaar4463f292005-09-25 22:20:24 +000013011 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13012 &xp_arg) == FAIL)
13013 return;
13014 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013015 }
13016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017 if (defstr != NULL)
13018 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013019 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13020 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013021 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013022 && argvars[1].v_type != VAR_UNKNOWN
13023 && argvars[2].v_type != VAR_UNKNOWN)
13024 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13025 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013026
13027 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 /* since the user typed this, no need to wait for return */
13030 need_wait_return = FALSE;
13031 msg_didout = FALSE;
13032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 cmd_silent = cmd_silent_save;
13034}
13035
13036/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013037 * "input()" function
13038 * Also handles inputsecret() when inputsecret is set.
13039 */
13040 static void
13041f_input(argvars, rettv)
13042 typval_T *argvars;
13043 typval_T *rettv;
13044{
13045 get_user_input(argvars, rettv, FALSE);
13046}
13047
13048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 * "inputdialog()" function
13050 */
13051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013053 typval_T *argvars;
13054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055{
13056#if defined(FEAT_GUI_TEXTDIALOG)
13057 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13058 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13059 {
13060 char_u *message;
13061 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013064 message = get_tv_string_chk(&argvars[0]);
13065 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013066 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013067 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 else
13069 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070 if (message != NULL && defstr != NULL
13071 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013072 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 else
13075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013076 if (message != NULL && defstr != NULL
13077 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013078 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->vval.v_string = vim_strsave(
13080 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013084 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085 }
13086 else
13087#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013088 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089}
13090
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013091/*
13092 * "inputlist()" function
13093 */
13094 static void
13095f_inputlist(argvars, rettv)
13096 typval_T *argvars;
13097 typval_T *rettv;
13098{
13099 listitem_T *li;
13100 int selected;
13101 int mouse_used;
13102
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013103#ifdef NO_CONSOLE_INPUT
13104 /* While starting up, there is no place to enter text. */
13105 if (no_console_input())
13106 return;
13107#endif
13108 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13109 {
13110 EMSG2(_(e_listarg), "inputlist()");
13111 return;
13112 }
13113
13114 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013115 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013116 lines_left = Rows; /* avoid more prompt */
13117 msg_scroll = TRUE;
13118 msg_clr_eos();
13119
13120 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13121 {
13122 msg_puts(get_tv_string(&li->li_tv));
13123 msg_putchar('\n');
13124 }
13125
13126 /* Ask for choice. */
13127 selected = prompt_for_number(&mouse_used);
13128 if (mouse_used)
13129 selected -= lines_left;
13130
13131 rettv->vval.v_number = selected;
13132}
13133
13134
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13136
13137/*
13138 * "inputrestore()" function
13139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013142 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144{
13145 if (ga_userinput.ga_len > 0)
13146 {
13147 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13149 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013150 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 }
13152 else if (p_verbose > 1)
13153 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013154 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 }
13157}
13158
13159/*
13160 * "inputsave()" function
13161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013167 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 if (ga_grow(&ga_userinput, 1) == OK)
13169 {
13170 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13171 + ga_userinput.ga_len);
13172 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013173 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 }
13175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177}
13178
13179/*
13180 * "inputsecret()" function
13181 */
13182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013183f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013184 typval_T *argvars;
13185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186{
13187 ++cmdline_star;
13188 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 --cmdline_star;
13191 --inputsecret_flag;
13192}
13193
13194/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195 * "insert()" function
13196 */
13197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013199 typval_T *argvars;
13200 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013201{
13202 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 listitem_T *item;
13204 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013205 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013206
13207 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013208 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013209 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013210 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013211 {
13212 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013213 before = get_tv_number_chk(&argvars[2], &error);
13214 if (error)
13215 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013216
Bram Moolenaar758711c2005-02-02 23:11:38 +000013217 if (before == l->lv_len)
13218 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013219 else
13220 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013221 item = list_find(l, before);
13222 if (item == NULL)
13223 {
13224 EMSGN(_(e_listidx), before);
13225 l = NULL;
13226 }
13227 }
13228 if (l != NULL)
13229 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013230 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013231 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013232 }
13233 }
13234}
13235
13236/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013237 * "invert(expr)" function
13238 */
13239 static void
13240f_invert(argvars, rettv)
13241 typval_T *argvars;
13242 typval_T *rettv;
13243{
13244 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13245}
13246
13247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 * "isdirectory()" function
13249 */
13250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013252 typval_T *argvars;
13253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256}
13257
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013258/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013259 * "islocked()" function
13260 */
13261 static void
13262f_islocked(argvars, rettv)
13263 typval_T *argvars;
13264 typval_T *rettv;
13265{
13266 lval_T lv;
13267 char_u *end;
13268 dictitem_T *di;
13269
13270 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013271 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13272 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013273 if (end != NULL && lv.ll_name != NULL)
13274 {
13275 if (*end != NUL)
13276 EMSG(_(e_trailing));
13277 else
13278 {
13279 if (lv.ll_tv == NULL)
13280 {
13281 if (check_changedtick(lv.ll_name))
13282 rettv->vval.v_number = 1; /* always locked */
13283 else
13284 {
13285 di = find_var(lv.ll_name, NULL);
13286 if (di != NULL)
13287 {
13288 /* Consider a variable locked when:
13289 * 1. the variable itself is locked
13290 * 2. the value of the variable is locked.
13291 * 3. the List or Dict value is locked.
13292 */
13293 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13294 || tv_islocked(&di->di_tv));
13295 }
13296 }
13297 }
13298 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013299 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013300 else if (lv.ll_newkey != NULL)
13301 EMSG2(_(e_dictkey), lv.ll_newkey);
13302 else if (lv.ll_list != NULL)
13303 /* List item. */
13304 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13305 else
13306 /* Dictionary item. */
13307 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13308 }
13309 }
13310
13311 clear_lval(&lv);
13312}
13313
Bram Moolenaar33570922005-01-25 22:26:29 +000013314static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013315
13316/*
13317 * Turn a dict into a list:
13318 * "what" == 0: list of keys
13319 * "what" == 1: list of values
13320 * "what" == 2: list of items
13321 */
13322 static void
13323dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013324 typval_T *argvars;
13325 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013326 int what;
13327{
Bram Moolenaar33570922005-01-25 22:26:29 +000013328 list_T *l2;
13329 dictitem_T *di;
13330 hashitem_T *hi;
13331 listitem_T *li;
13332 listitem_T *li2;
13333 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013334 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013335
Bram Moolenaar8c711452005-01-14 21:53:12 +000013336 if (argvars[0].v_type != VAR_DICT)
13337 {
13338 EMSG(_(e_dictreq));
13339 return;
13340 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013341 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013342 return;
13343
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013344 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013345 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013346
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013347 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013348 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013349 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013350 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013351 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013352 --todo;
13353 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013354
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013355 li = listitem_alloc();
13356 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013357 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013358 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013359
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013360 if (what == 0)
13361 {
13362 /* keys() */
13363 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013364 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013365 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13366 }
13367 else if (what == 1)
13368 {
13369 /* values() */
13370 copy_tv(&di->di_tv, &li->li_tv);
13371 }
13372 else
13373 {
13374 /* items() */
13375 l2 = list_alloc();
13376 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013377 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013378 li->li_tv.vval.v_list = l2;
13379 if (l2 == NULL)
13380 break;
13381 ++l2->lv_refcount;
13382
13383 li2 = listitem_alloc();
13384 if (li2 == NULL)
13385 break;
13386 list_append(l2, li2);
13387 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013388 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013389 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13390
13391 li2 = listitem_alloc();
13392 if (li2 == NULL)
13393 break;
13394 list_append(l2, li2);
13395 copy_tv(&di->di_tv, &li2->li_tv);
13396 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013397 }
13398 }
13399}
13400
13401/*
13402 * "items(dict)" function
13403 */
13404 static void
13405f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013406 typval_T *argvars;
13407 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013408{
13409 dict_list(argvars, rettv, 2);
13410}
13411
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013413 * "join()" function
13414 */
13415 static void
13416f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 typval_T *argvars;
13418 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013419{
13420 garray_T ga;
13421 char_u *sep;
13422
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013423 if (argvars[0].v_type != VAR_LIST)
13424 {
13425 EMSG(_(e_listreq));
13426 return;
13427 }
13428 if (argvars[0].vval.v_list == NULL)
13429 return;
13430 if (argvars[1].v_type == VAR_UNKNOWN)
13431 sep = (char_u *)" ";
13432 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013433 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013434
13435 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436
13437 if (sep != NULL)
13438 {
13439 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013440 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013441 ga_append(&ga, NUL);
13442 rettv->vval.v_string = (char_u *)ga.ga_data;
13443 }
13444 else
13445 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013446}
13447
13448/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013449 * "keys()" function
13450 */
13451 static void
13452f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 typval_T *argvars;
13454 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013455{
13456 dict_list(argvars, rettv, 0);
13457}
13458
13459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 * "last_buffer_nr()" function.
13461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466{
13467 int n = 0;
13468 buf_T *buf;
13469
13470 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13471 if (n < buf->b_fnum)
13472 n = buf->b_fnum;
13473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475}
13476
13477/*
13478 * "len()" function
13479 */
13480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 typval_T *argvars;
13483 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013484{
13485 switch (argvars[0].v_type)
13486 {
13487 case VAR_STRING:
13488 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013489 rettv->vval.v_number = (varnumber_T)STRLEN(
13490 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013491 break;
13492 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013494 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013495 case VAR_DICT:
13496 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13497 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013498 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013499 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013500 break;
13501 }
13502}
13503
Bram Moolenaar33570922005-01-25 22:26:29 +000013504static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013505
13506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013508 typval_T *argvars;
13509 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013510 int type;
13511{
13512#ifdef FEAT_LIBCALL
13513 char_u *string_in;
13514 char_u **string_result;
13515 int nr_result;
13516#endif
13517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013519 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013521
13522 if (check_restricted() || check_secure())
13523 return;
13524
13525#ifdef FEAT_LIBCALL
13526 /* The first two args must be strings, otherwise its meaningless */
13527 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13528 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013529 string_in = NULL;
13530 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013531 string_in = argvars[2].vval.v_string;
13532 if (type == VAR_NUMBER)
13533 string_result = NULL;
13534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013536 if (mch_libcall(argvars[0].vval.v_string,
13537 argvars[1].vval.v_string,
13538 string_in,
13539 argvars[2].vval.v_number,
13540 string_result,
13541 &nr_result) == OK
13542 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013544 }
13545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546}
13547
13548/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549 * "libcall()" function
13550 */
13551 static void
13552f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013553 typval_T *argvars;
13554 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013555{
13556 libcall_common(argvars, rettv, VAR_STRING);
13557}
13558
13559/*
13560 * "libcallnr()" function
13561 */
13562 static void
13563f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *argvars;
13565 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013566{
13567 libcall_common(argvars, rettv, VAR_NUMBER);
13568}
13569
13570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 * "line(string)" function
13572 */
13573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 typval_T *argvars;
13576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577{
13578 linenr_T lnum = 0;
13579 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013580 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013582 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 if (fp != NULL)
13584 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
13588/*
13589 * "line2byte(lnum)" function
13590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013593 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
13596#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598#else
13599 linenr_T lnum;
13600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13606 if (rettv->vval.v_number >= 0)
13607 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608#endif
13609}
13610
13611/*
13612 * "lispindent(lnum)" function
13613 */
13614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013616 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618{
13619#ifdef FEAT_LISP
13620 pos_T pos;
13621 linenr_T lnum;
13622
13623 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13626 {
13627 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 curwin->w_cursor = pos;
13630 }
13631 else
13632#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634}
13635
13636/*
13637 * "localtime()" function
13638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013641 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645}
13646
Bram Moolenaar33570922005-01-25 22:26:29 +000013647static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648
13649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013651 typval_T *argvars;
13652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 int exact;
13654{
13655 char_u *keys;
13656 char_u *which;
13657 char_u buf[NUMBUFLEN];
13658 char_u *keys_buf = NULL;
13659 char_u *rhs;
13660 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013661 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013662 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013663 mapblock_T *mp;
13664 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665
13666 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->v_type = VAR_STRING;
13668 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 if (*keys == NUL)
13672 return;
13673
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013674 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013677 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013678 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013679 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013680 if (argvars[3].v_type != VAR_UNKNOWN)
13681 get_dict = get_tv_number(&argvars[3]);
13682 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684 else
13685 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013686 if (which == NULL)
13687 return;
13688
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 mode = get_map_mode(&which, 0);
13690
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013691 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013692 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013694
13695 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013697 /* Return a string. */
13698 if (rhs != NULL)
13699 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700
Bram Moolenaarbd743252010-10-20 21:23:33 +020013701 }
13702 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13703 {
13704 /* Return a dictionary. */
13705 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13706 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13707 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708
Bram Moolenaarbd743252010-10-20 21:23:33 +020013709 dict_add_nr_str(dict, "lhs", 0L, lhs);
13710 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13711 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13712 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13713 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13714 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13715 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13716 dict_add_nr_str(dict, "mode", 0L, mapmode);
13717
13718 vim_free(lhs);
13719 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 }
13721}
13722
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013723#ifdef FEAT_FLOAT
13724/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013725 * "log()" function
13726 */
13727 static void
13728f_log(argvars, rettv)
13729 typval_T *argvars;
13730 typval_T *rettv;
13731{
13732 float_T f;
13733
13734 rettv->v_type = VAR_FLOAT;
13735 if (get_float_arg(argvars, &f) == OK)
13736 rettv->vval.v_float = log(f);
13737 else
13738 rettv->vval.v_float = 0.0;
13739}
13740
13741/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013742 * "log10()" function
13743 */
13744 static void
13745f_log10(argvars, rettv)
13746 typval_T *argvars;
13747 typval_T *rettv;
13748{
13749 float_T f;
13750
13751 rettv->v_type = VAR_FLOAT;
13752 if (get_float_arg(argvars, &f) == OK)
13753 rettv->vval.v_float = log10(f);
13754 else
13755 rettv->vval.v_float = 0.0;
13756}
13757#endif
13758
Bram Moolenaar1dced572012-04-05 16:54:08 +020013759#ifdef FEAT_LUA
13760/*
13761 * "luaeval()" function
13762 */
13763 static void
13764f_luaeval(argvars, rettv)
13765 typval_T *argvars;
13766 typval_T *rettv;
13767{
13768 char_u *str;
13769 char_u buf[NUMBUFLEN];
13770
13771 str = get_tv_string_buf(&argvars[0], buf);
13772 do_luaeval(str, argvars + 1, rettv);
13773}
13774#endif
13775
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013777 * "map()" function
13778 */
13779 static void
13780f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013781 typval_T *argvars;
13782 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013783{
13784 filter_map(argvars, rettv, TRUE);
13785}
13786
13787/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013788 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789 */
13790 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013791f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 typval_T *argvars;
13793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013795 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796}
13797
13798/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 */
13801 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013802f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013803 typval_T *argvars;
13804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013806 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807}
13808
Bram Moolenaar33570922005-01-25 22:26:29 +000013809static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810
13811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 typval_T *argvars;
13814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 int type;
13816{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013817 char_u *str = NULL;
13818 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 char_u *pat;
13820 regmatch_T regmatch;
13821 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013822 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 char_u *save_cpo;
13824 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013825 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013826 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013827 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013828 list_T *l = NULL;
13829 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013830 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013831 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832
13833 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13834 save_cpo = p_cpo;
13835 p_cpo = (char_u *)"";
13836
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013837 rettv->vval.v_number = -1;
13838 if (type == 3)
13839 {
13840 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013841 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013842 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013843 }
13844 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013846 rettv->v_type = VAR_STRING;
13847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013850 if (argvars[0].v_type == VAR_LIST)
13851 {
13852 if ((l = argvars[0].vval.v_list) == NULL)
13853 goto theend;
13854 li = l->lv_first;
13855 }
13856 else
13857 expr = str = get_tv_string(&argvars[0]);
13858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13860 if (pat == NULL)
13861 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013862
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013863 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 int error = FALSE;
13866
13867 start = get_tv_number_chk(&argvars[2], &error);
13868 if (error)
13869 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013870 if (l != NULL)
13871 {
13872 li = list_find(l, start);
13873 if (li == NULL)
13874 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013875 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013876 }
13877 else
13878 {
13879 if (start < 0)
13880 start = 0;
13881 if (start > (long)STRLEN(str))
13882 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013883 /* When "count" argument is there ignore matches before "start",
13884 * otherwise skip part of the string. Differs when pattern is "^"
13885 * or "\<". */
13886 if (argvars[3].v_type != VAR_UNKNOWN)
13887 startcol = start;
13888 else
13889 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013890 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013891
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013892 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893 nth = get_tv_number_chk(&argvars[3], &error);
13894 if (error)
13895 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 }
13897
13898 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13899 if (regmatch.regprog != NULL)
13900 {
13901 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013902
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013903 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013904 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 if (l != NULL)
13906 {
13907 if (li == NULL)
13908 {
13909 match = FALSE;
13910 break;
13911 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013912 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013913 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013914 if (str == NULL)
13915 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013916 }
13917
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013918 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013919
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013921 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013922 if (l == NULL && !match)
13923 break;
13924
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013925 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013926 if (l != NULL)
13927 {
13928 li = li->li_next;
13929 ++idx;
13930 }
13931 else
13932 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013933#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013934 startcol = (colnr_T)(regmatch.startp[0]
13935 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013936#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013937 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013938#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013939 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013940 }
13941
13942 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013944 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013945 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013946 int i;
13947
13948 /* return list with matched string and submatches */
13949 for (i = 0; i < NSUBEXP; ++i)
13950 {
13951 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013952 {
13953 if (list_append_string(rettv->vval.v_list,
13954 (char_u *)"", 0) == FAIL)
13955 break;
13956 }
13957 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013958 regmatch.startp[i],
13959 (int)(regmatch.endp[i] - regmatch.startp[i]))
13960 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013961 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013962 }
13963 }
13964 else if (type == 2)
13965 {
13966 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013967 if (l != NULL)
13968 copy_tv(&li->li_tv, rettv);
13969 else
13970 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013972 }
13973 else if (l != NULL)
13974 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 else
13976 {
13977 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 (varnumber_T)(regmatch.startp[0] - str);
13980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013983 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 }
13985 }
Bram Moolenaar473de612013-06-08 18:19:48 +020013986 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 }
13988
13989theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013990 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991 p_cpo = save_cpo;
13992}
13993
13994/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013995 * "match()" function
13996 */
13997 static void
13998f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013999 typval_T *argvars;
14000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014001{
14002 find_some_match(argvars, rettv, 1);
14003}
14004
14005/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014006 * "matchadd()" function
14007 */
14008 static void
14009f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014010 typval_T *argvars UNUSED;
14011 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014012{
14013#ifdef FEAT_SEARCH_EXTRA
14014 char_u buf[NUMBUFLEN];
14015 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14016 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14017 int prio = 10; /* default priority */
14018 int id = -1;
14019 int error = FALSE;
14020
14021 rettv->vval.v_number = -1;
14022
14023 if (grp == NULL || pat == NULL)
14024 return;
14025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014026 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014027 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014028 if (argvars[3].v_type != VAR_UNKNOWN)
14029 id = get_tv_number_chk(&argvars[3], &error);
14030 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014031 if (error == TRUE)
14032 return;
14033 if (id >= 1 && id <= 3)
14034 {
14035 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14036 return;
14037 }
14038
14039 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14040#endif
14041}
14042
14043/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014044 * "matcharg()" function
14045 */
14046 static void
14047f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014048 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014049 typval_T *rettv;
14050{
14051 if (rettv_list_alloc(rettv) == OK)
14052 {
14053#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014054 int id = get_tv_number(&argvars[0]);
14055 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014056
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014057 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014058 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014059 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14060 {
14061 list_append_string(rettv->vval.v_list,
14062 syn_id2name(m->hlg_id), -1);
14063 list_append_string(rettv->vval.v_list, m->pattern, -1);
14064 }
14065 else
14066 {
14067 list_append_string(rettv->vval.v_list, NUL, -1);
14068 list_append_string(rettv->vval.v_list, NUL, -1);
14069 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014070 }
14071#endif
14072 }
14073}
14074
14075/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014076 * "matchdelete()" function
14077 */
14078 static void
14079f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014080 typval_T *argvars UNUSED;
14081 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014082{
14083#ifdef FEAT_SEARCH_EXTRA
14084 rettv->vval.v_number = match_delete(curwin,
14085 (int)get_tv_number(&argvars[0]), TRUE);
14086#endif
14087}
14088
14089/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090 * "matchend()" function
14091 */
14092 static void
14093f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014094 typval_T *argvars;
14095 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096{
14097 find_some_match(argvars, rettv, 0);
14098}
14099
14100/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014101 * "matchlist()" function
14102 */
14103 static void
14104f_matchlist(argvars, rettv)
14105 typval_T *argvars;
14106 typval_T *rettv;
14107{
14108 find_some_match(argvars, rettv, 3);
14109}
14110
14111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112 * "matchstr()" function
14113 */
14114 static void
14115f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 typval_T *argvars;
14117 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014118{
14119 find_some_match(argvars, rettv, 2);
14120}
14121
Bram Moolenaar33570922005-01-25 22:26:29 +000014122static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014123
14124 static void
14125max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014126 typval_T *argvars;
14127 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014128 int domax;
14129{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014130 long n = 0;
14131 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014133
14134 if (argvars[0].v_type == VAR_LIST)
14135 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014136 list_T *l;
14137 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014138
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014139 l = argvars[0].vval.v_list;
14140 if (l != NULL)
14141 {
14142 li = l->lv_first;
14143 if (li != NULL)
14144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014146 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014147 {
14148 li = li->li_next;
14149 if (li == NULL)
14150 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014152 if (domax ? i > n : i < n)
14153 n = i;
14154 }
14155 }
14156 }
14157 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014158 else if (argvars[0].v_type == VAR_DICT)
14159 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014161 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014162 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014163 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014164
14165 d = argvars[0].vval.v_dict;
14166 if (d != NULL)
14167 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014168 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014170 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014171 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014172 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014173 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014175 if (first)
14176 {
14177 n = i;
14178 first = FALSE;
14179 }
14180 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014181 n = i;
14182 }
14183 }
14184 }
14185 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014186 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014187 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014189}
14190
14191/*
14192 * "max()" function
14193 */
14194 static void
14195f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014196 typval_T *argvars;
14197 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014198{
14199 max_min(argvars, rettv, TRUE);
14200}
14201
14202/*
14203 * "min()" function
14204 */
14205 static void
14206f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014207 typval_T *argvars;
14208 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014209{
14210 max_min(argvars, rettv, FALSE);
14211}
14212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014213static int mkdir_recurse __ARGS((char_u *dir, int prot));
14214
14215/*
14216 * Create the directory in which "dir" is located, and higher levels when
14217 * needed.
14218 */
14219 static int
14220mkdir_recurse(dir, prot)
14221 char_u *dir;
14222 int prot;
14223{
14224 char_u *p;
14225 char_u *updir;
14226 int r = FAIL;
14227
14228 /* Get end of directory name in "dir".
14229 * We're done when it's "/" or "c:/". */
14230 p = gettail_sep(dir);
14231 if (p <= get_past_head(dir))
14232 return OK;
14233
14234 /* If the directory exists we're done. Otherwise: create it.*/
14235 updir = vim_strnsave(dir, (int)(p - dir));
14236 if (updir == NULL)
14237 return FAIL;
14238 if (mch_isdir(updir))
14239 r = OK;
14240 else if (mkdir_recurse(updir, prot) == OK)
14241 r = vim_mkdir_emsg(updir, prot);
14242 vim_free(updir);
14243 return r;
14244}
14245
14246#ifdef vim_mkdir
14247/*
14248 * "mkdir()" function
14249 */
14250 static void
14251f_mkdir(argvars, rettv)
14252 typval_T *argvars;
14253 typval_T *rettv;
14254{
14255 char_u *dir;
14256 char_u buf[NUMBUFLEN];
14257 int prot = 0755;
14258
14259 rettv->vval.v_number = FAIL;
14260 if (check_restricted() || check_secure())
14261 return;
14262
14263 dir = get_tv_string_buf(&argvars[0], buf);
14264 if (argvars[1].v_type != VAR_UNKNOWN)
14265 {
14266 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 prot = get_tv_number_chk(&argvars[2], NULL);
14268 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014269 mkdir_recurse(dir, prot);
14270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014272}
14273#endif
14274
Bram Moolenaar0d660222005-01-07 21:51:51 +000014275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 * "mode()" function
14277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014280 typval_T *argvars;
14281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014283 char_u buf[3];
14284
14285 buf[1] = NUL;
14286 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287
14288#ifdef FEAT_VISUAL
14289 if (VIsual_active)
14290 {
14291 if (VIsual_select)
14292 buf[0] = VIsual_mode + 's' - 'v';
14293 else
14294 buf[0] = VIsual_mode;
14295 }
14296 else
14297#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014298 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14299 || State == CONFIRM)
14300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014302 if (State == ASKMORE)
14303 buf[1] = 'm';
14304 else if (State == CONFIRM)
14305 buf[1] = '?';
14306 }
14307 else if (State == EXTERNCMD)
14308 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 else if (State & INSERT)
14310 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014311#ifdef FEAT_VREPLACE
14312 if (State & VREPLACE_FLAG)
14313 {
14314 buf[0] = 'R';
14315 buf[1] = 'v';
14316 }
14317 else
14318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 if (State & REPLACE_FLAG)
14320 buf[0] = 'R';
14321 else
14322 buf[0] = 'i';
14323 }
14324 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014325 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014327 if (exmode_active)
14328 buf[1] = 'v';
14329 }
14330 else if (exmode_active)
14331 {
14332 buf[0] = 'c';
14333 buf[1] = 'e';
14334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014338 if (finish_op)
14339 buf[1] = 'o';
14340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014342 /* Clear out the minor mode when the argument is not a non-zero number or
14343 * non-empty string. */
14344 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014345 buf[1] = NUL;
14346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 rettv->vval.v_string = vim_strsave(buf);
14348 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349}
14350
Bram Moolenaar429fa852013-04-15 12:27:36 +020014351#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014352/*
14353 * "mzeval()" function
14354 */
14355 static void
14356f_mzeval(argvars, rettv)
14357 typval_T *argvars;
14358 typval_T *rettv;
14359{
14360 char_u *str;
14361 char_u buf[NUMBUFLEN];
14362
14363 str = get_tv_string_buf(&argvars[0], buf);
14364 do_mzeval(str, rettv);
14365}
Bram Moolenaar75676462013-01-30 14:55:42 +010014366
14367 void
14368mzscheme_call_vim(name, args, rettv)
14369 char_u *name;
14370 typval_T *args;
14371 typval_T *rettv;
14372{
14373 typval_T argvars[3];
14374
14375 argvars[0].v_type = VAR_STRING;
14376 argvars[0].vval.v_string = name;
14377 copy_tv(args, &argvars[1]);
14378 argvars[2].v_type = VAR_UNKNOWN;
14379 f_call(argvars, rettv);
14380 clear_tv(&argvars[1]);
14381}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014382#endif
14383
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385 * "nextnonblank()" function
14386 */
14387 static void
14388f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014389 typval_T *argvars;
14390 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014391{
14392 linenr_T lnum;
14393
14394 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014397 {
14398 lnum = 0;
14399 break;
14400 }
14401 if (*skipwhite(ml_get(lnum)) != NUL)
14402 break;
14403 }
14404 rettv->vval.v_number = lnum;
14405}
14406
14407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 * "nr2char()" function
14409 */
14410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014412 typval_T *argvars;
14413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414{
14415 char_u buf[NUMBUFLEN];
14416
14417#ifdef FEAT_MBYTE
14418 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014419 {
14420 int utf8 = 0;
14421
14422 if (argvars[1].v_type != VAR_UNKNOWN)
14423 utf8 = get_tv_number_chk(&argvars[1], NULL);
14424 if (utf8)
14425 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14426 else
14427 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 else
14430#endif
14431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 buf[1] = NUL;
14434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 rettv->v_type = VAR_STRING;
14436 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014437}
14438
14439/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014440 * "or(expr, expr)" function
14441 */
14442 static void
14443f_or(argvars, rettv)
14444 typval_T *argvars;
14445 typval_T *rettv;
14446{
14447 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14448 | get_tv_number_chk(&argvars[1], NULL);
14449}
14450
14451/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014452 * "pathshorten()" function
14453 */
14454 static void
14455f_pathshorten(argvars, rettv)
14456 typval_T *argvars;
14457 typval_T *rettv;
14458{
14459 char_u *p;
14460
14461 rettv->v_type = VAR_STRING;
14462 p = get_tv_string_chk(&argvars[0]);
14463 if (p == NULL)
14464 rettv->vval.v_string = NULL;
14465 else
14466 {
14467 p = vim_strsave(p);
14468 rettv->vval.v_string = p;
14469 if (p != NULL)
14470 shorten_dir(p);
14471 }
14472}
14473
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014474#ifdef FEAT_FLOAT
14475/*
14476 * "pow()" function
14477 */
14478 static void
14479f_pow(argvars, rettv)
14480 typval_T *argvars;
14481 typval_T *rettv;
14482{
14483 float_T fx, fy;
14484
14485 rettv->v_type = VAR_FLOAT;
14486 if (get_float_arg(argvars, &fx) == OK
14487 && get_float_arg(&argvars[1], &fy) == OK)
14488 rettv->vval.v_float = pow(fx, fy);
14489 else
14490 rettv->vval.v_float = 0.0;
14491}
14492#endif
14493
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014494/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495 * "prevnonblank()" function
14496 */
14497 static void
14498f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014499 typval_T *argvars;
14500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014501{
14502 linenr_T lnum;
14503
14504 lnum = get_tv_lnum(argvars);
14505 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14506 lnum = 0;
14507 else
14508 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14509 --lnum;
14510 rettv->vval.v_number = lnum;
14511}
14512
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014513#ifdef HAVE_STDARG_H
14514/* This dummy va_list is here because:
14515 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14516 * - locally in the function results in a "used before set" warning
14517 * - using va_start() to initialize it gives "function with fixed args" error */
14518static va_list ap;
14519#endif
14520
Bram Moolenaar8c711452005-01-14 21:53:12 +000014521/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014522 * "printf()" function
14523 */
14524 static void
14525f_printf(argvars, rettv)
14526 typval_T *argvars;
14527 typval_T *rettv;
14528{
14529 rettv->v_type = VAR_STRING;
14530 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014531#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014532 {
14533 char_u buf[NUMBUFLEN];
14534 int len;
14535 char_u *s;
14536 int saved_did_emsg = did_emsg;
14537 char *fmt;
14538
14539 /* Get the required length, allocate the buffer and do it for real. */
14540 did_emsg = FALSE;
14541 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014542 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014543 if (!did_emsg)
14544 {
14545 s = alloc(len + 1);
14546 if (s != NULL)
14547 {
14548 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014549 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014550 }
14551 }
14552 did_emsg |= saved_did_emsg;
14553 }
14554#endif
14555}
14556
14557/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014558 * "pumvisible()" function
14559 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014560 static void
14561f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014562 typval_T *argvars UNUSED;
14563 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014564{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014565#ifdef FEAT_INS_EXPAND
14566 if (pum_visible())
14567 rettv->vval.v_number = 1;
14568#endif
14569}
14570
Bram Moolenaardb913952012-06-29 12:54:53 +020014571#ifdef FEAT_PYTHON3
14572/*
14573 * "py3eval()" function
14574 */
14575 static void
14576f_py3eval(argvars, rettv)
14577 typval_T *argvars;
14578 typval_T *rettv;
14579{
14580 char_u *str;
14581 char_u buf[NUMBUFLEN];
14582
14583 str = get_tv_string_buf(&argvars[0], buf);
14584 do_py3eval(str, rettv);
14585}
14586#endif
14587
14588#ifdef FEAT_PYTHON
14589/*
14590 * "pyeval()" function
14591 */
14592 static void
14593f_pyeval(argvars, rettv)
14594 typval_T *argvars;
14595 typval_T *rettv;
14596{
14597 char_u *str;
14598 char_u buf[NUMBUFLEN];
14599
14600 str = get_tv_string_buf(&argvars[0], buf);
14601 do_pyeval(str, rettv);
14602}
14603#endif
14604
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014606 * "range()" function
14607 */
14608 static void
14609f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014610 typval_T *argvars;
14611 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014612{
14613 long start;
14614 long end;
14615 long stride = 1;
14616 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014617 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014620 if (argvars[1].v_type == VAR_UNKNOWN)
14621 {
14622 end = start - 1;
14623 start = 0;
14624 }
14625 else
14626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014630 }
14631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 if (error)
14633 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014634 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014635 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014636 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014637 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014638 else
14639 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014640 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014641 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014642 if (list_append_number(rettv->vval.v_list,
14643 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014644 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014645 }
14646}
14647
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014648/*
14649 * "readfile()" function
14650 */
14651 static void
14652f_readfile(argvars, rettv)
14653 typval_T *argvars;
14654 typval_T *rettv;
14655{
14656 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014657 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014658 char_u *fname;
14659 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014660 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14661 int io_size = sizeof(buf);
14662 int readlen; /* size of last fread() */
14663 char_u *prev = NULL; /* previously read bytes, if any */
14664 long prevlen = 0; /* length of data in prev */
14665 long prevsize = 0; /* size of prev buffer */
14666 long maxline = MAXLNUM;
14667 long cnt = 0;
14668 char_u *p; /* position in buf */
14669 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014670
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014671 if (argvars[1].v_type != VAR_UNKNOWN)
14672 {
14673 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14674 binary = TRUE;
14675 if (argvars[2].v_type != VAR_UNKNOWN)
14676 maxline = get_tv_number(&argvars[2]);
14677 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014679 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014680 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014681
14682 /* Always open the file in binary mode, library functions have a mind of
14683 * their own about CR-LF conversion. */
14684 fname = get_tv_string(&argvars[0]);
14685 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14686 {
14687 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14688 return;
14689 }
14690
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014691 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014692 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014693 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014694
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014695 /* This for loop processes what was read, but is also entered at end
14696 * of file so that either:
14697 * - an incomplete line gets written
14698 * - a "binary" file gets an empty line at the end if it ends in a
14699 * newline. */
14700 for (p = buf, start = buf;
14701 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14702 ++p)
14703 {
14704 if (*p == '\n' || readlen <= 0)
14705 {
14706 listitem_T *li;
14707 char_u *s = NULL;
14708 long_u len = p - start;
14709
14710 /* Finished a line. Remove CRs before NL. */
14711 if (readlen > 0 && !binary)
14712 {
14713 while (len > 0 && start[len - 1] == '\r')
14714 --len;
14715 /* removal may cross back to the "prev" string */
14716 if (len == 0)
14717 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14718 --prevlen;
14719 }
14720 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014721 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014722 else
14723 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014724 /* Change "prev" buffer to be the right size. This way
14725 * the bytes are only copied once, and very long lines are
14726 * allocated only once. */
14727 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014729 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014730 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014731 prev = NULL; /* the list will own the string */
14732 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014733 }
14734 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014735 if (s == NULL)
14736 {
14737 do_outofmem_msg((long_u) prevlen + len + 1);
14738 failed = TRUE;
14739 break;
14740 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014741
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014742 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014743 {
14744 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014745 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014746 break;
14747 }
14748 li->li_tv.v_type = VAR_STRING;
14749 li->li_tv.v_lock = 0;
14750 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014751 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 start = p + 1; /* step over newline */
14754 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014755 break;
14756 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014757 else if (*p == NUL)
14758 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014759#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014760 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14761 * when finding the BF and check the previous two bytes. */
14762 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014763 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014764 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14765 * + 1, these may be in the "prev" string. */
14766 char_u back1 = p >= buf + 1 ? p[-1]
14767 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14768 char_u back2 = p >= buf + 2 ? p[-2]
14769 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14770 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014771
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014772 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014773 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014774 char_u *dest = p - 2;
14775
14776 /* Usually a BOM is at the beginning of a file, and so at
14777 * the beginning of a line; then we can just step over it.
14778 */
14779 if (start == dest)
14780 start = p + 1;
14781 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014782 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014783 /* have to shuffle buf to close gap */
14784 int adjust_prevlen = 0;
14785
14786 if (dest < buf)
14787 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014788 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014789 dest = buf;
14790 }
14791 if (readlen > p - buf + 1)
14792 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14793 readlen -= 3 - adjust_prevlen;
14794 prevlen -= adjust_prevlen;
14795 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014796 }
14797 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014798 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014799#endif
14800 } /* for */
14801
14802 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14803 break;
14804 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014805 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014806 /* There's part of a line in buf, store it in "prev". */
14807 if (p - start + prevlen >= prevsize)
14808 {
14809 /* need bigger "prev" buffer */
14810 char_u *newprev;
14811
14812 /* A common use case is ordinary text files and "prev" gets a
14813 * fragment of a line, so the first allocation is made
14814 * small, to avoid repeatedly 'allocing' large and
14815 * 'reallocing' small. */
14816 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014817 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014818 else
14819 {
14820 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014821 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822 prevsize = grow50pc > growmin ? grow50pc : growmin;
14823 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014824 newprev = prev == NULL ? alloc(prevsize)
14825 : vim_realloc(prev, prevsize);
14826 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014827 {
14828 do_outofmem_msg((long_u)prevsize);
14829 failed = TRUE;
14830 break;
14831 }
14832 prev = newprev;
14833 }
14834 /* Add the line part to end of "prev". */
14835 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014836 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014837 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014838 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014839
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014840 /*
14841 * For a negative line count use only the lines at the end of the file,
14842 * free the rest.
14843 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014844 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014845 while (cnt > -maxline)
14846 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014847 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014848 --cnt;
14849 }
14850
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014851 if (failed)
14852 {
14853 list_free(rettv->vval.v_list, TRUE);
14854 /* readfile doc says an empty list is returned on error */
14855 rettv->vval.v_list = list_alloc();
14856 }
14857
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014858 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014859 fclose(fd);
14860}
14861
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014862#if defined(FEAT_RELTIME)
14863static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14864
14865/*
14866 * Convert a List to proftime_T.
14867 * Return FAIL when there is something wrong.
14868 */
14869 static int
14870list2proftime(arg, tm)
14871 typval_T *arg;
14872 proftime_T *tm;
14873{
14874 long n1, n2;
14875 int error = FALSE;
14876
14877 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14878 || arg->vval.v_list->lv_len != 2)
14879 return FAIL;
14880 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14881 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14882# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014883 tm->HighPart = n1;
14884 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014885# else
14886 tm->tv_sec = n1;
14887 tm->tv_usec = n2;
14888# endif
14889 return error ? FAIL : OK;
14890}
14891#endif /* FEAT_RELTIME */
14892
14893/*
14894 * "reltime()" function
14895 */
14896 static void
14897f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014898 typval_T *argvars UNUSED;
14899 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014900{
14901#ifdef FEAT_RELTIME
14902 proftime_T res;
14903 proftime_T start;
14904
14905 if (argvars[0].v_type == VAR_UNKNOWN)
14906 {
14907 /* No arguments: get current time. */
14908 profile_start(&res);
14909 }
14910 else if (argvars[1].v_type == VAR_UNKNOWN)
14911 {
14912 if (list2proftime(&argvars[0], &res) == FAIL)
14913 return;
14914 profile_end(&res);
14915 }
14916 else
14917 {
14918 /* Two arguments: compute the difference. */
14919 if (list2proftime(&argvars[0], &start) == FAIL
14920 || list2proftime(&argvars[1], &res) == FAIL)
14921 return;
14922 profile_sub(&res, &start);
14923 }
14924
14925 if (rettv_list_alloc(rettv) == OK)
14926 {
14927 long n1, n2;
14928
14929# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014930 n1 = res.HighPart;
14931 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014932# else
14933 n1 = res.tv_sec;
14934 n2 = res.tv_usec;
14935# endif
14936 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14937 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14938 }
14939#endif
14940}
14941
14942/*
14943 * "reltimestr()" function
14944 */
14945 static void
14946f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014947 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014948 typval_T *rettv;
14949{
14950#ifdef FEAT_RELTIME
14951 proftime_T tm;
14952#endif
14953
14954 rettv->v_type = VAR_STRING;
14955 rettv->vval.v_string = NULL;
14956#ifdef FEAT_RELTIME
14957 if (list2proftime(&argvars[0], &tm) == OK)
14958 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14959#endif
14960}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014961
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14963static void make_connection __ARGS((void));
14964static int check_connection __ARGS((void));
14965
14966 static void
14967make_connection()
14968{
14969 if (X_DISPLAY == NULL
14970# ifdef FEAT_GUI
14971 && !gui.in_use
14972# endif
14973 )
14974 {
14975 x_force_connect = TRUE;
14976 setup_term_clip();
14977 x_force_connect = FALSE;
14978 }
14979}
14980
14981 static int
14982check_connection()
14983{
14984 make_connection();
14985 if (X_DISPLAY == NULL)
14986 {
14987 EMSG(_("E240: No connection to Vim server"));
14988 return FAIL;
14989 }
14990 return OK;
14991}
14992#endif
14993
14994#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014995static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996
14997 static void
14998remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014999 typval_T *argvars;
15000 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015001 int expr;
15002{
15003 char_u *server_name;
15004 char_u *keys;
15005 char_u *r = NULL;
15006 char_u buf[NUMBUFLEN];
15007# ifdef WIN32
15008 HWND w;
15009# else
15010 Window w;
15011# endif
15012
15013 if (check_restricted() || check_secure())
15014 return;
15015
15016# ifdef FEAT_X11
15017 if (check_connection() == FAIL)
15018 return;
15019# endif
15020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015021 server_name = get_tv_string_chk(&argvars[0]);
15022 if (server_name == NULL)
15023 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 keys = get_tv_string_buf(&argvars[1], buf);
15025# ifdef WIN32
15026 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15027# else
15028 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15029 < 0)
15030# endif
15031 {
15032 if (r != NULL)
15033 EMSG(r); /* sending worked but evaluation failed */
15034 else
15035 EMSG2(_("E241: Unable to send to %s"), server_name);
15036 return;
15037 }
15038
15039 rettv->vval.v_string = r;
15040
15041 if (argvars[2].v_type != VAR_UNKNOWN)
15042 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015043 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015044 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015047 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015048 v.di_tv.v_type = VAR_STRING;
15049 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 idvar = get_tv_string_chk(&argvars[2]);
15051 if (idvar != NULL)
15052 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015053 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015054 }
15055}
15056#endif
15057
15058/*
15059 * "remote_expr()" function
15060 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061 static void
15062f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065{
15066 rettv->v_type = VAR_STRING;
15067 rettv->vval.v_string = NULL;
15068#ifdef FEAT_CLIENTSERVER
15069 remote_common(argvars, rettv, TRUE);
15070#endif
15071}
15072
15073/*
15074 * "remote_foreground()" function
15075 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076 static void
15077f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015078 typval_T *argvars UNUSED;
15079 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081#ifdef FEAT_CLIENTSERVER
15082# ifdef WIN32
15083 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015084 {
15085 char_u *server_name = get_tv_string_chk(&argvars[0]);
15086
15087 if (server_name != NULL)
15088 serverForeground(server_name);
15089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090# else
15091 /* Send a foreground() expression to the server. */
15092 argvars[1].v_type = VAR_STRING;
15093 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15094 argvars[2].v_type = VAR_UNKNOWN;
15095 remote_common(argvars, rettv, TRUE);
15096 vim_free(argvars[1].vval.v_string);
15097# endif
15098#endif
15099}
15100
Bram Moolenaar0d660222005-01-07 21:51:51 +000015101 static void
15102f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015103 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015105{
15106#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015107 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108 char_u *s = NULL;
15109# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015110 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015112 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113
15114 if (check_restricted() || check_secure())
15115 {
15116 rettv->vval.v_number = -1;
15117 return;
15118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015119 serverid = get_tv_string_chk(&argvars[0]);
15120 if (serverid == NULL)
15121 {
15122 rettv->vval.v_number = -1;
15123 return; /* type error; errmsg already given */
15124 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015125# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015126 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127 if (n == 0)
15128 rettv->vval.v_number = -1;
15129 else
15130 {
15131 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15132 rettv->vval.v_number = (s != NULL);
15133 }
15134# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135 if (check_connection() == FAIL)
15136 return;
15137
15138 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015139 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015140# endif
15141
15142 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015144 char_u *retvar;
15145
Bram Moolenaar33570922005-01-25 22:26:29 +000015146 v.di_tv.v_type = VAR_STRING;
15147 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 retvar = get_tv_string_chk(&argvars[1]);
15149 if (retvar != NULL)
15150 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015151 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152 }
15153#else
15154 rettv->vval.v_number = -1;
15155#endif
15156}
15157
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 static void
15159f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015160 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162{
15163 char_u *r = NULL;
15164
15165#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 char_u *serverid = get_tv_string_chk(&argvars[0]);
15167
15168 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169 {
15170# ifdef WIN32
15171 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015172 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015174 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175 if (n != 0)
15176 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15177 if (r == NULL)
15178# else
15179 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015180 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181# endif
15182 EMSG(_("E277: Unable to read a server reply"));
15183 }
15184#endif
15185 rettv->v_type = VAR_STRING;
15186 rettv->vval.v_string = r;
15187}
15188
15189/*
15190 * "remote_send()" function
15191 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192 static void
15193f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015194 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015195 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015196{
15197 rettv->v_type = VAR_STRING;
15198 rettv->vval.v_string = NULL;
15199#ifdef FEAT_CLIENTSERVER
15200 remote_common(argvars, rettv, FALSE);
15201#endif
15202}
15203
15204/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015205 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015206 */
15207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015209 typval_T *argvars;
15210 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015211{
Bram Moolenaar33570922005-01-25 22:26:29 +000015212 list_T *l;
15213 listitem_T *item, *item2;
15214 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015215 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015216 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015217 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 dict_T *d;
15219 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015220 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015221
Bram Moolenaar8c711452005-01-14 21:53:12 +000015222 if (argvars[0].v_type == VAR_DICT)
15223 {
15224 if (argvars[2].v_type != VAR_UNKNOWN)
15225 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015226 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015227 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 key = get_tv_string_chk(&argvars[1]);
15230 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015232 di = dict_find(d, key, -1);
15233 if (di == NULL)
15234 EMSG2(_(e_dictkey), key);
15235 else
15236 {
15237 *rettv = di->di_tv;
15238 init_tv(&di->di_tv);
15239 dictitem_remove(d, di);
15240 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015241 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015242 }
15243 }
15244 else if (argvars[0].v_type != VAR_LIST)
15245 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015246 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015247 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 int error = FALSE;
15250
15251 idx = get_tv_number_chk(&argvars[1], &error);
15252 if (error)
15253 ; /* type error: do nothing, errmsg already given */
15254 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015255 EMSGN(_(e_listidx), idx);
15256 else
15257 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015258 if (argvars[2].v_type == VAR_UNKNOWN)
15259 {
15260 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015261 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015262 *rettv = item->li_tv;
15263 vim_free(item);
15264 }
15265 else
15266 {
15267 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015268 end = get_tv_number_chk(&argvars[2], &error);
15269 if (error)
15270 ; /* type error: do nothing */
15271 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015272 EMSGN(_(e_listidx), end);
15273 else
15274 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015275 int cnt = 0;
15276
15277 for (li = item; li != NULL; li = li->li_next)
15278 {
15279 ++cnt;
15280 if (li == item2)
15281 break;
15282 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015283 if (li == NULL) /* didn't find "item2" after "item" */
15284 EMSG(_(e_invrange));
15285 else
15286 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015287 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015288 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015289 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015290 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015291 l->lv_first = item;
15292 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015293 item->li_prev = NULL;
15294 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015295 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015296 }
15297 }
15298 }
15299 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015300 }
15301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302}
15303
15304/*
15305 * "rename({from}, {to})" function
15306 */
15307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015309 typval_T *argvars;
15310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311{
15312 char_u buf[NUMBUFLEN];
15313
15314 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015315 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015317 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15318 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319}
15320
15321/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015322 * "repeat()" function
15323 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015324 static void
15325f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015326 typval_T *argvars;
15327 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015328{
15329 char_u *p;
15330 int n;
15331 int slen;
15332 int len;
15333 char_u *r;
15334 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015335
15336 n = get_tv_number(&argvars[1]);
15337 if (argvars[0].v_type == VAR_LIST)
15338 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015339 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015340 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015341 if (list_extend(rettv->vval.v_list,
15342 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015343 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015344 }
15345 else
15346 {
15347 p = get_tv_string(&argvars[0]);
15348 rettv->v_type = VAR_STRING;
15349 rettv->vval.v_string = NULL;
15350
15351 slen = (int)STRLEN(p);
15352 len = slen * n;
15353 if (len <= 0)
15354 return;
15355
15356 r = alloc(len + 1);
15357 if (r != NULL)
15358 {
15359 for (i = 0; i < n; i++)
15360 mch_memmove(r + i * slen, p, (size_t)slen);
15361 r[len] = NUL;
15362 }
15363
15364 rettv->vval.v_string = r;
15365 }
15366}
15367
15368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 * "resolve()" function
15370 */
15371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015373 typval_T *argvars;
15374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375{
15376 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015377#ifdef HAVE_READLINK
15378 char_u *buf = NULL;
15379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382#ifdef FEAT_SHORTCUT
15383 {
15384 char_u *v = NULL;
15385
15386 v = mch_resolve_shortcut(p);
15387 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 }
15392#else
15393# ifdef HAVE_READLINK
15394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 char_u *cpy;
15396 int len;
15397 char_u *remain = NULL;
15398 char_u *q;
15399 int is_relative_to_current = FALSE;
15400 int has_trailing_pathsep = FALSE;
15401 int limit = 100;
15402
15403 p = vim_strsave(p);
15404
15405 if (p[0] == '.' && (vim_ispathsep(p[1])
15406 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15407 is_relative_to_current = TRUE;
15408
15409 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015410 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015413 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415
15416 q = getnextcomp(p);
15417 if (*q != NUL)
15418 {
15419 /* Separate the first path component in "p", and keep the
15420 * remainder (beginning with the path separator). */
15421 remain = vim_strsave(q - 1);
15422 q[-1] = NUL;
15423 }
15424
Bram Moolenaard9462e32011-04-11 21:35:11 +020015425 buf = alloc(MAXPATHL + 1);
15426 if (buf == NULL)
15427 goto fail;
15428
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 for (;;)
15430 {
15431 for (;;)
15432 {
15433 len = readlink((char *)p, (char *)buf, MAXPATHL);
15434 if (len <= 0)
15435 break;
15436 buf[len] = NUL;
15437
15438 if (limit-- == 0)
15439 {
15440 vim_free(p);
15441 vim_free(remain);
15442 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 goto fail;
15445 }
15446
15447 /* Ensure that the result will have a trailing path separator
15448 * if the argument has one. */
15449 if (remain == NULL && has_trailing_pathsep)
15450 add_pathsep(buf);
15451
15452 /* Separate the first path component in the link value and
15453 * concatenate the remainders. */
15454 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15455 if (*q != NUL)
15456 {
15457 if (remain == NULL)
15458 remain = vim_strsave(q - 1);
15459 else
15460 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015461 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 if (cpy != NULL)
15463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 vim_free(remain);
15465 remain = cpy;
15466 }
15467 }
15468 q[-1] = NUL;
15469 }
15470
15471 q = gettail(p);
15472 if (q > p && *q == NUL)
15473 {
15474 /* Ignore trailing path separator. */
15475 q[-1] = NUL;
15476 q = gettail(p);
15477 }
15478 if (q > p && !mch_isFullName(buf))
15479 {
15480 /* symlink is relative to directory of argument */
15481 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15482 if (cpy != NULL)
15483 {
15484 STRCPY(cpy, p);
15485 STRCPY(gettail(cpy), buf);
15486 vim_free(p);
15487 p = cpy;
15488 }
15489 }
15490 else
15491 {
15492 vim_free(p);
15493 p = vim_strsave(buf);
15494 }
15495 }
15496
15497 if (remain == NULL)
15498 break;
15499
15500 /* Append the first path component of "remain" to "p". */
15501 q = getnextcomp(remain + 1);
15502 len = q - remain - (*q != NUL);
15503 cpy = vim_strnsave(p, STRLEN(p) + len);
15504 if (cpy != NULL)
15505 {
15506 STRNCAT(cpy, remain, len);
15507 vim_free(p);
15508 p = cpy;
15509 }
15510 /* Shorten "remain". */
15511 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015512 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 else
15514 {
15515 vim_free(remain);
15516 remain = NULL;
15517 }
15518 }
15519
15520 /* If the result is a relative path name, make it explicitly relative to
15521 * the current directory if and only if the argument had this form. */
15522 if (!vim_ispathsep(*p))
15523 {
15524 if (is_relative_to_current
15525 && *p != NUL
15526 && !(p[0] == '.'
15527 && (p[1] == NUL
15528 || vim_ispathsep(p[1])
15529 || (p[1] == '.'
15530 && (p[2] == NUL
15531 || vim_ispathsep(p[2]))))))
15532 {
15533 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015534 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 if (cpy != NULL)
15536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 vim_free(p);
15538 p = cpy;
15539 }
15540 }
15541 else if (!is_relative_to_current)
15542 {
15543 /* Strip leading "./". */
15544 q = p;
15545 while (q[0] == '.' && vim_ispathsep(q[1]))
15546 q += 2;
15547 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015548 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 }
15550 }
15551
15552 /* Ensure that the result will have no trailing path separator
15553 * if the argument had none. But keep "/" or "//". */
15554 if (!has_trailing_pathsep)
15555 {
15556 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015557 if (after_pathsep(p, q))
15558 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 }
15560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015561 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 }
15563# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565# endif
15566#endif
15567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015568 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569
15570#ifdef HAVE_READLINK
15571fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015572 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575}
15576
15577/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015578 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 */
15580 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015582 typval_T *argvars;
15583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584{
Bram Moolenaar33570922005-01-25 22:26:29 +000015585 list_T *l;
15586 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587
Bram Moolenaar0d660222005-01-07 21:51:51 +000015588 if (argvars[0].v_type != VAR_LIST)
15589 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015590 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015591 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 {
15593 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015594 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015595 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 while (li != NULL)
15597 {
15598 ni = li->li_prev;
15599 list_append(l, li);
15600 li = ni;
15601 }
15602 rettv->vval.v_list = l;
15603 rettv->v_type = VAR_LIST;
15604 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015605 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015609#define SP_NOMOVE 0x01 /* don't move cursor */
15610#define SP_REPEAT 0x02 /* repeat to find outer pair */
15611#define SP_RETCOUNT 0x04 /* return matchcount */
15612#define SP_SETPCMARK 0x08 /* set previous context mark */
15613#define SP_START 0x10 /* accept match at start position */
15614#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15615#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015616
Bram Moolenaar33570922005-01-25 22:26:29 +000015617static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015618
15619/*
15620 * Get flags for a search function.
15621 * Possibly sets "p_ws".
15622 * Returns BACKWARD, FORWARD or zero (for an error).
15623 */
15624 static int
15625get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015626 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015627 int *flagsp;
15628{
15629 int dir = FORWARD;
15630 char_u *flags;
15631 char_u nbuf[NUMBUFLEN];
15632 int mask;
15633
15634 if (varp->v_type != VAR_UNKNOWN)
15635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636 flags = get_tv_string_buf_chk(varp, nbuf);
15637 if (flags == NULL)
15638 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015639 while (*flags != NUL)
15640 {
15641 switch (*flags)
15642 {
15643 case 'b': dir = BACKWARD; break;
15644 case 'w': p_ws = TRUE; break;
15645 case 'W': p_ws = FALSE; break;
15646 default: mask = 0;
15647 if (flagsp != NULL)
15648 switch (*flags)
15649 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015650 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015651 case 'e': mask = SP_END; break;
15652 case 'm': mask = SP_RETCOUNT; break;
15653 case 'n': mask = SP_NOMOVE; break;
15654 case 'p': mask = SP_SUBPAT; break;
15655 case 'r': mask = SP_REPEAT; break;
15656 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657 }
15658 if (mask == 0)
15659 {
15660 EMSG2(_(e_invarg2), flags);
15661 dir = 0;
15662 }
15663 else
15664 *flagsp |= mask;
15665 }
15666 if (dir == 0)
15667 break;
15668 ++flags;
15669 }
15670 }
15671 return dir;
15672}
15673
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015675 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015677 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015678search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015679 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015680 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015681 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015683 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 char_u *pat;
15685 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015686 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 int save_p_ws = p_ws;
15688 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015689 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015690 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015691 proftime_T tm;
15692#ifdef FEAT_RELTIME
15693 long time_limit = 0;
15694#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015695 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015696 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015698 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015699 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015700 if (dir == 0)
15701 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015702 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015703 if (flags & SP_START)
15704 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015705 if (flags & SP_END)
15706 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015707
Bram Moolenaar76929292008-01-06 19:07:36 +000015708 /* Optional arguments: line number to stop searching and timeout. */
15709 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015710 {
15711 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15712 if (lnum_stop < 0)
15713 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015714#ifdef FEAT_RELTIME
15715 if (argvars[3].v_type != VAR_UNKNOWN)
15716 {
15717 time_limit = get_tv_number_chk(&argvars[3], NULL);
15718 if (time_limit < 0)
15719 goto theend;
15720 }
15721#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015722 }
15723
Bram Moolenaar76929292008-01-06 19:07:36 +000015724#ifdef FEAT_RELTIME
15725 /* Set the time limit, if there is one. */
15726 profile_setlimit(time_limit, &tm);
15727#endif
15728
Bram Moolenaar231334e2005-07-25 20:46:57 +000015729 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015730 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015731 * Check to make sure only those flags are set.
15732 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15733 * flags cannot be set. Check for that condition also.
15734 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015735 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015736 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015739 goto theend;
15740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015742 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015743 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015744 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015745 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015747 if (flags & SP_SUBPAT)
15748 retval = subpatnum;
15749 else
15750 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015751 if (flags & SP_SETPCMARK)
15752 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015754 if (match_pos != NULL)
15755 {
15756 /* Store the match cursor position */
15757 match_pos->lnum = pos.lnum;
15758 match_pos->col = pos.col + 1;
15759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 /* "/$" will put the cursor after the end of the line, may need to
15761 * correct that here */
15762 check_cursor();
15763 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015764
15765 /* If 'n' flag is used: restore cursor position. */
15766 if (flags & SP_NOMOVE)
15767 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015768 else
15769 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015770theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015772
15773 return retval;
15774}
15775
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015776#ifdef FEAT_FLOAT
15777/*
15778 * "round({float})" function
15779 */
15780 static void
15781f_round(argvars, rettv)
15782 typval_T *argvars;
15783 typval_T *rettv;
15784{
15785 float_T f;
15786
15787 rettv->v_type = VAR_FLOAT;
15788 if (get_float_arg(argvars, &f) == OK)
15789 /* round() is not in C90, use ceil() or floor() instead. */
15790 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15791 else
15792 rettv->vval.v_float = 0.0;
15793}
15794#endif
15795
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015796/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015797 * "screencol()" function
15798 *
15799 * First column is 1 to be consistent with virtcol().
15800 */
15801 static void
15802f_screencol(argvars, rettv)
15803 typval_T *argvars UNUSED;
15804 typval_T *rettv;
15805{
15806 rettv->vval.v_number = screen_screencol() + 1;
15807}
15808
15809/*
15810 * "screenrow()" function
15811 */
15812 static void
15813f_screenrow(argvars, rettv)
15814 typval_T *argvars UNUSED;
15815 typval_T *rettv;
15816{
15817 rettv->vval.v_number = screen_screenrow() + 1;
15818}
15819
15820/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015821 * "search()" function
15822 */
15823 static void
15824f_search(argvars, rettv)
15825 typval_T *argvars;
15826 typval_T *rettv;
15827{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015828 int flags = 0;
15829
15830 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831}
15832
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015834 * "searchdecl()" function
15835 */
15836 static void
15837f_searchdecl(argvars, rettv)
15838 typval_T *argvars;
15839 typval_T *rettv;
15840{
15841 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015842 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015843 int error = FALSE;
15844 char_u *name;
15845
15846 rettv->vval.v_number = 1; /* default: FAIL */
15847
15848 name = get_tv_string_chk(&argvars[0]);
15849 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015850 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015851 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015852 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15853 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15854 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015855 if (!error && name != NULL)
15856 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015857 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015858}
15859
15860/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015861 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015863 static int
15864searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015865 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015866 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867{
15868 char_u *spat, *mpat, *epat;
15869 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 int dir;
15872 int flags = 0;
15873 char_u nbuf1[NUMBUFLEN];
15874 char_u nbuf2[NUMBUFLEN];
15875 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015876 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015877 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015878 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015881 spat = get_tv_string_chk(&argvars[0]);
15882 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15883 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15884 if (spat == NULL || mpat == NULL || epat == NULL)
15885 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 /* Handle the optional fourth argument: flags */
15888 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015889 if (dir == 0)
15890 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015891
15892 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015893 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15894 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015895 if ((flags & (SP_END | SP_SUBPAT)) != 0
15896 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015897 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015898 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015899 goto theend;
15900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015902 /* Using 'r' implies 'W', otherwise it doesn't work. */
15903 if (flags & SP_REPEAT)
15904 p_ws = FALSE;
15905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015906 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907 if (argvars[3].v_type == VAR_UNKNOWN
15908 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909 skip = (char_u *)"";
15910 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015912 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015913 if (argvars[5].v_type != VAR_UNKNOWN)
15914 {
15915 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15916 if (lnum_stop < 0)
15917 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015918#ifdef FEAT_RELTIME
15919 if (argvars[6].v_type != VAR_UNKNOWN)
15920 {
15921 time_limit = get_tv_number_chk(&argvars[6], NULL);
15922 if (time_limit < 0)
15923 goto theend;
15924 }
15925#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015926 }
15927 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015928 if (skip == NULL)
15929 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015931 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015932 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015933
15934theend:
15935 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015936
15937 return retval;
15938}
15939
15940/*
15941 * "searchpair()" function
15942 */
15943 static void
15944f_searchpair(argvars, rettv)
15945 typval_T *argvars;
15946 typval_T *rettv;
15947{
15948 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15949}
15950
15951/*
15952 * "searchpairpos()" function
15953 */
15954 static void
15955f_searchpairpos(argvars, rettv)
15956 typval_T *argvars;
15957 typval_T *rettv;
15958{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015959 pos_T match_pos;
15960 int lnum = 0;
15961 int col = 0;
15962
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015963 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015964 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015965
15966 if (searchpair_cmn(argvars, &match_pos) > 0)
15967 {
15968 lnum = match_pos.lnum;
15969 col = match_pos.col;
15970 }
15971
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015972 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15973 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015974}
15975
15976/*
15977 * Search for a start/middle/end thing.
15978 * Used by searchpair(), see its documentation for the details.
15979 * Returns 0 or -1 for no match,
15980 */
15981 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015982do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15983 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015984 char_u *spat; /* start pattern */
15985 char_u *mpat; /* middle pattern */
15986 char_u *epat; /* end pattern */
15987 int dir; /* BACKWARD or FORWARD */
15988 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015989 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015990 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015991 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015992 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015993{
15994 char_u *save_cpo;
15995 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15996 long retval = 0;
15997 pos_T pos;
15998 pos_T firstpos;
15999 pos_T foundpos;
16000 pos_T save_cursor;
16001 pos_T save_pos;
16002 int n;
16003 int r;
16004 int nest = 1;
16005 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016006 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016007 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016008
16009 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16010 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016011 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016012
Bram Moolenaar76929292008-01-06 19:07:36 +000016013#ifdef FEAT_RELTIME
16014 /* Set the time limit, if there is one. */
16015 profile_setlimit(time_limit, &tm);
16016#endif
16017
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016018 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16019 * start/middle/end (pat3, for the top pair). */
16020 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16021 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16022 if (pat2 == NULL || pat3 == NULL)
16023 goto theend;
16024 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16025 if (*mpat == NUL)
16026 STRCPY(pat3, pat2);
16027 else
16028 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16029 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016030 if (flags & SP_START)
16031 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016032
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 save_cursor = curwin->w_cursor;
16034 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016035 clearpos(&firstpos);
16036 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 pat = pat3;
16038 for (;;)
16039 {
16040 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016041 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16043 /* didn't find it or found the first match again: FAIL */
16044 break;
16045
16046 if (firstpos.lnum == 0)
16047 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016048 if (equalpos(pos, foundpos))
16049 {
16050 /* Found the same position again. Can happen with a pattern that
16051 * has "\zs" at the end and searching backwards. Advance one
16052 * character and try again. */
16053 if (dir == BACKWARD)
16054 decl(&pos);
16055 else
16056 incl(&pos);
16057 }
16058 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016060 /* clear the start flag to avoid getting stuck here */
16061 options &= ~SEARCH_START;
16062
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 /* If the skip pattern matches, ignore this match. */
16064 if (*skip != NUL)
16065 {
16066 save_pos = curwin->w_cursor;
16067 curwin->w_cursor = pos;
16068 r = eval_to_bool(skip, &err, NULL, FALSE);
16069 curwin->w_cursor = save_pos;
16070 if (err)
16071 {
16072 /* Evaluating {skip} caused an error, break here. */
16073 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016074 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075 break;
16076 }
16077 if (r)
16078 continue;
16079 }
16080
16081 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16082 {
16083 /* Found end when searching backwards or start when searching
16084 * forward: nested pair. */
16085 ++nest;
16086 pat = pat2; /* nested, don't search for middle */
16087 }
16088 else
16089 {
16090 /* Found end when searching forward or start when searching
16091 * backward: end of (nested) pair; or found middle in outer pair. */
16092 if (--nest == 1)
16093 pat = pat3; /* outer level, search for middle */
16094 }
16095
16096 if (nest == 0)
16097 {
16098 /* Found the match: return matchcount or line number. */
16099 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016100 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016102 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016103 if (flags & SP_SETPCMARK)
16104 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 curwin->w_cursor = pos;
16106 if (!(flags & SP_REPEAT))
16107 break;
16108 nest = 1; /* search for next unmatched */
16109 }
16110 }
16111
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016112 if (match_pos != NULL)
16113 {
16114 /* Store the match cursor position */
16115 match_pos->lnum = curwin->w_cursor.lnum;
16116 match_pos->col = curwin->w_cursor.col + 1;
16117 }
16118
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016120 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 curwin->w_cursor = save_cursor;
16122
16123theend:
16124 vim_free(pat2);
16125 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016126 if (p_cpo == empty_option)
16127 p_cpo = save_cpo;
16128 else
16129 /* Darn, evaluating the {skip} expression changed the value. */
16130 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016131
16132 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133}
16134
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016135/*
16136 * "searchpos()" function
16137 */
16138 static void
16139f_searchpos(argvars, rettv)
16140 typval_T *argvars;
16141 typval_T *rettv;
16142{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016143 pos_T match_pos;
16144 int lnum = 0;
16145 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016146 int n;
16147 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016148
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016149 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016150 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016151
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016152 n = search_cmn(argvars, &match_pos, &flags);
16153 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016154 {
16155 lnum = match_pos.lnum;
16156 col = match_pos.col;
16157 }
16158
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016159 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16160 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016161 if (flags & SP_SUBPAT)
16162 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016163}
16164
16165
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166 static void
16167f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171#ifdef FEAT_CLIENTSERVER
16172 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016173 char_u *server = get_tv_string_chk(&argvars[0]);
16174 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175
Bram Moolenaar0d660222005-01-07 21:51:51 +000016176 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016177 if (server == NULL || reply == NULL)
16178 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016179 if (check_restricted() || check_secure())
16180 return;
16181# ifdef FEAT_X11
16182 if (check_connection() == FAIL)
16183 return;
16184# endif
16185
16186 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016188 EMSG(_("E258: Unable to send to client"));
16189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016191 rettv->vval.v_number = 0;
16192#else
16193 rettv->vval.v_number = -1;
16194#endif
16195}
16196
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197 static void
16198f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016200 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201{
16202 char_u *r = NULL;
16203
16204#ifdef FEAT_CLIENTSERVER
16205# ifdef WIN32
16206 r = serverGetVimNames();
16207# else
16208 make_connection();
16209 if (X_DISPLAY != NULL)
16210 r = serverGetVimNames(X_DISPLAY);
16211# endif
16212#endif
16213 rettv->v_type = VAR_STRING;
16214 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215}
16216
16217/*
16218 * "setbufvar()" function
16219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016221f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016222 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016223 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224{
16225 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016228 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 char_u nbuf[NUMBUFLEN];
16230
16231 if (check_restricted() || check_secure())
16232 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016233 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16234 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016235 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 varp = &argvars[2];
16237
16238 if (buf != NULL && varname != NULL && varp != NULL)
16239 {
16240 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242
16243 if (*varname == '&')
16244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016245 long numval;
16246 char_u *strval;
16247 int error = FALSE;
16248
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016250 numval = get_tv_number_chk(varp, &error);
16251 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016252 if (!error && strval != NULL)
16253 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 }
16255 else
16256 {
16257 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16258 if (bufvarname != NULL)
16259 {
16260 STRCPY(bufvarname, "b:");
16261 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016262 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 vim_free(bufvarname);
16264 }
16265 }
16266
16267 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270}
16271
16272/*
16273 * "setcmdpos()" function
16274 */
16275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016276f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 typval_T *argvars;
16278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016280 int pos = (int)get_tv_number(&argvars[0]) - 1;
16281
16282 if (pos >= 0)
16283 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284}
16285
16286/*
16287 * "setline()" function
16288 */
16289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016290f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 typval_T *argvars;
16292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293{
16294 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016295 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016296 list_T *l = NULL;
16297 listitem_T *li = NULL;
16298 long added = 0;
16299 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016301 lnum = get_tv_lnum(&argvars[0]);
16302 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016304 l = argvars[1].vval.v_list;
16305 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016307 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016308 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016309
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016310 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016311 for (;;)
16312 {
16313 if (l != NULL)
16314 {
16315 /* list argument, get next string */
16316 if (li == NULL)
16317 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016318 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016319 li = li->li_next;
16320 }
16321
16322 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016323 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016324 break;
16325 if (lnum <= curbuf->b_ml.ml_line_count)
16326 {
16327 /* existing line, replace it */
16328 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16329 {
16330 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016331 if (lnum == curwin->w_cursor.lnum)
16332 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016333 rettv->vval.v_number = 0; /* OK */
16334 }
16335 }
16336 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16337 {
16338 /* lnum is one past the last line, append the line */
16339 ++added;
16340 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16341 rettv->vval.v_number = 0; /* OK */
16342 }
16343
16344 if (l == NULL) /* only one string argument */
16345 break;
16346 ++lnum;
16347 }
16348
16349 if (added > 0)
16350 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351}
16352
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016353static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16354
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016356 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016357 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016358 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016359set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016360 win_T *wp UNUSED;
16361 typval_T *list_arg UNUSED;
16362 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016363 typval_T *rettv;
16364{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016365#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016366 char_u *act;
16367 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016368#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016369
Bram Moolenaar2641f772005-03-25 21:58:17 +000016370 rettv->vval.v_number = -1;
16371
16372#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016373 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016374 EMSG(_(e_listreq));
16375 else
16376 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016377 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016378
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016379 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016380 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016381 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016382 if (act == NULL)
16383 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016384 if (*act == 'a' || *act == 'r')
16385 action = *act;
16386 }
16387
Bram Moolenaar81484f42012-12-05 15:16:47 +010016388 if (l != NULL && set_errorlist(wp, l, action,
16389 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016390 rettv->vval.v_number = 0;
16391 }
16392#endif
16393}
16394
16395/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016396 * "setloclist()" function
16397 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016398 static void
16399f_setloclist(argvars, rettv)
16400 typval_T *argvars;
16401 typval_T *rettv;
16402{
16403 win_T *win;
16404
16405 rettv->vval.v_number = -1;
16406
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016407 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016408 if (win != NULL)
16409 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16410}
16411
16412/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016413 * "setmatches()" function
16414 */
16415 static void
16416f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016417 typval_T *argvars UNUSED;
16418 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016419{
16420#ifdef FEAT_SEARCH_EXTRA
16421 list_T *l;
16422 listitem_T *li;
16423 dict_T *d;
16424
16425 rettv->vval.v_number = -1;
16426 if (argvars[0].v_type != VAR_LIST)
16427 {
16428 EMSG(_(e_listreq));
16429 return;
16430 }
16431 if ((l = argvars[0].vval.v_list) != NULL)
16432 {
16433
16434 /* To some extent make sure that we are dealing with a list from
16435 * "getmatches()". */
16436 li = l->lv_first;
16437 while (li != NULL)
16438 {
16439 if (li->li_tv.v_type != VAR_DICT
16440 || (d = li->li_tv.vval.v_dict) == NULL)
16441 {
16442 EMSG(_(e_invarg));
16443 return;
16444 }
16445 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16446 && dict_find(d, (char_u *)"pattern", -1) != NULL
16447 && dict_find(d, (char_u *)"priority", -1) != NULL
16448 && dict_find(d, (char_u *)"id", -1) != NULL))
16449 {
16450 EMSG(_(e_invarg));
16451 return;
16452 }
16453 li = li->li_next;
16454 }
16455
16456 clear_matches(curwin);
16457 li = l->lv_first;
16458 while (li != NULL)
16459 {
16460 d = li->li_tv.vval.v_dict;
16461 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16462 get_dict_string(d, (char_u *)"pattern", FALSE),
16463 (int)get_dict_number(d, (char_u *)"priority"),
16464 (int)get_dict_number(d, (char_u *)"id"));
16465 li = li->li_next;
16466 }
16467 rettv->vval.v_number = 0;
16468 }
16469#endif
16470}
16471
16472/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016473 * "setpos()" function
16474 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016475 static void
16476f_setpos(argvars, rettv)
16477 typval_T *argvars;
16478 typval_T *rettv;
16479{
16480 pos_T pos;
16481 int fnum;
16482 char_u *name;
16483
Bram Moolenaar08250432008-02-13 11:42:46 +000016484 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016485 name = get_tv_string_chk(argvars);
16486 if (name != NULL)
16487 {
16488 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16489 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016490 if (--pos.col < 0)
16491 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016492 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016493 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016494 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016495 if (fnum == curbuf->b_fnum)
16496 {
16497 curwin->w_cursor = pos;
16498 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016499 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016500 }
16501 else
16502 EMSG(_(e_invarg));
16503 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016504 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16505 {
16506 /* set mark */
16507 if (setmark_pos(name[1], &pos, fnum) == OK)
16508 rettv->vval.v_number = 0;
16509 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016510 else
16511 EMSG(_(e_invarg));
16512 }
16513 }
16514}
16515
16516/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016517 * "setqflist()" function
16518 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016519 static void
16520f_setqflist(argvars, rettv)
16521 typval_T *argvars;
16522 typval_T *rettv;
16523{
16524 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16525}
16526
16527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 * "setreg()" function
16529 */
16530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016531f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016532 typval_T *argvars;
16533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534{
16535 int regname;
16536 char_u *strregname;
16537 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016538 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 int append;
16540 char_u yank_type;
16541 long block_len;
16542
16543 block_len = -1;
16544 yank_type = MAUTO;
16545 append = FALSE;
16546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016547 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016548 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016550 if (strregname == NULL)
16551 return; /* type error; errmsg already given */
16552 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 if (regname == 0 || regname == '@')
16554 regname = '"';
16555 else if (regname == '=')
16556 return;
16557
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016558 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016560 stropt = get_tv_string_chk(&argvars[2]);
16561 if (stropt == NULL)
16562 return; /* type error */
16563 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564 switch (*stropt)
16565 {
16566 case 'a': case 'A': /* append */
16567 append = TRUE;
16568 break;
16569 case 'v': case 'c': /* character-wise selection */
16570 yank_type = MCHAR;
16571 break;
16572 case 'V': case 'l': /* line-wise selection */
16573 yank_type = MLINE;
16574 break;
16575#ifdef FEAT_VISUAL
16576 case 'b': case Ctrl_V: /* block-wise selection */
16577 yank_type = MBLOCK;
16578 if (VIM_ISDIGIT(stropt[1]))
16579 {
16580 ++stropt;
16581 block_len = getdigits(&stropt) - 1;
16582 --stropt;
16583 }
16584 break;
16585#endif
16586 }
16587 }
16588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016589 strval = get_tv_string_chk(&argvars[1]);
16590 if (strval != NULL)
16591 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016593 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594}
16595
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016596/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016597 * "settabvar()" function
16598 */
16599 static void
16600f_settabvar(argvars, rettv)
16601 typval_T *argvars;
16602 typval_T *rettv;
16603{
16604 tabpage_T *save_curtab;
16605 char_u *varname, *tabvarname;
16606 typval_T *varp;
16607 tabpage_T *tp;
16608
16609 rettv->vval.v_number = 0;
16610
16611 if (check_restricted() || check_secure())
16612 return;
16613
16614 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16615 varname = get_tv_string_chk(&argvars[1]);
16616 varp = &argvars[2];
16617
16618 if (tp != NULL && varname != NULL && varp != NULL)
16619 {
16620 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016621 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016622
16623 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16624 if (tabvarname != NULL)
16625 {
16626 STRCPY(tabvarname, "t:");
16627 STRCPY(tabvarname + 2, varname);
16628 set_var(tabvarname, varp, TRUE);
16629 vim_free(tabvarname);
16630 }
16631
16632 /* Restore current tabpage */
16633 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016634 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016635 }
16636}
16637
16638/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016639 * "settabwinvar()" function
16640 */
16641 static void
16642f_settabwinvar(argvars, rettv)
16643 typval_T *argvars;
16644 typval_T *rettv;
16645{
16646 setwinvar(argvars, rettv, 1);
16647}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648
16649/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016650 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016653f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016654 typval_T *argvars;
16655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016657 setwinvar(argvars, rettv, 0);
16658}
16659
16660/*
16661 * "setwinvar()" and "settabwinvar()" functions
16662 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016663
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016664 static void
16665setwinvar(argvars, rettv, off)
16666 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016667 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016668 int off;
16669{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 win_T *win;
16671#ifdef FEAT_WINDOWS
16672 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016673 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674#endif
16675 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016676 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016678 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679
16680 if (check_restricted() || check_secure())
16681 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016682
16683#ifdef FEAT_WINDOWS
16684 if (off == 1)
16685 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16686 else
16687 tp = curtab;
16688#endif
16689 win = find_win_by_nr(&argvars[off], tp);
16690 varname = get_tv_string_chk(&argvars[off + 1]);
16691 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692
16693 if (win != NULL && varname != NULL && varp != NULL)
16694 {
16695#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016696 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016697 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698#endif
16699
16700 if (*varname == '&')
16701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016702 long numval;
16703 char_u *strval;
16704 int error = FALSE;
16705
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016707 numval = get_tv_number_chk(varp, &error);
16708 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 if (!error && strval != NULL)
16710 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 }
16712 else
16713 {
16714 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16715 if (winvarname != NULL)
16716 {
16717 STRCPY(winvarname, "w:");
16718 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016719 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 vim_free(winvarname);
16721 }
16722 }
16723
16724#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016725 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726#endif
16727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728}
16729
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016730#ifdef FEAT_CRYPT
16731/*
16732 * "sha256({string})" function
16733 */
16734 static void
16735f_sha256(argvars, rettv)
16736 typval_T *argvars;
16737 typval_T *rettv;
16738{
16739 char_u *p;
16740
16741 p = get_tv_string(&argvars[0]);
16742 rettv->vval.v_string = vim_strsave(
16743 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16744 rettv->v_type = VAR_STRING;
16745}
16746#endif /* FEAT_CRYPT */
16747
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016749 * "shellescape({string})" function
16750 */
16751 static void
16752f_shellescape(argvars, rettv)
16753 typval_T *argvars;
16754 typval_T *rettv;
16755{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016756 rettv->vval.v_string = vim_strsave_shellescape(
16757 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016758 rettv->v_type = VAR_STRING;
16759}
16760
16761/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016762 * shiftwidth() function
16763 */
16764 static void
16765f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016766 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016767 typval_T *rettv;
16768{
16769 rettv->vval.v_number = get_sw_value();
16770}
16771
16772/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016773 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 */
16775 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016776f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016777 typval_T *argvars;
16778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016780 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781
Bram Moolenaar0d660222005-01-07 21:51:51 +000016782 p = get_tv_string(&argvars[0]);
16783 rettv->vval.v_string = vim_strsave(p);
16784 simplify_filename(rettv->vval.v_string); /* simplify in place */
16785 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786}
16787
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016788#ifdef FEAT_FLOAT
16789/*
16790 * "sin()" function
16791 */
16792 static void
16793f_sin(argvars, rettv)
16794 typval_T *argvars;
16795 typval_T *rettv;
16796{
16797 float_T f;
16798
16799 rettv->v_type = VAR_FLOAT;
16800 if (get_float_arg(argvars, &f) == OK)
16801 rettv->vval.v_float = sin(f);
16802 else
16803 rettv->vval.v_float = 0.0;
16804}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016805
16806/*
16807 * "sinh()" function
16808 */
16809 static void
16810f_sinh(argvars, rettv)
16811 typval_T *argvars;
16812 typval_T *rettv;
16813{
16814 float_T f;
16815
16816 rettv->v_type = VAR_FLOAT;
16817 if (get_float_arg(argvars, &f) == OK)
16818 rettv->vval.v_float = sinh(f);
16819 else
16820 rettv->vval.v_float = 0.0;
16821}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016822#endif
16823
Bram Moolenaar0d660222005-01-07 21:51:51 +000016824static int
16825#ifdef __BORLANDC__
16826 _RTLENTRYF
16827#endif
16828 item_compare __ARGS((const void *s1, const void *s2));
16829static int
16830#ifdef __BORLANDC__
16831 _RTLENTRYF
16832#endif
16833 item_compare2 __ARGS((const void *s1, const void *s2));
16834
16835static int item_compare_ic;
16836static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016837static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016838static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839#define ITEM_COMPARE_FAIL 999
16840
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016842 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016844 static int
16845#ifdef __BORLANDC__
16846_RTLENTRYF
16847#endif
16848item_compare(s1, s2)
16849 const void *s1;
16850 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016852 char_u *p1, *p2;
16853 char_u *tofree1, *tofree2;
16854 int res;
16855 char_u numbuf1[NUMBUFLEN];
16856 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016858 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16859 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016860 if (p1 == NULL)
16861 p1 = (char_u *)"";
16862 if (p2 == NULL)
16863 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864 if (item_compare_ic)
16865 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867 res = STRCMP(p1, p2);
16868 vim_free(tofree1);
16869 vim_free(tofree2);
16870 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871}
16872
16873 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874#ifdef __BORLANDC__
16875_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877item_compare2(s1, s2)
16878 const void *s1;
16879 const void *s2;
16880{
16881 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016882 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016883 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016884 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016886 /* shortcut after failure in previous call; compare all items equal */
16887 if (item_compare_func_err)
16888 return 0;
16889
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016890 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16891 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016892 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16893 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016894
16895 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016896 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016897 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16898 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899 clear_tv(&argv[0]);
16900 clear_tv(&argv[1]);
16901
16902 if (res == FAIL)
16903 res = ITEM_COMPARE_FAIL;
16904 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016905 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16906 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016907 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908 clear_tv(&rettv);
16909 return res;
16910}
16911
16912/*
16913 * "sort({list})" function
16914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016916f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016917 typval_T *argvars;
16918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919{
Bram Moolenaar33570922005-01-25 22:26:29 +000016920 list_T *l;
16921 listitem_T *li;
16922 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 long len;
16924 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926 if (argvars[0].v_type != VAR_LIST)
16927 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 else
16929 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016931 if (l == NULL || tv_check_lock(l->lv_lock,
16932 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 return;
16934 rettv->vval.v_list = l;
16935 rettv->v_type = VAR_LIST;
16936 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938 len = list_len(l);
16939 if (len <= 1)
16940 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 item_compare_ic = FALSE;
16943 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016944 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 if (argvars[1].v_type != VAR_UNKNOWN)
16946 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016947 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016949 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950 else
16951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016952 int error = FALSE;
16953
16954 i = get_tv_number_chk(&argvars[1], &error);
16955 if (error)
16956 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 if (i == 1)
16958 item_compare_ic = TRUE;
16959 else
16960 item_compare_func = get_tv_string(&argvars[1]);
16961 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016962
16963 if (argvars[2].v_type != VAR_UNKNOWN)
16964 {
16965 /* optional third argument: {dict} */
16966 if (argvars[2].v_type != VAR_DICT)
16967 {
16968 EMSG(_(e_dictreq));
16969 return;
16970 }
16971 item_compare_selfdict = argvars[2].vval.v_dict;
16972 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 if (ptrs == NULL)
16978 return;
16979 i = 0;
16980 for (li = l->lv_first; li != NULL; li = li->li_next)
16981 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016983 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 /* test the compare function */
16985 if (item_compare_func != NULL
16986 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16987 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016988 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990 {
16991 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016992 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993 item_compare_func == NULL ? item_compare : item_compare2);
16994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016995 if (!item_compare_func_err)
16996 {
16997 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016998 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 l->lv_len = 0;
17000 for (i = 0; i < len; ++i)
17001 list_append(l, ptrs[i]);
17002 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017003 }
17004
17005 vim_free(ptrs);
17006 }
17007}
17008
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017009/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017010 * "soundfold({word})" function
17011 */
17012 static void
17013f_soundfold(argvars, rettv)
17014 typval_T *argvars;
17015 typval_T *rettv;
17016{
17017 char_u *s;
17018
17019 rettv->v_type = VAR_STRING;
17020 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017021#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017022 rettv->vval.v_string = eval_soundfold(s);
17023#else
17024 rettv->vval.v_string = vim_strsave(s);
17025#endif
17026}
17027
17028/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017029 * "spellbadword()" function
17030 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017031 static void
17032f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017033 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017034 typval_T *rettv;
17035{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017036 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017037 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017038 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017039
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017040 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017041 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017042
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017043#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017044 if (argvars[0].v_type == VAR_UNKNOWN)
17045 {
17046 /* Find the start and length of the badly spelled word. */
17047 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17048 if (len != 0)
17049 word = ml_get_cursor();
17050 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017051 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017052 {
17053 char_u *str = get_tv_string_chk(&argvars[0]);
17054 int capcol = -1;
17055
17056 if (str != NULL)
17057 {
17058 /* Check the argument for spelling. */
17059 while (*str != NUL)
17060 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017061 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017062 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017063 {
17064 word = str;
17065 break;
17066 }
17067 str += len;
17068 }
17069 }
17070 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017071#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017072
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017073 list_append_string(rettv->vval.v_list, word, len);
17074 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017075 attr == HLF_SPB ? "bad" :
17076 attr == HLF_SPR ? "rare" :
17077 attr == HLF_SPL ? "local" :
17078 attr == HLF_SPC ? "caps" :
17079 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017080}
17081
17082/*
17083 * "spellsuggest()" function
17084 */
17085 static void
17086f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017087 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017088 typval_T *rettv;
17089{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017090#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017091 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017092 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017093 int maxcount;
17094 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017095 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017096 listitem_T *li;
17097 int need_capital = FALSE;
17098#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017099
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017100 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017101 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017102
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017103#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017104 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017105 {
17106 str = get_tv_string(&argvars[0]);
17107 if (argvars[1].v_type != VAR_UNKNOWN)
17108 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017109 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017110 if (maxcount <= 0)
17111 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017112 if (argvars[2].v_type != VAR_UNKNOWN)
17113 {
17114 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17115 if (typeerr)
17116 return;
17117 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017118 }
17119 else
17120 maxcount = 25;
17121
Bram Moolenaar4770d092006-01-12 23:22:24 +000017122 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017123
17124 for (i = 0; i < ga.ga_len; ++i)
17125 {
17126 str = ((char_u **)ga.ga_data)[i];
17127
17128 li = listitem_alloc();
17129 if (li == NULL)
17130 vim_free(str);
17131 else
17132 {
17133 li->li_tv.v_type = VAR_STRING;
17134 li->li_tv.v_lock = 0;
17135 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017136 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017137 }
17138 }
17139 ga_clear(&ga);
17140 }
17141#endif
17142}
17143
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017145f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017146 typval_T *argvars;
17147 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017148{
17149 char_u *str;
17150 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017151 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 regmatch_T regmatch;
17153 char_u patbuf[NUMBUFLEN];
17154 char_u *save_cpo;
17155 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017157 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017158 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159
17160 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17161 save_cpo = p_cpo;
17162 p_cpo = (char_u *)"";
17163
17164 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017165 if (argvars[1].v_type != VAR_UNKNOWN)
17166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017167 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17168 if (pat == NULL)
17169 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017170 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017171 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017172 }
17173 if (pat == NULL || *pat == NUL)
17174 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017175
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017176 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017178 if (typeerr)
17179 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17182 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017185 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017186 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017187 if (*str == NUL)
17188 match = FALSE; /* empty item at the end */
17189 else
17190 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191 if (match)
17192 end = regmatch.startp[0];
17193 else
17194 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017195 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17196 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017198 if (list_append_string(rettv->vval.v_list, str,
17199 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017200 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201 }
17202 if (!match)
17203 break;
17204 /* Advance to just after the match. */
17205 if (regmatch.endp[0] > str)
17206 col = 0;
17207 else
17208 {
17209 /* Don't get stuck at the same match. */
17210#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017211 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017212#else
17213 col = 1;
17214#endif
17215 }
17216 str = regmatch.endp[0];
17217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218
Bram Moolenaar473de612013-06-08 18:19:48 +020017219 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223}
17224
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017225#ifdef FEAT_FLOAT
17226/*
17227 * "sqrt()" function
17228 */
17229 static void
17230f_sqrt(argvars, rettv)
17231 typval_T *argvars;
17232 typval_T *rettv;
17233{
17234 float_T f;
17235
17236 rettv->v_type = VAR_FLOAT;
17237 if (get_float_arg(argvars, &f) == OK)
17238 rettv->vval.v_float = sqrt(f);
17239 else
17240 rettv->vval.v_float = 0.0;
17241}
17242
17243/*
17244 * "str2float()" function
17245 */
17246 static void
17247f_str2float(argvars, rettv)
17248 typval_T *argvars;
17249 typval_T *rettv;
17250{
17251 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17252
17253 if (*p == '+')
17254 p = skipwhite(p + 1);
17255 (void)string2float(p, &rettv->vval.v_float);
17256 rettv->v_type = VAR_FLOAT;
17257}
17258#endif
17259
Bram Moolenaar2c932302006-03-18 21:42:09 +000017260/*
17261 * "str2nr()" function
17262 */
17263 static void
17264f_str2nr(argvars, rettv)
17265 typval_T *argvars;
17266 typval_T *rettv;
17267{
17268 int base = 10;
17269 char_u *p;
17270 long n;
17271
17272 if (argvars[1].v_type != VAR_UNKNOWN)
17273 {
17274 base = get_tv_number(&argvars[1]);
17275 if (base != 8 && base != 10 && base != 16)
17276 {
17277 EMSG(_(e_invarg));
17278 return;
17279 }
17280 }
17281
17282 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017283 if (*p == '+')
17284 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017285 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17286 rettv->vval.v_number = n;
17287}
17288
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289#ifdef HAVE_STRFTIME
17290/*
17291 * "strftime({format}[, {time}])" function
17292 */
17293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017294f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017295 typval_T *argvars;
17296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297{
17298 char_u result_buf[256];
17299 struct tm *curtime;
17300 time_t seconds;
17301 char_u *p;
17302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017303 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017305 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017306 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 seconds = time(NULL);
17308 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017309 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 curtime = localtime(&seconds);
17311 /* MSVC returns NULL for an invalid value of seconds. */
17312 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017313 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 else
17315 {
17316# ifdef FEAT_MBYTE
17317 vimconv_T conv;
17318 char_u *enc;
17319
17320 conv.vc_type = CONV_NONE;
17321 enc = enc_locale();
17322 convert_setup(&conv, p_enc, enc);
17323 if (conv.vc_type != CONV_NONE)
17324 p = string_convert(&conv, p, NULL);
17325# endif
17326 if (p != NULL)
17327 (void)strftime((char *)result_buf, sizeof(result_buf),
17328 (char *)p, curtime);
17329 else
17330 result_buf[0] = NUL;
17331
17332# ifdef FEAT_MBYTE
17333 if (conv.vc_type != CONV_NONE)
17334 vim_free(p);
17335 convert_setup(&conv, enc, p_enc);
17336 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017337 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 else
17339# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017340 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341
17342# ifdef FEAT_MBYTE
17343 /* Release conversion descriptors */
17344 convert_setup(&conv, NULL, NULL);
17345 vim_free(enc);
17346# endif
17347 }
17348}
17349#endif
17350
17351/*
17352 * "stridx()" function
17353 */
17354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017355f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017356 typval_T *argvars;
17357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358{
17359 char_u buf[NUMBUFLEN];
17360 char_u *needle;
17361 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017364 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017366 needle = get_tv_string_chk(&argvars[1]);
17367 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 if (needle == NULL || haystack == NULL)
17370 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
Bram Moolenaar33570922005-01-25 22:26:29 +000017372 if (argvars[2].v_type != VAR_UNKNOWN)
17373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017374 int error = FALSE;
17375
17376 start_idx = get_tv_number_chk(&argvars[2], &error);
17377 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017379 if (start_idx >= 0)
17380 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017381 }
17382
17383 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17384 if (pos != NULL)
17385 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386}
17387
17388/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017389 * "string()" function
17390 */
17391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017392f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017393 typval_T *argvars;
17394 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017395{
17396 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017397 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017399 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017400 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017401 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017402 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404}
17405
17406/*
17407 * "strlen()" function
17408 */
17409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017411 typval_T *argvars;
17412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414 rettv->vval.v_number = (varnumber_T)(STRLEN(
17415 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416}
17417
17418/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017419 * "strchars()" function
17420 */
17421 static void
17422f_strchars(argvars, rettv)
17423 typval_T *argvars;
17424 typval_T *rettv;
17425{
17426 char_u *s = get_tv_string(&argvars[0]);
17427#ifdef FEAT_MBYTE
17428 varnumber_T len = 0;
17429
17430 while (*s != NUL)
17431 {
17432 mb_cptr2char_adv(&s);
17433 ++len;
17434 }
17435 rettv->vval.v_number = len;
17436#else
17437 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17438#endif
17439}
17440
17441/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017442 * "strdisplaywidth()" function
17443 */
17444 static void
17445f_strdisplaywidth(argvars, rettv)
17446 typval_T *argvars;
17447 typval_T *rettv;
17448{
17449 char_u *s = get_tv_string(&argvars[0]);
17450 int col = 0;
17451
17452 if (argvars[1].v_type != VAR_UNKNOWN)
17453 col = get_tv_number(&argvars[1]);
17454
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017455 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017456}
17457
17458/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017459 * "strwidth()" function
17460 */
17461 static void
17462f_strwidth(argvars, rettv)
17463 typval_T *argvars;
17464 typval_T *rettv;
17465{
17466 char_u *s = get_tv_string(&argvars[0]);
17467
17468 rettv->vval.v_number = (varnumber_T)(
17469#ifdef FEAT_MBYTE
17470 mb_string2cells(s, -1)
17471#else
17472 STRLEN(s)
17473#endif
17474 );
17475}
17476
17477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 * "strpart()" function
17479 */
17480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017481f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017482 typval_T *argvars;
17483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484{
17485 char_u *p;
17486 int n;
17487 int len;
17488 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017489 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017491 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 slen = (int)STRLEN(p);
17493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017494 n = get_tv_number_chk(&argvars[1], &error);
17495 if (error)
17496 len = 0;
17497 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017498 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 else
17500 len = slen - n; /* default len: all bytes that are available. */
17501
17502 /*
17503 * Only return the overlap between the specified part and the actual
17504 * string.
17505 */
17506 if (n < 0)
17507 {
17508 len += n;
17509 n = 0;
17510 }
17511 else if (n > slen)
17512 n = slen;
17513 if (len < 0)
17514 len = 0;
17515 else if (n + len > slen)
17516 len = slen - n;
17517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017518 rettv->v_type = VAR_STRING;
17519 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520}
17521
17522/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017523 * "strridx()" function
17524 */
17525 static void
17526f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017527 typval_T *argvars;
17528 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017529{
17530 char_u buf[NUMBUFLEN];
17531 char_u *needle;
17532 char_u *haystack;
17533 char_u *rest;
17534 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017535 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017537 needle = get_tv_string_chk(&argvars[1]);
17538 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017539
17540 rettv->vval.v_number = -1;
17541 if (needle == NULL || haystack == NULL)
17542 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017543
17544 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017545 if (argvars[2].v_type != VAR_UNKNOWN)
17546 {
17547 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017548 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017549 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017550 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017551 }
17552 else
17553 end_idx = haystack_len;
17554
Bram Moolenaar0d660222005-01-07 21:51:51 +000017555 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017556 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017557 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017558 lastmatch = haystack + end_idx;
17559 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017560 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017561 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017562 for (rest = haystack; *rest != '\0'; ++rest)
17563 {
17564 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017565 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017566 break;
17567 lastmatch = rest;
17568 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017569 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017570
17571 if (lastmatch == NULL)
17572 rettv->vval.v_number = -1;
17573 else
17574 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17575}
17576
17577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 * "strtrans()" function
17579 */
17580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017581f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017582 typval_T *argvars;
17583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017585 rettv->v_type = VAR_STRING;
17586 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587}
17588
17589/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017590 * "submatch()" function
17591 */
17592 static void
17593f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017594 typval_T *argvars;
17595 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596{
17597 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017598 rettv->vval.v_string =
17599 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017600}
17601
17602/*
17603 * "substitute()" function
17604 */
17605 static void
17606f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017607 typval_T *argvars;
17608 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017609{
17610 char_u patbuf[NUMBUFLEN];
17611 char_u subbuf[NUMBUFLEN];
17612 char_u flagsbuf[NUMBUFLEN];
17613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017614 char_u *str = get_tv_string_chk(&argvars[0]);
17615 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17616 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17617 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17618
Bram Moolenaar0d660222005-01-07 21:51:51 +000017619 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017620 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17621 rettv->vval.v_string = NULL;
17622 else
17623 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017624}
17625
17626/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017627 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633{
17634 int id = 0;
17635#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017636 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 long col;
17638 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017639 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017641 lnum = get_tv_lnum(argvars); /* -1 on type error */
17642 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17643 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017645 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017646 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017647 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648#endif
17649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651}
17652
17653/*
17654 * "synIDattr(id, what [, mode])" function
17655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017657f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017658 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660{
17661 char_u *p = NULL;
17662#ifdef FEAT_SYN_HL
17663 int id;
17664 char_u *what;
17665 char_u *mode;
17666 char_u modebuf[NUMBUFLEN];
17667 int modec;
17668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017669 id = get_tv_number(&argvars[0]);
17670 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017671 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017673 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017675 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 modec = 0; /* replace invalid with current */
17677 }
17678 else
17679 {
17680#ifdef FEAT_GUI
17681 if (gui.in_use)
17682 modec = 'g';
17683 else
17684#endif
17685 if (t_colors > 1)
17686 modec = 'c';
17687 else
17688 modec = 't';
17689 }
17690
17691
17692 switch (TOLOWER_ASC(what[0]))
17693 {
17694 case 'b':
17695 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17696 p = highlight_color(id, what, modec);
17697 else /* bold */
17698 p = highlight_has_attr(id, HL_BOLD, modec);
17699 break;
17700
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017701 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 p = highlight_color(id, what, modec);
17703 break;
17704
17705 case 'i':
17706 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17707 p = highlight_has_attr(id, HL_INVERSE, modec);
17708 else /* italic */
17709 p = highlight_has_attr(id, HL_ITALIC, modec);
17710 break;
17711
17712 case 'n': /* name */
17713 p = get_highlight_name(NULL, id - 1);
17714 break;
17715
17716 case 'r': /* reverse */
17717 p = highlight_has_attr(id, HL_INVERSE, modec);
17718 break;
17719
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017720 case 's':
17721 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17722 p = highlight_color(id, what, modec);
17723 else /* standout */
17724 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 break;
17726
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017727 case 'u':
17728 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17729 /* underline */
17730 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17731 else
17732 /* undercurl */
17733 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 break;
17735 }
17736
17737 if (p != NULL)
17738 p = vim_strsave(p);
17739#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017740 rettv->v_type = VAR_STRING;
17741 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742}
17743
17744/*
17745 * "synIDtrans(id)" function
17746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017748f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017749 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751{
17752 int id;
17753
17754#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017755 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756
17757 if (id > 0)
17758 id = syn_get_final_id(id);
17759 else
17760#endif
17761 id = 0;
17762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017763 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764}
17765
17766/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017767 * "synconcealed(lnum, col)" function
17768 */
17769 static void
17770f_synconcealed(argvars, rettv)
17771 typval_T *argvars UNUSED;
17772 typval_T *rettv;
17773{
17774#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17775 long lnum;
17776 long col;
17777 int syntax_flags = 0;
17778 int cchar;
17779 int matchid = 0;
17780 char_u str[NUMBUFLEN];
17781#endif
17782
17783 rettv->v_type = VAR_LIST;
17784 rettv->vval.v_list = NULL;
17785
17786#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17787 lnum = get_tv_lnum(argvars); /* -1 on type error */
17788 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17789
17790 vim_memset(str, NUL, sizeof(str));
17791
17792 if (rettv_list_alloc(rettv) != FAIL)
17793 {
17794 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17795 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17796 && curwin->w_p_cole > 0)
17797 {
17798 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17799 syntax_flags = get_syntax_info(&matchid);
17800
17801 /* get the conceal character */
17802 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17803 {
17804 cchar = syn_get_sub_char();
17805 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17806 cchar = lcs_conceal;
17807 if (cchar != NUL)
17808 {
17809# ifdef FEAT_MBYTE
17810 if (has_mbyte)
17811 (*mb_char2bytes)(cchar, str);
17812 else
17813# endif
17814 str[0] = cchar;
17815 }
17816 }
17817 }
17818
17819 list_append_number(rettv->vval.v_list,
17820 (syntax_flags & HL_CONCEAL) != 0);
17821 /* -1 to auto-determine strlen */
17822 list_append_string(rettv->vval.v_list, str, -1);
17823 list_append_number(rettv->vval.v_list, matchid);
17824 }
17825#endif
17826}
17827
17828/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017829 * "synstack(lnum, col)" function
17830 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017831 static void
17832f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017833 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017834 typval_T *rettv;
17835{
17836#ifdef FEAT_SYN_HL
17837 long lnum;
17838 long col;
17839 int i;
17840 int id;
17841#endif
17842
17843 rettv->v_type = VAR_LIST;
17844 rettv->vval.v_list = NULL;
17845
17846#ifdef FEAT_SYN_HL
17847 lnum = get_tv_lnum(argvars); /* -1 on type error */
17848 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17849
17850 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017851 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017852 && rettv_list_alloc(rettv) != FAIL)
17853 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017854 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017855 for (i = 0; ; ++i)
17856 {
17857 id = syn_get_stack_item(i);
17858 if (id < 0)
17859 break;
17860 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17861 break;
17862 }
17863 }
17864#endif
17865}
17866
17867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 * "system()" function
17869 */
17870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017871f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017872 typval_T *argvars;
17873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017875 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017877 char_u *infile = NULL;
17878 char_u buf[NUMBUFLEN];
17879 int err = FALSE;
17880 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017882 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017883 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017884
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017885 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017886 {
17887 /*
17888 * Write the string to a temp file, to be used for input of the shell
17889 * command.
17890 */
17891 if ((infile = vim_tempname('i')) == NULL)
17892 {
17893 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017894 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017895 }
17896
17897 fd = mch_fopen((char *)infile, WRITEBIN);
17898 if (fd == NULL)
17899 {
17900 EMSG2(_(e_notopen), infile);
17901 goto done;
17902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017903 p = get_tv_string_buf_chk(&argvars[1], buf);
17904 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017905 {
17906 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017907 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017908 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017909 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17910 err = TRUE;
17911 if (fclose(fd) != 0)
17912 err = TRUE;
17913 if (err)
17914 {
17915 EMSG(_("E677: Error writing temp file"));
17916 goto done;
17917 }
17918 }
17919
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017920 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17921 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017922
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923#ifdef USE_CR
17924 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017925 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 {
17927 char_u *s;
17928
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017929 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 {
17931 if (*s == CAR)
17932 *s = NL;
17933 }
17934 }
17935#else
17936# ifdef USE_CRNL
17937 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017938 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 {
17940 char_u *s, *d;
17941
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017942 d = res;
17943 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 {
17945 if (s[0] == CAR && s[1] == NL)
17946 ++s;
17947 *d++ = *s;
17948 }
17949 *d = NUL;
17950 }
17951# endif
17952#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017953
17954done:
17955 if (infile != NULL)
17956 {
17957 mch_remove(infile);
17958 vim_free(infile);
17959 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017960 rettv->v_type = VAR_STRING;
17961 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962}
17963
17964/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017965 * "tabpagebuflist()" function
17966 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017967 static void
17968f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017969 typval_T *argvars UNUSED;
17970 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017971{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017972#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017973 tabpage_T *tp;
17974 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017975
17976 if (argvars[0].v_type == VAR_UNKNOWN)
17977 wp = firstwin;
17978 else
17979 {
17980 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17981 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017982 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017983 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017984 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017985 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017986 for (; wp != NULL; wp = wp->w_next)
17987 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017988 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017989 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017990 }
17991#endif
17992}
17993
17994
17995/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017996 * "tabpagenr()" function
17997 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017998 static void
17999f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018000 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018001 typval_T *rettv;
18002{
18003 int nr = 1;
18004#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018005 char_u *arg;
18006
18007 if (argvars[0].v_type != VAR_UNKNOWN)
18008 {
18009 arg = get_tv_string_chk(&argvars[0]);
18010 nr = 0;
18011 if (arg != NULL)
18012 {
18013 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018014 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018015 else
18016 EMSG2(_(e_invexpr2), arg);
18017 }
18018 }
18019 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018020 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018021#endif
18022 rettv->vval.v_number = nr;
18023}
18024
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018025
18026#ifdef FEAT_WINDOWS
18027static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18028
18029/*
18030 * Common code for tabpagewinnr() and winnr().
18031 */
18032 static int
18033get_winnr(tp, argvar)
18034 tabpage_T *tp;
18035 typval_T *argvar;
18036{
18037 win_T *twin;
18038 int nr = 1;
18039 win_T *wp;
18040 char_u *arg;
18041
18042 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18043 if (argvar->v_type != VAR_UNKNOWN)
18044 {
18045 arg = get_tv_string_chk(argvar);
18046 if (arg == NULL)
18047 nr = 0; /* type error; errmsg already given */
18048 else if (STRCMP(arg, "$") == 0)
18049 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18050 else if (STRCMP(arg, "#") == 0)
18051 {
18052 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18053 if (twin == NULL)
18054 nr = 0;
18055 }
18056 else
18057 {
18058 EMSG2(_(e_invexpr2), arg);
18059 nr = 0;
18060 }
18061 }
18062
18063 if (nr > 0)
18064 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18065 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018066 {
18067 if (wp == NULL)
18068 {
18069 /* didn't find it in this tabpage */
18070 nr = 0;
18071 break;
18072 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018073 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018074 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018075 return nr;
18076}
18077#endif
18078
18079/*
18080 * "tabpagewinnr()" function
18081 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018082 static void
18083f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018084 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018085 typval_T *rettv;
18086{
18087 int nr = 1;
18088#ifdef FEAT_WINDOWS
18089 tabpage_T *tp;
18090
18091 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18092 if (tp == NULL)
18093 nr = 0;
18094 else
18095 nr = get_winnr(tp, &argvars[1]);
18096#endif
18097 rettv->vval.v_number = nr;
18098}
18099
18100
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018101/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018102 * "tagfiles()" function
18103 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018104 static void
18105f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018106 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018107 typval_T *rettv;
18108{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018109 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018110 tagname_T tn;
18111 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018112
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018113 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018114 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018115 fname = alloc(MAXPATHL);
18116 if (fname == NULL)
18117 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018118
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018119 for (first = TRUE; ; first = FALSE)
18120 if (get_tagfname(&tn, first, fname) == FAIL
18121 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018122 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018123 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018124 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018125}
18126
18127/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018128 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018129 */
18130 static void
18131f_taglist(argvars, rettv)
18132 typval_T *argvars;
18133 typval_T *rettv;
18134{
18135 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018136
18137 tag_pattern = get_tv_string(&argvars[0]);
18138
18139 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018140 if (*tag_pattern == NUL)
18141 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018142
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018143 if (rettv_list_alloc(rettv) == OK)
18144 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018145}
18146
18147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 * "tempname()" function
18149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018151f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018152 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154{
18155 static int x = 'A';
18156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018157 rettv->v_type = VAR_STRING;
18158 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159
18160 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18161 * names. Skip 'I' and 'O', they are used for shell redirection. */
18162 do
18163 {
18164 if (x == 'Z')
18165 x = '0';
18166 else if (x == '9')
18167 x = 'A';
18168 else
18169 {
18170#ifdef EBCDIC
18171 if (x == 'I')
18172 x = 'J';
18173 else if (x == 'R')
18174 x = 'S';
18175 else
18176#endif
18177 ++x;
18178 }
18179 } while (x == 'I' || x == 'O');
18180}
18181
18182/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018183 * "test(list)" function: Just checking the walls...
18184 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018185 static void
18186f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018187 typval_T *argvars UNUSED;
18188 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018189{
18190 /* Used for unit testing. Change the code below to your liking. */
18191#if 0
18192 listitem_T *li;
18193 list_T *l;
18194 char_u *bad, *good;
18195
18196 if (argvars[0].v_type != VAR_LIST)
18197 return;
18198 l = argvars[0].vval.v_list;
18199 if (l == NULL)
18200 return;
18201 li = l->lv_first;
18202 if (li == NULL)
18203 return;
18204 bad = get_tv_string(&li->li_tv);
18205 li = li->li_next;
18206 if (li == NULL)
18207 return;
18208 good = get_tv_string(&li->li_tv);
18209 rettv->vval.v_number = test_edit_score(bad, good);
18210#endif
18211}
18212
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018213#ifdef FEAT_FLOAT
18214/*
18215 * "tan()" function
18216 */
18217 static void
18218f_tan(argvars, rettv)
18219 typval_T *argvars;
18220 typval_T *rettv;
18221{
18222 float_T f;
18223
18224 rettv->v_type = VAR_FLOAT;
18225 if (get_float_arg(argvars, &f) == OK)
18226 rettv->vval.v_float = tan(f);
18227 else
18228 rettv->vval.v_float = 0.0;
18229}
18230
18231/*
18232 * "tanh()" function
18233 */
18234 static void
18235f_tanh(argvars, rettv)
18236 typval_T *argvars;
18237 typval_T *rettv;
18238{
18239 float_T f;
18240
18241 rettv->v_type = VAR_FLOAT;
18242 if (get_float_arg(argvars, &f) == OK)
18243 rettv->vval.v_float = tanh(f);
18244 else
18245 rettv->vval.v_float = 0.0;
18246}
18247#endif
18248
Bram Moolenaard52d9742005-08-21 22:20:28 +000018249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 * "tolower(string)" function
18251 */
18252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018253f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018254 typval_T *argvars;
18255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256{
18257 char_u *p;
18258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018259 p = vim_strsave(get_tv_string(&argvars[0]));
18260 rettv->v_type = VAR_STRING;
18261 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262
18263 if (p != NULL)
18264 while (*p != NUL)
18265 {
18266#ifdef FEAT_MBYTE
18267 int l;
18268
18269 if (enc_utf8)
18270 {
18271 int c, lc;
18272
18273 c = utf_ptr2char(p);
18274 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018275 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 /* TODO: reallocate string when byte count changes. */
18277 if (utf_char2len(lc) == l)
18278 utf_char2bytes(lc, p);
18279 p += l;
18280 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018281 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 p += l; /* skip multi-byte character */
18283 else
18284#endif
18285 {
18286 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18287 ++p;
18288 }
18289 }
18290}
18291
18292/*
18293 * "toupper(string)" function
18294 */
18295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018296f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018297 typval_T *argvars;
18298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018300 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302}
18303
18304/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018305 * "tr(string, fromstr, tostr)" function
18306 */
18307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018308f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018309 typval_T *argvars;
18310 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018311{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018312 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018313 char_u *fromstr;
18314 char_u *tostr;
18315 char_u *p;
18316#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018317 int inlen;
18318 int fromlen;
18319 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018320 int idx;
18321 char_u *cpstr;
18322 int cplen;
18323 int first = TRUE;
18324#endif
18325 char_u buf[NUMBUFLEN];
18326 char_u buf2[NUMBUFLEN];
18327 garray_T ga;
18328
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018329 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018330 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18331 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018332
18333 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018334 rettv->v_type = VAR_STRING;
18335 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018336 if (fromstr == NULL || tostr == NULL)
18337 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018338 ga_init2(&ga, (int)sizeof(char), 80);
18339
18340#ifdef FEAT_MBYTE
18341 if (!has_mbyte)
18342#endif
18343 /* not multi-byte: fromstr and tostr must be the same length */
18344 if (STRLEN(fromstr) != STRLEN(tostr))
18345 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018346#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018347error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018348#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018349 EMSG2(_(e_invarg2), fromstr);
18350 ga_clear(&ga);
18351 return;
18352 }
18353
18354 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018355 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018356 {
18357#ifdef FEAT_MBYTE
18358 if (has_mbyte)
18359 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018360 inlen = (*mb_ptr2len)(in_str);
18361 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018362 cplen = inlen;
18363 idx = 0;
18364 for (p = fromstr; *p != NUL; p += fromlen)
18365 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018366 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018367 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018368 {
18369 for (p = tostr; *p != NUL; p += tolen)
18370 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018371 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018372 if (idx-- == 0)
18373 {
18374 cplen = tolen;
18375 cpstr = p;
18376 break;
18377 }
18378 }
18379 if (*p == NUL) /* tostr is shorter than fromstr */
18380 goto error;
18381 break;
18382 }
18383 ++idx;
18384 }
18385
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018386 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018387 {
18388 /* Check that fromstr and tostr have the same number of
18389 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018390 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018391 first = FALSE;
18392 for (p = tostr; *p != NUL; p += tolen)
18393 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018394 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018395 --idx;
18396 }
18397 if (idx != 0)
18398 goto error;
18399 }
18400
18401 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018402 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018403 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018404
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018405 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018406 }
18407 else
18408#endif
18409 {
18410 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018411 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018412 if (p != NULL)
18413 ga_append(&ga, tostr[p - fromstr]);
18414 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018415 ga_append(&ga, *in_str);
18416 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018417 }
18418 }
18419
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018420 /* add a terminating NUL */
18421 ga_grow(&ga, 1);
18422 ga_append(&ga, NUL);
18423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018424 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018425}
18426
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018427#ifdef FEAT_FLOAT
18428/*
18429 * "trunc({float})" function
18430 */
18431 static void
18432f_trunc(argvars, rettv)
18433 typval_T *argvars;
18434 typval_T *rettv;
18435{
18436 float_T f;
18437
18438 rettv->v_type = VAR_FLOAT;
18439 if (get_float_arg(argvars, &f) == OK)
18440 /* trunc() is not in C90, use floor() or ceil() instead. */
18441 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18442 else
18443 rettv->vval.v_float = 0.0;
18444}
18445#endif
18446
Bram Moolenaar8299df92004-07-10 09:47:34 +000018447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 * "type(expr)" function
18449 */
18450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018451f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018452 typval_T *argvars;
18453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018455 int n;
18456
18457 switch (argvars[0].v_type)
18458 {
18459 case VAR_NUMBER: n = 0; break;
18460 case VAR_STRING: n = 1; break;
18461 case VAR_FUNC: n = 2; break;
18462 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018463 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018464#ifdef FEAT_FLOAT
18465 case VAR_FLOAT: n = 5; break;
18466#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018467 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18468 }
18469 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470}
18471
18472/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018473 * "undofile(name)" function
18474 */
18475 static void
18476f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018477 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018478 typval_T *rettv;
18479{
18480 rettv->v_type = VAR_STRING;
18481#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018482 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018483 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018484
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018485 if (*fname == NUL)
18486 {
18487 /* If there is no file name there will be no undo file. */
18488 rettv->vval.v_string = NULL;
18489 }
18490 else
18491 {
18492 char_u *ffname = FullName_save(fname, FALSE);
18493
18494 if (ffname != NULL)
18495 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18496 vim_free(ffname);
18497 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018498 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018499#else
18500 rettv->vval.v_string = NULL;
18501#endif
18502}
18503
18504/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018505 * "undotree()" function
18506 */
18507 static void
18508f_undotree(argvars, rettv)
18509 typval_T *argvars UNUSED;
18510 typval_T *rettv;
18511{
18512 if (rettv_dict_alloc(rettv) == OK)
18513 {
18514 dict_T *dict = rettv->vval.v_dict;
18515 list_T *list;
18516
Bram Moolenaar730cde92010-06-27 05:18:54 +020018517 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018518 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018519 dict_add_nr_str(dict, "save_last",
18520 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018521 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18522 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018523 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018524
18525 list = list_alloc();
18526 if (list != NULL)
18527 {
18528 u_eval_tree(curbuf->b_u_oldhead, list);
18529 dict_add_list(dict, "entries", list);
18530 }
18531 }
18532}
18533
18534/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018535 * "values(dict)" function
18536 */
18537 static void
18538f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018539 typval_T *argvars;
18540 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018541{
18542 dict_list(argvars, rettv, 1);
18543}
18544
18545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 * "virtcol(string)" function
18547 */
18548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018549f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018550 typval_T *argvars;
18551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552{
18553 colnr_T vcol = 0;
18554 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018555 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018557 fp = var2fpos(&argvars[0], FALSE, &fnum);
18558 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18559 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 {
18561 getvvcol(curwin, fp, NULL, NULL, &vcol);
18562 ++vcol;
18563 }
18564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018565 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566}
18567
18568/*
18569 * "visualmode()" function
18570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018573 typval_T *argvars UNUSED;
18574 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575{
18576#ifdef FEAT_VISUAL
18577 char_u str[2];
18578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018579 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 str[0] = curbuf->b_visual_mode_eval;
18581 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583
18584 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018585 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587#endif
18588}
18589
18590/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018591 * "wildmenumode()" function
18592 */
18593 static void
18594f_wildmenumode(argvars, rettv)
18595 typval_T *argvars UNUSED;
18596 typval_T *rettv UNUSED;
18597{
18598#ifdef FEAT_WILDMENU
18599 if (wild_menu_showing)
18600 rettv->vval.v_number = 1;
18601#endif
18602}
18603
18604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 * "winbufnr(nr)" function
18606 */
18607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018608f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018609 typval_T *argvars;
18610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611{
18612 win_T *wp;
18613
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018614 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018618 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619}
18620
18621/*
18622 * "wincol()" function
18623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018626 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628{
18629 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631}
18632
18633/*
18634 * "winheight(nr)" function
18635 */
18636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018637f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018638 typval_T *argvars;
18639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640{
18641 win_T *wp;
18642
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018643 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018645 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018647 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648}
18649
18650/*
18651 * "winline()" function
18652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018654f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018655 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657{
18658 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660}
18661
18662/*
18663 * "winnr()" function
18664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018666f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669{
18670 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018671
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018673 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676}
18677
18678/*
18679 * "winrestcmd()" function
18680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018682f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685{
18686#ifdef FEAT_WINDOWS
18687 win_T *wp;
18688 int winnr = 1;
18689 garray_T ga;
18690 char_u buf[50];
18691
18692 ga_init2(&ga, (int)sizeof(char), 70);
18693 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18694 {
18695 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18696 ga_concat(&ga, buf);
18697# ifdef FEAT_VERTSPLIT
18698 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18699 ga_concat(&ga, buf);
18700# endif
18701 ++winnr;
18702 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018703 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018705 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018707 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018709 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710}
18711
18712/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018713 * "winrestview()" function
18714 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018715 static void
18716f_winrestview(argvars, rettv)
18717 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018718 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018719{
18720 dict_T *dict;
18721
18722 if (argvars[0].v_type != VAR_DICT
18723 || (dict = argvars[0].vval.v_dict) == NULL)
18724 EMSG(_(e_invarg));
18725 else
18726 {
18727 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18728 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18729#ifdef FEAT_VIRTUALEDIT
18730 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18731#endif
18732 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018733 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018734
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018735 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018736#ifdef FEAT_DIFF
18737 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18738#endif
18739 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18740 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18741
18742 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018743 win_new_height(curwin, curwin->w_height);
18744# ifdef FEAT_VERTSPLIT
18745 win_new_width(curwin, W_WIDTH(curwin));
18746# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018747 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018748
18749 if (curwin->w_topline == 0)
18750 curwin->w_topline = 1;
18751 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18752 curwin->w_topline = curbuf->b_ml.ml_line_count;
18753#ifdef FEAT_DIFF
18754 check_topfill(curwin, TRUE);
18755#endif
18756 }
18757}
18758
18759/*
18760 * "winsaveview()" function
18761 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018762 static void
18763f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018764 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018765 typval_T *rettv;
18766{
18767 dict_T *dict;
18768
Bram Moolenaara800b422010-06-27 01:15:55 +020018769 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018770 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018771 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018772
18773 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18774 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18775#ifdef FEAT_VIRTUALEDIT
18776 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18777#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018778 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018779 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18780
18781 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18782#ifdef FEAT_DIFF
18783 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18784#endif
18785 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18786 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18787}
18788
18789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 * "winwidth(nr)" function
18791 */
18792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018794 typval_T *argvars;
18795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796{
18797 win_T *wp;
18798
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018799 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 else
18803#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018806 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807#endif
18808}
18809
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018811 * "writefile()" function
18812 */
18813 static void
18814f_writefile(argvars, rettv)
18815 typval_T *argvars;
18816 typval_T *rettv;
18817{
18818 int binary = FALSE;
18819 char_u *fname;
18820 FILE *fd;
18821 listitem_T *li;
18822 char_u *s;
18823 int ret = 0;
18824 int c;
18825
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018826 if (check_restricted() || check_secure())
18827 return;
18828
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018829 if (argvars[0].v_type != VAR_LIST)
18830 {
18831 EMSG2(_(e_listarg), "writefile()");
18832 return;
18833 }
18834 if (argvars[0].vval.v_list == NULL)
18835 return;
18836
18837 if (argvars[2].v_type != VAR_UNKNOWN
18838 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18839 binary = TRUE;
18840
18841 /* Always open the file in binary mode, library functions have a mind of
18842 * their own about CR-LF conversion. */
18843 fname = get_tv_string(&argvars[1]);
18844 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18845 {
18846 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18847 ret = -1;
18848 }
18849 else
18850 {
18851 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18852 li = li->li_next)
18853 {
18854 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18855 {
18856 if (*s == '\n')
18857 c = putc(NUL, fd);
18858 else
18859 c = putc(*s, fd);
18860 if (c == EOF)
18861 {
18862 ret = -1;
18863 break;
18864 }
18865 }
18866 if (!binary || li->li_next != NULL)
18867 if (putc('\n', fd) == EOF)
18868 {
18869 ret = -1;
18870 break;
18871 }
18872 if (ret < 0)
18873 {
18874 EMSG(_(e_write));
18875 break;
18876 }
18877 }
18878 fclose(fd);
18879 }
18880
18881 rettv->vval.v_number = ret;
18882}
18883
18884/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018885 * "xor(expr, expr)" function
18886 */
18887 static void
18888f_xor(argvars, rettv)
18889 typval_T *argvars;
18890 typval_T *rettv;
18891{
18892 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18893 ^ get_tv_number_chk(&argvars[1], NULL);
18894}
18895
18896
18897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018899 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 */
18901 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018902var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018903 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018904 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018905 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018907 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018909 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910
Bram Moolenaara5525202006-03-02 22:52:09 +000018911 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018912 if (varp->v_type == VAR_LIST)
18913 {
18914 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018915 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018916 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018917 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018918
18919 l = varp->vval.v_list;
18920 if (l == NULL)
18921 return NULL;
18922
18923 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018924 pos.lnum = list_find_nr(l, 0L, &error);
18925 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018926 return NULL; /* invalid line number */
18927
18928 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018929 pos.col = list_find_nr(l, 1L, &error);
18930 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018931 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018932 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018933
18934 /* We accept "$" for the column number: last column. */
18935 li = list_find(l, 1L);
18936 if (li != NULL && li->li_tv.v_type == VAR_STRING
18937 && li->li_tv.vval.v_string != NULL
18938 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18939 pos.col = len + 1;
18940
Bram Moolenaara5525202006-03-02 22:52:09 +000018941 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018942 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018943 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018944 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018945
Bram Moolenaara5525202006-03-02 22:52:09 +000018946#ifdef FEAT_VIRTUALEDIT
18947 /* Get the virtual offset. Defaults to zero. */
18948 pos.coladd = list_find_nr(l, 2L, &error);
18949 if (error)
18950 pos.coladd = 0;
18951#endif
18952
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018953 return &pos;
18954 }
18955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018956 name = get_tv_string_chk(varp);
18957 if (name == NULL)
18958 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018959 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018961#ifdef FEAT_VISUAL
18962 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18963 {
18964 if (VIsual_active)
18965 return &VIsual;
18966 return &curwin->w_cursor;
18967 }
18968#endif
18969 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018971 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18973 return NULL;
18974 return pp;
18975 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018976
18977#ifdef FEAT_VIRTUALEDIT
18978 pos.coladd = 0;
18979#endif
18980
Bram Moolenaar477933c2007-07-17 14:32:23 +000018981 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018982 {
18983 pos.col = 0;
18984 if (name[1] == '0') /* "w0": first visible line */
18985 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018986 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018987 pos.lnum = curwin->w_topline;
18988 return &pos;
18989 }
18990 else if (name[1] == '$') /* "w$": last visible line */
18991 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018992 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018993 pos.lnum = curwin->w_botline - 1;
18994 return &pos;
18995 }
18996 }
18997 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018999 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 {
19001 pos.lnum = curbuf->b_ml.ml_line_count;
19002 pos.col = 0;
19003 }
19004 else
19005 {
19006 pos.lnum = curwin->w_cursor.lnum;
19007 pos.col = (colnr_T)STRLEN(ml_get_curline());
19008 }
19009 return &pos;
19010 }
19011 return NULL;
19012}
19013
19014/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019015 * Convert list in "arg" into a position and optional file number.
19016 * When "fnump" is NULL there is no file number, only 3 items.
19017 * Note that the column is passed on as-is, the caller may want to decrement
19018 * it to use 1 for the first column.
19019 * Return FAIL when conversion is not possible, doesn't check the position for
19020 * validity.
19021 */
19022 static int
19023list2fpos(arg, posp, fnump)
19024 typval_T *arg;
19025 pos_T *posp;
19026 int *fnump;
19027{
19028 list_T *l = arg->vval.v_list;
19029 long i = 0;
19030 long n;
19031
Bram Moolenaarbde35262006-07-23 20:12:24 +000019032 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19033 * when "fnump" isn't NULL and "coladd" is optional. */
19034 if (arg->v_type != VAR_LIST
19035 || l == NULL
19036 || l->lv_len < (fnump == NULL ? 2 : 3)
19037 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019038 return FAIL;
19039
19040 if (fnump != NULL)
19041 {
19042 n = list_find_nr(l, i++, NULL); /* fnum */
19043 if (n < 0)
19044 return FAIL;
19045 if (n == 0)
19046 n = curbuf->b_fnum; /* current buffer */
19047 *fnump = n;
19048 }
19049
19050 n = list_find_nr(l, i++, NULL); /* lnum */
19051 if (n < 0)
19052 return FAIL;
19053 posp->lnum = n;
19054
19055 n = list_find_nr(l, i++, NULL); /* col */
19056 if (n < 0)
19057 return FAIL;
19058 posp->col = n;
19059
19060#ifdef FEAT_VIRTUALEDIT
19061 n = list_find_nr(l, i, NULL);
19062 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019063 posp->coladd = 0;
19064 else
19065 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019066#endif
19067
19068 return OK;
19069}
19070
19071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 * Get the length of an environment variable name.
19073 * Advance "arg" to the first character after the name.
19074 * Return 0 for error.
19075 */
19076 static int
19077get_env_len(arg)
19078 char_u **arg;
19079{
19080 char_u *p;
19081 int len;
19082
19083 for (p = *arg; vim_isIDc(*p); ++p)
19084 ;
19085 if (p == *arg) /* no name found */
19086 return 0;
19087
19088 len = (int)(p - *arg);
19089 *arg = p;
19090 return len;
19091}
19092
19093/*
19094 * Get the length of the name of a function or internal variable.
19095 * "arg" is advanced to the first non-white character after the name.
19096 * Return 0 if something is wrong.
19097 */
19098 static int
19099get_id_len(arg)
19100 char_u **arg;
19101{
19102 char_u *p;
19103 int len;
19104
19105 /* Find the end of the name. */
19106 for (p = *arg; eval_isnamec(*p); ++p)
19107 ;
19108 if (p == *arg) /* no name found */
19109 return 0;
19110
19111 len = (int)(p - *arg);
19112 *arg = skipwhite(p);
19113
19114 return len;
19115}
19116
19117/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019118 * Get the length of the name of a variable or function.
19119 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019121 * Return -1 if curly braces expansion failed.
19122 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 * If the name contains 'magic' {}'s, expand them and return the
19124 * expanded name in an allocated string via 'alias' - caller must free.
19125 */
19126 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019127get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 char_u **arg;
19129 char_u **alias;
19130 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019131 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132{
19133 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 char_u *p;
19135 char_u *expr_start;
19136 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137
19138 *alias = NULL; /* default to no alias */
19139
19140 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19141 && (*arg)[2] == (int)KE_SNR)
19142 {
19143 /* hard coded <SNR>, already translated */
19144 *arg += 3;
19145 return get_id_len(arg) + 3;
19146 }
19147 len = eval_fname_script(*arg);
19148 if (len > 0)
19149 {
19150 /* literal "<SID>", "s:" or "<SNR>" */
19151 *arg += len;
19152 }
19153
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019155 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019157 p = find_name_end(*arg, &expr_start, &expr_end,
19158 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 if (expr_start != NULL)
19160 {
19161 char_u *temp_string;
19162
19163 if (!evaluate)
19164 {
19165 len += (int)(p - *arg);
19166 *arg = skipwhite(p);
19167 return len;
19168 }
19169
19170 /*
19171 * Include any <SID> etc in the expanded string:
19172 * Thus the -len here.
19173 */
19174 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19175 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019176 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 *alias = temp_string;
19178 *arg = skipwhite(p);
19179 return (int)STRLEN(temp_string);
19180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181
19182 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019183 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 EMSG2(_(e_invexpr2), *arg);
19185
19186 return len;
19187}
19188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019189/*
19190 * Find the end of a variable or function name, taking care of magic braces.
19191 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19192 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019193 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194 * Return a pointer to just after the name. Equal to "arg" if there is no
19195 * valid name.
19196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019198find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 char_u *arg;
19200 char_u **expr_start;
19201 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019202 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 int mb_nest = 0;
19205 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 char_u *p;
19207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019208 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019210 *expr_start = NULL;
19211 *expr_end = NULL;
19212 }
19213
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019214 /* Quick check for valid starting character. */
19215 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19216 return arg;
19217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019218 for (p = arg; *p != NUL
19219 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019220 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019221 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019222 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019223 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019224 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019225 if (*p == '\'')
19226 {
19227 /* skip over 'string' to avoid counting [ and ] inside it. */
19228 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19229 ;
19230 if (*p == NUL)
19231 break;
19232 }
19233 else if (*p == '"')
19234 {
19235 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19236 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19237 if (*p == '\\' && p[1] != NUL)
19238 ++p;
19239 if (*p == NUL)
19240 break;
19241 }
19242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019243 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019245 if (*p == '[')
19246 ++br_nest;
19247 else if (*p == ']')
19248 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019253 if (*p == '{')
19254 {
19255 mb_nest++;
19256 if (expr_start != NULL && *expr_start == NULL)
19257 *expr_start = p;
19258 }
19259 else if (*p == '}')
19260 {
19261 mb_nest--;
19262 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19263 *expr_end = p;
19264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 }
19267
19268 return p;
19269}
19270
19271/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019272 * Expands out the 'magic' {}'s in a variable/function name.
19273 * Note that this can call itself recursively, to deal with
19274 * constructs like foo{bar}{baz}{bam}
19275 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19276 * "in_start" ^
19277 * "expr_start" ^
19278 * "expr_end" ^
19279 * "in_end" ^
19280 *
19281 * Returns a new allocated string, which the caller must free.
19282 * Returns NULL for failure.
19283 */
19284 static char_u *
19285make_expanded_name(in_start, expr_start, expr_end, in_end)
19286 char_u *in_start;
19287 char_u *expr_start;
19288 char_u *expr_end;
19289 char_u *in_end;
19290{
19291 char_u c1;
19292 char_u *retval = NULL;
19293 char_u *temp_result;
19294 char_u *nextcmd = NULL;
19295
19296 if (expr_end == NULL || in_end == NULL)
19297 return NULL;
19298 *expr_start = NUL;
19299 *expr_end = NUL;
19300 c1 = *in_end;
19301 *in_end = NUL;
19302
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019303 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019304 if (temp_result != NULL && nextcmd == NULL)
19305 {
19306 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19307 + (in_end - expr_end) + 1));
19308 if (retval != NULL)
19309 {
19310 STRCPY(retval, in_start);
19311 STRCAT(retval, temp_result);
19312 STRCAT(retval, expr_end + 1);
19313 }
19314 }
19315 vim_free(temp_result);
19316
19317 *in_end = c1; /* put char back for error messages */
19318 *expr_start = '{';
19319 *expr_end = '}';
19320
19321 if (retval != NULL)
19322 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019323 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019324 if (expr_start != NULL)
19325 {
19326 /* Further expansion! */
19327 temp_result = make_expanded_name(retval, expr_start,
19328 expr_end, temp_result);
19329 vim_free(retval);
19330 retval = temp_result;
19331 }
19332 }
19333
19334 return retval;
19335}
19336
19337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019339 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 */
19341 static int
19342eval_isnamec(c)
19343 int c;
19344{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019345 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19346}
19347
19348/*
19349 * Return TRUE if character "c" can be used as the first character in a
19350 * variable or function name (excluding '{' and '}').
19351 */
19352 static int
19353eval_isnamec1(c)
19354 int c;
19355{
19356 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357}
19358
19359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 * Set number v: variable to "val".
19361 */
19362 void
19363set_vim_var_nr(idx, val)
19364 int idx;
19365 long val;
19366{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019367 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368}
19369
19370/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019371 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 */
19373 long
19374get_vim_var_nr(idx)
19375 int idx;
19376{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019377 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378}
19379
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019380/*
19381 * Get string v: variable value. Uses a static buffer, can only be used once.
19382 */
19383 char_u *
19384get_vim_var_str(idx)
19385 int idx;
19386{
19387 return get_tv_string(&vimvars[idx].vv_tv);
19388}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019389
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019391 * Get List v: variable value. Caller must take care of reference count when
19392 * needed.
19393 */
19394 list_T *
19395get_vim_var_list(idx)
19396 int idx;
19397{
19398 return vimvars[idx].vv_list;
19399}
19400
19401/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019402 * Set v:char to character "c".
19403 */
19404 void
19405set_vim_var_char(c)
19406 int c;
19407{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019408 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019409
19410#ifdef FEAT_MBYTE
19411 if (has_mbyte)
19412 buf[(*mb_char2bytes)(c, buf)] = NUL;
19413 else
19414#endif
19415 {
19416 buf[0] = c;
19417 buf[1] = NUL;
19418 }
19419 set_vim_var_string(VV_CHAR, buf, -1);
19420}
19421
19422/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019423 * Set v:count to "count" and v:count1 to "count1".
19424 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 */
19426 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019427set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428 long count;
19429 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019430 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019432 if (set_prevcount)
19433 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019434 vimvars[VV_COUNT].vv_nr = count;
19435 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436}
19437
19438/*
19439 * Set string v: variable to a copy of "val".
19440 */
19441 void
19442set_vim_var_string(idx, val, len)
19443 int idx;
19444 char_u *val;
19445 int len; /* length of "val" to use or -1 (whole string) */
19446{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019447 /* Need to do this (at least) once, since we can't initialize a union.
19448 * Will always be invoked when "v:progname" is set. */
19449 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19450
Bram Moolenaare9a41262005-01-15 22:18:47 +000019451 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019453 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019455 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019457 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458}
19459
19460/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019461 * Set List v: variable to "val".
19462 */
19463 void
19464set_vim_var_list(idx, val)
19465 int idx;
19466 list_T *val;
19467{
19468 list_unref(vimvars[idx].vv_list);
19469 vimvars[idx].vv_list = val;
19470 if (val != NULL)
19471 ++val->lv_refcount;
19472}
19473
19474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 * Set v:register if needed.
19476 */
19477 void
19478set_reg_var(c)
19479 int c;
19480{
19481 char_u regname;
19482
19483 if (c == 0 || c == ' ')
19484 regname = '"';
19485 else
19486 regname = c;
19487 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019488 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 set_vim_var_string(VV_REG, &regname, 1);
19490}
19491
19492/*
19493 * Get or set v:exception. If "oldval" == NULL, return the current value.
19494 * Otherwise, restore the value to "oldval" and return NULL.
19495 * Must always be called in pairs to save and restore v:exception! Does not
19496 * take care of memory allocations.
19497 */
19498 char_u *
19499v_exception(oldval)
19500 char_u *oldval;
19501{
19502 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019503 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504
Bram Moolenaare9a41262005-01-15 22:18:47 +000019505 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 return NULL;
19507}
19508
19509/*
19510 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19511 * Otherwise, restore the value to "oldval" and return NULL.
19512 * Must always be called in pairs to save and restore v:throwpoint! Does not
19513 * take care of memory allocations.
19514 */
19515 char_u *
19516v_throwpoint(oldval)
19517 char_u *oldval;
19518{
19519 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019520 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521
Bram Moolenaare9a41262005-01-15 22:18:47 +000019522 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 return NULL;
19524}
19525
19526#if defined(FEAT_AUTOCMD) || defined(PROTO)
19527/*
19528 * Set v:cmdarg.
19529 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19530 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19531 * Must always be called in pairs!
19532 */
19533 char_u *
19534set_cmdarg(eap, oldarg)
19535 exarg_T *eap;
19536 char_u *oldarg;
19537{
19538 char_u *oldval;
19539 char_u *newval;
19540 unsigned len;
19541
Bram Moolenaare9a41262005-01-15 22:18:47 +000019542 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019543 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019545 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019546 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019547 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 }
19549
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019550 if (eap->force_bin == FORCE_BIN)
19551 len = 6;
19552 else if (eap->force_bin == FORCE_NOBIN)
19553 len = 8;
19554 else
19555 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019556
19557 if (eap->read_edit)
19558 len += 7;
19559
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019560 if (eap->force_ff != 0)
19561 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19562# ifdef FEAT_MBYTE
19563 if (eap->force_enc != 0)
19564 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019565 if (eap->bad_char != 0)
19566 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019567# endif
19568
19569 newval = alloc(len + 1);
19570 if (newval == NULL)
19571 return NULL;
19572
19573 if (eap->force_bin == FORCE_BIN)
19574 sprintf((char *)newval, " ++bin");
19575 else if (eap->force_bin == FORCE_NOBIN)
19576 sprintf((char *)newval, " ++nobin");
19577 else
19578 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019579
19580 if (eap->read_edit)
19581 STRCAT(newval, " ++edit");
19582
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019583 if (eap->force_ff != 0)
19584 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19585 eap->cmd + eap->force_ff);
19586# ifdef FEAT_MBYTE
19587 if (eap->force_enc != 0)
19588 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19589 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019590 if (eap->bad_char == BAD_KEEP)
19591 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19592 else if (eap->bad_char == BAD_DROP)
19593 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19594 else if (eap->bad_char != 0)
19595 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019596# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019597 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019598 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599}
19600#endif
19601
19602/*
19603 * Get the value of internal variable "name".
19604 * Return OK or FAIL.
19605 */
19606 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019607get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 char_u *name;
19609 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019610 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019611 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612{
19613 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019614 typval_T *tv = NULL;
19615 typval_T atv;
19616 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618
19619 /* truncate the name, so that we can use strcmp() */
19620 cc = name[len];
19621 name[len] = NUL;
19622
19623 /*
19624 * Check for "b:changedtick".
19625 */
19626 if (STRCMP(name, "b:changedtick") == 0)
19627 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019628 atv.v_type = VAR_NUMBER;
19629 atv.vval.v_number = curbuf->b_changedtick;
19630 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 }
19632
19633 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 * Check for user-defined variables.
19635 */
19636 else
19637 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019638 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019640 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 }
19642
Bram Moolenaare9a41262005-01-15 22:18:47 +000019643 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019645 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 ret = FAIL;
19648 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019649 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019650 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651
19652 name[len] = cc;
19653
19654 return ret;
19655}
19656
19657/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019658 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19659 * Also handle function call with Funcref variable: func(expr)
19660 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19661 */
19662 static int
19663handle_subscript(arg, rettv, evaluate, verbose)
19664 char_u **arg;
19665 typval_T *rettv;
19666 int evaluate; /* do more than finding the end */
19667 int verbose; /* give error messages */
19668{
19669 int ret = OK;
19670 dict_T *selfdict = NULL;
19671 char_u *s;
19672 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019673 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019674
19675 while (ret == OK
19676 && (**arg == '['
19677 || (**arg == '.' && rettv->v_type == VAR_DICT)
19678 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19679 && !vim_iswhite(*(*arg - 1)))
19680 {
19681 if (**arg == '(')
19682 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019683 /* need to copy the funcref so that we can clear rettv */
19684 functv = *rettv;
19685 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019686
19687 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019688 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019689 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019690 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19691 &len, evaluate, selfdict);
19692
19693 /* Clear the funcref afterwards, so that deleting it while
19694 * evaluating the arguments is possible (see test55). */
19695 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019696
19697 /* Stop the expression evaluation when immediately aborting on
19698 * error, or when an interrupt occurred or an exception was thrown
19699 * but not caught. */
19700 if (aborting())
19701 {
19702 if (ret == OK)
19703 clear_tv(rettv);
19704 ret = FAIL;
19705 }
19706 dict_unref(selfdict);
19707 selfdict = NULL;
19708 }
19709 else /* **arg == '[' || **arg == '.' */
19710 {
19711 dict_unref(selfdict);
19712 if (rettv->v_type == VAR_DICT)
19713 {
19714 selfdict = rettv->vval.v_dict;
19715 if (selfdict != NULL)
19716 ++selfdict->dv_refcount;
19717 }
19718 else
19719 selfdict = NULL;
19720 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19721 {
19722 clear_tv(rettv);
19723 ret = FAIL;
19724 }
19725 }
19726 }
19727 dict_unref(selfdict);
19728 return ret;
19729}
19730
19731/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019732 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019733 * value).
19734 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019737{
Bram Moolenaar33570922005-01-25 22:26:29 +000019738 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019739}
19740
19741/*
19742 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 * The string "s" must have been allocated, it is consumed.
19744 * Return NULL for out of memory, the variable otherwise.
19745 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019747alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 char_u *s;
19749{
Bram Moolenaar33570922005-01-25 22:26:29 +000019750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019752 rettv = alloc_tv();
19753 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019755 rettv->v_type = VAR_STRING;
19756 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 }
19758 else
19759 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761}
19762
19763/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019764 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019766 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019767free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769{
19770 if (varp != NULL)
19771 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019772 switch (varp->v_type)
19773 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019774 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019775 func_unref(varp->vval.v_string);
19776 /*FALLTHROUGH*/
19777 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019778 vim_free(varp->vval.v_string);
19779 break;
19780 case VAR_LIST:
19781 list_unref(varp->vval.v_list);
19782 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019783 case VAR_DICT:
19784 dict_unref(varp->vval.v_dict);
19785 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019786 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019787#ifdef FEAT_FLOAT
19788 case VAR_FLOAT:
19789#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019790 case VAR_UNKNOWN:
19791 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019792 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019793 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019794 break;
19795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 vim_free(varp);
19797 }
19798}
19799
19800/*
19801 * Free the memory for a variable value and set the value to NULL or 0.
19802 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019803 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019804clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019805 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806{
19807 if (varp != NULL)
19808 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019809 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019811 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019812 func_unref(varp->vval.v_string);
19813 /*FALLTHROUGH*/
19814 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019815 vim_free(varp->vval.v_string);
19816 varp->vval.v_string = NULL;
19817 break;
19818 case VAR_LIST:
19819 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019820 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019821 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019822 case VAR_DICT:
19823 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019824 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019825 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019827 varp->vval.v_number = 0;
19828 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019829#ifdef FEAT_FLOAT
19830 case VAR_FLOAT:
19831 varp->vval.v_float = 0.0;
19832 break;
19833#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 case VAR_UNKNOWN:
19835 break;
19836 default:
19837 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019839 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 }
19841}
19842
19843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019844 * Set the value of a variable to NULL without freeing items.
19845 */
19846 static void
19847init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019848 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019849{
19850 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019851 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019852}
19853
19854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 * Get the number value of a variable.
19856 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019857 * For incompatible types, return 0.
19858 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19859 * caller of incompatible types: it sets *denote to TRUE if "denote"
19860 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 */
19862 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019863get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019866 int error = FALSE;
19867
19868 return get_tv_number_chk(varp, &error); /* return 0L on error */
19869}
19870
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019871 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019872get_tv_number_chk(varp, denote)
19873 typval_T *varp;
19874 int *denote;
19875{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019876 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019878 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019880 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019881 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019882#ifdef FEAT_FLOAT
19883 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019884 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019885 break;
19886#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019887 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019888 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019889 break;
19890 case VAR_STRING:
19891 if (varp->vval.v_string != NULL)
19892 vim_str2nr(varp->vval.v_string, NULL, NULL,
19893 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019894 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019895 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019896 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019897 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019898 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019899 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019900 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019901 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019902 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019903 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019905 if (denote == NULL) /* useful for values that must be unsigned */
19906 n = -1;
19907 else
19908 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910}
19911
19912/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019913 * Get the lnum from the first argument.
19914 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019915 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 */
19917 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019918get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019919 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920{
Bram Moolenaar33570922005-01-25 22:26:29 +000019921 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 linenr_T lnum;
19923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019924 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 if (lnum == 0) /* no valid number, try using line() */
19926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019927 rettv.v_type = VAR_NUMBER;
19928 f_line(argvars, &rettv);
19929 lnum = rettv.vval.v_number;
19930 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 }
19932 return lnum;
19933}
19934
19935/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019936 * Get the lnum from the first argument.
19937 * Also accepts "$", then "buf" is used.
19938 * Returns 0 on error.
19939 */
19940 static linenr_T
19941get_tv_lnum_buf(argvars, buf)
19942 typval_T *argvars;
19943 buf_T *buf;
19944{
19945 if (argvars[0].v_type == VAR_STRING
19946 && argvars[0].vval.v_string != NULL
19947 && argvars[0].vval.v_string[0] == '$'
19948 && buf != NULL)
19949 return buf->b_ml.ml_line_count;
19950 return get_tv_number_chk(&argvars[0], NULL);
19951}
19952
19953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 * Get the string value of a variable.
19955 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019956 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19957 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 * If the String variable has never been set, return an empty string.
19959 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019960 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19961 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 */
19963 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019965 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966{
19967 static char_u mybuf[NUMBUFLEN];
19968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970}
19971
19972 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019974 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019975 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019977 char_u *res = get_tv_string_buf_chk(varp, buf);
19978
19979 return res != NULL ? res : (char_u *)"";
19980}
19981
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019982 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019983get_tv_string_chk(varp)
19984 typval_T *varp;
19985{
19986 static char_u mybuf[NUMBUFLEN];
19987
19988 return get_tv_string_buf_chk(varp, mybuf);
19989}
19990
19991 static char_u *
19992get_tv_string_buf_chk(varp, buf)
19993 typval_T *varp;
19994 char_u *buf;
19995{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019996 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019998 case VAR_NUMBER:
19999 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20000 return buf;
20001 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020002 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020003 break;
20004 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020005 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020006 break;
20007 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020008 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020009 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020010#ifdef FEAT_FLOAT
20011 case VAR_FLOAT:
20012 EMSG(_("E806: using Float as a String"));
20013 break;
20014#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020015 case VAR_STRING:
20016 if (varp->vval.v_string != NULL)
20017 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020018 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020019 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020021 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020023 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024}
20025
20026/*
20027 * Find variable "name" in the list of variables.
20028 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020029 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020030 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020031 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020033 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020034find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020036 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020039 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040
Bram Moolenaara7043832005-01-21 11:56:39 +000020041 ht = find_var_ht(name, &varname);
20042 if (htp != NULL)
20043 *htp = ht;
20044 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020046 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047}
20048
20049/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020050 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020051 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020054find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020056 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020057 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020058 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020059{
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 hashitem_T *hi;
20061
20062 if (*varname == NUL)
20063 {
20064 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020065 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020067 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020068 case 'g': return &globvars_var;
20069 case 'v': return &vimvars_var;
20070 case 'b': return &curbuf->b_bufvar;
20071 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020072#ifdef FEAT_WINDOWS
20073 case 't': return &curtab->tp_winvar;
20074#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020075 case 'l': return current_funccal == NULL
20076 ? NULL : &current_funccal->l_vars_var;
20077 case 'a': return current_funccal == NULL
20078 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020079 }
20080 return NULL;
20081 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020082
20083 hi = hash_find(ht, varname);
20084 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020085 {
20086 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020087 * worked find the variable again. Don't auto-load a script if it was
20088 * loaded already, otherwise it would be loaded every time when
20089 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020090 if (ht == &globvarht && !writing)
20091 {
20092 /* Note: script_autoload() may make "hi" invalid. It must either
20093 * be obtained again or not used. */
20094 if (!script_autoload(varname, FALSE) || aborting())
20095 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020096 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020098 if (HASHITEM_EMPTY(hi))
20099 return NULL;
20100 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020101 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020102}
20103
20104/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020105 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020106 * Set "varname" to the start of name without ':'.
20107 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020108 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020109find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 char_u *name;
20111 char_u **varname;
20112{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020113 hashitem_T *hi;
20114
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 if (name[1] != ':')
20116 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020117 /* The name must not start with a colon or #. */
20118 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 return NULL;
20120 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020121
20122 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020123 hi = hash_find(&compat_hashtab, name);
20124 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020125 return &compat_hashtab;
20126
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 return &globvarht; /* global variable */
20129 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 }
20131 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020132 if (*name == 'g') /* global variable */
20133 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020134 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20135 */
20136 if (vim_strchr(name + 2, ':') != NULL
20137 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020138 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020140 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020142 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020143#ifdef FEAT_WINDOWS
20144 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020145 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020146#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020147 if (*name == 'v') /* v: variable */
20148 return &vimvarht;
20149 if (*name == 'a' && current_funccal != NULL) /* function argument */
20150 return &current_funccal->l_avars.dv_hashtab;
20151 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20152 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 if (*name == 's' /* script variable */
20154 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20155 return &SCRIPT_VARS(current_SID);
20156 return NULL;
20157}
20158
20159/*
20160 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020161 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 * Returns NULL when it doesn't exist.
20163 */
20164 char_u *
20165get_var_value(name)
20166 char_u *name;
20167{
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169
Bram Moolenaara7043832005-01-21 11:56:39 +000020170 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 if (v == NULL)
20172 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174}
20175
20176/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020177 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 * sourcing this script and when executing functions defined in the script.
20179 */
20180 void
20181new_script_vars(id)
20182 scid_T id;
20183{
Bram Moolenaara7043832005-01-21 11:56:39 +000020184 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020185 hashtab_T *ht;
20186 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020187
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20189 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020190 /* Re-allocating ga_data means that an ht_array pointing to
20191 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020192 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020193 for (i = 1; i <= ga_scripts.ga_len; ++i)
20194 {
20195 ht = &SCRIPT_VARS(i);
20196 if (ht->ht_mask == HT_INIT_SIZE - 1)
20197 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020198 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020200 }
20201
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 while (ga_scripts.ga_len < id)
20203 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020204 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020205 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020206 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 }
20209 }
20210}
20211
20212/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020213 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20214 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 */
20216 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020217init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020218 dict_T *dict;
20219 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020220 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221{
Bram Moolenaar33570922005-01-25 22:26:29 +000020222 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020223 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020224 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020225 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020226 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020227 dict_var->di_tv.vval.v_dict = dict;
20228 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020229 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20231 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232}
20233
20234/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020235 * Unreference a dictionary initialized by init_var_dict().
20236 */
20237 void
20238unref_var_dict(dict)
20239 dict_T *dict;
20240{
20241 /* Now the dict needs to be freed if no one else is using it, go back to
20242 * normal reference counting. */
20243 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20244 dict_unref(dict);
20245}
20246
20247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020249 * Frees all allocated variables and the value they contain.
20250 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 */
20252 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020253vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 hashtab_T *ht;
20255{
20256 vars_clear_ext(ht, TRUE);
20257}
20258
20259/*
20260 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20261 */
20262 static void
20263vars_clear_ext(ht, free_val)
20264 hashtab_T *ht;
20265 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266{
Bram Moolenaara7043832005-01-21 11:56:39 +000020267 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020268 hashitem_T *hi;
20269 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270
Bram Moolenaar33570922005-01-25 22:26:29 +000020271 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020272 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020273 for (hi = ht->ht_array; todo > 0; ++hi)
20274 {
20275 if (!HASHITEM_EMPTY(hi))
20276 {
20277 --todo;
20278
Bram Moolenaar33570922005-01-25 22:26:29 +000020279 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020280 * ht_array might change then. hash_clear() takes care of it
20281 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020282 v = HI2DI(hi);
20283 if (free_val)
20284 clear_tv(&v->di_tv);
20285 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20286 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020287 }
20288 }
20289 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020290 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291}
20292
Bram Moolenaara7043832005-01-21 11:56:39 +000020293/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020294 * Delete a variable from hashtab "ht" at item "hi".
20295 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020298delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020299 hashtab_T *ht;
20300 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301{
Bram Moolenaar33570922005-01-25 22:26:29 +000020302 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020303
20304 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020305 clear_tv(&di->di_tv);
20306 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307}
20308
20309/*
20310 * List the value of one internal variable.
20311 */
20312 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020313list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020314 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020316 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020318 char_u *tofree;
20319 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020320 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020321
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020322 current_copyID += COPYID_INC;
20323 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020325 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020326 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327}
20328
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020330list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 char_u *prefix;
20332 char_u *name;
20333 int type;
20334 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020335 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336{
Bram Moolenaar31859182007-08-14 20:41:13 +000020337 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20338 msg_start();
20339 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 if (name != NULL) /* "a:" vars don't have a name stored */
20341 msg_puts(name);
20342 msg_putchar(' ');
20343 msg_advance(22);
20344 if (type == VAR_NUMBER)
20345 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020346 else if (type == VAR_FUNC)
20347 msg_putchar('*');
20348 else if (type == VAR_LIST)
20349 {
20350 msg_putchar('[');
20351 if (*string == '[')
20352 ++string;
20353 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020354 else if (type == VAR_DICT)
20355 {
20356 msg_putchar('{');
20357 if (*string == '{')
20358 ++string;
20359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 else
20361 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020362
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020364
20365 if (type == VAR_FUNC)
20366 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020367 if (*first)
20368 {
20369 msg_clr_eos();
20370 *first = FALSE;
20371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372}
20373
20374/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020375 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 * If the variable already exists, the value is updated.
20377 * Otherwise the variable is created.
20378 */
20379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020380set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020383 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384{
Bram Moolenaar33570922005-01-25 22:26:29 +000020385 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020387 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020389 ht = find_var_ht(name, &varname);
20390 if (ht == NULL || *varname == NUL)
20391 {
20392 EMSG2(_(e_illvar), name);
20393 return;
20394 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020395 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020396
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020397 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20398 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020399
Bram Moolenaar33570922005-01-25 22:26:29 +000020400 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020402 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020403 if (var_check_ro(v->di_flags, name)
20404 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020405 return;
20406 if (v->di_tv.v_type != tv->v_type
20407 && !((v->di_tv.v_type == VAR_STRING
20408 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020409 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020410 || tv->v_type == VAR_NUMBER))
20411#ifdef FEAT_FLOAT
20412 && !((v->di_tv.v_type == VAR_NUMBER
20413 || v->di_tv.v_type == VAR_FLOAT)
20414 && (tv->v_type == VAR_NUMBER
20415 || tv->v_type == VAR_FLOAT))
20416#endif
20417 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020418 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020419 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020420 return;
20421 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020422
20423 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020424 * Handle setting internal v: variables separately: we don't change
20425 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020426 */
20427 if (ht == &vimvarht)
20428 {
20429 if (v->di_tv.v_type == VAR_STRING)
20430 {
20431 vim_free(v->di_tv.vval.v_string);
20432 if (copy || tv->v_type != VAR_STRING)
20433 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20434 else
20435 {
20436 /* Take over the string to avoid an extra alloc/free. */
20437 v->di_tv.vval.v_string = tv->vval.v_string;
20438 tv->vval.v_string = NULL;
20439 }
20440 }
20441 else if (v->di_tv.v_type != VAR_NUMBER)
20442 EMSG2(_(e_intern2), "set_var()");
20443 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020444 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020445 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020446 if (STRCMP(varname, "searchforward") == 0)
20447 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20448 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020449 return;
20450 }
20451
20452 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 }
20454 else /* add a new variable */
20455 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020456 /* Can't add "v:" variable. */
20457 if (ht == &vimvarht)
20458 {
20459 EMSG2(_(e_illvar), name);
20460 return;
20461 }
20462
Bram Moolenaar92124a32005-06-17 22:03:40 +000020463 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020464 if (!valid_varname(varname))
20465 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020466
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020467 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20468 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020469 if (v == NULL)
20470 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020471 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020472 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020474 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 return;
20476 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020477 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020479
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020480 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020481 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020482 else
20483 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020484 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020485 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020486 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488}
20489
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020490/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020491 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020492 * Also give an error message.
20493 */
20494 static int
20495var_check_ro(flags, name)
20496 int flags;
20497 char_u *name;
20498{
20499 if (flags & DI_FLAGS_RO)
20500 {
20501 EMSG2(_(e_readonlyvar), name);
20502 return TRUE;
20503 }
20504 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20505 {
20506 EMSG2(_(e_readonlysbx), name);
20507 return TRUE;
20508 }
20509 return FALSE;
20510}
20511
20512/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020513 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20514 * Also give an error message.
20515 */
20516 static int
20517var_check_fixed(flags, name)
20518 int flags;
20519 char_u *name;
20520{
20521 if (flags & DI_FLAGS_FIX)
20522 {
20523 EMSG2(_("E795: Cannot delete variable %s"), name);
20524 return TRUE;
20525 }
20526 return FALSE;
20527}
20528
20529/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020530 * Check if a funcref is assigned to a valid variable name.
20531 * Return TRUE and give an error if not.
20532 */
20533 static int
20534var_check_func_name(name, new_var)
20535 char_u *name; /* points to start of variable name */
20536 int new_var; /* TRUE when creating the variable */
20537{
20538 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20539 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20540 ? name[2] : name[0]))
20541 {
20542 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20543 name);
20544 return TRUE;
20545 }
20546 /* Don't allow hiding a function. When "v" is not NULL we might be
20547 * assigning another function to the same var, the type is checked
20548 * below. */
20549 if (new_var && function_exists(name))
20550 {
20551 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20552 name);
20553 return TRUE;
20554 }
20555 return FALSE;
20556}
20557
20558/*
20559 * Check if a variable name is valid.
20560 * Return FALSE and give an error if not.
20561 */
20562 static int
20563valid_varname(varname)
20564 char_u *varname;
20565{
20566 char_u *p;
20567
20568 for (p = varname; *p != NUL; ++p)
20569 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20570 && *p != AUTOLOAD_CHAR)
20571 {
20572 EMSG2(_(e_illvar), varname);
20573 return FALSE;
20574 }
20575 return TRUE;
20576}
20577
20578/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020579 * Return TRUE if typeval "tv" is set to be locked (immutable).
20580 * Also give an error message, using "name".
20581 */
20582 static int
20583tv_check_lock(lock, name)
20584 int lock;
20585 char_u *name;
20586{
20587 if (lock & VAR_LOCKED)
20588 {
20589 EMSG2(_("E741: Value is locked: %s"),
20590 name == NULL ? (char_u *)_("Unknown") : name);
20591 return TRUE;
20592 }
20593 if (lock & VAR_FIXED)
20594 {
20595 EMSG2(_("E742: Cannot change value of %s"),
20596 name == NULL ? (char_u *)_("Unknown") : name);
20597 return TRUE;
20598 }
20599 return FALSE;
20600}
20601
20602/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020603 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020604 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020605 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020606 * It is OK for "from" and "to" to point to the same item. This is used to
20607 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020608 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020609 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020610copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020611 typval_T *from;
20612 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020614 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020615 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020616 switch (from->v_type)
20617 {
20618 case VAR_NUMBER:
20619 to->vval.v_number = from->vval.v_number;
20620 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020621#ifdef FEAT_FLOAT
20622 case VAR_FLOAT:
20623 to->vval.v_float = from->vval.v_float;
20624 break;
20625#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020626 case VAR_STRING:
20627 case VAR_FUNC:
20628 if (from->vval.v_string == NULL)
20629 to->vval.v_string = NULL;
20630 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020631 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020632 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020633 if (from->v_type == VAR_FUNC)
20634 func_ref(to->vval.v_string);
20635 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020636 break;
20637 case VAR_LIST:
20638 if (from->vval.v_list == NULL)
20639 to->vval.v_list = NULL;
20640 else
20641 {
20642 to->vval.v_list = from->vval.v_list;
20643 ++to->vval.v_list->lv_refcount;
20644 }
20645 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020646 case VAR_DICT:
20647 if (from->vval.v_dict == NULL)
20648 to->vval.v_dict = NULL;
20649 else
20650 {
20651 to->vval.v_dict = from->vval.v_dict;
20652 ++to->vval.v_dict->dv_refcount;
20653 }
20654 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020655 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020656 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020657 break;
20658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659}
20660
20661/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020662 * Make a copy of an item.
20663 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020664 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20665 * reference to an already copied list/dict can be used.
20666 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020667 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020668 static int
20669item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020670 typval_T *from;
20671 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020672 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020673 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020674{
20675 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020676 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020677
Bram Moolenaar33570922005-01-25 22:26:29 +000020678 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020679 {
20680 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020681 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020682 }
20683 ++recurse;
20684
20685 switch (from->v_type)
20686 {
20687 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020688#ifdef FEAT_FLOAT
20689 case VAR_FLOAT:
20690#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020691 case VAR_STRING:
20692 case VAR_FUNC:
20693 copy_tv(from, to);
20694 break;
20695 case VAR_LIST:
20696 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020697 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020698 if (from->vval.v_list == NULL)
20699 to->vval.v_list = NULL;
20700 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20701 {
20702 /* use the copy made earlier */
20703 to->vval.v_list = from->vval.v_list->lv_copylist;
20704 ++to->vval.v_list->lv_refcount;
20705 }
20706 else
20707 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20708 if (to->vval.v_list == NULL)
20709 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020710 break;
20711 case VAR_DICT:
20712 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020713 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020714 if (from->vval.v_dict == NULL)
20715 to->vval.v_dict = NULL;
20716 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20717 {
20718 /* use the copy made earlier */
20719 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20720 ++to->vval.v_dict->dv_refcount;
20721 }
20722 else
20723 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20724 if (to->vval.v_dict == NULL)
20725 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020726 break;
20727 default:
20728 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020729 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020730 }
20731 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020732 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020733}
20734
20735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 * ":echo expr1 ..." print each argument separated with a space, add a
20737 * newline at the end.
20738 * ":echon expr1 ..." print each argument plain.
20739 */
20740 void
20741ex_echo(eap)
20742 exarg_T *eap;
20743{
20744 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020745 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020746 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 char_u *p;
20748 int needclr = TRUE;
20749 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020750 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751
20752 if (eap->skip)
20753 ++emsg_skip;
20754 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20755 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020756 /* If eval1() causes an error message the text from the command may
20757 * still need to be cleared. E.g., "echo 22,44". */
20758 need_clr_eos = needclr;
20759
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020761 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 {
20763 /*
20764 * Report the invalid expression unless the expression evaluation
20765 * has been cancelled due to an aborting error, an interrupt, or an
20766 * exception.
20767 */
20768 if (!aborting())
20769 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020770 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 break;
20772 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020773 need_clr_eos = FALSE;
20774
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 if (!eap->skip)
20776 {
20777 if (atstart)
20778 {
20779 atstart = FALSE;
20780 /* Call msg_start() after eval1(), evaluating the expression
20781 * may cause a message to appear. */
20782 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020783 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020784 /* Mark the saved text as finishing the line, so that what
20785 * follows is displayed on a new line when scrolling back
20786 * at the more prompt. */
20787 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 }
20791 else if (eap->cmdidx == CMD_echo)
20792 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020793 current_copyID += COPYID_INC;
20794 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020795 if (p != NULL)
20796 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020798 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020800 if (*p != TAB && needclr)
20801 {
20802 /* remove any text still there from the command */
20803 msg_clr_eos();
20804 needclr = FALSE;
20805 }
20806 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 }
20808 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020809 {
20810#ifdef FEAT_MBYTE
20811 if (has_mbyte)
20812 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020813 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020814
20815 (void)msg_outtrans_len_attr(p, i, echo_attr);
20816 p += i - 1;
20817 }
20818 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020820 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020823 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020825 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 arg = skipwhite(arg);
20827 }
20828 eap->nextcmd = check_nextcmd(arg);
20829
20830 if (eap->skip)
20831 --emsg_skip;
20832 else
20833 {
20834 /* remove text that may still be there from the command */
20835 if (needclr)
20836 msg_clr_eos();
20837 if (eap->cmdidx == CMD_echo)
20838 msg_end();
20839 }
20840}
20841
20842/*
20843 * ":echohl {name}".
20844 */
20845 void
20846ex_echohl(eap)
20847 exarg_T *eap;
20848{
20849 int id;
20850
20851 id = syn_name2id(eap->arg);
20852 if (id == 0)
20853 echo_attr = 0;
20854 else
20855 echo_attr = syn_id2attr(id);
20856}
20857
20858/*
20859 * ":execute expr1 ..." execute the result of an expression.
20860 * ":echomsg expr1 ..." Print a message
20861 * ":echoerr expr1 ..." Print an error
20862 * Each gets spaces around each argument and a newline at the end for
20863 * echo commands
20864 */
20865 void
20866ex_execute(eap)
20867 exarg_T *eap;
20868{
20869 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020870 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 int ret = OK;
20872 char_u *p;
20873 garray_T ga;
20874 int len;
20875 int save_did_emsg;
20876
20877 ga_init2(&ga, 1, 80);
20878
20879 if (eap->skip)
20880 ++emsg_skip;
20881 while (*arg != NUL && *arg != '|' && *arg != '\n')
20882 {
20883 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020884 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 {
20886 /*
20887 * Report the invalid expression unless the expression evaluation
20888 * has been cancelled due to an aborting error, an interrupt, or an
20889 * exception.
20890 */
20891 if (!aborting())
20892 EMSG2(_(e_invexpr2), p);
20893 ret = FAIL;
20894 break;
20895 }
20896
20897 if (!eap->skip)
20898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020899 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 len = (int)STRLEN(p);
20901 if (ga_grow(&ga, len + 2) == FAIL)
20902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020903 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 ret = FAIL;
20905 break;
20906 }
20907 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 ga.ga_len += len;
20911 }
20912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 arg = skipwhite(arg);
20915 }
20916
20917 if (ret != FAIL && ga.ga_data != NULL)
20918 {
20919 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020922 out_flush();
20923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 else if (eap->cmdidx == CMD_echoerr)
20925 {
20926 /* We don't want to abort following commands, restore did_emsg. */
20927 save_did_emsg = did_emsg;
20928 EMSG((char_u *)ga.ga_data);
20929 if (!force_abort)
20930 did_emsg = save_did_emsg;
20931 }
20932 else if (eap->cmdidx == CMD_execute)
20933 do_cmdline((char_u *)ga.ga_data,
20934 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20935 }
20936
20937 ga_clear(&ga);
20938
20939 if (eap->skip)
20940 --emsg_skip;
20941
20942 eap->nextcmd = check_nextcmd(arg);
20943}
20944
20945/*
20946 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20947 * "arg" points to the "&" or '+' when called, to "option" when returning.
20948 * Returns NULL when no option name found. Otherwise pointer to the char
20949 * after the option name.
20950 */
20951 static char_u *
20952find_option_end(arg, opt_flags)
20953 char_u **arg;
20954 int *opt_flags;
20955{
20956 char_u *p = *arg;
20957
20958 ++p;
20959 if (*p == 'g' && p[1] == ':')
20960 {
20961 *opt_flags = OPT_GLOBAL;
20962 p += 2;
20963 }
20964 else if (*p == 'l' && p[1] == ':')
20965 {
20966 *opt_flags = OPT_LOCAL;
20967 p += 2;
20968 }
20969 else
20970 *opt_flags = 0;
20971
20972 if (!ASCII_ISALPHA(*p))
20973 return NULL;
20974 *arg = p;
20975
20976 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20977 p += 4; /* termcap option */
20978 else
20979 while (ASCII_ISALPHA(*p))
20980 ++p;
20981 return p;
20982}
20983
20984/*
20985 * ":function"
20986 */
20987 void
20988ex_function(eap)
20989 exarg_T *eap;
20990{
20991 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020992 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 int j;
20994 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 char_u *name = NULL;
20997 char_u *p;
20998 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020999 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 garray_T newargs;
21001 garray_T newlines;
21002 int varargs = FALSE;
21003 int mustend = FALSE;
21004 int flags = 0;
21005 ufunc_T *fp;
21006 int indent;
21007 int nesting;
21008 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021009 dictitem_T *v;
21010 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021011 static int func_nr = 0; /* number for nameless function */
21012 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021013 hashtab_T *ht;
21014 int todo;
21015 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021016 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017
21018 /*
21019 * ":function" without argument: list functions.
21020 */
21021 if (ends_excmd(*eap->arg))
21022 {
21023 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021024 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021025 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021026 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021027 {
21028 if (!HASHITEM_EMPTY(hi))
21029 {
21030 --todo;
21031 fp = HI2UF(hi);
21032 if (!isdigit(*fp->uf_name))
21033 list_func_head(fp, FALSE);
21034 }
21035 }
21036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 eap->nextcmd = check_nextcmd(eap->arg);
21038 return;
21039 }
21040
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021041 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021042 * ":function /pat": list functions matching pattern.
21043 */
21044 if (*eap->arg == '/')
21045 {
21046 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21047 if (!eap->skip)
21048 {
21049 regmatch_T regmatch;
21050
21051 c = *p;
21052 *p = NUL;
21053 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21054 *p = c;
21055 if (regmatch.regprog != NULL)
21056 {
21057 regmatch.rm_ic = p_ic;
21058
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021059 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021060 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21061 {
21062 if (!HASHITEM_EMPTY(hi))
21063 {
21064 --todo;
21065 fp = HI2UF(hi);
21066 if (!isdigit(*fp->uf_name)
21067 && vim_regexec(&regmatch, fp->uf_name, 0))
21068 list_func_head(fp, FALSE);
21069 }
21070 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021071 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021072 }
21073 }
21074 if (*p == '/')
21075 ++p;
21076 eap->nextcmd = check_nextcmd(p);
21077 return;
21078 }
21079
21080 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021081 * Get the function name. There are these situations:
21082 * func normal function name
21083 * "name" == func, "fudi.fd_dict" == NULL
21084 * dict.func new dictionary entry
21085 * "name" == NULL, "fudi.fd_dict" set,
21086 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21087 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021088 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021089 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21090 * dict.func existing dict entry that's not a Funcref
21091 * "name" == NULL, "fudi.fd_dict" set,
21092 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021095 name = trans_function_name(&p, eap->skip, 0, &fudi);
21096 paren = (vim_strchr(p, '(') != NULL);
21097 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 {
21099 /*
21100 * Return on an invalid expression in braces, unless the expression
21101 * evaluation has been cancelled due to an aborting error, an
21102 * interrupt, or an exception.
21103 */
21104 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021105 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021106 if (!eap->skip && fudi.fd_newkey != NULL)
21107 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021108 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 else
21112 eap->skip = TRUE;
21113 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021114
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 /* An error in a function call during evaluation of an expression in magic
21116 * braces should not cause the function not to be defined. */
21117 saved_did_emsg = did_emsg;
21118 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119
21120 /*
21121 * ":function func" with only function name: list function.
21122 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021123 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 {
21125 if (!ends_excmd(*skipwhite(p)))
21126 {
21127 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021128 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 }
21130 eap->nextcmd = check_nextcmd(p);
21131 if (eap->nextcmd != NULL)
21132 *p = NUL;
21133 if (!eap->skip && !got_int)
21134 {
21135 fp = find_func(name);
21136 if (fp != NULL)
21137 {
21138 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021139 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021141 if (FUNCLINE(fp, j) == NULL)
21142 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 msg_putchar('\n');
21144 msg_outnum((long)(j + 1));
21145 if (j < 9)
21146 msg_putchar(' ');
21147 if (j < 99)
21148 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021149 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 out_flush(); /* show a line at a time */
21151 ui_breakcheck();
21152 }
21153 if (!got_int)
21154 {
21155 msg_putchar('\n');
21156 msg_puts((char_u *)" endfunction");
21157 }
21158 }
21159 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021160 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021162 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 }
21164
21165 /*
21166 * ":function name(arg1, arg2)" Define function.
21167 */
21168 p = skipwhite(p);
21169 if (*p != '(')
21170 {
21171 if (!eap->skip)
21172 {
21173 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021174 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 }
21176 /* attempt to continue by skipping some text */
21177 if (vim_strchr(p, '(') != NULL)
21178 p = vim_strchr(p, '(');
21179 }
21180 p = skipwhite(p + 1);
21181
21182 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21183 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21184
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021185 if (!eap->skip)
21186 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021187 /* Check the name of the function. Unless it's a dictionary function
21188 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021189 if (name != NULL)
21190 arg = name;
21191 else
21192 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021193 if (arg != NULL && (fudi.fd_di == NULL
21194 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021195 {
21196 if (*arg == K_SPECIAL)
21197 j = 3;
21198 else
21199 j = 0;
21200 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21201 : eval_isnamec(arg[j])))
21202 ++j;
21203 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021204 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021205 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021206 /* Disallow using the g: dict. */
21207 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21208 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021209 }
21210
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 /*
21212 * Isolate the arguments: "arg1, arg2, ...)"
21213 */
21214 while (*p != ')')
21215 {
21216 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21217 {
21218 varargs = TRUE;
21219 p += 3;
21220 mustend = TRUE;
21221 }
21222 else
21223 {
21224 arg = p;
21225 while (ASCII_ISALNUM(*p) || *p == '_')
21226 ++p;
21227 if (arg == p || isdigit(*arg)
21228 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21229 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21230 {
21231 if (!eap->skip)
21232 EMSG2(_("E125: Illegal argument: %s"), arg);
21233 break;
21234 }
21235 if (ga_grow(&newargs, 1) == FAIL)
21236 goto erret;
21237 c = *p;
21238 *p = NUL;
21239 arg = vim_strsave(arg);
21240 if (arg == NULL)
21241 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021242
21243 /* Check for duplicate argument name. */
21244 for (i = 0; i < newargs.ga_len; ++i)
21245 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21246 {
21247 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21248 goto erret;
21249 }
21250
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21252 *p = c;
21253 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 if (*p == ',')
21255 ++p;
21256 else
21257 mustend = TRUE;
21258 }
21259 p = skipwhite(p);
21260 if (mustend && *p != ')')
21261 {
21262 if (!eap->skip)
21263 EMSG2(_(e_invarg2), eap->arg);
21264 break;
21265 }
21266 }
21267 ++p; /* skip the ')' */
21268
Bram Moolenaare9a41262005-01-15 22:18:47 +000021269 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 for (;;)
21271 {
21272 p = skipwhite(p);
21273 if (STRNCMP(p, "range", 5) == 0)
21274 {
21275 flags |= FC_RANGE;
21276 p += 5;
21277 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021278 else if (STRNCMP(p, "dict", 4) == 0)
21279 {
21280 flags |= FC_DICT;
21281 p += 4;
21282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 else if (STRNCMP(p, "abort", 5) == 0)
21284 {
21285 flags |= FC_ABORT;
21286 p += 5;
21287 }
21288 else
21289 break;
21290 }
21291
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021292 /* When there is a line break use what follows for the function body.
21293 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21294 if (*p == '\n')
21295 line_arg = p + 1;
21296 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 EMSG(_(e_trailing));
21298
21299 /*
21300 * Read the body of the function, until ":endfunction" is found.
21301 */
21302 if (KeyTyped)
21303 {
21304 /* Check if the function already exists, don't let the user type the
21305 * whole function before telling him it doesn't work! For a script we
21306 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021307 if (!eap->skip && !eap->forceit)
21308 {
21309 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21310 EMSG(_(e_funcdict));
21311 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021312 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021315 if (!eap->skip && did_emsg)
21316 goto erret;
21317
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 msg_putchar('\n'); /* don't overwrite the function name */
21319 cmdline_row = msg_row;
21320 }
21321
21322 indent = 2;
21323 nesting = 0;
21324 for (;;)
21325 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021326 if (KeyTyped)
21327 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021329 sourcing_lnum_off = sourcing_lnum;
21330
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021331 if (line_arg != NULL)
21332 {
21333 /* Use eap->arg, split up in parts by line breaks. */
21334 theline = line_arg;
21335 p = vim_strchr(theline, '\n');
21336 if (p == NULL)
21337 line_arg += STRLEN(line_arg);
21338 else
21339 {
21340 *p = NUL;
21341 line_arg = p + 1;
21342 }
21343 }
21344 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 theline = getcmdline(':', 0L, indent);
21346 else
21347 theline = eap->getline(':', eap->cookie, indent);
21348 if (KeyTyped)
21349 lines_left = Rows - 1;
21350 if (theline == NULL)
21351 {
21352 EMSG(_("E126: Missing :endfunction"));
21353 goto erret;
21354 }
21355
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021356 /* Detect line continuation: sourcing_lnum increased more than one. */
21357 if (sourcing_lnum > sourcing_lnum_off + 1)
21358 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21359 else
21360 sourcing_lnum_off = 0;
21361
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 if (skip_until != NULL)
21363 {
21364 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21365 * don't check for ":endfunc". */
21366 if (STRCMP(theline, skip_until) == 0)
21367 {
21368 vim_free(skip_until);
21369 skip_until = NULL;
21370 }
21371 }
21372 else
21373 {
21374 /* skip ':' and blanks*/
21375 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21376 ;
21377
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021378 /* Check for "endfunction". */
21379 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021381 if (line_arg == NULL)
21382 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 break;
21384 }
21385
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021386 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 * at "end". */
21388 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21389 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021390 else if (STRNCMP(p, "if", 2) == 0
21391 || STRNCMP(p, "wh", 2) == 0
21392 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 || STRNCMP(p, "try", 3) == 0)
21394 indent += 2;
21395
21396 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021397 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021399 if (*p == '!')
21400 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 p += eval_fname_script(p);
21402 if (ASCII_ISALPHA(*p))
21403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021404 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 if (*skipwhite(p) == '(')
21406 {
21407 ++nesting;
21408 indent += 2;
21409 }
21410 }
21411 }
21412
21413 /* Check for ":append" or ":insert". */
21414 p = skip_range(p, NULL);
21415 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21416 || (p[0] == 'i'
21417 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21418 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21419 skip_until = vim_strsave((char_u *)".");
21420
21421 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21422 arg = skipwhite(skiptowhite(p));
21423 if (arg[0] == '<' && arg[1] =='<'
21424 && ((p[0] == 'p' && p[1] == 'y'
21425 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21426 || (p[0] == 'p' && p[1] == 'e'
21427 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21428 || (p[0] == 't' && p[1] == 'c'
21429 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021430 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21431 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21433 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021434 || (p[0] == 'm' && p[1] == 'z'
21435 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436 ))
21437 {
21438 /* ":python <<" continues until a dot, like ":append" */
21439 p = skipwhite(arg + 2);
21440 if (*p == NUL)
21441 skip_until = vim_strsave((char_u *)".");
21442 else
21443 skip_until = vim_strsave(p);
21444 }
21445 }
21446
21447 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021448 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021449 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021450 if (line_arg == NULL)
21451 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021453 }
21454
21455 /* Copy the line to newly allocated memory. get_one_sourceline()
21456 * allocates 250 bytes per line, this saves 80% on average. The cost
21457 * is an extra alloc/free. */
21458 p = vim_strsave(theline);
21459 if (p != NULL)
21460 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021461 if (line_arg == NULL)
21462 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021463 theline = p;
21464 }
21465
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021466 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21467
21468 /* Add NULL lines for continuation lines, so that the line count is
21469 * equal to the index in the growarray. */
21470 while (sourcing_lnum_off-- > 0)
21471 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021472
21473 /* Check for end of eap->arg. */
21474 if (line_arg != NULL && *line_arg == NUL)
21475 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 }
21477
21478 /* Don't define the function when skipping commands or when an error was
21479 * detected. */
21480 if (eap->skip || did_emsg)
21481 goto erret;
21482
21483 /*
21484 * If there are no errors, add the function
21485 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021486 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021487 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021488 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021490 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021491 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021492 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021493 goto erret;
21494 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021495
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021496 fp = find_func(name);
21497 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021499 if (!eap->forceit)
21500 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021501 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 goto erret;
21503 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021504 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021506 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021507 name);
21508 goto erret;
21509 }
21510 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021511 ga_clear_strings(&(fp->uf_args));
21512 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021513 vim_free(name);
21514 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 }
21517 else
21518 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021519 char numbuf[20];
21520
21521 fp = NULL;
21522 if (fudi.fd_newkey == NULL && !eap->forceit)
21523 {
21524 EMSG(_(e_funcdict));
21525 goto erret;
21526 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021527 if (fudi.fd_di == NULL)
21528 {
21529 /* Can't add a function to a locked dictionary */
21530 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21531 goto erret;
21532 }
21533 /* Can't change an existing function if it is locked */
21534 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21535 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021536
21537 /* Give the function a sequential number. Can only be used with a
21538 * Funcref! */
21539 vim_free(name);
21540 sprintf(numbuf, "%d", ++func_nr);
21541 name = vim_strsave((char_u *)numbuf);
21542 if (name == NULL)
21543 goto erret;
21544 }
21545
21546 if (fp == NULL)
21547 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021548 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021549 {
21550 int slen, plen;
21551 char_u *scriptname;
21552
21553 /* Check that the autoload name matches the script name. */
21554 j = FAIL;
21555 if (sourcing_name != NULL)
21556 {
21557 scriptname = autoload_name(name);
21558 if (scriptname != NULL)
21559 {
21560 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021561 plen = (int)STRLEN(p);
21562 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021563 if (slen > plen && fnamecmp(p,
21564 sourcing_name + slen - plen) == 0)
21565 j = OK;
21566 vim_free(scriptname);
21567 }
21568 }
21569 if (j == FAIL)
21570 {
21571 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21572 goto erret;
21573 }
21574 }
21575
21576 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 if (fp == NULL)
21578 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021579
21580 if (fudi.fd_dict != NULL)
21581 {
21582 if (fudi.fd_di == NULL)
21583 {
21584 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021585 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021586 if (fudi.fd_di == NULL)
21587 {
21588 vim_free(fp);
21589 goto erret;
21590 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021591 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21592 {
21593 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021594 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021595 goto erret;
21596 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021597 }
21598 else
21599 /* overwrite existing dict entry */
21600 clear_tv(&fudi.fd_di->di_tv);
21601 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021602 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021603 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021604 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021605
21606 /* behave like "dict" was used */
21607 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021608 }
21609
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021611 STRCPY(fp->uf_name, name);
21612 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021614 fp->uf_args = newargs;
21615 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021616#ifdef FEAT_PROFILE
21617 fp->uf_tml_count = NULL;
21618 fp->uf_tml_total = NULL;
21619 fp->uf_tml_self = NULL;
21620 fp->uf_profiling = FALSE;
21621 if (prof_def_func())
21622 func_do_profile(fp);
21623#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021624 fp->uf_varargs = varargs;
21625 fp->uf_flags = flags;
21626 fp->uf_calls = 0;
21627 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021628 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629
21630erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 ga_clear_strings(&newargs);
21632 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633ret_free:
21634 vim_free(skip_until);
21635 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638}
21639
21640/*
21641 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021642 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021644 * flags:
21645 * TFN_INT: internal function name OK
21646 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 * Advances "pp" to just after the function name (if no error).
21648 */
21649 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021650trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 char_u **pp;
21652 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021653 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021654 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021656 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 char_u *start;
21658 char_u *end;
21659 int lead;
21660 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021663
21664 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021665 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021667
21668 /* Check for hard coded <SNR>: already translated function ID (from a user
21669 * command). */
21670 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21671 && (*pp)[2] == (int)KE_SNR)
21672 {
21673 *pp += 3;
21674 len = get_id_len(pp) + 3;
21675 return vim_strnsave(start, len);
21676 }
21677
21678 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21679 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021681 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021683
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021684 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21685 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021686 if (end == start)
21687 {
21688 if (!skip)
21689 EMSG(_("E129: Function name required"));
21690 goto theend;
21691 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021692 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021693 {
21694 /*
21695 * Report an invalid expression in braces, unless the expression
21696 * evaluation has been cancelled due to an aborting error, an
21697 * interrupt, or an exception.
21698 */
21699 if (!aborting())
21700 {
21701 if (end != NULL)
21702 EMSG2(_(e_invarg2), start);
21703 }
21704 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021705 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021706 goto theend;
21707 }
21708
21709 if (lv.ll_tv != NULL)
21710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711 if (fdp != NULL)
21712 {
21713 fdp->fd_dict = lv.ll_dict;
21714 fdp->fd_newkey = lv.ll_newkey;
21715 lv.ll_newkey = NULL;
21716 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021718 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21719 {
21720 name = vim_strsave(lv.ll_tv->vval.v_string);
21721 *pp = end;
21722 }
21723 else
21724 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021725 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21726 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021727 EMSG(_(e_funcref));
21728 else
21729 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021730 name = NULL;
21731 }
21732 goto theend;
21733 }
21734
21735 if (lv.ll_name == NULL)
21736 {
21737 /* Error found, but continue after the function name. */
21738 *pp = end;
21739 goto theend;
21740 }
21741
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021742 /* Check if the name is a Funcref. If so, use the value. */
21743 if (lv.ll_exp_name != NULL)
21744 {
21745 len = (int)STRLEN(lv.ll_exp_name);
21746 name = deref_func_name(lv.ll_exp_name, &len);
21747 if (name == lv.ll_exp_name)
21748 name = NULL;
21749 }
21750 else
21751 {
21752 len = (int)(end - *pp);
21753 name = deref_func_name(*pp, &len);
21754 if (name == *pp)
21755 name = NULL;
21756 }
21757 if (name != NULL)
21758 {
21759 name = vim_strsave(name);
21760 *pp = end;
21761 goto theend;
21762 }
21763
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021764 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021765 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021766 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021767 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21768 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21769 {
21770 /* When there was "s:" already or the name expanded to get a
21771 * leading "s:" then remove it. */
21772 lv.ll_name += 2;
21773 len -= 2;
21774 lead = 2;
21775 }
21776 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021777 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021778 {
21779 if (lead == 2) /* skip over "s:" */
21780 lv.ll_name += 2;
21781 len = (int)(end - lv.ll_name);
21782 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021783
21784 /*
21785 * Copy the function name to allocated memory.
21786 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21787 * Accept <SNR>123_name() outside a script.
21788 */
21789 if (skip)
21790 lead = 0; /* do nothing */
21791 else if (lead > 0)
21792 {
21793 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021794 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21795 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021796 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021797 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021798 if (current_SID <= 0)
21799 {
21800 EMSG(_(e_usingsid));
21801 goto theend;
21802 }
21803 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21804 lead += (int)STRLEN(sid_buf);
21805 }
21806 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021807 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021808 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021809 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021810 goto theend;
21811 }
21812 name = alloc((unsigned)(len + lead + 1));
21813 if (name != NULL)
21814 {
21815 if (lead > 0)
21816 {
21817 name[0] = K_SPECIAL;
21818 name[1] = KS_EXTRA;
21819 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021820 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021821 STRCPY(name + 3, sid_buf);
21822 }
21823 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21824 name[len + lead] = NUL;
21825 }
21826 *pp = end;
21827
21828theend:
21829 clear_lval(&lv);
21830 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831}
21832
21833/*
21834 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21835 * Return 2 if "p" starts with "s:".
21836 * Return 0 otherwise.
21837 */
21838 static int
21839eval_fname_script(p)
21840 char_u *p;
21841{
21842 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21843 || STRNICMP(p + 1, "SNR>", 4) == 0))
21844 return 5;
21845 if (p[0] == 's' && p[1] == ':')
21846 return 2;
21847 return 0;
21848}
21849
21850/*
21851 * Return TRUE if "p" starts with "<SID>" or "s:".
21852 * Only works if eval_fname_script() returned non-zero for "p"!
21853 */
21854 static int
21855eval_fname_sid(p)
21856 char_u *p;
21857{
21858 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21859}
21860
21861/*
21862 * List the head of the function: "name(arg1, arg2)".
21863 */
21864 static void
21865list_func_head(fp, indent)
21866 ufunc_T *fp;
21867 int indent;
21868{
21869 int j;
21870
21871 msg_start();
21872 if (indent)
21873 MSG_PUTS(" ");
21874 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021875 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 {
21877 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021878 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879 }
21880 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021881 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021883 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 {
21885 if (j)
21886 MSG_PUTS(", ");
21887 msg_puts(FUNCARG(fp, j));
21888 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021889 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 {
21891 if (j)
21892 MSG_PUTS(", ");
21893 MSG_PUTS("...");
21894 }
21895 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020021896 if (fp->uf_flags & FC_ABORT)
21897 MSG_PUTS(" abort");
21898 if (fp->uf_flags & FC_RANGE)
21899 MSG_PUTS(" range");
21900 if (fp->uf_flags & FC_DICT)
21901 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021902 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021903 if (p_verbose > 0)
21904 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905}
21906
21907/*
21908 * Find a function by name, return pointer to it in ufuncs.
21909 * Return NULL for unknown function.
21910 */
21911 static ufunc_T *
21912find_func(name)
21913 char_u *name;
21914{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021915 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021917 hi = hash_find(&func_hashtab, name);
21918 if (!HASHITEM_EMPTY(hi))
21919 return HI2UF(hi);
21920 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921}
21922
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021923#if defined(EXITFREE) || defined(PROTO)
21924 void
21925free_all_functions()
21926{
21927 hashitem_T *hi;
21928
21929 /* Need to start all over every time, because func_free() may change the
21930 * hash table. */
21931 while (func_hashtab.ht_used > 0)
21932 for (hi = func_hashtab.ht_array; ; ++hi)
21933 if (!HASHITEM_EMPTY(hi))
21934 {
21935 func_free(HI2UF(hi));
21936 break;
21937 }
21938}
21939#endif
21940
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021941 int
21942translated_function_exists(name)
21943 char_u *name;
21944{
21945 if (builtin_function(name))
21946 return find_internal_func(name) >= 0;
21947 return find_func(name) != NULL;
21948}
21949
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950/*
21951 * Return TRUE if a function "name" exists.
21952 */
21953 static int
21954function_exists(name)
21955 char_u *name;
21956{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021957 char_u *nm = name;
21958 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021959 int n = FALSE;
21960
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021961 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021962 nm = skipwhite(nm);
21963
21964 /* Only accept "funcname", "funcname ", "funcname (..." and
21965 * "funcname(...", not "funcname!...". */
21966 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021967 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000021968 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021969 return n;
21970}
21971
Bram Moolenaara1544c02013-05-30 12:35:52 +020021972 char_u *
21973get_expanded_name(name, check)
21974 char_u *name;
21975 int check;
21976{
21977 char_u *nm = name;
21978 char_u *p;
21979
21980 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
21981
21982 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021983 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020021984 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021985
Bram Moolenaara1544c02013-05-30 12:35:52 +020021986 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 Moolenaar018acca2013-05-30 13:37:28 +020022198 static 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
Bram Moolenaar473de612013-06-08 18:19:48 +020024225 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 }
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) */