blob: 12794d4cbd4020b94250eba244c6f043e234d177 [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 Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
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 */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000364};
365
366/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000367#define vv_type vv_di.di_tv.v_type
368#define vv_nr vv_di.di_tv.vval.v_number
369#define vv_float vv_di.di_tv.vval.v_float
370#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000371#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000372#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000373
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200374static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000375#define vimvarht vimvardict.dv_hashtab
376
Bram Moolenaara40058a2005-07-11 22:42:07 +0000377static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
378static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
380static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
381static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000382static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
383static void list_glob_vars __ARGS((int *first));
384static void list_buf_vars __ARGS((int *first));
385static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000386#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_vim_vars __ARGS((int *first));
390static void list_script_vars __ARGS((int *first));
391static void list_func_vars __ARGS((int *first));
392static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
394static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100395static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static void clear_lval __ARGS((lval_T *lp));
397static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
398static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
400static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
401static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
402static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
403static void item_lock __ARGS((typval_T *tv, int deep, int lock));
404static int tv_islocked __ARGS((typval_T *tv));
405
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
407static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000412static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
413static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000414
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000415static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000420static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100422static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
423static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
424static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000425static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000431static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100432static 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 +0000433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000434static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200435static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
437static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000445#ifdef FEAT_FLOAT
446static int string2float __ARGS((char_u *text, float_T *value));
447#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
449static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100450static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static 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 +0200452static 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 +0000453static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000454static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200458static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100461static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200469static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100480static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000481static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100482static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
485static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
486#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000487static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000490static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000491static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000492#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000493static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000494static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200501static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
506static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200517#ifdef FEAT_FLOAT
518static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000522static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
529static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000533static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000542static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000544static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000550static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000558static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000559static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000560static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000561static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200564static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000565static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000573static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000587static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100592static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000594static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000606#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200607static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
609#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200610#ifdef FEAT_LUA
611static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000617static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000618static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000621static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000625#ifdef vim_mkdir
626static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100629#ifdef FEAT_MZSCHEME
630static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100634static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000635static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000636#ifdef FEAT_FLOAT
637static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000640static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000641static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200642#ifdef FEAT_PYTHON3
643static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
645#ifdef FEAT_PYTHON
646static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000650static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000662#ifdef FEAT_FLOAT
663static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
664#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200665static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100667static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100687#ifdef FEAT_CRYPT
688static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
689#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000690static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200691static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#ifdef FEAT_FLOAT
694static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200695static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000698static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000699static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000706static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200707static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708#ifdef HAVE_STRFTIME
709static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
710#endif
711static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200717static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200718static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000724static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200725static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000728static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000730static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000731static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000733static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200734#ifdef FEAT_FLOAT
735static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000741#ifdef FEAT_FLOAT
742static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
743#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200745static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200746static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100750static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static 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 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100775static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
787static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static 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 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static 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 +0000828static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
829static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
832static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000833static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200837
838#ifdef EBCDIC
839static int compare_func_name __ARGS((const void *s1, const void *s2));
840static void sortFunctions __ARGS(());
841#endif
842
Bram Moolenaar33570922005-01-25 22:26:29 +0000843/*
844 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000845 */
846 void
847eval_init()
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 int i;
850 struct vimvar *p;
851
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200852 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
853 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200854 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000855 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000856 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000857
858 for (i = 0; i < VV_LEN; ++i)
859 {
860 p = &vimvars[i];
861 STRCPY(p->vv_di.di_key, p->vv_name);
862 if (p->vv_flags & VV_RO)
863 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
864 else if (p->vv_flags & VV_RO_SBX)
865 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
866 else
867 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000868
869 /* add to v: scope dict, unless the value is not always available */
870 if (p->vv_type != VAR_UNKNOWN)
871 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000873 /* add to compat scope dict */
874 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000876 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100877 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100914# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200915 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100916# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917
918 /* global variables */
919 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000921 /* autoloaded script names */
922 ga_clear_strings(&ga_loaded);
923
Bram Moolenaarcca74132013-09-25 21:00:28 +0200924 /* Script-local variables. First clear all the variables and in a second
925 * loop free the scriptvar_T, because a variable in one script might hold
926 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200927 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200928 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200929 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 ga_clear(&ga_scripts);
932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000933 /* unreferenced lists and dicts */
934 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000935
936 /* functions */
937 free_all_functions();
938 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939}
940#endif
941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 * Return the name of the executed function.
944 */
945 char_u *
946func_name(cookie)
947 void *cookie;
948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the next breakpoint line for a funccall cookie.
954 */
955 linenr_T *
956func_breakpoint(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the address holding the debug tick for a funccall cookie.
964 */
965 int *
966func_dbg_tick(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/*
973 * Return the nesting level for a funccall cookie.
974 */
975 int
976func_level(cookie)
977 void *cookie;
978{
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000983funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000985/* pointer to list of previously used funccal, still around because some
986 * item in it is still being used. */
987funccall_T *previous_funccal = NULL;
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Return TRUE when a function was ended by a ":return" command.
991 */
992 int
993current_func_returned()
994{
995 return current_funccal->returned;
996}
997
998
999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Set an internal variable to a string value. Creates the variable if it does
1001 * not already exist.
1002 */
1003 void
1004set_internal_string_var(name, value)
1005 char_u *name;
1006 char_u *value;
1007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 val = vim_strsave(value);
1012 if (val != NULL)
1013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 }
1021}
1022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static char_u *redir_endp = NULL;
1026static char_u *redir_varname = NULL;
1027
1028/*
1029 * Start recording command output to a variable
1030 * Returns OK if successfully completed the setup. FAIL otherwise.
1031 */
1032 int
1033var_redir_start(name, append)
1034 char_u *name;
1035 int append; /* append to an existing variable */
1036{
1037 int save_emsg;
1038 int err;
1039 typval_T tv;
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001042 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 {
1044 EMSG(_(e_invarg));
1045 return FAIL;
1046 }
1047
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001048 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 redir_varname = vim_strsave(name);
1050 if (redir_varname == NULL)
1051 return FAIL;
1052
1053 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1054 if (redir_lval == NULL)
1055 {
1056 var_redir_stop();
1057 return FAIL;
1058 }
1059
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 /* The output is stored in growarray "redir_ga" until redirection ends. */
1061 ga_init2(&redir_ga, (int)sizeof(char), 500);
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001064 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001065 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1067 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001068 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp != NULL && *redir_endp != NUL)
1070 /* Trailing characters are present after the variable name */
1071 EMSG(_(e_trailing));
1072 else
1073 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 var_redir_stop();
1076 return FAIL;
1077 }
1078
1079 /* check if we can write to the variable: set it to or append an empty
1080 * string */
1081 save_emsg = did_emsg;
1082 did_emsg = FALSE;
1083 tv.v_type = VAR_STRING;
1084 tv.vval.v_string = (char_u *)"";
1085 if (append)
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1087 else
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001091 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (err)
1093 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001094 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 var_redir_stop();
1096 return FAIL;
1097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103 * Append "value[value_len]" to the variable set by var_redir_start().
1104 * The actual appending is postponed until redirection ends, because the value
1105 * appended may in fact be the string we write to, changing it may cause freed
1106 * memory to be used:
1107 * :redir => foo
1108 * :let foo
1109 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 if (redir_lval == NULL)
1119 return;
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 {
1128 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 }
1131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133}
1134
1135/*
1136 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 */
1139 void
1140var_redir_stop()
1141{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 typval_T tv;
1143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_lval != NULL)
1145 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* If there was no error: assign the text to the variable. */
1147 if (redir_endp != NULL)
1148 {
1149 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1150 tv.v_type = VAR_STRING;
1151 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001152 /* Call get_lval() again, if it's inside a Dict or List it may
1153 * have changed. */
1154 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001155 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001156 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1157 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1158 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* free the collected output */
1162 vim_free(redir_ga.ga_data);
1163 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 vim_free(redir_lval);
1166 redir_lval = NULL;
1167 }
1168 vim_free(redir_varname);
1169 redir_varname = NULL;
1170}
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# if defined(FEAT_MBYTE) || defined(PROTO)
1173 int
1174eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1175 char_u *enc_from;
1176 char_u *enc_to;
1177 char_u *fname_from;
1178 char_u *fname_to;
1179{
1180 int err = FALSE;
1181
1182 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1183 set_vim_var_string(VV_CC_TO, enc_to, -1);
1184 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1185 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1186 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1187 err = TRUE;
1188 set_vim_var_string(VV_CC_FROM, NULL, -1);
1189 set_vim_var_string(VV_CC_TO, NULL, -1);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1192
1193 if (err)
1194 return FAIL;
1195 return OK;
1196}
1197# endif
1198
1199# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1200 int
1201eval_printexpr(fname, args)
1202 char_u *fname;
1203 char_u *args;
1204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_FNAME_IN, fname, -1);
1208 set_vim_var_string(VV_CMDARG, args, -1);
1209 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1210 err = TRUE;
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_CMDARG, NULL, -1);
1213
1214 if (err)
1215 {
1216 mch_remove(fname);
1217 return FAIL;
1218 }
1219 return OK;
1220}
1221# endif
1222
1223# if defined(FEAT_DIFF) || defined(PROTO)
1224 void
1225eval_diff(origfile, newfile, outfile)
1226 char_u *origfile;
1227 char_u *newfile;
1228 char_u *outfile;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1233 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1234 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1235 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239}
1240
1241 void
1242eval_patch(origfile, difffile, outfile)
1243 char_u *origfile;
1244 char_u *difffile;
1245 char_u *outfile;
1246{
1247 int err;
1248
1249 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1251 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1252 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1253 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1254 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256}
1257# endif
1258
1259/*
1260 * Top level evaluation function, returning a boolean.
1261 * Sets "error" to TRUE if there was an error.
1262 * Return TRUE or FALSE.
1263 */
1264 int
1265eval_to_bool(arg, error, nextcmd, skip)
1266 char_u *arg;
1267 int *error;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int retval = FALSE;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 else
1279 {
1280 *error = FALSE;
1281 if (!skip)
1282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001283 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 }
1287 if (skip)
1288 --emsg_skip;
1289
1290 return retval;
1291}
1292
1293/*
1294 * Top level evaluation function, returning a string. If "skip" is TRUE,
1295 * only parsing to "nextcmd" is done, without reporting errors. Return
1296 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1297 */
1298 char_u *
1299eval_to_string_skip(arg, nextcmd, skip)
1300 char_u *arg;
1301 char_u **nextcmd;
1302 int skip; /* only parse, don't execute */
1303{
Bram Moolenaar33570922005-01-25 22:26:29 +00001304 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *retval;
1306
1307 if (skip)
1308 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 retval = NULL;
1311 else
1312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 retval = vim_strsave(get_tv_string(&tv));
1314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323 * Skip over an expression at "*pp".
1324 * Return FAIL for an error, OK otherwise.
1325 */
1326 int
1327skip_expr(pp)
1328 char_u **pp;
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331
1332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334}
1335
1336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 * When "convert" is TRUE convert a List into a sequence of lines and convert
1339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Return pointer to allocated memory, or NULL for failure.
1341 */
1342 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *arg;
1345 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = NULL;
1357 else
1358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 {
1361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 if (tv.vval.v_list->lv_len > 0)
1366 ga_append(&ga, NL);
1367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001368 ga_append(&ga, NUL);
1369 retval = (char_u *)ga.ga_data;
1370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371#ifdef FEAT_FLOAT
1372 else if (convert && tv.v_type == VAR_FLOAT)
1373 {
1374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1375 retval = vim_strsave(numbuf);
1376 }
1377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 else
1379 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 * Call eval_to_string() without using current local variables and using
1388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *arg;
1393 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 char_u *retval;
1397 void *save_funccalp;
1398
1399 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 ++sandbox;
1402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404 if (use_sandbox)
1405 --sandbox;
1406 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 restore_funccal(save_funccalp);
1408 return retval;
1409}
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411/*
1412 * Top level evaluation function, returning a number.
1413 * Evaluates "expr" silently.
1414 * Returns -1 for an error.
1415 */
1416 int
1417eval_to_number(expr)
1418 char_u *expr;
1419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001422 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 ++emsg_off;
1425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 retval = -1;
1428 else
1429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001430 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 --emsg_off;
1434
1435 return retval;
1436}
1437
Bram Moolenaara40058a2005-07-11 22:42:07 +00001438/*
1439 * Prepare v: variable "idx" to be used.
1440 * Save the current typeval in "save_tv".
1441 * When not used yet add the variable to the v: hashtable.
1442 */
1443 static void
1444prepare_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 *save_tv = vimvars[idx].vv_tv;
1449 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1450 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1451}
1452
1453/*
1454 * Restore v: variable "idx" to typeval "save_tv".
1455 * When no longer defined, remove the variable from the v: hashtable.
1456 */
1457 static void
1458restore_vimvar(idx, save_tv)
1459 int idx;
1460 typval_T *save_tv;
1461{
1462 hashitem_T *hi;
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464 vimvars[idx].vv_tv = *save_tv;
1465 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1466 {
1467 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1468 if (HASHITEM_EMPTY(hi))
1469 EMSG2(_(e_intern2), "restore_vimvar()");
1470 else
1471 hash_remove(&vimvarht, hi);
1472 }
1473}
1474
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001475#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476/*
1477 * Evaluate an expression to a list with suggestions.
1478 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001479 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001480 */
1481 list_T *
1482eval_spell_expr(badword, expr)
1483 char_u *badword;
1484 char_u *expr;
1485{
1486 typval_T save_val;
1487 typval_T rettv;
1488 list_T *list = NULL;
1489 char_u *p = skipwhite(expr);
1490
1491 /* Set "v:val" to the bad word. */
1492 prepare_vimvar(VV_VAL, &save_val);
1493 vimvars[VV_VAL].vv_type = VAR_STRING;
1494 vimvars[VV_VAL].vv_str = badword;
1495 if (p_verbose == 0)
1496 ++emsg_off;
1497
1498 if (eval1(&p, &rettv, TRUE) == OK)
1499 {
1500 if (rettv.v_type != VAR_LIST)
1501 clear_tv(&rettv);
1502 else
1503 list = rettv.vval.v_list;
1504 }
1505
1506 if (p_verbose == 0)
1507 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508 restore_vimvar(VV_VAL, &save_val);
1509
1510 return list;
1511}
1512
1513/*
1514 * "list" is supposed to contain two items: a word and a number. Return the
1515 * word in "pp" and the number as the return value.
1516 * Return -1 if anything isn't right.
1517 * Used to get the good word and score from the eval_spell_expr() result.
1518 */
1519 int
1520get_spellword(list, pp)
1521 list_T *list;
1522 char_u **pp;
1523{
1524 listitem_T *li;
1525
1526 li = list->lv_first;
1527 if (li == NULL)
1528 return -1;
1529 *pp = get_tv_string(&li->li_tv);
1530
1531 li = li->li_next;
1532 if (li == NULL)
1533 return -1;
1534 return get_tv_number(&li->li_tv);
1535}
1536#endif
1537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 * Top level evaluation function.
1540 * Returns an allocated typval_T with the result.
1541 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 */
1543 typval_T *
1544eval_expr(arg, nextcmd)
1545 char_u *arg;
1546 char_u **nextcmd;
1547{
1548 typval_T *tv;
1549
1550 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 {
1553 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 }
1556
1557 return tv;
1558}
1559
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 * Uses argv[argc] for the function arguments. Only Number and String
1564 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001567 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001568call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 char_u *func;
1570 int argc;
1571 char_u **argv;
1572 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001573 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
Bram Moolenaar33570922005-01-25 22:26:29 +00001576 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 long n;
1578 int len;
1579 int i;
1580 int doesrange;
1581 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001584 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 for (i = 0; i < argc; i++)
1589 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001590 /* Pass a NULL or empty argument as an empty string */
1591 if (argv[i] == NULL || *argv[i] == NUL)
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_STRING;
1594 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001595 continue;
1596 }
1597
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598 if (str_arg_only)
1599 len = 0;
1600 else
1601 /* Recognize a number argument, the others must be strings. */
1602 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (len != 0 && len == (int)STRLEN(argv[i]))
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_NUMBER;
1606 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 else
1609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001610 argvars[i].v_type = VAR_STRING;
1611 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
1614
1615 if (safe)
1616 {
1617 save_funccalp = save_funccal();
1618 ++sandbox;
1619 }
1620
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1622 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (safe)
1626 {
1627 --sandbox;
1628 restore_funccal(save_funccalp);
1629 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 vim_free(argvars);
1631
1632 if (ret == FAIL)
1633 clear_tv(rettv);
1634
1635 return ret;
1636}
1637
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001638/*
1639 * Call vimL function "func" and return the result as a number.
1640 * Returns -1 when calling the function fails.
1641 * Uses argv[argc] for the function arguments.
1642 */
1643 long
1644call_func_retnr(func, argc, argv, safe)
1645 char_u *func;
1646 int argc;
1647 char_u **argv;
1648 int safe; /* use the sandbox */
1649{
1650 typval_T rettv;
1651 long retval;
1652
1653 /* All arguments are passed as strings, no conversion to number. */
1654 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1655 return -1;
1656
1657 retval = get_tv_number_chk(&rettv, NULL);
1658 clear_tv(&rettv);
1659 return retval;
1660}
1661
1662#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1663 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1664
Bram Moolenaar4f688582007-07-24 12:34:30 +00001665# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667 * Call vimL function "func" and return the result as a string.
1668 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 * Uses argv[argc] for the function arguments.
1670 */
1671 void *
1672call_func_retstr(func, argc, argv, safe)
1673 char_u *func;
1674 int argc;
1675 char_u **argv;
1676 int safe; /* use the sandbox */
1677{
1678 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 return NULL;
1684
1685 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return retval;
1688}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001689# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001692 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001694 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 */
1696 void *
1697call_func_retlist(func, argc, argv, safe)
1698 char_u *func;
1699 int argc;
1700 char_u **argv;
1701 int safe; /* use the sandbox */
1702{
1703 typval_T rettv;
1704
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001705 /* All arguments are passed as strings, no conversion to number. */
1706 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 return NULL;
1708
1709 if (rettv.v_type != VAR_LIST)
1710 {
1711 clear_tv(&rettv);
1712 return NULL;
1713 }
1714
1715 return rettv.vval.v_list;
1716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717#endif
1718
1719/*
1720 * Save the current function call pointer, and set it to NULL.
1721 * Used when executing autocommands and for ":source".
1722 */
1723 void *
1724save_funccal()
1725{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 current_funccal = NULL;
1729 return (void *)fc;
1730}
1731
1732 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733restore_funccal(vfc)
1734 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736 funccall_T *fc = (funccall_T *)vfc;
1737
1738 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739}
1740
Bram Moolenaar05159a02005-02-26 23:04:13 +00001741#if defined(FEAT_PROFILE) || defined(PROTO)
1742/*
1743 * Prepare profiling for entering a child or something else that is not
1744 * counted for the script/function itself.
1745 * Should always be called in pair with prof_child_exit().
1746 */
1747 void
1748prof_child_enter(tm)
1749 proftime_T *tm; /* place to store waittime */
1750{
1751 funccall_T *fc = current_funccal;
1752
1753 if (fc != NULL && fc->func->uf_profiling)
1754 profile_start(&fc->prof_child);
1755 script_prof_save(tm);
1756}
1757
1758/*
1759 * Take care of time spent in a child.
1760 * Should always be called after prof_child_enter().
1761 */
1762 void
1763prof_child_exit(tm)
1764 proftime_T *tm; /* where waittime was stored */
1765{
1766 funccall_T *fc = current_funccal;
1767
1768 if (fc != NULL && fc->func->uf_profiling)
1769 {
1770 profile_end(&fc->prof_child);
1771 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1772 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1773 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1774 }
1775 script_prof_restore(tm);
1776}
1777#endif
1778
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_FOLDING
1781/*
1782 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1783 * it in "*cp". Doesn't give error messages.
1784 */
1785 int
1786eval_foldexpr(arg, cp)
1787 char_u *arg;
1788 int *cp;
1789{
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 int retval;
1792 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001793 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1794 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001797 if (use_sandbox)
1798 ++sandbox;
1799 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 if (tv.v_type == VAR_NUMBER)
1807 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001808 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 retval = 0;
1810 else
1811 {
1812 /* If the result is a string, check if there is a non-digit before
1813 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (!VIM_ISDIGIT(*s) && *s != '-')
1816 *cp = *s++;
1817 retval = atol((char *)s);
1818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 --sandbox;
1824 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 return retval;
1827}
1828#endif
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let" list all variable values
1832 * ":let var1 var2" list variable values
1833 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001834 * ":let var += expr" assignment command.
1835 * ":let var -= expr" assignment command.
1836 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839 void
1840ex_let(eap)
1841 exarg_T *eap;
1842{
1843 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 int var_count = 0;
1848 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001851 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 argend = skip_var_list(arg, &var_count, &semicolon);
1854 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001856 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1857 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001858 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001861 /*
1862 * ":let" without "=": list variables
1863 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 if (*arg == '[')
1865 EMSG(_(e_invarg));
1866 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_glob_vars(&first);
1873 list_buf_vars(&first);
1874 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001875#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001877#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_script_vars(&first);
1879 list_func_vars(&first);
1880 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 eap->nextcmd = check_nextcmd(arg);
1883 }
1884 else
1885 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 op[0] = '=';
1887 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001888 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 {
1890 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1891 op[0] = expr[-1]; /* +=, -= or .= */
1892 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (eap->skip)
1896 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (eap->skip)
1899 {
1900 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 --emsg_skip;
1903 }
1904 else if (i != FAIL)
1905 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 }
1911}
1912
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913/*
1914 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1915 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 * When "nextchars" is not NULL it points to a string with characters that
1917 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1918 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 * Returns OK or FAIL;
1920 */
1921 static int
1922ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1923 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int copy; /* copy values from "tv", don't move */
1926 int semicolon; /* from skip_var_list() */
1927 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929{
1930 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 listitem_T *item;
1934 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935
1936 if (*arg != '[')
1937 {
1938 /*
1939 * ":let var = expr" or ":for var in list"
1940 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return FAIL;
1943 return OK;
1944 }
1945
1946 /*
1947 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1948 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001949 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 {
1951 EMSG(_(e_listreq));
1952 return FAIL;
1953 }
1954
1955 i = list_len(l);
1956 if (semicolon == 0 && var_count < i)
1957 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001958 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 return FAIL;
1960 }
1961 if (var_count - semicolon > i)
1962 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001963 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 }
1966
1967 item = l->lv_first;
1968 while (*arg != ']')
1969 {
1970 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 item = item->li_next;
1973 if (arg == NULL)
1974 return FAIL;
1975
1976 arg = skipwhite(arg);
1977 if (*arg == ';')
1978 {
1979 /* Put the rest of the list (may be empty) in the var after ';'.
1980 * Create a new list for this. */
1981 l = list_alloc();
1982 if (l == NULL)
1983 return FAIL;
1984 while (item != NULL)
1985 {
1986 list_append_tv(l, &item->li_tv);
1987 item = item->li_next;
1988 }
1989
1990 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001991 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 ltv.vval.v_list = l;
1993 l->lv_refcount = 1;
1994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1996 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 clear_tv(&ltv);
1998 if (arg == NULL)
1999 return FAIL;
2000 break;
2001 }
2002 else if (*arg != ',' && *arg != ']')
2003 {
2004 EMSG2(_(e_intern2), "ex_let_vars()");
2005 return FAIL;
2006 }
2007 }
2008
2009 return OK;
2010}
2011
2012/*
2013 * Skip over assignable variable "var" or list of variables "[var, var]".
2014 * Used for ":let varvar = expr" and ":for varvar in expr".
2015 * For "[var, var]" increment "*var_count" for each variable.
2016 * for "[var, var; var]" set "semicolon".
2017 * Return NULL for an error.
2018 */
2019 static char_u *
2020skip_var_list(arg, var_count, semicolon)
2021 char_u *arg;
2022 int *var_count;
2023 int *semicolon;
2024{
2025 char_u *p, *s;
2026
2027 if (*arg == '[')
2028 {
2029 /* "[var, var]": find the matching ']'. */
2030 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002031 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 {
2033 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2034 s = skip_var_one(p);
2035 if (s == p)
2036 {
2037 EMSG2(_(e_invarg2), p);
2038 return NULL;
2039 }
2040 ++*var_count;
2041
2042 p = skipwhite(s);
2043 if (*p == ']')
2044 break;
2045 else if (*p == ';')
2046 {
2047 if (*semicolon == 1)
2048 {
2049 EMSG(_("Double ; in list of variables"));
2050 return NULL;
2051 }
2052 *semicolon = 1;
2053 }
2054 else if (*p != ',')
2055 {
2056 EMSG2(_(e_invarg2), p);
2057 return NULL;
2058 }
2059 }
2060 return p + 1;
2061 }
2062 else
2063 return skip_var_one(arg);
2064}
2065
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002067 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002069 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 static char_u *
2071skip_var_one(arg)
2072 char_u *arg;
2073{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 if (*arg == '@' && arg[1] != NUL)
2075 return arg + 2;
2076 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2077 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078}
2079
Bram Moolenaara7043832005-01-21 11:56:39 +00002080/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 * List variables for hashtab "ht" with prefix "prefix".
2082 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002089 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090{
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 hashitem_T *hi;
2092 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 int todo;
2094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2097 {
2098 if (!HASHITEM_EMPTY(hi))
2099 {
2100 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 di = HI2DI(hi);
2102 if (empty || di->di_tv.v_type != VAR_STRING
2103 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
2106 }
2107}
2108
2109/*
2110 * List global variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_glob_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002117}
2118
2119/*
2120 * List buffer variables.
2121 */
2122 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123list_buf_vars(first)
2124 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 char_u numbuf[NUMBUFLEN];
2127
Bram Moolenaar429fa852013-04-15 12:27:36 +02002128 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130
2131 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2133 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134}
2135
2136/*
2137 * List window variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_win_vars(first)
2141 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147#ifdef FEAT_WINDOWS
2148/*
2149 * List tab page variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_tab_vars(first)
2153 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002155 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157}
2158#endif
2159
Bram Moolenaara7043832005-01-21 11:56:39 +00002160/*
2161 * List Vim variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_vim_vars(first)
2165 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168}
2169
2170/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002171 * List script-local variables, if there is a script.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_script_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2179 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
2183 * List function variables, if there is a function.
2184 */
2185 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_func_vars(first)
2187 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188{
2189 if (current_funccal != NULL)
2190 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192}
2193
2194/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 * List variables in "arg".
2196 */
2197 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 exarg_T *eap;
2200 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202{
2203 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 char_u *name_start;
2207 char_u *arg_subsc;
2208 char_u *tofree;
2209 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210
2211 while (!ends_excmd(*arg) && !got_int)
2212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002215 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2217 {
2218 emsg_severe = TRUE;
2219 EMSG(_(e_trailing));
2220 break;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 /* get_name_len() takes care of expanding curly braces */
2226 name_start = name = arg;
2227 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2228 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* This is mainly to keep test 49 working: when expanding
2231 * curly braces fails overrule the exception error message. */
2232 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 emsg_severe = TRUE;
2235 EMSG2(_(e_invarg2), arg);
2236 break;
2237 }
2238 error = TRUE;
2239 }
2240 else
2241 {
2242 if (tofree != NULL)
2243 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002244 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
2247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 /* handle d.key, l[idx], f(expr) */
2249 arg_subsc = arg;
2250 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'g': list_glob_vars(first); break;
2259 case 'b': list_buf_vars(first); break;
2260 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002261#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002262 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'v': list_vim_vars(first); break;
2265 case 's': list_script_vars(first); break;
2266 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 default:
2268 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 }
2271 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 {
2273 char_u numbuf[NUMBUFLEN];
2274 char_u *tf;
2275 int c;
2276 char_u *s;
2277
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002278 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 c = *arg;
2280 *arg = NUL;
2281 list_one_var_a((char_u *)"",
2282 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 tv.v_type,
2284 s == NULL ? (char_u *)"" : s,
2285 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 *arg = c;
2287 vim_free(tf);
2288 }
2289 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293
2294 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296
2297 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 }
2299
2300 return arg;
2301}
2302
2303/*
2304 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2305 * Returns a pointer to the char just after the var name.
2306 * Returns NULL if there is an error.
2307 */
2308 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002313 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315{
2316 int c1;
2317 char_u *name;
2318 char_u *p;
2319 char_u *arg_end = NULL;
2320 int len;
2321 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323
2324 /*
2325 * ":let $VAR = expr": Set environment variable.
2326 */
2327 if (*arg == '$')
2328 {
2329 /* Find the end of the name. */
2330 ++arg;
2331 name = arg;
2332 len = get_env_len(&arg);
2333 if (len == 0)
2334 EMSG2(_(e_invarg2), name - 1);
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 if (op != NULL && (*op == '+' || *op == '-'))
2338 EMSG2(_(e_letwrong), op);
2339 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002342 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
2344 c1 = name[len];
2345 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 p = get_tv_string_chk(tv);
2347 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 {
2349 int mustfree = FALSE;
2350 char_u *s = vim_getenv(name, &mustfree);
2351
2352 if (s != NULL)
2353 {
2354 p = tofree = concat_str(s, p);
2355 if (mustfree)
2356 vim_free(s);
2357 }
2358 }
2359 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 if (STRICMP(name, "HOME") == 0)
2363 init_homedir();
2364 else if (didset_vim && STRICMP(name, "VIM") == 0)
2365 didset_vim = FALSE;
2366 else if (didset_vimruntime
2367 && STRICMP(name, "VIMRUNTIME") == 0)
2368 didset_vimruntime = FALSE;
2369 arg_end = arg;
2370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
2374 }
2375 }
2376
2377 /*
2378 * ":let &option = expr": Set option value.
2379 * ":let &l:option = expr": Set local option value.
2380 * ":let &g:option = expr": Set global option value.
2381 */
2382 else if (*arg == '&')
2383 {
2384 /* Find the end of the name. */
2385 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 if (p == NULL || (endchars != NULL
2387 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 EMSG(_(e_letunexp));
2389 else
2390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 long n;
2392 int opt_type;
2393 long numval;
2394 char_u *stringval = NULL;
2395 char_u *s;
2396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 c1 = *p;
2398 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399
2400 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 s = get_tv_string_chk(tv); /* != NULL if number or string */
2402 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 {
2404 opt_type = get_option_value(arg, &numval,
2405 &stringval, opt_flags);
2406 if ((opt_type == 1 && *op == '.')
2407 || (opt_type == 0 && *op != '.'))
2408 EMSG2(_(e_letwrong), op);
2409 else
2410 {
2411 if (opt_type == 1) /* number */
2412 {
2413 if (*op == '+')
2414 n = numval + n;
2415 else
2416 n = numval - n;
2417 }
2418 else if (opt_type == 0 && stringval != NULL) /* string */
2419 {
2420 s = concat_str(stringval, s);
2421 vim_free(stringval);
2422 stringval = s;
2423 }
2424 }
2425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 if (s != NULL)
2427 {
2428 set_option_value(arg, n, s, opt_flags);
2429 arg_end = p;
2430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 }
2434 }
2435
2436 /*
2437 * ":let @r = expr": Set register contents.
2438 */
2439 else if (*arg == '@')
2440 {
2441 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (op != NULL && (*op == '+' || *op == '-'))
2443 EMSG2(_(e_letwrong), op);
2444 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002445 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 EMSG(_(e_letunexp));
2447 else
2448 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 char_u *s;
2451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 p = get_tv_string_chk(tv);
2453 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002455 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 if (s != NULL)
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 vim_free(s);
2460 }
2461 }
2462 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 arg_end = arg + 1;
2466 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470
2471 /*
2472 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002479 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 arg_end = p;
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492
2493 else
2494 EMSG2(_(e_invarg2), arg);
2495
2496 return arg_end;
2497}
2498
2499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2501 */
2502 static int
2503check_changedtick(arg)
2504 char_u *arg;
2505{
2506 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2507 {
2508 EMSG2(_(e_readonlyvar), arg);
2509 return TRUE;
2510 }
2511 return FALSE;
2512}
2513
2514/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * Get an lval: variable, Dict item or List item that can be assigned a value
2516 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2517 * "name.key", "name.key[expr]" etc.
2518 * Indexing only works if "name" is an existing List or Dictionary.
2519 * "name" points to the start of the name.
2520 * If "rettv" is not NULL it points to the value to be assigned.
2521 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2522 * wrong; must end in space or cmd separator.
2523 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002524 * flags:
2525 * GLV_QUIET: do not give error messages
2526 * GLV_NO_AUTOLOAD: do not use script autoloading
2527 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002529 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 */
2532 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 typval_T *rettv;
2536 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int unlet;
2538 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002540 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 char_u *expr_start, *expr_end;
2544 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 dictitem_T *v;
2546 typval_T var1;
2547 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557
2558 if (skip)
2559 {
2560 /* When skipping just find the end of the name. */
2561 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 }
2564
2565 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (expr_start != NULL)
2568 {
2569 /* Don't expand the name when we already know there is an error. */
2570 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2571 && *p != '[' && *p != '.')
2572 {
2573 EMSG(_(e_trailing));
2574 return NULL;
2575 }
2576
2577 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2578 if (lp->ll_exp_name == NULL)
2579 {
2580 /* Report an invalid expression in braces, unless the
2581 * expression evaluation has been cancelled due to an
2582 * aborting error, an interrupt, or an exception. */
2583 if (!aborting() && !quiet)
2584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 EMSG2(_(e_invarg2), name);
2587 return NULL;
2588 }
2589 }
2590 lp->ll_name = lp->ll_exp_name;
2591 }
2592 else
2593 lp->ll_name = name;
2594
2595 /* Without [idx] or .key we are done. */
2596 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2597 return p;
2598
2599 cc = *p;
2600 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (v == NULL && !quiet)
2603 EMSG2(_(e_undefvar), lp->ll_name);
2604 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 if (v == NULL)
2606 return NULL;
2607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 /*
2609 * Loop until no more [idx] or .key is following.
2610 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2615 && !(lp->ll_tv->v_type == VAR_DICT
2616 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_("E689: Can only index a List or Dictionary"));
2620 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E708: [:] must come last"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 len = -1;
2630 if (*p == '.')
2631 {
2632 key = p + 1;
2633 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2634 ;
2635 if (len == 0)
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_(e_emptykey));
2639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641 p = key + len;
2642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 else
2644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (*p == ':')
2648 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 empty1 = FALSE;
2652 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002654 if (get_tv_string_chk(&var1) == NULL)
2655 {
2656 /* not a number or string */
2657 clear_tv(&var1);
2658 return NULL;
2659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661
2662 /* Optionally get the second index [ :expr]. */
2663 if (*p == ':')
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (rettv != NULL && (rettv->v_type != VAR_LIST
2674 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
2682 p = skipwhite(p + 1);
2683 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 else
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (!empty1)
2691 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var2) == NULL)
2695 {
2696 /* not a number or string */
2697 if (!empty1)
2698 clear_tv(&var1);
2699 clear_tv(&var2);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718
2719 /* Skip to past ']'. */
2720 ++p;
2721 }
2722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
2725 if (len == -1)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (*key == NUL)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
2736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 lp->ll_list = NULL;
2738 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002739 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002740
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002741 /* When assigning to a scope dictionary check that a function and
2742 * variable name is valid (only variable name unless it is l: or
2743 * g: dictionary). Disallow overwriting a builtin function. */
2744 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002746 int prevval;
2747 int wrong;
2748
2749 if (len != -1)
2750 {
2751 prevval = key[len];
2752 key[len] = NUL;
2753 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002754 else
2755 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2757 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002759 || !valid_varname(key);
2760 if (len != -1)
2761 key[len] = prevval;
2762 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 return NULL;
2764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 /* Can't add "v:" variable. */
2769 if (lp->ll_dict == &vimvardict)
2770 {
2771 EMSG2(_(e_illvar), name);
2772 return NULL;
2773 }
2774
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002775 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (len == -1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 if (len == -1)
2789 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 p = NULL;
2792 break;
2793 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 /* existing variable, need to check if it can be changed */
2795 else if (var_check_ro(lp->ll_di->di_flags, name))
2796 return NULL;
2797
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (len == -1)
2799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 else
2803 {
2804 /*
2805 * Get the number and item for the only or first index of the List.
2806 */
2807 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 else
2810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002811 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var1);
2813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_list = lp->ll_tv->vval.v_list;
2816 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2817 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002819 if (lp->ll_n1 < 0)
2820 {
2821 lp->ll_n1 = 0;
2822 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2823 }
2824 }
2825 if (lp->ll_li == NULL)
2826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 if (!quiet)
2830 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833
2834 /*
2835 * May need to find the item or absolute index for the second
2836 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 * When no index given: "lp->ll_empty2" is TRUE.
2838 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2857 if (lp->ll_n1 < 0)
2858 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2859 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002860 {
2861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 }
2866
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return p;
2872}
2873
2874/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 */
2877 static void
2878clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880{
2881 vim_free(lp->ll_exp_name);
2882 vim_free(lp->ll_newkey);
2883}
2884
2885/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 */
2890 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897{
2898 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 listitem_T *ri;
2900 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901
2902 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 cc = *endp;
2907 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 if (op != NULL && *op != '=')
2909 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002913 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002914 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 {
2916 if (tv_op(&tv, rettv, op) == OK)
2917 set_var(lp->ll_name, &tv, FALSE);
2918 clear_tv(&tv);
2919 }
2920 }
2921 else
2922 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 else if (tv_check_lock(lp->ll_newkey == NULL
2927 ? lp->ll_tv->v_lock
2928 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2929 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 else if (lp->ll_range)
2931 {
2932 /*
2933 * Assign the List values to the list items.
2934 */
2935 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 if (op != NULL && *op != '=')
2938 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2939 else
2940 {
2941 clear_tv(&lp->ll_li->li_tv);
2942 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 ri = ri->li_next;
2945 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2946 break;
2947 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002950 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 ri = NULL;
2953 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 lp->ll_li = lp->ll_li->li_next;
2957 ++lp->ll_n1;
2958 }
2959 if (ri != NULL)
2960 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 else if (lp->ll_empty2
2962 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 : lp->ll_n1 != lp->ll_n2)
2964 EMSG(_("E711: List value has not enough items"));
2965 }
2966 else
2967 {
2968 /*
2969 * Assign to a List or Dictionary item.
2970 */
2971 if (lp->ll_newkey != NULL)
2972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 {
2975 EMSG2(_(e_letwrong), op);
2976 return;
2977 }
2978
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002980 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 if (di == NULL)
2982 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002983 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2984 {
2985 vim_free(di);
2986 return;
2987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 else if (op != NULL && *op != '=')
2991 {
2992 tv_op(lp->ll_tv, rettv, op);
2993 return;
2994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /*
2999 * Assign the value to the variable or list item.
3000 */
3001 if (copy)
3002 copy_tv(rettv, lp->ll_tv);
3003 else
3004 {
3005 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003006 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008 }
3009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010}
3011
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3014 * Returns OK or FAIL.
3015 */
3016 static int
3017tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 typval_T *tv1;
3019 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 char_u *op;
3021{
3022 long n;
3023 char_u numbuf[NUMBUFLEN];
3024 char_u *s;
3025
3026 /* Can't do anything with a Funcref or a Dict on the right. */
3027 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3028 {
3029 switch (tv1->v_type)
3030 {
3031 case VAR_DICT:
3032 case VAR_FUNC:
3033 break;
3034
3035 case VAR_LIST:
3036 if (*op != '+' || tv2->v_type != VAR_LIST)
3037 break;
3038 /* List += List */
3039 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3040 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3041 return OK;
3042
3043 case VAR_NUMBER:
3044 case VAR_STRING:
3045 if (tv2->v_type == VAR_LIST)
3046 break;
3047 if (*op == '+' || *op == '-')
3048 {
3049 /* nr += nr or nr -= nr*/
3050 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051#ifdef FEAT_FLOAT
3052 if (tv2->v_type == VAR_FLOAT)
3053 {
3054 float_T f = n;
3055
3056 if (*op == '+')
3057 f += tv2->vval.v_float;
3058 else
3059 f -= tv2->vval.v_float;
3060 clear_tv(tv1);
3061 tv1->v_type = VAR_FLOAT;
3062 tv1->vval.v_float = f;
3063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#endif
3066 {
3067 if (*op == '+')
3068 n += get_tv_number(tv2);
3069 else
3070 n -= get_tv_number(tv2);
3071 clear_tv(tv1);
3072 tv1->v_type = VAR_NUMBER;
3073 tv1->vval.v_number = n;
3074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 }
3076 else
3077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078 if (tv2->v_type == VAR_FLOAT)
3079 break;
3080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 /* str .= str */
3082 s = get_tv_string(tv1);
3083 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3084 clear_tv(tv1);
3085 tv1->v_type = VAR_STRING;
3086 tv1->vval.v_string = s;
3087 }
3088 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089
3090#ifdef FEAT_FLOAT
3091 case VAR_FLOAT:
3092 {
3093 float_T f;
3094
3095 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3096 && tv2->v_type != VAR_NUMBER
3097 && tv2->v_type != VAR_STRING))
3098 break;
3099 if (tv2->v_type == VAR_FLOAT)
3100 f = tv2->vval.v_float;
3101 else
3102 f = get_tv_number(tv2);
3103 if (*op == '+')
3104 tv1->vval.v_float += f;
3105 else
3106 tv1->vval.v_float -= f;
3107 }
3108 return OK;
3109#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 }
3111 }
3112
3113 EMSG2(_(e_letwrong), op);
3114 return FAIL;
3115}
3116
3117/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 * Add a watcher to a list.
3119 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003120 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 list_T *l;
3123 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124{
3125 lw->lw_next = l->lv_watch;
3126 l->lv_watch = lw;
3127}
3128
3129/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003130 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 * No warning when it isn't found...
3132 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003133 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 list_T *l;
3136 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137{
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139
3140 lwp = &l->lv_watch;
3141 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3142 {
3143 if (lw == lwrem)
3144 {
3145 *lwp = lw->lw_next;
3146 break;
3147 }
3148 lwp = &lw->lw_next;
3149 }
3150}
3151
3152/*
3153 * Just before removing an item from a list: advance watchers to the next
3154 * item.
3155 */
3156 static void
3157list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 list_T *l;
3159 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160{
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162
3163 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3164 if (lw->lw_item == item)
3165 lw->lw_item = item->li_next;
3166}
3167
3168/*
3169 * Evaluate the expression used in a ":for var in expr" command.
3170 * "arg" points to "var".
3171 * Set "*errp" to TRUE for an error, FALSE otherwise;
3172 * Return a pointer that holds the info. Null when there is an error.
3173 */
3174 void *
3175eval_for_line(arg, errp, nextcmdp, skip)
3176 char_u *arg;
3177 int *errp;
3178 char_u **nextcmdp;
3179 int skip;
3180{
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 typval_T tv;
3184 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 *errp = TRUE; /* default: there is an error */
3187
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 if (fi == NULL)
3190 return NULL;
3191
3192 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3193 if (expr == NULL)
3194 return fi;
3195
3196 expr = skipwhite(expr);
3197 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3198 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003199 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 return fi;
3201 }
3202
3203 if (skip)
3204 ++emsg_skip;
3205 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3206 {
3207 *errp = FALSE;
3208 if (!skip)
3209 {
3210 l = tv.vval.v_list;
3211 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003212 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003214 clear_tv(&tv);
3215 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 else
3217 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003218 /* No need to increment the refcount, it's already set for the
3219 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 fi->fi_list = l;
3221 list_add_watch(l, &fi->fi_lw);
3222 fi->fi_lw.lw_item = l->lv_first;
3223 }
3224 }
3225 }
3226 if (skip)
3227 --emsg_skip;
3228
3229 return fi;
3230}
3231
3232/*
3233 * Use the first item in a ":for" list. Advance to the next.
3234 * Assign the values to the variable (list). "arg" points to the first one.
3235 * Return TRUE when a valid item was found, FALSE when at end of list or
3236 * something wrong.
3237 */
3238 int
3239next_for_item(fi_void, arg)
3240 void *fi_void;
3241 char_u *arg;
3242{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246
3247 item = fi->fi_lw.lw_item;
3248 if (item == NULL)
3249 result = FALSE;
3250 else
3251 {
3252 fi->fi_lw.lw_item = item->li_next;
3253 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3254 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3255 }
3256 return result;
3257}
3258
3259/*
3260 * Free the structure used to store info used by ":for".
3261 */
3262 void
3263free_for_info(fi_void)
3264 void *fi_void;
3265{
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003268 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 list_unref(fi->fi_list);
3272 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 vim_free(fi);
3274}
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3277
3278 void
3279set_context_for_expression(xp, arg, cmdidx)
3280 expand_T *xp;
3281 char_u *arg;
3282 cmdidx_T cmdidx;
3283{
3284 int got_eq = FALSE;
3285 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 if (cmdidx == CMD_let)
3289 {
3290 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003291 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 {
3293 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003294 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 {
3296 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 if (vim_iswhite(*p))
3299 break;
3300 }
3301 return;
3302 }
3303 }
3304 else
3305 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3306 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 while ((xp->xp_pattern = vim_strpbrk(arg,
3308 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3309 {
3310 c = *xp->xp_pattern;
3311 if (c == '&')
3312 {
3313 c = xp->xp_pattern[1];
3314 if (c == '&')
3315 {
3316 ++xp->xp_pattern;
3317 xp->xp_context = cmdidx != CMD_let || got_eq
3318 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3319 }
3320 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003323 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3324 xp->xp_pattern += 2;
3325
3326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 else if (c == '$')
3329 {
3330 /* environment variable */
3331 xp->xp_context = EXPAND_ENV_VARS;
3332 }
3333 else if (c == '=')
3334 {
3335 got_eq = TRUE;
3336 xp->xp_context = EXPAND_EXPRESSION;
3337 }
3338 else if (c == '<'
3339 && xp->xp_context == EXPAND_FUNCTIONS
3340 && vim_strchr(xp->xp_pattern, '(') == NULL)
3341 {
3342 /* Function name can start with "<SNR>" */
3343 break;
3344 }
3345 else if (cmdidx != CMD_let || got_eq)
3346 {
3347 if (c == '"') /* string */
3348 {
3349 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3350 if (c == '\\' && xp->xp_pattern[1] != NUL)
3351 ++xp->xp_pattern;
3352 xp->xp_context = EXPAND_NOTHING;
3353 }
3354 else if (c == '\'') /* literal string */
3355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003356 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3358 /* skip */ ;
3359 xp->xp_context = EXPAND_NOTHING;
3360 }
3361 else if (c == '|')
3362 {
3363 if (xp->xp_pattern[1] == '|')
3364 {
3365 ++xp->xp_pattern;
3366 xp->xp_context = EXPAND_EXPRESSION;
3367 }
3368 else
3369 xp->xp_context = EXPAND_COMMANDS;
3370 }
3371 else
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
3374 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375 /* Doesn't look like something valid, expand as an expression
3376 * anyway. */
3377 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 arg = xp->xp_pattern;
3379 if (*arg != NUL)
3380 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3381 /* skip */ ;
3382 }
3383 xp->xp_pattern = arg;
3384}
3385
3386#endif /* FEAT_CMDL_COMPL */
3387
3388/*
3389 * ":1,25call func(arg1, arg2)" function call.
3390 */
3391 void
3392ex_call(eap)
3393 exarg_T *eap;
3394{
3395 char_u *arg = eap->arg;
3396 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003400 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 linenr_T lnum;
3402 int doesrange;
3403 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003404 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003406 if (eap->skip)
3407 {
3408 /* trans_function_name() doesn't work well when skipping, use eval0()
3409 * instead to skip to any following command, e.g. for:
3410 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003411 ++emsg_skip;
3412 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3413 clear_tv(&rettv);
3414 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 return;
3416 }
3417
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003418 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003419 if (fudi.fd_newkey != NULL)
3420 {
3421 /* Still need to give an error message for missing key. */
3422 EMSG2(_(e_dictkey), fudi.fd_newkey);
3423 vim_free(fudi.fd_newkey);
3424 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 if (tofree == NULL)
3426 return;
3427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003428 /* Increase refcount on dictionary, it could get deleted when evaluating
3429 * the arguments. */
3430 if (fudi.fd_dict != NULL)
3431 ++fudi.fd_dict->dv_refcount;
3432
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003433 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003435 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar532c7802005-01-27 14:44:31 +00003437 /* Skip white space to allow ":call func ()". Not good, but required for
3438 * backward compatibility. */
3439 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003440 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 if (*startarg != '(')
3443 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003444 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 goto end;
3446 }
3447
3448 /*
3449 * When skipping, evaluate the function once, to find the end of the
3450 * arguments.
3451 * When the function takes a range, this is discovered after the first
3452 * call, and the loop is broken.
3453 */
3454 if (eap->skip)
3455 {
3456 ++emsg_skip;
3457 lnum = eap->line2; /* do it once, also with an invalid range */
3458 }
3459 else
3460 lnum = eap->line1;
3461 for ( ; lnum <= eap->line2; ++lnum)
3462 {
3463 if (!eap->skip && eap->addr_count > 0)
3464 {
3465 curwin->w_cursor.lnum = lnum;
3466 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003467#ifdef FEAT_VIRTUALEDIT
3468 curwin->w_cursor.coladd = 0;
3469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 eap->line1, eap->line2, &doesrange,
3474 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 failed = TRUE;
3477 break;
3478 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003479
3480 /* Handle a function returning a Funcref, Dictionary or List. */
3481 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3482 {
3483 failed = TRUE;
3484 break;
3485 }
3486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (doesrange || eap->skip)
3489 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003492 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003494 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (aborting())
3496 break;
3497 }
3498 if (eap->skip)
3499 --emsg_skip;
3500
3501 if (!failed)
3502 {
3503 /* Check for trailing illegal characters and a following command. */
3504 if (!ends_excmd(*arg))
3505 {
3506 emsg_severe = TRUE;
3507 EMSG(_(e_trailing));
3508 }
3509 else
3510 eap->nextcmd = check_nextcmd(arg);
3511 }
3512
3513end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003514 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003515 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517
3518/*
3519 * ":unlet[!] var1 ... " command.
3520 */
3521 void
3522ex_unlet(eap)
3523 exarg_T *eap;
3524{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003525 ex_unletlock(eap, eap->arg, 0);
3526}
3527
3528/*
3529 * ":lockvar" and ":unlockvar" commands
3530 */
3531 void
3532ex_lockvar(eap)
3533 exarg_T *eap;
3534{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003536 int deep = 2;
3537
3538 if (eap->forceit)
3539 deep = -1;
3540 else if (vim_isdigit(*arg))
3541 {
3542 deep = getdigits(&arg);
3543 arg = skipwhite(arg);
3544 }
3545
3546 ex_unletlock(eap, arg, deep);
3547}
3548
3549/*
3550 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3551 */
3552 static void
3553ex_unletlock(eap, argstart, deep)
3554 exarg_T *eap;
3555 char_u *argstart;
3556 int deep;
3557{
3558 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003561 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 do
3564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003566 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003567 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 if (lv.ll_name == NULL)
3569 error = TRUE; /* error but continue parsing */
3570 if (name_end == NULL || (!vim_iswhite(*name_end)
3571 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003573 if (name_end != NULL)
3574 {
3575 emsg_severe = TRUE;
3576 EMSG(_(e_trailing));
3577 }
3578 if (!(eap->skip || error))
3579 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 break;
3581 }
3582
3583 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 {
3585 if (eap->cmdidx == CMD_unlet)
3586 {
3587 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3588 error = TRUE;
3589 }
3590 else
3591 {
3592 if (do_lock_var(&lv, name_end, deep,
3593 eap->cmdidx == CMD_lockvar) == FAIL)
3594 error = TRUE;
3595 }
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 if (!eap->skip)
3599 clear_lval(&lv);
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 arg = skipwhite(name_end);
3602 } while (!ends_excmd(*arg));
3603
3604 eap->nextcmd = check_nextcmd(arg);
3605}
3606
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 int forceit;
3612{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 int ret = OK;
3614 int cc;
3615
3616 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 cc = *name_end;
3619 *name_end = NUL;
3620
3621 /* Normal name or expanded name. */
3622 if (check_changedtick(lp->ll_name))
3623 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3629 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 else if (lp->ll_range)
3631 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003632 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633
3634 /* Delete a range of List items. */
3635 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3636 {
3637 li = lp->ll_li->li_next;
3638 listitem_remove(lp->ll_list, lp->ll_li);
3639 lp->ll_li = li;
3640 ++lp->ll_n1;
3641 }
3642 }
3643 else
3644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 /* unlet a List item. */
3647 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003650 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 }
3652
3653 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654}
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
3657 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
3660 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 hashtab_T *ht;
3666 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003668 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 ht = find_var_ht(name, &varname);
3671 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 hi = hash_find(ht, varname);
3674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003676 di = HI2DI(hi);
3677 if (var_check_fixed(di->di_flags, name)
3678 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 return FAIL;
3680 delete_var(ht, hi);
3681 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 if (forceit)
3685 return OK;
3686 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return FAIL;
3688}
3689
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690/*
3691 * Lock or unlock variable indicated by "lp".
3692 * "deep" is the levels to go (-1 for unlimited);
3693 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3694 */
3695 static int
3696do_lock_var(lp, name_end, deep, lock)
3697 lval_T *lp;
3698 char_u *name_end;
3699 int deep;
3700 int lock;
3701{
3702 int ret = OK;
3703 int cc;
3704 dictitem_T *di;
3705
3706 if (deep == 0) /* nothing to do */
3707 return OK;
3708
3709 if (lp->ll_tv == NULL)
3710 {
3711 cc = *name_end;
3712 *name_end = NUL;
3713
3714 /* Normal name or expanded name. */
3715 if (check_changedtick(lp->ll_name))
3716 ret = FAIL;
3717 else
3718 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003719 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 if (di == NULL)
3721 ret = FAIL;
3722 else
3723 {
3724 if (lock)
3725 di->di_flags |= DI_FLAGS_LOCK;
3726 else
3727 di->di_flags &= ~DI_FLAGS_LOCK;
3728 item_lock(&di->di_tv, deep, lock);
3729 }
3730 }
3731 *name_end = cc;
3732 }
3733 else if (lp->ll_range)
3734 {
3735 listitem_T *li = lp->ll_li;
3736
3737 /* (un)lock a range of List items. */
3738 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3739 {
3740 item_lock(&li->li_tv, deep, lock);
3741 li = li->li_next;
3742 ++lp->ll_n1;
3743 }
3744 }
3745 else if (lp->ll_list != NULL)
3746 /* (un)lock a List item. */
3747 item_lock(&lp->ll_li->li_tv, deep, lock);
3748 else
3749 /* un(lock) a Dictionary item. */
3750 item_lock(&lp->ll_di->di_tv, deep, lock);
3751
3752 return ret;
3753}
3754
3755/*
3756 * Lock or unlock an item. "deep" is nr of levels to go.
3757 */
3758 static void
3759item_lock(tv, deep, lock)
3760 typval_T *tv;
3761 int deep;
3762 int lock;
3763{
3764 static int recurse = 0;
3765 list_T *l;
3766 listitem_T *li;
3767 dict_T *d;
3768 hashitem_T *hi;
3769 int todo;
3770
3771 if (recurse >= DICT_MAXNEST)
3772 {
3773 EMSG(_("E743: variable nested too deep for (un)lock"));
3774 return;
3775 }
3776 if (deep == 0)
3777 return;
3778 ++recurse;
3779
3780 /* lock/unlock the item itself */
3781 if (lock)
3782 tv->v_lock |= VAR_LOCKED;
3783 else
3784 tv->v_lock &= ~VAR_LOCKED;
3785
3786 switch (tv->v_type)
3787 {
3788 case VAR_LIST:
3789 if ((l = tv->vval.v_list) != NULL)
3790 {
3791 if (lock)
3792 l->lv_lock |= VAR_LOCKED;
3793 else
3794 l->lv_lock &= ~VAR_LOCKED;
3795 if (deep < 0 || deep > 1)
3796 /* recursive: lock/unlock the items the List contains */
3797 for (li = l->lv_first; li != NULL; li = li->li_next)
3798 item_lock(&li->li_tv, deep - 1, lock);
3799 }
3800 break;
3801 case VAR_DICT:
3802 if ((d = tv->vval.v_dict) != NULL)
3803 {
3804 if (lock)
3805 d->dv_lock |= VAR_LOCKED;
3806 else
3807 d->dv_lock &= ~VAR_LOCKED;
3808 if (deep < 0 || deep > 1)
3809 {
3810 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003811 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003812 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3813 {
3814 if (!HASHITEM_EMPTY(hi))
3815 {
3816 --todo;
3817 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3818 }
3819 }
3820 }
3821 }
3822 }
3823 --recurse;
3824}
3825
Bram Moolenaara40058a2005-07-11 22:42:07 +00003826/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003827 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3828 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003829 */
3830 static int
3831tv_islocked(tv)
3832 typval_T *tv;
3833{
3834 return (tv->v_lock & VAR_LOCKED)
3835 || (tv->v_type == VAR_LIST
3836 && tv->vval.v_list != NULL
3837 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3838 || (tv->v_type == VAR_DICT
3839 && tv->vval.v_dict != NULL
3840 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3841}
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3844/*
3845 * Delete all "menutrans_" variables.
3846 */
3847 void
3848del_menutrans_vars()
3849{
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003854 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003856 {
3857 if (!HASHITEM_EMPTY(hi))
3858 {
3859 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3861 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 }
3863 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865}
3866#endif
3867
3868#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3869
3870/*
3871 * Local string buffer for the next two functions to store a variable name
3872 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3873 * get_user_var_name().
3874 */
3875
3876static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3877
3878static char_u *varnamebuf = NULL;
3879static int varnamebuflen = 0;
3880
3881/*
3882 * Function to concatenate a prefix and a variable name.
3883 */
3884 static char_u *
3885cat_prefix_varname(prefix, name)
3886 int prefix;
3887 char_u *name;
3888{
3889 int len;
3890
3891 len = (int)STRLEN(name) + 3;
3892 if (len > varnamebuflen)
3893 {
3894 vim_free(varnamebuf);
3895 len += 10; /* some additional space */
3896 varnamebuf = alloc(len);
3897 if (varnamebuf == NULL)
3898 {
3899 varnamebuflen = 0;
3900 return NULL;
3901 }
3902 varnamebuflen = len;
3903 }
3904 *varnamebuf = prefix;
3905 varnamebuf[1] = ':';
3906 STRCPY(varnamebuf + 2, name);
3907 return varnamebuf;
3908}
3909
3910/*
3911 * Function given to ExpandGeneric() to obtain the list of user defined
3912 * (global/buffer/window/built-in) variable names.
3913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 char_u *
3915get_user_var_name(xp, idx)
3916 expand_T *xp;
3917 int idx;
3918{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003919 static long_u gdone;
3920 static long_u bdone;
3921 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003922#ifdef FEAT_WINDOWS
3923 static long_u tdone;
3924#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 static int vidx;
3926 static hashitem_T *hi;
3927 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003930 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003932#ifdef FEAT_WINDOWS
3933 tdone = 0;
3934#endif
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936
3937 /* Global variables */
3938 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003942 else
3943 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 while (HASHITEM_EMPTY(hi))
3945 ++hi;
3946 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3947 return cat_prefix_varname('g', hi->hi_key);
3948 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950
3951 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003952 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003957 else
3958 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 while (HASHITEM_EMPTY(hi))
3960 ++hi;
3961 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return (char_u *)"b:changedtick";
3967 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003968
3969 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003970 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003973 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003975 else
3976 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003977 while (HASHITEM_EMPTY(hi))
3978 ++hi;
3979 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003981
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982#ifdef FEAT_WINDOWS
3983 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003984 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003985 if (tdone < ht->ht_used)
3986 {
3987 if (tdone++ == 0)
3988 hi = ht->ht_array;
3989 else
3990 ++hi;
3991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 return cat_prefix_varname('t', hi->hi_key);
3994 }
3995#endif
3996
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 /* v: variables */
3998 if (vidx < VV_LEN)
3999 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 vim_free(varnamebuf);
4002 varnamebuf = NULL;
4003 varnamebuflen = 0;
4004 return NULL;
4005}
4006
4007#endif /* FEAT_CMDL_COMPL */
4008
4009/*
4010 * types for expressions.
4011 */
4012typedef enum
4013{
4014 TYPE_UNKNOWN = 0
4015 , TYPE_EQUAL /* == */
4016 , TYPE_NEQUAL /* != */
4017 , TYPE_GREATER /* > */
4018 , TYPE_GEQUAL /* >= */
4019 , TYPE_SMALLER /* < */
4020 , TYPE_SEQUAL /* <= */
4021 , TYPE_MATCH /* =~ */
4022 , TYPE_NOMATCH /* !~ */
4023} exptype_T;
4024
4025/*
4026 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4029 */
4030
4031/*
4032 * Handle zero level expression.
4033 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004035 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 * Return OK or FAIL.
4037 */
4038 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 char_u **nextcmd;
4043 int evaluate;
4044{
4045 int ret;
4046 char_u *p;
4047
4048 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (ret == FAIL || !ends_excmd(*p))
4051 {
4052 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /*
4055 * Report the invalid expression unless the expression evaluation has
4056 * been cancelled due to an aborting error, an interrupt, or an
4057 * exception.
4058 */
4059 if (!aborting())
4060 EMSG2(_(e_invexpr2), arg);
4061 ret = FAIL;
4062 }
4063 if (nextcmd != NULL)
4064 *nextcmd = check_nextcmd(p);
4065
4066 return ret;
4067}
4068
4069/*
4070 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004071 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 *
4073 * "arg" must point to the first non-white of the expression.
4074 * "arg" is advanced to the next non-white after the recognized expression.
4075 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004076 * Note: "rettv.v_lock" is not set.
4077 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * Return OK or FAIL.
4079 */
4080 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 int evaluate;
4085{
4086 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004087 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 /*
4090 * Get the first variable.
4091 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return FAIL;
4094
4095 if ((*arg)[0] == '?')
4096 {
4097 result = FALSE;
4098 if (evaluate)
4099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 int error = FALSE;
4101
4102 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (error)
4106 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108
4109 /*
4110 * Get the second variable.
4111 */
4112 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 return FAIL;
4115
4116 /*
4117 * Check for the ":".
4118 */
4119 if ((*arg)[0] != ':')
4120 {
4121 EMSG(_("E109: Missing ':' after '?'"));
4122 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125 }
4126
4127 /*
4128 * Get the third variable.
4129 */
4130 *arg = skipwhite(*arg + 1);
4131 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4132 {
4133 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 return FAIL;
4136 }
4137 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140
4141 return OK;
4142}
4143
4144/*
4145 * Handle first level expression:
4146 * expr2 || expr2 || expr2 logical OR
4147 *
4148 * "arg" must point to the first non-white of the expression.
4149 * "arg" is advanced to the next non-white after the recognized expression.
4150 *
4151 * Return OK or FAIL.
4152 */
4153 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int evaluate;
4158{
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 long result;
4161 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 /*
4165 * Get the first variable.
4166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169
4170 /*
4171 * Repeat until there is no following "||".
4172 */
4173 first = TRUE;
4174 result = FALSE;
4175 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4176 {
4177 if (evaluate && first)
4178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 first = FALSE;
4185 }
4186
4187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 2);
4191 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4192 return FAIL;
4193
4194 /*
4195 * Compute the result.
4196 */
4197 if (evaluate && !result)
4198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 if (error)
4203 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 if (evaluate)
4206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 rettv->v_type = VAR_NUMBER;
4208 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 }
4211
4212 return OK;
4213}
4214
4215/*
4216 * Handle second level expression:
4217 * expr3 && expr3 && expr3 logical AND
4218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
4222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int evaluate;
4229{
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 long result;
4232 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 /*
4236 * Get the first variable.
4237 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 return FAIL;
4240
4241 /*
4242 * Repeat until there is no following "&&".
4243 */
4244 first = TRUE;
4245 result = TRUE;
4246 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4247 {
4248 if (evaluate && first)
4249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (error)
4254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 first = FALSE;
4256 }
4257
4258 /*
4259 * Get the second variable.
4260 */
4261 *arg = skipwhite(*arg + 2);
4262 if (eval4(arg, &var2, evaluate && result) == FAIL)
4263 return FAIL;
4264
4265 /*
4266 * Compute the result.
4267 */
4268 if (evaluate && result)
4269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (error)
4274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 if (evaluate)
4277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 rettv->v_type = VAR_NUMBER;
4279 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 }
4282
4283 return OK;
4284}
4285
4286/*
4287 * Handle third level expression:
4288 * var1 == var2
4289 * var1 =~ var2
4290 * var1 != var2
4291 * var1 !~ var2
4292 * var1 > var2
4293 * var1 >= var2
4294 * var1 < var2
4295 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004296 * var1 is var2
4297 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 *
4299 * "arg" must point to the first non-white of the expression.
4300 * "arg" is advanced to the next non-white after the recognized expression.
4301 *
4302 * Return OK or FAIL.
4303 */
4304 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 int evaluate;
4309{
Bram Moolenaar33570922005-01-25 22:26:29 +00004310 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 char_u *p;
4312 int i;
4313 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004314 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int len = 2;
4316 long n1, n2;
4317 char_u *s1, *s2;
4318 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4319 regmatch_T regmatch;
4320 int ic;
4321 char_u *save_cpo;
4322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 p = *arg;
4330 switch (p[0])
4331 {
4332 case '=': if (p[1] == '=')
4333 type = TYPE_EQUAL;
4334 else if (p[1] == '~')
4335 type = TYPE_MATCH;
4336 break;
4337 case '!': if (p[1] == '=')
4338 type = TYPE_NEQUAL;
4339 else if (p[1] == '~')
4340 type = TYPE_NOMATCH;
4341 break;
4342 case '>': if (p[1] != '=')
4343 {
4344 type = TYPE_GREATER;
4345 len = 1;
4346 }
4347 else
4348 type = TYPE_GEQUAL;
4349 break;
4350 case '<': if (p[1] != '=')
4351 {
4352 type = TYPE_SMALLER;
4353 len = 1;
4354 }
4355 else
4356 type = TYPE_SEQUAL;
4357 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 case 'i': if (p[1] == 's')
4359 {
4360 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4361 len = 5;
4362 if (!vim_isIDc(p[len]))
4363 {
4364 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4365 type_is = TRUE;
4366 }
4367 }
4368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 */
4374 if (type != TYPE_UNKNOWN)
4375 {
4376 /* extra question mark appended: ignore case */
4377 if (p[len] == '?')
4378 {
4379 ic = TRUE;
4380 ++len;
4381 }
4382 /* extra '#' appended: match case */
4383 else if (p[len] == '#')
4384 {
4385 ic = FALSE;
4386 ++len;
4387 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 else
4390 ic = p_ic;
4391
4392 /*
4393 * Get the second variable.
4394 */
4395 *arg = skipwhite(p + len);
4396 if (eval5(arg, &var2, evaluate) == FAIL)
4397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400 }
4401
4402 if (evaluate)
4403 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 if (type_is && rettv->v_type != var2.v_type)
4405 {
4406 /* For "is" a different type always means FALSE, for "notis"
4407 * it means TRUE. */
4408 n1 = (type == TYPE_NEQUAL);
4409 }
4410 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4411 {
4412 if (type_is)
4413 {
4414 n1 = (rettv->v_type == var2.v_type
4415 && rettv->vval.v_list == var2.vval.v_list);
4416 if (type == TYPE_NEQUAL)
4417 n1 = !n1;
4418 }
4419 else if (rettv->v_type != var2.v_type
4420 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4421 {
4422 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004423 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004425 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 clear_tv(rettv);
4427 clear_tv(&var2);
4428 return FAIL;
4429 }
4430 else
4431 {
4432 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004433 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4434 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 }
4439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004440 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4441 {
4442 if (type_is)
4443 {
4444 n1 = (rettv->v_type == var2.v_type
4445 && rettv->vval.v_dict == var2.vval.v_dict);
4446 if (type == TYPE_NEQUAL)
4447 n1 = !n1;
4448 }
4449 else if (rettv->v_type != var2.v_type
4450 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4451 {
4452 if (rettv->v_type != var2.v_type)
4453 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4454 else
4455 EMSG(_("E736: Invalid operation for Dictionary"));
4456 clear_tv(rettv);
4457 clear_tv(&var2);
4458 return FAIL;
4459 }
4460 else
4461 {
4462 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004463 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4464 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004465 if (type == TYPE_NEQUAL)
4466 n1 = !n1;
4467 }
4468 }
4469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4471 {
4472 if (rettv->v_type != var2.v_type
4473 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4474 {
4475 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004476 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004478 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 clear_tv(rettv);
4480 clear_tv(&var2);
4481 return FAIL;
4482 }
4483 else
4484 {
4485 /* Compare two Funcrefs for being equal or unequal. */
4486 if (rettv->vval.v_string == NULL
4487 || var2.vval.v_string == NULL)
4488 n1 = FALSE;
4489 else
4490 n1 = STRCMP(rettv->vval.v_string,
4491 var2.vval.v_string) == 0;
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 }
4496
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004497#ifdef FEAT_FLOAT
4498 /*
4499 * If one of the two variables is a float, compare as a float.
4500 * When using "=~" or "!~", always compare as string.
4501 */
4502 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4503 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4504 {
4505 float_T f1, f2;
4506
4507 if (rettv->v_type == VAR_FLOAT)
4508 f1 = rettv->vval.v_float;
4509 else
4510 f1 = get_tv_number(rettv);
4511 if (var2.v_type == VAR_FLOAT)
4512 f2 = var2.vval.v_float;
4513 else
4514 f2 = get_tv_number(&var2);
4515 n1 = FALSE;
4516 switch (type)
4517 {
4518 case TYPE_EQUAL: n1 = (f1 == f2); break;
4519 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4520 case TYPE_GREATER: n1 = (f1 > f2); break;
4521 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4522 case TYPE_SMALLER: n1 = (f1 < f2); break;
4523 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4524 case TYPE_UNKNOWN:
4525 case TYPE_MATCH:
4526 case TYPE_NOMATCH: break; /* avoid gcc warning */
4527 }
4528 }
4529#endif
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /*
4532 * If one of the two variables is a number, compare as a number.
4533 * When using "=~" or "!~", always compare as string.
4534 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004535 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 n1 = get_tv_number(rettv);
4539 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 switch (type)
4541 {
4542 case TYPE_EQUAL: n1 = (n1 == n2); break;
4543 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4544 case TYPE_GREATER: n1 = (n1 > n2); break;
4545 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4546 case TYPE_SMALLER: n1 = (n1 < n2); break;
4547 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4548 case TYPE_UNKNOWN:
4549 case TYPE_MATCH:
4550 case TYPE_NOMATCH: break; /* avoid gcc warning */
4551 }
4552 }
4553 else
4554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 s1 = get_tv_string_buf(rettv, buf1);
4556 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4558 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4559 else
4560 i = 0;
4561 n1 = FALSE;
4562 switch (type)
4563 {
4564 case TYPE_EQUAL: n1 = (i == 0); break;
4565 case TYPE_NEQUAL: n1 = (i != 0); break;
4566 case TYPE_GREATER: n1 = (i > 0); break;
4567 case TYPE_GEQUAL: n1 = (i >= 0); break;
4568 case TYPE_SMALLER: n1 = (i < 0); break;
4569 case TYPE_SEQUAL: n1 = (i <= 0); break;
4570
4571 case TYPE_MATCH:
4572 case TYPE_NOMATCH:
4573 /* avoid 'l' flag in 'cpoptions' */
4574 save_cpo = p_cpo;
4575 p_cpo = (char_u *)"";
4576 regmatch.regprog = vim_regcomp(s2,
4577 RE_MAGIC + RE_STRING);
4578 regmatch.rm_ic = ic;
4579 if (regmatch.regprog != NULL)
4580 {
4581 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004582 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (type == TYPE_NOMATCH)
4584 n1 = !n1;
4585 }
4586 p_cpo = save_cpo;
4587 break;
4588
4589 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4590 }
4591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 clear_tv(rettv);
4593 clear_tv(&var2);
4594 rettv->v_type = VAR_NUMBER;
4595 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 }
4598
4599 return OK;
4600}
4601
4602/*
4603 * Handle fourth level expression:
4604 * + number addition
4605 * - number subtraction
4606 * . string concatenation
4607 *
4608 * "arg" must point to the first non-white of the expression.
4609 * "arg" is advanced to the next non-white after the recognized expression.
4610 *
4611 * Return OK or FAIL.
4612 */
4613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 int evaluate;
4618{
Bram Moolenaar33570922005-01-25 22:26:29 +00004619 typval_T var2;
4620 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 int op;
4622 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623#ifdef FEAT_FLOAT
4624 float_T f1 = 0, f2 = 0;
4625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 char_u *s1, *s2;
4627 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4628 char_u *p;
4629
4630 /*
4631 * Get the first variable.
4632 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004633 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return FAIL;
4635
4636 /*
4637 * Repeat computing, until no '+', '-' or '.' is following.
4638 */
4639 for (;;)
4640 {
4641 op = **arg;
4642 if (op != '+' && op != '-' && op != '.')
4643 break;
4644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645 if ((op != '+' || rettv->v_type != VAR_LIST)
4646#ifdef FEAT_FLOAT
4647 && (op == '.' || rettv->v_type != VAR_FLOAT)
4648#endif
4649 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 {
4651 /* For "list + ...", an illegal use of the first operand as
4652 * a number cannot be determined before evaluating the 2nd
4653 * operand: if this is also a list, all is ok.
4654 * For "something . ...", "something - ..." or "non-list + ...",
4655 * we know that the first operand needs to be a string or number
4656 * without evaluating the 2nd operand. So check before to avoid
4657 * side effects after an error. */
4658 if (evaluate && get_tv_string_chk(rettv) == NULL)
4659 {
4660 clear_tv(rettv);
4661 return FAIL;
4662 }
4663 }
4664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /*
4666 * Get the second variable.
4667 */
4668 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004669 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 return FAIL;
4673 }
4674
4675 if (evaluate)
4676 {
4677 /*
4678 * Compute the result.
4679 */
4680 if (op == '.')
4681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4683 s2 = get_tv_string_buf_chk(&var2, buf2);
4684 if (s2 == NULL) /* type error ? */
4685 {
4686 clear_tv(rettv);
4687 clear_tv(&var2);
4688 return FAIL;
4689 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004690 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 clear_tv(rettv);
4692 rettv->v_type = VAR_STRING;
4693 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004695 else if (op == '+' && rettv->v_type == VAR_LIST
4696 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004697 {
4698 /* concatenate Lists */
4699 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4700 &var3) == FAIL)
4701 {
4702 clear_tv(rettv);
4703 clear_tv(&var2);
4704 return FAIL;
4705 }
4706 clear_tv(rettv);
4707 *rettv = var3;
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 else
4710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 int error = FALSE;
4712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713#ifdef FEAT_FLOAT
4714 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 f1 = rettv->vval.v_float;
4717 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 else
4720#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 n1 = get_tv_number_chk(rettv, &error);
4723 if (error)
4724 {
4725 /* This can only happen for "list + non-list". For
4726 * "non-list + ..." or "something - ...", we returned
4727 * before evaluating the 2nd operand. */
4728 clear_tv(rettv);
4729 return FAIL;
4730 }
4731#ifdef FEAT_FLOAT
4732 if (var2.v_type == VAR_FLOAT)
4733 f1 = n1;
4734#endif
4735 }
4736#ifdef FEAT_FLOAT
4737 if (var2.v_type == VAR_FLOAT)
4738 {
4739 f2 = var2.vval.v_float;
4740 n2 = 0;
4741 }
4742 else
4743#endif
4744 {
4745 n2 = get_tv_number_chk(&var2, &error);
4746 if (error)
4747 {
4748 clear_tv(rettv);
4749 clear_tv(&var2);
4750 return FAIL;
4751 }
4752#ifdef FEAT_FLOAT
4753 if (rettv->v_type == VAR_FLOAT)
4754 f2 = n2;
4755#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758
4759#ifdef FEAT_FLOAT
4760 /* If there is a float on either side the result is a float. */
4761 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4762 {
4763 if (op == '+')
4764 f1 = f1 + f2;
4765 else
4766 f1 = f1 - f2;
4767 rettv->v_type = VAR_FLOAT;
4768 rettv->vval.v_float = f1;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771#endif
4772 {
4773 if (op == '+')
4774 n1 = n1 + n2;
4775 else
4776 n1 = n1 - n2;
4777 rettv->v_type = VAR_NUMBER;
4778 rettv->vval.v_number = n1;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 }
4784 return OK;
4785}
4786
4787/*
4788 * Handle fifth level expression:
4789 * * number multiplication
4790 * / number division
4791 * % number modulo
4792 *
4793 * "arg" must point to the first non-white of the expression.
4794 * "arg" is advanced to the next non-white after the recognized expression.
4795 *
4796 * Return OK or FAIL.
4797 */
4798 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004799eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004803 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
Bram Moolenaar33570922005-01-25 22:26:29 +00004805 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 int op;
4807 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 int use_float = FALSE;
4810 float_T f1 = 0, f2;
4811#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 /*
4815 * Get the first variable.
4816 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004817 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
4819
4820 /*
4821 * Repeat computing, until no '*', '/' or '%' is following.
4822 */
4823 for (;;)
4824 {
4825 op = **arg;
4826 if (op != '*' && op != '/' && op != '%')
4827 break;
4828
4829 if (evaluate)
4830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831#ifdef FEAT_FLOAT
4832 if (rettv->v_type == VAR_FLOAT)
4833 {
4834 f1 = rettv->vval.v_float;
4835 use_float = TRUE;
4836 n1 = 0;
4837 }
4838 else
4839#endif
4840 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 if (error)
4843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 else
4846 n1 = 0;
4847
4848 /*
4849 * Get the second variable.
4850 */
4851 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004852 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 return FAIL;
4854
4855 if (evaluate)
4856 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857#ifdef FEAT_FLOAT
4858 if (var2.v_type == VAR_FLOAT)
4859 {
4860 if (!use_float)
4861 {
4862 f1 = n1;
4863 use_float = TRUE;
4864 }
4865 f2 = var2.vval.v_float;
4866 n2 = 0;
4867 }
4868 else
4869#endif
4870 {
4871 n2 = get_tv_number_chk(&var2, &error);
4872 clear_tv(&var2);
4873 if (error)
4874 return FAIL;
4875#ifdef FEAT_FLOAT
4876 if (use_float)
4877 f2 = n2;
4878#endif
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 /*
4882 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#ifdef FEAT_FLOAT
4886 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 if (op == '*')
4889 f1 = f1 * f2;
4890 else if (op == '/')
4891 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004892# ifdef VMS
4893 /* VMS crashes on divide by zero, work around it */
4894 if (f2 == 0.0)
4895 {
4896 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004897 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004899 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004900 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004901 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004902 }
4903 else
4904 f1 = f1 / f2;
4905# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906 /* We rely on the floating point library to handle divide
4907 * by zero to result in "inf" and not a crash. */
4908 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004913 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914 return FAIL;
4915 }
4916 rettv->v_type = VAR_FLOAT;
4917 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 if (op == '*')
4923 n1 = n1 * n2;
4924 else if (op == '/')
4925 {
4926 if (n2 == 0) /* give an error message? */
4927 {
4928 if (n1 == 0)
4929 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4930 else if (n1 < 0)
4931 n1 = -0x7fffffffL;
4932 else
4933 n1 = 0x7fffffffL;
4934 }
4935 else
4936 n1 = n1 / n2;
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 {
4940 if (n2 == 0) /* give an error message? */
4941 n1 = 0;
4942 else
4943 n1 = n1 % n2;
4944 }
4945 rettv->v_type = VAR_NUMBER;
4946 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 }
4950
4951 return OK;
4952}
4953
4954/*
4955 * Handle sixth level expression:
4956 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004957 * "string" string constant
4958 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * &option-name option value
4960 * @r register contents
4961 * identifier variable value
4962 * function() function call
4963 * $VAR environment variable
4964 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004965 * [expr, expr] List
4966 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 *
4968 * Also handle:
4969 * ! in front logical NOT
4970 * - in front unary minus
4971 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 * trailing [] subscript in String or List
4973 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 *
4975 * "arg" must point to the first non-white of the expression.
4976 * "arg" is advanced to the next non-white after the recognized expression.
4977 *
4978 * Return OK or FAIL.
4979 */
4980 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004981eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004985 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 long n;
4988 int len;
4989 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 char_u *start_leader, *end_leader;
4991 int ret = OK;
4992 char_u *alias;
4993
4994 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 /*
5001 * Skip '!' and '-' characters. They are handled later.
5002 */
5003 start_leader = *arg;
5004 while (**arg == '!' || **arg == '-' || **arg == '+')
5005 *arg = skipwhite(*arg + 1);
5006 end_leader = *arg;
5007
5008 switch (**arg)
5009 {
5010 /*
5011 * Number constant.
5012 */
5013 case '0':
5014 case '1':
5015 case '2':
5016 case '3':
5017 case '4':
5018 case '5':
5019 case '6':
5020 case '7':
5021 case '8':
5022 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 {
5024#ifdef FEAT_FLOAT
5025 char_u *p = skipdigits(*arg + 1);
5026 int get_float = FALSE;
5027
5028 /* We accept a float when the format matches
5029 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005030 * strict to avoid backwards compatibility problems.
5031 * Don't look for a float after the "." operator, so that
5032 * ":let vers = 1.2.3" doesn't fail. */
5033 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 get_float = TRUE;
5036 p = skipdigits(p + 2);
5037 if (*p == 'e' || *p == 'E')
5038 {
5039 ++p;
5040 if (*p == '-' || *p == '+')
5041 ++p;
5042 if (!vim_isdigit(*p))
5043 get_float = FALSE;
5044 else
5045 p = skipdigits(p + 1);
5046 }
5047 if (ASCII_ISALPHA(*p) || *p == '.')
5048 get_float = FALSE;
5049 }
5050 if (get_float)
5051 {
5052 float_T f;
5053
5054 *arg += string2float(*arg, &f);
5055 if (evaluate)
5056 {
5057 rettv->v_type = VAR_FLOAT;
5058 rettv->vval.v_float = f;
5059 }
5060 }
5061 else
5062#endif
5063 {
5064 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5065 *arg += len;
5066 if (evaluate)
5067 {
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 /*
5076 * String constant: "string".
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 break;
5086
5087 /*
5088 * List: [expr, expr]
5089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 * Dictionary: {key: val, key: val}
5095 */
5096 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5097 break;
5098
5099 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005100 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005102 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 break;
5104
5105 /*
5106 * Environment variable: $VAR.
5107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /*
5112 * Register contents: @r.
5113 */
5114 case '@': ++*arg;
5115 if (evaluate)
5116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005118 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 if (**arg != NUL)
5121 ++*arg;
5122 break;
5123
5124 /*
5125 * nested expression: (expression).
5126 */
5127 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (**arg == ')')
5130 ++*arg;
5131 else if (ret == OK)
5132 {
5133 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 ret = FAIL;
5136 }
5137 break;
5138
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 break;
5141 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142
5143 if (ret == NOTDONE)
5144 {
5145 /*
5146 * Must be a variable or function name.
5147 * Can also be a curly-braces kind of name: {expr}.
5148 */
5149 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005150 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 if (alias != NULL)
5152 s = alias;
5153
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005154 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 ret = FAIL;
5156 else
5157 {
5158 if (**arg == '(') /* recursive! */
5159 {
5160 /* If "s" is the name of a variable of type VAR_FUNC
5161 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005162 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163
5164 /* Invoke the function. */
5165 ret = get_func_tv(s, len, rettv, arg,
5166 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005168
5169 /* If evaluate is FALSE rettv->v_type was not set in
5170 * get_func_tv, but it's needed in handle_subscript() to parse
5171 * what follows. So set it here. */
5172 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5173 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005174 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005175 rettv->v_type = VAR_FUNC;
5176 }
5177
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 /* Stop the expression evaluation when immediately
5179 * aborting on error, or when an interrupt occurred or
5180 * an exception was thrown but not caught. */
5181 if (aborting())
5182 {
5183 if (ret == OK)
5184 clear_tv(rettv);
5185 ret = FAIL;
5186 }
5187 }
5188 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005189 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005190 else
5191 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005193 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 }
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 *arg = skipwhite(*arg);
5197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005198 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5199 * expr(expr). */
5200 if (ret == OK)
5201 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 /*
5204 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5205 */
5206 if (ret == OK && evaluate && end_leader > start_leader)
5207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005209 int val = 0;
5210#ifdef FEAT_FLOAT
5211 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 if (rettv->v_type == VAR_FLOAT)
5214 f = rettv->vval.v_float;
5215 else
5216#endif
5217 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 clear_tv(rettv);
5221 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 else
5224 {
5225 while (end_leader > start_leader)
5226 {
5227 --end_leader;
5228 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005229 {
5230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 f = !f;
5233 else
5234#endif
5235 val = !val;
5236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005238 {
5239#ifdef FEAT_FLOAT
5240 if (rettv->v_type == VAR_FLOAT)
5241 f = -f;
5242 else
5243#endif
5244 val = -val;
5245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247#ifdef FEAT_FLOAT
5248 if (rettv->v_type == VAR_FLOAT)
5249 {
5250 clear_tv(rettv);
5251 rettv->vval.v_float = f;
5252 }
5253 else
5254#endif
5255 {
5256 clear_tv(rettv);
5257 rettv->v_type = VAR_NUMBER;
5258 rettv->vval.v_number = val;
5259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262
5263 return ret;
5264}
5265
5266/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005267 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5268 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5270 */
5271 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277{
5278 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 long len = -1;
5282 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005286 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 if (verbose)
5289 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 return FAIL;
5291 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005292#ifdef FEAT_FLOAT
5293 else if (rettv->v_type == VAR_FLOAT)
5294 {
5295 if (verbose)
5296 EMSG(_(e_float_as_string));
5297 return FAIL;
5298 }
5299#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * dict.name
5305 */
5306 key = *arg + 1;
5307 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5308 ;
5309 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 }
5313 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 /*
5316 * something[idx]
5317 *
5318 * Get the (first) variable from inside the [].
5319 */
5320 *arg = skipwhite(*arg + 1);
5321 if (**arg == ':')
5322 empty1 = TRUE;
5323 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5324 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5326 {
5327 /* not a number or string */
5328 clear_tv(&var1);
5329 return FAIL;
5330 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331
5332 /*
5333 * Get the second variable from inside the [:].
5334 */
5335 if (**arg == ':')
5336 {
5337 range = TRUE;
5338 *arg = skipwhite(*arg + 1);
5339 if (**arg == ']')
5340 empty2 = TRUE;
5341 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343 if (!empty1)
5344 clear_tv(&var1);
5345 return FAIL;
5346 }
5347 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5348 {
5349 /* not a number or string */
5350 if (!empty1)
5351 clear_tv(&var1);
5352 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 return FAIL;
5354 }
5355 }
5356
5357 /* Check for the ']'. */
5358 if (**arg != ']')
5359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 if (verbose)
5361 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362 clear_tv(&var1);
5363 if (range)
5364 clear_tv(&var2);
5365 return FAIL;
5366 }
5367 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 }
5369
5370 if (evaluate)
5371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 n1 = 0;
5373 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 n1 = get_tv_number(&var1);
5376 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 }
5378 if (range)
5379 {
5380 if (empty2)
5381 n2 = -1;
5382 else
5383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 n2 = get_tv_number(&var2);
5385 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 }
5387 }
5388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
5391 case VAR_NUMBER:
5392 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 len = (long)STRLEN(s);
5395 if (range)
5396 {
5397 /* The resulting variable is a substring. If the indexes
5398 * are out of range the result is empty. */
5399 if (n1 < 0)
5400 {
5401 n1 = len + n1;
5402 if (n1 < 0)
5403 n1 = 0;
5404 }
5405 if (n2 < 0)
5406 n2 = len + n2;
5407 else if (n2 >= len)
5408 n2 = len;
5409 if (n1 >= len || n2 < 0 || n1 > n2)
5410 s = NULL;
5411 else
5412 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5413 }
5414 else
5415 {
5416 /* The resulting variable is a string of a single
5417 * character. If the index is too big or negative the
5418 * result is empty. */
5419 if (n1 >= len || n1 < 0)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, 1);
5423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 clear_tv(rettv);
5425 rettv->v_type = VAR_STRING;
5426 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 break;
5428
5429 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 if (n1 < 0)
5432 n1 = len + n1;
5433 if (!empty1 && (n1 < 0 || n1 >= len))
5434 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005435 /* For a range we allow invalid values and return an empty
5436 * list. A list index out of range is an error. */
5437 if (!range)
5438 {
5439 if (verbose)
5440 EMSGN(_(e_listidx), n1);
5441 return FAIL;
5442 }
5443 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 if (range)
5446 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 list_T *l;
5448 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449
5450 if (n2 < 0)
5451 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005452 else if (n2 >= len)
5453 n2 = len - 1;
5454 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005455 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 l = list_alloc();
5457 if (l == NULL)
5458 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 n1 <= n2; ++n1)
5461 {
5462 if (list_append_tv(l, &item->li_tv) == FAIL)
5463 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005464 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 return FAIL;
5466 }
5467 item = item->li_next;
5468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(rettv);
5470 rettv->v_type = VAR_LIST;
5471 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005472 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 }
5474 else
5475 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005476 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 clear_tv(rettv);
5478 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 case VAR_DICT:
5483 if (range)
5484 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 if (verbose)
5486 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 if (len == -1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493
5494 if (len == -1)
5495 {
5496 key = get_tv_string(&var1);
5497 if (*key == NUL)
5498 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005499 if (verbose)
5500 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 clear_tv(&var1);
5502 return FAIL;
5503 }
5504 }
5505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005506 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005508 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510 if (len == -1)
5511 clear_tv(&var1);
5512 if (item == NULL)
5513 return FAIL;
5514
5515 copy_tv(&item->di_tv, &var1);
5516 clear_tv(rettv);
5517 *rettv = var1;
5518 }
5519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 }
5521 }
5522
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 return OK;
5524}
5525
5526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * Get an option value.
5528 * "arg" points to the '&' or '+' before the option name.
5529 * "arg" is advanced to character after the option name.
5530 * Return OK or FAIL.
5531 */
5532 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 int evaluate;
5537{
5538 char_u *option_end;
5539 long numval;
5540 char_u *stringval;
5541 int opt_type;
5542 int c;
5543 int working = (**arg == '+'); /* has("+option") */
5544 int ret = OK;
5545 int opt_flags;
5546
5547 /*
5548 * Isolate the option name and find its value.
5549 */
5550 option_end = find_option_end(arg, &opt_flags);
5551 if (option_end == NULL)
5552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 EMSG2(_("E112: Option name missing: %s"), *arg);
5555 return FAIL;
5556 }
5557
5558 if (!evaluate)
5559 {
5560 *arg = option_end;
5561 return OK;
5562 }
5563
5564 c = *option_end;
5565 *option_end = NUL;
5566 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (opt_type == -3) /* invalid name */
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 EMSG2(_("E113: Unknown option: %s"), *arg);
5573 ret = FAIL;
5574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 if (opt_type == -2) /* hidden string option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else if (opt_type == -1) /* hidden number option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_NUMBER;
5585 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else if (opt_type == 1) /* number option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_NUMBER;
5590 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else /* string option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 }
5598 else if (working && (opt_type == -2 || opt_type == -1))
5599 ret = FAIL;
5600
5601 *option_end = c; /* put back for error messages */
5602 *arg = option_end;
5603
5604 return ret;
5605}
5606
5607/*
5608 * Allocate a variable for a string constant.
5609 * Return OK or FAIL.
5610 */
5611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 int evaluate;
5616{
5617 char_u *p;
5618 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 int extra = 0;
5620
5621 /*
5622 * Find the end of the string, skipping backslashed characters.
5623 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005624 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
5626 if (*p == '\\' && p[1] != NUL)
5627 {
5628 ++p;
5629 /* A "\<x>" form occupies at least 4 characters, and produces up
5630 * to 6 characters: reserve space for 2 extra */
5631 if (*p == '<')
5632 extra += 2;
5633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635
5636 if (*p != '"')
5637 {
5638 EMSG2(_("E114: Missing quote: %s"), *arg);
5639 return FAIL;
5640 }
5641
5642 /* If only parsing, set *arg and return here */
5643 if (!evaluate)
5644 {
5645 *arg = p + 1;
5646 return OK;
5647 }
5648
5649 /*
5650 * Copy the string into allocated memory, handling backslashed
5651 * characters.
5652 */
5653 name = alloc((unsigned)(p - *arg + extra));
5654 if (name == NULL)
5655 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 rettv->v_type = VAR_STRING;
5657 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 if (*p == '\\')
5662 {
5663 switch (*++p)
5664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 case 'b': *name++ = BS; ++p; break;
5666 case 'e': *name++ = ESC; ++p; break;
5667 case 'f': *name++ = FF; ++p; break;
5668 case 'n': *name++ = NL; ++p; break;
5669 case 'r': *name++ = CAR; ++p; break;
5670 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 case 'X': /* hex: "\x1", "\x12" */
5673 case 'x':
5674 case 'u': /* Unicode: "\u0023" */
5675 case 'U':
5676 if (vim_isxdigit(p[1]))
5677 {
5678 int n, nr;
5679 int c = toupper(*p);
5680
5681 if (c == 'X')
5682 n = 2;
5683 else
5684 n = 4;
5685 nr = 0;
5686 while (--n >= 0 && vim_isxdigit(p[1]))
5687 {
5688 ++p;
5689 nr = (nr << 4) + hex2nr(*p);
5690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#ifdef FEAT_MBYTE
5693 /* For "\u" store the number according to
5694 * 'encoding'. */
5695 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else
5698#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 break;
5702
5703 /* octal: "\1", "\12", "\123" */
5704 case '0':
5705 case '1':
5706 case '2':
5707 case '3':
5708 case '4':
5709 case '5':
5710 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 case '7': *name = *p++ - '0';
5712 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 *name = (*name << 3) + *p++ - '0';
5715 if (*p >= '0' && *p <= '7')
5716 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720
5721 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (extra != 0)
5724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 }
5728 /* FALLTHROUGH */
5729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 break;
5732 }
5733 }
5734 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 *arg = p + 1;
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return OK;
5742}
5743
5744/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 * Return OK or FAIL.
5747 */
5748 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 int evaluate;
5753{
5754 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 int reduce = 0;
5757
5758 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 */
5761 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 break;
5767 ++reduce;
5768 ++p;
5769 }
5770 }
5771
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 return FAIL;
5776 }
5777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 if (!evaluate)
5780 {
5781 *arg = p + 1;
5782 return OK;
5783 }
5784
5785 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 */
5788 str = alloc((unsigned)((p - *arg) - reduce));
5789 if (str == NULL)
5790 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 rettv->v_type = VAR_STRING;
5792 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 break;
5800 ++p;
5801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 *arg = p + 1;
5806
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 return OK;
5808}
5809
5810/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 * Allocate a variable for a List and fill it from "*arg".
5812 * Return OK or FAIL.
5813 */
5814 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 int evaluate;
5819{
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 list_T *l = NULL;
5821 typval_T tv;
5822 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823
5824 if (evaluate)
5825 {
5826 l = list_alloc();
5827 if (l == NULL)
5828 return FAIL;
5829 }
5830
5831 *arg = skipwhite(*arg + 1);
5832 while (**arg != ']' && **arg != NUL)
5833 {
5834 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5835 goto failret;
5836 if (evaluate)
5837 {
5838 item = listitem_alloc();
5839 if (item != NULL)
5840 {
5841 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005842 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 list_append(l, item);
5844 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005845 else
5846 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 }
5848
5849 if (**arg == ']')
5850 break;
5851 if (**arg != ',')
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 goto failret;
5855 }
5856 *arg = skipwhite(*arg + 1);
5857 }
5858
5859 if (**arg != ']')
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862failret:
5863 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005864 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 return FAIL;
5866 }
5867
5868 *arg = skipwhite(*arg + 1);
5869 if (evaluate)
5870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005871 rettv->v_type = VAR_LIST;
5872 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 ++l->lv_refcount;
5874 }
5875
5876 return OK;
5877}
5878
5879/*
5880 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005881 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005883 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884list_alloc()
5885{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 list_T *l;
5887
5888 l = (list_T *)alloc_clear(sizeof(list_T));
5889 if (l != NULL)
5890 {
5891 /* Prepend the list to the list of lists for garbage collection. */
5892 if (first_list != NULL)
5893 first_list->lv_used_prev = l;
5894 l->lv_used_prev = NULL;
5895 l->lv_used_next = first_list;
5896 first_list = l;
5897 }
5898 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899}
5900
5901/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005902 * Allocate an empty list for a return value.
5903 * Returns OK or FAIL.
5904 */
5905 static int
5906rettv_list_alloc(rettv)
5907 typval_T *rettv;
5908{
5909 list_T *l = list_alloc();
5910
5911 if (l == NULL)
5912 return FAIL;
5913
5914 rettv->vval.v_list = l;
5915 rettv->v_type = VAR_LIST;
5916 ++l->lv_refcount;
5917 return OK;
5918}
5919
5920/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 * Unreference a list: decrement the reference count and free it when it
5922 * becomes zero.
5923 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005924 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005928 if (l != NULL && --l->lv_refcount <= 0)
5929 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930}
5931
5932/*
5933 * Free a list, including all items it points to.
5934 * Ignores the reference count.
5935 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005936 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005937list_free(l, recurse)
5938 list_T *l;
5939 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940{
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 /* Remove the list from the list of lists for garbage collection. */
5944 if (l->lv_used_prev == NULL)
5945 first_list = l->lv_used_next;
5946 else
5947 l->lv_used_prev->lv_used_next = l->lv_used_next;
5948 if (l->lv_used_next != NULL)
5949 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5950
Bram Moolenaard9fba312005-06-26 22:34:35 +00005951 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005953 /* Remove the item before deleting it. */
5954 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005955 if (recurse || (item->li_tv.v_type != VAR_LIST
5956 && item->li_tv.v_type != VAR_DICT))
5957 clear_tv(&item->li_tv);
5958 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 }
5960 vim_free(l);
5961}
5962
5963/*
5964 * Allocate a list item.
5965 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005966 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967listitem_alloc()
5968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970}
5971
5972/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005973 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005975 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 vim_free(item);
5981}
5982
5983/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005984 * Remove a list item from a List and free it. Also clears the value.
5985 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005986 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005987listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
5989 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005990{
5991 list_remove(l, item, item);
5992 listitem_free(item);
5993}
5994
5995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 * Get the number of items in a list.
5997 */
5998 static long
5999list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 if (l == NULL)
6003 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006004 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006008 * Return TRUE when two lists have exactly the same values.
6009 */
6010 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006011list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 list_T *l1;
6013 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016{
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006019 if (l1 == NULL || l2 == NULL)
6020 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006021 if (l1 == l2)
6022 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006023 if (list_len(l1) != list_len(l2))
6024 return FALSE;
6025
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026 for (item1 = l1->lv_first, item2 = l2->lv_first;
6027 item1 != NULL && item2 != NULL;
6028 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006029 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006030 return FALSE;
6031 return item1 == NULL && item2 == NULL;
6032}
6033
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006034#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6035 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036/*
6037 * Return the dictitem that an entry in a hashtable points to.
6038 */
6039 dictitem_T *
6040dict_lookup(hi)
6041 hashitem_T *hi;
6042{
6043 return HI2DI(hi);
6044}
6045#endif
6046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048 * Return TRUE when two dictionaries have exactly the same key/values.
6049 */
6050 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006051dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 dict_T *d1;
6053 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 hashitem_T *hi;
6058 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006061 if (d1 == NULL || d2 == NULL)
6062 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006063 if (d1 == d2)
6064 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 if (dict_len(d1) != dict_len(d2))
6066 return FALSE;
6067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006068 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006071 if (!HASHITEM_EMPTY(hi))
6072 {
6073 item2 = dict_find(d2, hi->hi_key, -1);
6074 if (item2 == NULL)
6075 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 return FALSE;
6078 --todo;
6079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 }
6081 return TRUE;
6082}
6083
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084static int tv_equal_recurse_limit;
6085
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006088 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006089 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 */
6091 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 typval_T *tv1;
6094 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 int ic; /* ignore case */
6096 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097{
6098 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006099 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006101 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 * recursiveness to a limit. We guess they are equal then.
6108 * A fixed limit has the problem of still taking an awful long time.
6109 * Reduce the limit every time running into it. That should work fine for
6110 * deeply linked structures that are not recursively linked and catch
6111 * recursiveness quickly. */
6112 if (!recursive)
6113 tv_equal_recurse_limit = 1000;
6114 if (recursive_cnt >= tv_equal_recurse_limit)
6115 {
6116 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006117 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006119
6120 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 ++recursive_cnt;
6124 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6125 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006126 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006127
6128 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 ++recursive_cnt;
6130 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6131 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006132 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 case VAR_FUNC:
6135 return (tv1->vval.v_string != NULL
6136 && tv2->vval.v_string != NULL
6137 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6138
6139 case VAR_NUMBER:
6140 return tv1->vval.v_number == tv2->vval.v_number;
6141
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006142#ifdef FEAT_FLOAT
6143 case VAR_FLOAT:
6144 return tv1->vval.v_float == tv2->vval.v_float;
6145#endif
6146
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147 case VAR_STRING:
6148 s1 = get_tv_string_buf(tv1, buf1);
6149 s2 = get_tv_string_buf(tv2, buf2);
6150 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006151 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006152
6153 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006154 return TRUE;
6155}
6156
6157/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 * Locate item with index "n" in list "l" and return it.
6159 * A negative index is counted from the end; -1 is the last item.
6160 * Returns NULL when "n" is out of range.
6161 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006162 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 long n;
6166{
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 long idx;
6169
6170 if (l == NULL)
6171 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172
6173 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006175 n = l->lv_len + n;
6176
6177 /* Check for index out of range. */
6178 if (n < 0 || n >= l->lv_len)
6179 return NULL;
6180
6181 /* When there is a cached index may start search from there. */
6182 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 if (n < l->lv_idx / 2)
6185 {
6186 /* closest to the start of the list */
6187 item = l->lv_first;
6188 idx = 0;
6189 }
6190 else if (n > (l->lv_idx + l->lv_len) / 2)
6191 {
6192 /* closest to the end of the list */
6193 item = l->lv_last;
6194 idx = l->lv_len - 1;
6195 }
6196 else
6197 {
6198 /* closest to the cached index */
6199 item = l->lv_idx_item;
6200 idx = l->lv_idx;
6201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 }
6203 else
6204 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 if (n < l->lv_len / 2)
6206 {
6207 /* closest to the start of the list */
6208 item = l->lv_first;
6209 idx = 0;
6210 }
6211 else
6212 {
6213 /* closest to the end of the list */
6214 item = l->lv_last;
6215 idx = l->lv_len - 1;
6216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006218
6219 while (n > idx)
6220 {
6221 /* search forward */
6222 item = item->li_next;
6223 ++idx;
6224 }
6225 while (n < idx)
6226 {
6227 /* search backward */
6228 item = item->li_prev;
6229 --idx;
6230 }
6231
6232 /* cache the used index */
6233 l->lv_idx = idx;
6234 l->lv_idx_item = item;
6235
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 return item;
6237}
6238
6239/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006240 * Get list item "l[idx]" as a number.
6241 */
6242 static long
6243list_find_nr(l, idx, errorp)
6244 list_T *l;
6245 long idx;
6246 int *errorp; /* set to TRUE when something wrong */
6247{
6248 listitem_T *li;
6249
6250 li = list_find(l, idx);
6251 if (li == NULL)
6252 {
6253 if (errorp != NULL)
6254 *errorp = TRUE;
6255 return -1L;
6256 }
6257 return get_tv_number_chk(&li->li_tv, errorp);
6258}
6259
6260/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006261 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6262 */
6263 char_u *
6264list_find_str(l, idx)
6265 list_T *l;
6266 long idx;
6267{
6268 listitem_T *li;
6269
6270 li = list_find(l, idx - 1);
6271 if (li == NULL)
6272 {
6273 EMSGN(_(e_listidx), idx);
6274 return NULL;
6275 }
6276 return get_tv_string(&li->li_tv);
6277}
6278
6279/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280 * Locate "item" list "l" and return its index.
6281 * Returns -1 when "item" is not in the list.
6282 */
6283 static long
6284list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287{
6288 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290
6291 if (l == NULL)
6292 return -1;
6293 idx = 0;
6294 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6295 ++idx;
6296 if (li == NULL)
6297 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006298 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006299}
6300
6301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 * Append item "item" to the end of list "l".
6303 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 list_T *l;
6307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308{
6309 if (l->lv_last == NULL)
6310 {
6311 /* empty list */
6312 l->lv_first = item;
6313 l->lv_last = item;
6314 item->li_prev = NULL;
6315 }
6316 else
6317 {
6318 l->lv_last->li_next = item;
6319 item->li_prev = l->lv_last;
6320 l->lv_last = item;
6321 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006322 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 item->li_next = NULL;
6324}
6325
6326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006330 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 list_T *l;
6333 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006335 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336
Bram Moolenaar05159a02005-02-26 23:04:13 +00006337 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006339 copy_tv(tv, &li->li_tv);
6340 list_append(l, li);
6341 return OK;
6342}
6343
6344/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006345 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006346 * Return FAIL when out of memory.
6347 */
6348 int
6349list_append_dict(list, dict)
6350 list_T *list;
6351 dict_T *dict;
6352{
6353 listitem_T *li = listitem_alloc();
6354
6355 if (li == NULL)
6356 return FAIL;
6357 li->li_tv.v_type = VAR_DICT;
6358 li->li_tv.v_lock = 0;
6359 li->li_tv.vval.v_dict = dict;
6360 list_append(list, li);
6361 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 return OK;
6363}
6364
6365/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006367 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 * Returns FAIL when out of memory.
6369 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006371list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 list_T *l;
6373 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006374 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375{
6376 listitem_T *li = listitem_alloc();
6377
6378 if (li == NULL)
6379 return FAIL;
6380 list_append(l, li);
6381 li->li_tv.v_type = VAR_STRING;
6382 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006383 if (str == NULL)
6384 li->li_tv.vval.v_string = NULL;
6385 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006386 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387 return FAIL;
6388 return OK;
6389}
6390
6391/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006392 * Append "n" to list "l".
6393 * Returns FAIL when out of memory.
6394 */
6395 static int
6396list_append_number(l, n)
6397 list_T *l;
6398 varnumber_T n;
6399{
6400 listitem_T *li;
6401
6402 li = listitem_alloc();
6403 if (li == NULL)
6404 return FAIL;
6405 li->li_tv.v_type = VAR_NUMBER;
6406 li->li_tv.v_lock = 0;
6407 li->li_tv.vval.v_number = n;
6408 list_append(l, li);
6409 return OK;
6410}
6411
6412/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 * If "item" is NULL append at the end.
6415 * Return FAIL when out of memory.
6416 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006417 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 list_T *l;
6420 typval_T *tv;
6421 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424
6425 if (ni == NULL)
6426 return FAIL;
6427 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006428 list_insert(l, ni, item);
6429 return OK;
6430}
6431
6432 void
6433list_insert(l, ni, item)
6434 list_T *l;
6435 listitem_T *ni;
6436 listitem_T *item;
6437{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 if (item == NULL)
6439 /* Append new item at end of list. */
6440 list_append(l, ni);
6441 else
6442 {
6443 /* Insert new item before existing item. */
6444 ni->li_prev = item->li_prev;
6445 ni->li_next = item;
6446 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006447 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006449 ++l->lv_idx;
6450 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006452 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 l->lv_idx_item = NULL;
6455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459}
6460
6461/*
6462 * Extend "l1" with "l2".
6463 * If "bef" is NULL append at the end, otherwise insert before this item.
6464 * Returns FAIL when out of memory.
6465 */
6466 static int
6467list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l1;
6469 list_T *l2;
6470 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006473 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006475 /* We also quit the loop when we have inserted the original item count of
6476 * the list, avoid a hang when we extend a list with itself. */
6477 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6479 return FAIL;
6480 return OK;
6481}
6482
6483/*
6484 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6485 * Return FAIL when out of memory.
6486 */
6487 static int
6488list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *l1;
6490 list_T *l2;
6491 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006495 if (l1 == NULL || l2 == NULL)
6496 return FAIL;
6497
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 if (l == NULL)
6501 return FAIL;
6502 tv->v_type = VAR_LIST;
6503 tv->vval.v_list = l;
6504
6505 /* append all items from the second list */
6506 return list_extend(l, l2, NULL);
6507}
6508
6509/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006512 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 * Returns NULL when out of memory.
6514 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006516list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 list_T *copy;
6522 listitem_T *item;
6523 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524
6525 if (orig == NULL)
6526 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527
6528 copy = list_alloc();
6529 if (copy != NULL)
6530 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 if (copyID != 0)
6532 {
6533 /* Do this before adding the items, because one of the items may
6534 * refer back to this list. */
6535 orig->lv_copyID = copyID;
6536 orig->lv_copylist = copy;
6537 }
6538 for (item = orig->lv_first; item != NULL && !got_int;
6539 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 {
6541 ni = listitem_alloc();
6542 if (ni == NULL)
6543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 {
6546 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6547 {
6548 vim_free(ni);
6549 break;
6550 }
6551 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006553 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 list_append(copy, ni);
6555 }
6556 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006557 if (item != NULL)
6558 {
6559 list_unref(copy);
6560 copy = NULL;
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 }
6563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 return copy;
6565}
6566
6567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006569 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006571 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006572list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 list_T *l;
6574 listitem_T *item;
6575 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576{
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 /* notify watchers */
6580 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006582 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 list_fix_watch(l, ip);
6584 if (ip == item2)
6585 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587
6588 if (item2->li_next == NULL)
6589 l->lv_last = item->li_prev;
6590 else
6591 item2->li_next->li_prev = item->li_prev;
6592 if (item->li_prev == NULL)
6593 l->lv_first = item2->li_next;
6594 else
6595 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006596 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597}
6598
6599/*
6600 * Return an allocated string with the string representation of a list.
6601 * May return NULL.
6602 */
6603 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006604list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006606 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607{
6608 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609
6610 if (tv->vval.v_list == NULL)
6611 return NULL;
6612 ga_init2(&ga, (int)sizeof(char), 80);
6613 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 {
6616 vim_free(ga.ga_data);
6617 return NULL;
6618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 ga_append(&ga, ']');
6620 ga_append(&ga, NUL);
6621 return (char_u *)ga.ga_data;
6622}
6623
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006624typedef struct join_S {
6625 char_u *s;
6626 char_u *tofree;
6627} join_T;
6628
6629 static int
6630list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6631 garray_T *gap; /* to store the result in */
6632 list_T *l;
6633 char_u *sep;
6634 int echo_style;
6635 int copyID;
6636 garray_T *join_gap; /* to keep each list item string */
6637{
6638 int i;
6639 join_T *p;
6640 int len;
6641 int sumlen = 0;
6642 int first = TRUE;
6643 char_u *tofree;
6644 char_u numbuf[NUMBUFLEN];
6645 listitem_T *item;
6646 char_u *s;
6647
6648 /* Stringify each item in the list. */
6649 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6650 {
6651 if (echo_style)
6652 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6653 else
6654 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6655 if (s == NULL)
6656 return FAIL;
6657
6658 len = (int)STRLEN(s);
6659 sumlen += len;
6660
6661 ga_grow(join_gap, 1);
6662 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6663 if (tofree != NULL || s != numbuf)
6664 {
6665 p->s = s;
6666 p->tofree = tofree;
6667 }
6668 else
6669 {
6670 p->s = vim_strnsave(s, len);
6671 p->tofree = p->s;
6672 }
6673
6674 line_breakcheck();
6675 }
6676
6677 /* Allocate result buffer with its total size, avoid re-allocation and
6678 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6679 if (join_gap->ga_len >= 2)
6680 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6681 if (ga_grow(gap, sumlen + 2) == FAIL)
6682 return FAIL;
6683
6684 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6685 {
6686 if (first)
6687 first = FALSE;
6688 else
6689 ga_concat(gap, sep);
6690 p = ((join_T *)join_gap->ga_data) + i;
6691
6692 if (p->s != NULL)
6693 ga_concat(gap, p->s);
6694 line_breakcheck();
6695 }
6696
6697 return OK;
6698}
6699
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006702 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006703 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006705 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006706list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006710 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006712{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006713 garray_T join_ga;
6714 int retval;
6715 join_T *p;
6716 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6719 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6720
6721 /* Dispose each item in join_ga. */
6722 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = (join_T *)join_ga.ga_data;
6725 for (i = 0; i < join_ga.ga_len; ++i)
6726 {
6727 vim_free(p->tofree);
6728 ++p;
6729 }
6730 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732
6733 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734}
6735
6736/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006737 * Garbage collection for lists and dictionaries.
6738 *
6739 * We use reference counts to be able to free most items right away when they
6740 * are no longer used. But for composite items it's possible that it becomes
6741 * unused while the reference count is > 0: When there is a recursive
6742 * reference. Example:
6743 * :let l = [1, 2, 3]
6744 * :let d = {9: l}
6745 * :let l[1] = d
6746 *
6747 * Since this is quite unusual we handle this with garbage collection: every
6748 * once in a while find out which lists and dicts are not referenced from any
6749 * variable.
6750 *
6751 * Here is a good reference text about garbage collection (refers to Python
6752 * but it applies to all reference-counting mechanisms):
6753 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006754 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755
6756/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 * Do garbage collection for lists and dicts.
6758 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006759 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 int
6761garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006763 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 buf_T *buf;
6765 win_T *wp;
6766 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006767 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768 int did_free;
6769 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006770#ifdef FEAT_WINDOWS
6771 tabpage_T *tp;
6772#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006774 /* Only do this once. */
6775 want_garbage_collect = FALSE;
6776 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006777 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006778
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006779 /* We advance by two because we add one for items referenced through
6780 * previous_funccal. */
6781 current_copyID += COPYID_INC;
6782 copyID = current_copyID;
6783
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 /*
6785 * 1. Go through all accessible variables and mark all lists and dicts
6786 * with copyID.
6787 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006788
6789 /* Don't free variables in the previous_funccal list unless they are only
6790 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006791 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006792 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6793 {
6794 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6795 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6796 }
6797
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006798 /* script-local variables */
6799 for (i = 1; i <= ga_scripts.ga_len; ++i)
6800 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6801
6802 /* buffer-local variables */
6803 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006804 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805
6806 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006807 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006808 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006809#ifdef FEAT_AUTOCMD
6810 if (aucmd_win != NULL)
6811 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6812#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006814#ifdef FEAT_WINDOWS
6815 /* tabpage-local variables */
6816 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006817 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006818#endif
6819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 /* global variables */
6821 set_ref_in_ht(&globvarht, copyID);
6822
6823 /* function-local variables */
6824 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6825 {
6826 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6827 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6828 }
6829
Bram Moolenaard812df62008-11-09 12:46:09 +00006830 /* v: vars */
6831 set_ref_in_ht(&vimvarht, copyID);
6832
Bram Moolenaar1dced572012-04-05 16:54:08 +02006833#ifdef FEAT_LUA
6834 set_ref_in_lua(copyID);
6835#endif
6836
Bram Moolenaardb913952012-06-29 12:54:53 +02006837#ifdef FEAT_PYTHON
6838 set_ref_in_python(copyID);
6839#endif
6840
6841#ifdef FEAT_PYTHON3
6842 set_ref_in_python3(copyID);
6843#endif
6844
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006845 /*
6846 * 2. Free lists and dictionaries that are not referenced.
6847 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 did_free = free_unref_items(copyID);
6849
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006850 /*
6851 * 3. Check if any funccal can be freed now.
6852 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 for (pfc = &previous_funccal; *pfc != NULL; )
6854 {
6855 if (can_free_funccal(*pfc, copyID))
6856 {
6857 fc = *pfc;
6858 *pfc = fc->caller;
6859 free_funccal(fc, TRUE);
6860 did_free = TRUE;
6861 did_free_funccal = TRUE;
6862 }
6863 else
6864 pfc = &(*pfc)->caller;
6865 }
6866 if (did_free_funccal)
6867 /* When a funccal was freed some more items might be garbage
6868 * collected, so run again. */
6869 (void)garbage_collect();
6870
6871 return did_free;
6872}
6873
6874/*
6875 * Free lists and dictionaries that are no longer referenced.
6876 */
6877 static int
6878free_unref_items(copyID)
6879 int copyID;
6880{
6881 dict_T *dd;
6882 list_T *ll;
6883 int did_free = FALSE;
6884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 */
6888 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 /* Free the Dictionary and ordinary items it contains, but don't
6892 * recurse into Lists and Dictionaries, they will be in the list
6893 * of dicts or list of lists. */
6894 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 did_free = TRUE;
6896
6897 /* restart, next dict may also have been freed */
6898 dd = first_dict;
6899 }
6900 else
6901 dd = dd->dv_used_next;
6902
6903 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006904 * Go through the list of lists and free items without the copyID.
6905 * But don't free a list that has a watcher (used in a for loop), these
6906 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 */
6908 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6910 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006912 /* Free the List and ordinary items it contains, but don't recurse
6913 * into Lists and Dictionaries, they will be in the list of dicts
6914 * or list of lists. */
6915 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 did_free = TRUE;
6917
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006918 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 ll = first_list;
6920 }
6921 else
6922 ll = ll->lv_used_next;
6923
6924 return did_free;
6925}
6926
6927/*
6928 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6929 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006930 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931set_ref_in_ht(ht, copyID)
6932 hashtab_T *ht;
6933 int copyID;
6934{
6935 int todo;
6936 hashitem_T *hi;
6937
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006938 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 for (hi = ht->ht_array; todo > 0; ++hi)
6940 if (!HASHITEM_EMPTY(hi))
6941 {
6942 --todo;
6943 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6944 }
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through list "l" with "copyID".
6949 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006950 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951set_ref_in_list(l, copyID)
6952 list_T *l;
6953 int copyID;
6954{
6955 listitem_T *li;
6956
6957 for (li = l->lv_first; li != NULL; li = li->li_next)
6958 set_ref_in_item(&li->li_tv, copyID);
6959}
6960
6961/*
6962 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6963 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006964 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965set_ref_in_item(tv, copyID)
6966 typval_T *tv;
6967 int copyID;
6968{
6969 dict_T *dd;
6970 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971
6972 switch (tv->v_type)
6973 {
6974 case VAR_DICT:
6975 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006976 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 /* Didn't see this dict yet. */
6979 dd->dv_copyID = copyID;
6980 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006981 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983
6984 case VAR_LIST:
6985 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006986 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* Didn't see this list yet. */
6989 ll->lv_copyID = copyID;
6990 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995}
6996
6997/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006998 * Allocate an empty header for a dictionary.
6999 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007000 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001dict_alloc()
7002{
Bram Moolenaar33570922005-01-25 22:26:29 +00007003 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007004
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007006 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007007 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007008 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 if (first_dict != NULL)
7010 first_dict->dv_used_prev = d;
7011 d->dv_used_next = first_dict;
7012 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007013 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007016 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007017 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007018 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007019 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007020 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007021 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022}
7023
7024/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007025 * Allocate an empty dict for a return value.
7026 * Returns OK or FAIL.
7027 */
7028 static int
7029rettv_dict_alloc(rettv)
7030 typval_T *rettv;
7031{
7032 dict_T *d = dict_alloc();
7033
7034 if (d == NULL)
7035 return FAIL;
7036
7037 rettv->vval.v_dict = d;
7038 rettv->v_type = VAR_DICT;
7039 ++d->dv_refcount;
7040 return OK;
7041}
7042
7043
7044/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045 * Unreference a Dictionary: decrement the reference count and free it when it
7046 * becomes zero.
7047 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007048 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007052 if (d != NULL && --d->dv_refcount <= 0)
7053 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054}
7055
7056/*
7057 * Free a Dictionary, including all items it contains.
7058 * Ignores the reference count.
7059 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007060 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007061dict_free(d, recurse)
7062 dict_T *d;
7063 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007065 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007067 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 /* Remove the dict from the list of dicts for garbage collection. */
7070 if (d->dv_used_prev == NULL)
7071 first_dict = d->dv_used_next;
7072 else
7073 d->dv_used_prev->dv_used_next = d->dv_used_next;
7074 if (d->dv_used_next != NULL)
7075 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7076
7077 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007078 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007079 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007080 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 if (!HASHITEM_EMPTY(hi))
7083 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007084 /* Remove the item before deleting it, just in case there is
7085 * something recursive causing trouble. */
7086 di = HI2DI(hi);
7087 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007088 if (recurse || (di->di_tv.v_type != VAR_LIST
7089 && di->di_tv.v_type != VAR_DICT))
7090 clear_tv(&di->di_tv);
7091 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 --todo;
7093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 vim_free(d);
7097}
7098
7099/*
7100 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 * The "key" is copied to the new item.
7102 * Note that the value of the item "di_tv" still needs to be initialized!
7103 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007105 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106dictitem_alloc(key)
7107 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108{
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007111 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007115 di->di_flags = 0;
7116 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118}
7119
7120/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121 * Make a copy of a Dictionary item.
7122 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007124dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126{
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007129 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7130 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 if (di != NULL)
7132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007134 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 copy_tv(&org->di_tv, &di->di_tv);
7136 }
7137 return di;
7138}
7139
7140/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 * Remove item "item" from Dictionary "dict" and free it.
7142 */
7143 static void
7144dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dict_T *dict;
7146 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147{
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 if (HASHITEM_EMPTY(hi))
7152 EMSG2(_(e_intern2), "dictitem_remove()");
7153 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 dictitem_free(item);
7156}
7157
7158/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159 * Free a dict item. Also clears the value.
7160 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007161 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 clear_tv(&item->di_tv);
7166 vim_free(item);
7167}
7168
7169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7171 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 * Returns NULL when out of memory.
7174 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007179 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180{
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *copy;
7182 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185
7186 if (orig == NULL)
7187 return NULL;
7188
7189 copy = dict_alloc();
7190 if (copy != NULL)
7191 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 if (copyID != 0)
7193 {
7194 orig->dv_copyID = copyID;
7195 orig->dv_copydict = copy;
7196 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007197 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 --todo;
7203
7204 di = dictitem_alloc(hi->hi_key);
7205 if (di == NULL)
7206 break;
7207 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 {
7209 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7210 copyID) == FAIL)
7211 {
7212 vim_free(di);
7213 break;
7214 }
7215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 else
7217 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7218 if (dict_add(copy, di) == FAIL)
7219 {
7220 dictitem_free(di);
7221 break;
7222 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007225
Bram Moolenaare9a41262005-01-15 22:18:47 +00007226 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 if (todo > 0)
7228 {
7229 dict_unref(copy);
7230 copy = NULL;
7231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 }
7233
7234 return copy;
7235}
7236
7237/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007239 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007241 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 dict_T *d;
7244 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245{
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247}
7248
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007250 * Add a number or string entry to dictionary "d".
7251 * When "str" is NULL use number "nr", otherwise use "str".
7252 * Returns FAIL when out of memory and when key already exists.
7253 */
7254 int
7255dict_add_nr_str(d, key, nr, str)
7256 dict_T *d;
7257 char *key;
7258 long nr;
7259 char_u *str;
7260{
7261 dictitem_T *item;
7262
7263 item = dictitem_alloc((char_u *)key);
7264 if (item == NULL)
7265 return FAIL;
7266 item->di_tv.v_lock = 0;
7267 if (str == NULL)
7268 {
7269 item->di_tv.v_type = VAR_NUMBER;
7270 item->di_tv.vval.v_number = nr;
7271 }
7272 else
7273 {
7274 item->di_tv.v_type = VAR_STRING;
7275 item->di_tv.vval.v_string = vim_strsave(str);
7276 }
7277 if (dict_add(d, item) == FAIL)
7278 {
7279 dictitem_free(item);
7280 return FAIL;
7281 }
7282 return OK;
7283}
7284
7285/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007286 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007287 * Returns FAIL when out of memory and when key already exists.
7288 */
7289 int
7290dict_add_list(d, key, list)
7291 dict_T *d;
7292 char *key;
7293 list_T *list;
7294{
7295 dictitem_T *item;
7296
7297 item = dictitem_alloc((char_u *)key);
7298 if (item == NULL)
7299 return FAIL;
7300 item->di_tv.v_lock = 0;
7301 item->di_tv.v_type = VAR_LIST;
7302 item->di_tv.vval.v_list = list;
7303 if (dict_add(d, item) == FAIL)
7304 {
7305 dictitem_free(item);
7306 return FAIL;
7307 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007308 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007309 return OK;
7310}
7311
7312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 * Get the number of items in a Dictionary.
7314 */
7315 static long
7316dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 if (d == NULL)
7320 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322}
7323
7324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325 * Find item "key[len]" in Dictionary "d".
7326 * If "len" is negative use strlen(key).
7327 * Returns NULL when not found.
7328 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007329 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 char_u *key;
7333 int len;
7334{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335#define AKEYLEN 200
7336 char_u buf[AKEYLEN];
7337 char_u *akey;
7338 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 if (len < 0)
7342 akey = key;
7343 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 tofree = akey = vim_strnsave(key, len);
7346 if (akey == NULL)
7347 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007348 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 else
7350 {
7351 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007352 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 akey = buf;
7354 }
7355
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 vim_free(tofree);
7358 if (HASHITEM_EMPTY(hi))
7359 return NULL;
7360 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361}
7362
7363/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007364 * Get a string item from a dictionary.
7365 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007366 * Returns NULL if the entry doesn't exist or out of memory.
7367 */
7368 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007369get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007370 dict_T *d;
7371 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007372 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007373{
7374 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007375 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376
7377 di = dict_find(d, key, -1);
7378 if (di == NULL)
7379 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007380 s = get_tv_string(&di->di_tv);
7381 if (save && s != NULL)
7382 s = vim_strsave(s);
7383 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007384}
7385
7386/*
7387 * Get a number item from a dictionary.
7388 * Returns 0 if the entry doesn't exist or out of memory.
7389 */
7390 long
7391get_dict_number(d, key)
7392 dict_T *d;
7393 char_u *key;
7394{
7395 dictitem_T *di;
7396
7397 di = dict_find(d, key, -1);
7398 if (di == NULL)
7399 return 0;
7400 return get_tv_number(&di->di_tv);
7401}
7402
7403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 * Return an allocated string with the string representation of a Dictionary.
7405 * May return NULL.
7406 */
7407 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007408dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007410 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411{
7412 garray_T ga;
7413 int first = TRUE;
7414 char_u *tofree;
7415 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007418 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007419 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007421 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 return NULL;
7423 ga_init2(&ga, (int)sizeof(char), 80);
7424 ga_append(&ga, '{');
7425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007426 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007427 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 --todo;
7432
7433 if (first)
7434 first = FALSE;
7435 else
7436 ga_concat(&ga, (char_u *)", ");
7437
7438 tofree = string_quote(hi->hi_key, FALSE);
7439 if (tofree != NULL)
7440 {
7441 ga_concat(&ga, tofree);
7442 vim_free(tofree);
7443 }
7444 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007445 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007446 if (s != NULL)
7447 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007449 if (s == NULL)
7450 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007453 if (todo > 0)
7454 {
7455 vim_free(ga.ga_data);
7456 return NULL;
7457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458
7459 ga_append(&ga, '}');
7460 ga_append(&ga, NUL);
7461 return (char_u *)ga.ga_data;
7462}
7463
7464/*
7465 * Allocate a variable for a Dictionary and fill it from "*arg".
7466 * Return OK or FAIL. Returns NOTDONE for {expr}.
7467 */
7468 static int
7469get_dict_tv(arg, rettv, evaluate)
7470 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472 int evaluate;
7473{
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 dict_T *d = NULL;
7475 typval_T tvkey;
7476 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007477 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481
7482 /*
7483 * First check if it's not a curly-braces thing: {expr}.
7484 * Must do this without evaluating, otherwise a function may be called
7485 * twice. Unfortunately this means we need to call eval1() twice for the
7486 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007487 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489 if (*start != '}')
7490 {
7491 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7492 return FAIL;
7493 if (*start == '}')
7494 return NOTDONE;
7495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496
7497 if (evaluate)
7498 {
7499 d = dict_alloc();
7500 if (d == NULL)
7501 return FAIL;
7502 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 tvkey.v_type = VAR_UNKNOWN;
7504 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505
7506 *arg = skipwhite(*arg + 1);
7507 while (**arg != '}' && **arg != NUL)
7508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 goto failret;
7511 if (**arg != ':')
7512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007513 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 goto failret;
7516 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007517 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007519 key = get_tv_string_buf_chk(&tvkey, buf);
7520 if (key == NULL || *key == NUL)
7521 {
7522 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7523 if (key != NULL)
7524 EMSG(_(e_emptykey));
7525 clear_tv(&tvkey);
7526 goto failret;
7527 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529
7530 *arg = skipwhite(*arg + 1);
7531 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7532 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007533 if (evaluate)
7534 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 goto failret;
7536 }
7537 if (evaluate)
7538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 if (item != NULL)
7541 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007542 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 clear_tv(&tv);
7545 goto failret;
7546 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 item = dictitem_alloc(key);
7548 clear_tv(&tvkey);
7549 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007552 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 if (dict_add(d, item) == FAIL)
7554 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 }
7556 }
7557
7558 if (**arg == '}')
7559 break;
7560 if (**arg != ',')
7561 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007562 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 goto failret;
7564 }
7565 *arg = skipwhite(*arg + 1);
7566 }
7567
7568 if (**arg != '}')
7569 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007570 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571failret:
7572 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007573 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 return FAIL;
7575 }
7576
7577 *arg = skipwhite(*arg + 1);
7578 if (evaluate)
7579 {
7580 rettv->v_type = VAR_DICT;
7581 rettv->vval.v_dict = d;
7582 ++d->dv_refcount;
7583 }
7584
7585 return OK;
7586}
7587
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 * Return a string with the string representation of a variable.
7590 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007591 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007592 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007594 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 */
7596 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007597echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007599 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007600 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007603 static int recurse = 0;
7604 char_u *r = NULL;
7605
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007607 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007608 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 *tofree = NULL;
7610 return NULL;
7611 }
7612 ++recurse;
7613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 switch (tv->v_type)
7615 {
7616 case VAR_FUNC:
7617 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 r = tv->vval.v_string;
7619 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007622 if (tv->vval.v_list == NULL)
7623 {
7624 *tofree = NULL;
7625 r = NULL;
7626 }
7627 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7628 {
7629 *tofree = NULL;
7630 r = (char_u *)"[...]";
7631 }
7632 else
7633 {
7634 tv->vval.v_list->lv_copyID = copyID;
7635 *tofree = list2string(tv, copyID);
7636 r = *tofree;
7637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007639
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007641 if (tv->vval.v_dict == NULL)
7642 {
7643 *tofree = NULL;
7644 r = NULL;
7645 }
7646 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7647 {
7648 *tofree = NULL;
7649 r = (char_u *)"{...}";
7650 }
7651 else
7652 {
7653 tv->vval.v_dict->dv_copyID = copyID;
7654 *tofree = dict2string(tv, copyID);
7655 r = *tofree;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 case VAR_STRING:
7660 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007661 *tofree = NULL;
7662 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007665#ifdef FEAT_FLOAT
7666 case VAR_FLOAT:
7667 *tofree = NULL;
7668 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7669 r = numbuf;
7670 break;
7671#endif
7672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677
7678 --recurse;
7679 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680}
7681
7682/*
7683 * Return a string with the string representation of a variable.
7684 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7685 * "numbuf" is used for a number.
7686 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007687 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 */
7689 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007690tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692 char_u **tofree;
7693 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007694 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695{
7696 switch (tv->v_type)
7697 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 case VAR_FUNC:
7699 *tofree = string_quote(tv->vval.v_string, TRUE);
7700 return *tofree;
7701 case VAR_STRING:
7702 *tofree = string_quote(tv->vval.v_string, FALSE);
7703 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007704#ifdef FEAT_FLOAT
7705 case VAR_FLOAT:
7706 *tofree = NULL;
7707 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7708 return numbuf;
7709#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007714 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007716 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007717 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718}
7719
7720/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 * Return string "str" in ' quotes, doubling ' characters.
7722 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 */
7725 static char_u *
7726string_quote(str, function)
7727 char_u *str;
7728 int function;
7729{
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 char_u *p, *r, *s;
7732
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 len = (function ? 13 : 3);
7734 if (str != NULL)
7735 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007736 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 for (p = str; *p != NUL; mb_ptr_adv(p))
7738 if (*p == '\'')
7739 ++len;
7740 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 s = r = alloc(len);
7742 if (r != NULL)
7743 {
7744 if (function)
7745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007747 r += 10;
7748 }
7749 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 if (str != NULL)
7752 for (p = str; *p != NUL; )
7753 {
7754 if (*p == '\'')
7755 *r++ = '\'';
7756 MB_COPY_CHAR(p, r);
7757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 if (function)
7760 *r++ = ')';
7761 *r++ = NUL;
7762 }
7763 return s;
7764}
7765
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007766#ifdef FEAT_FLOAT
7767/*
7768 * Convert the string "text" to a floating point number.
7769 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7770 * this always uses a decimal point.
7771 * Returns the length of the text that was consumed.
7772 */
7773 static int
7774string2float(text, value)
7775 char_u *text;
7776 float_T *value; /* result stored here */
7777{
7778 char *s = (char *)text;
7779 float_T f;
7780
7781 f = strtod(s, &s);
7782 *value = f;
7783 return (int)((char_u *)s - text);
7784}
7785#endif
7786
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 * Get the value of an environment variable.
7789 * "arg" is pointing to the '$'. It is advanced to after the name.
7790 * If the environment variable was not set, silently assume it is empty.
7791 * Always return OK.
7792 */
7793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 int evaluate;
7798{
7799 char_u *string = NULL;
7800 int len;
7801 int cc;
7802 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007803 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805 ++*arg;
7806 name = *arg;
7807 len = get_env_len(arg);
7808 if (evaluate)
7809 {
7810 if (len != 0)
7811 {
7812 cc = name[len];
7813 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007814 /* first try vim_getenv(), fast for normal environment vars */
7815 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007817 {
7818 if (!mustfree)
7819 string = vim_strsave(string);
7820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 else
7822 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007823 if (mustfree)
7824 vim_free(string);
7825
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 /* next try expanding things like $VIM and ${HOME} */
7827 string = expand_env_save(name - 1);
7828 if (string != NULL && *string == '$')
7829 {
7830 vim_free(string);
7831 string = NULL;
7832 }
7833 }
7834 name[len] = cc;
7835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 rettv->v_type = VAR_STRING;
7837 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
7839
7840 return OK;
7841}
7842
7843/*
7844 * Array with names and number of arguments of all internal functions
7845 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7846 */
7847static struct fst
7848{
7849 char *f_name; /* function name */
7850 char f_min_argc; /* minimal number of arguments */
7851 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007853 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854} functions[] =
7855{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007856#ifdef FEAT_FLOAT
7857 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007858 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007860 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007861 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"append", 2, 2, f_append},
7863 {"argc", 0, 0, f_argc},
7864 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007865 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007869 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007872 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"bufexists", 1, 1, f_bufexists},
7874 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7875 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7876 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7877 {"buflisted", 1, 1, f_buflisted},
7878 {"bufloaded", 1, 1, f_bufloaded},
7879 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007880 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"bufwinnr", 1, 1, f_bufwinnr},
7882 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007883 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007884 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 {"ceil", 1, 1, f_ceil},
7888#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007889 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007890 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007892 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007894#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007895 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007896 {"complete_add", 1, 1, f_complete_add},
7897 {"complete_check", 0, 0, f_complete_check},
7898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#ifdef FEAT_FLOAT
7902 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007903 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007907 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007908 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"delete", 1, 1, f_delete},
7910 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007911 {"diff_filler", 1, 1, f_diff_filler},
7912 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007913 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"eventhandler", 0, 0, f_eventhandler},
7917 {"executable", 1, 1, f_executable},
7918 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007919#ifdef FEAT_FLOAT
7920 {"exp", 1, 1, f_exp},
7921#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007922 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007923 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007924 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7926 {"filereadable", 1, 1, f_filereadable},
7927 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007929 {"finddir", 1, 3, f_finddir},
7930 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007931#ifdef FEAT_FLOAT
7932 {"float2nr", 1, 1, f_float2nr},
7933 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007934 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007936 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"fnamemodify", 2, 2, f_fnamemodify},
7938 {"foldclosed", 1, 1, f_foldclosed},
7939 {"foldclosedend", 1, 1, f_foldclosedend},
7940 {"foldlevel", 1, 1, f_foldlevel},
7941 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007942 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007945 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007946 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007947 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007948 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"getchar", 0, 1, f_getchar},
7950 {"getcharmod", 0, 0, f_getcharmod},
7951 {"getcmdline", 0, 0, f_getcmdline},
7952 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007953 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007955 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007956 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"getfsize", 1, 1, f_getfsize},
7958 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007959 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007960 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007961 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007962 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007963 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007964 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007965 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007966 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007968 {"gettabvar", 2, 3, f_gettabvar},
7969 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"getwinposx", 0, 0, f_getwinposx},
7971 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007972 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007973 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007974 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007977 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007978 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7980 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7981 {"histadd", 2, 2, f_histadd},
7982 {"histdel", 1, 2, f_histdel},
7983 {"histget", 1, 2, f_histget},
7984 {"histnr", 1, 1, f_histnr},
7985 {"hlID", 1, 1, f_hlID},
7986 {"hlexists", 1, 1, f_hlexists},
7987 {"hostname", 0, 0, f_hostname},
7988 {"iconv", 3, 3, f_iconv},
7989 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007991 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007993 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {"inputrestore", 0, 0, f_inputrestore},
7995 {"inputsave", 0, 0, f_inputsave},
7996 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007998 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008000 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008001 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008003 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008005 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"libcall", 3, 3, f_libcall},
8007 {"libcallnr", 3, 3, f_libcallnr},
8008 {"line", 1, 1, f_line},
8009 {"line2byte", 1, 1, f_line2byte},
8010 {"lispindent", 1, 1, f_lispindent},
8011 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008013 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008014 {"log10", 1, 1, f_log10},
8015#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008016#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008017 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008018#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008019 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008020 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008021 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008022 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008023 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008024 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008025 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008026 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008027 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008028 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008029 {"max", 1, 1, f_max},
8030 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008031#ifdef vim_mkdir
8032 {"mkdir", 1, 3, f_mkdir},
8033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008034 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008035#ifdef FEAT_MZSCHEME
8036 {"mzeval", 1, 1, f_mzeval},
8037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008039 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008040 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008041 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#ifdef FEAT_FLOAT
8043 {"pow", 2, 2, f_pow},
8044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008046 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008047 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008048#ifdef FEAT_PYTHON3
8049 {"py3eval", 1, 1, f_py3eval},
8050#endif
8051#ifdef FEAT_PYTHON
8052 {"pyeval", 1, 1, f_pyeval},
8053#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008055 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008056 {"reltime", 0, 2, f_reltime},
8057 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"remote_expr", 2, 3, f_remote_expr},
8059 {"remote_foreground", 1, 1, f_remote_foreground},
8060 {"remote_peek", 1, 2, f_remote_peek},
8061 {"remote_read", 1, 1, f_remote_read},
8062 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008063 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008065 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008067 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#ifdef FEAT_FLOAT
8069 {"round", 1, 1, f_round},
8070#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008071 {"screenattr", 2, 2, f_screenattr},
8072 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008073 {"screencol", 0, 0, f_screencol},
8074 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008075 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008076 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008077 {"searchpair", 3, 7, f_searchpair},
8078 {"searchpairpos", 3, 7, f_searchpairpos},
8079 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"server2client", 2, 2, f_server2client},
8081 {"serverlist", 0, 0, f_serverlist},
8082 {"setbufvar", 3, 3, f_setbufvar},
8083 {"setcmdpos", 1, 1, f_setcmdpos},
8084 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008085 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008086 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008087 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008088 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008090 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008091 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008093#ifdef FEAT_CRYPT
8094 {"sha256", 1, 1, f_sha256},
8095#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008096 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008097 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
8100 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008101 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008103 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008104 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008105 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008106 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008107 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#ifdef FEAT_FLOAT
8109 {"sqrt", 1, 1, f_sqrt},
8110 {"str2float", 1, 1, f_str2float},
8111#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008112 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008113 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008114 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#ifdef HAVE_STRFTIME
8116 {"strftime", 1, 2, f_strftime},
8117#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008118 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"strlen", 1, 1, f_strlen},
8121 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008122 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008124 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"submatch", 1, 1, f_submatch},
8126 {"substitute", 4, 4, f_substitute},
8127 {"synID", 3, 3, f_synID},
8128 {"synIDattr", 2, 3, f_synIDattr},
8129 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008130 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008131 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008132 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008133 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008134 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008135 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008136 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008137 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138#ifdef FEAT_FLOAT
8139 {"tan", 1, 1, f_tan},
8140 {"tanh", 1, 1, f_tanh},
8141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008143 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"tolower", 1, 1, f_tolower},
8145 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008146 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008147#ifdef FEAT_FLOAT
8148 {"trunc", 1, 1, f_trunc},
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008151 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008152 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008153 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"virtcol", 1, 1, f_virtcol},
8155 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008156 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"winbufnr", 1, 1, f_winbufnr},
8158 {"wincol", 0, 0, f_wincol},
8159 {"winheight", 1, 1, f_winheight},
8160 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008161 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008163 {"winrestview", 1, 1, f_winrestview},
8164 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008166 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008167 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168};
8169
8170#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8171
8172/*
8173 * Function given to ExpandGeneric() to obtain the list of internal
8174 * or user defined function names.
8175 */
8176 char_u *
8177get_function_name(xp, idx)
8178 expand_T *xp;
8179 int idx;
8180{
8181 static int intidx = -1;
8182 char_u *name;
8183
8184 if (idx == 0)
8185 intidx = -1;
8186 if (intidx < 0)
8187 {
8188 name = get_user_func_name(xp, idx);
8189 if (name != NULL)
8190 return name;
8191 }
8192 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8193 {
8194 STRCPY(IObuff, functions[intidx].f_name);
8195 STRCAT(IObuff, "(");
8196 if (functions[intidx].f_max_argc == 0)
8197 STRCAT(IObuff, ")");
8198 return IObuff;
8199 }
8200
8201 return NULL;
8202}
8203
8204/*
8205 * Function given to ExpandGeneric() to obtain the list of internal or
8206 * user defined variable or function names.
8207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 char_u *
8209get_expr_name(xp, idx)
8210 expand_T *xp;
8211 int idx;
8212{
8213 static int intidx = -1;
8214 char_u *name;
8215
8216 if (idx == 0)
8217 intidx = -1;
8218 if (intidx < 0)
8219 {
8220 name = get_function_name(xp, idx);
8221 if (name != NULL)
8222 return name;
8223 }
8224 return get_user_var_name(xp, ++intidx);
8225}
8226
8227#endif /* FEAT_CMDL_COMPL */
8228
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008229#if defined(EBCDIC) || defined(PROTO)
8230/*
8231 * Compare struct fst by function name.
8232 */
8233 static int
8234compare_func_name(s1, s2)
8235 const void *s1;
8236 const void *s2;
8237{
8238 struct fst *p1 = (struct fst *)s1;
8239 struct fst *p2 = (struct fst *)s2;
8240
8241 return STRCMP(p1->f_name, p2->f_name);
8242}
8243
8244/*
8245 * Sort the function table by function name.
8246 * The sorting of the table above is ASCII dependant.
8247 * On machines using EBCDIC we have to sort it.
8248 */
8249 static void
8250sortFunctions()
8251{
8252 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8253
8254 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8255}
8256#endif
8257
8258
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259/*
8260 * Find internal function in table above.
8261 * Return index, or -1 if not found
8262 */
8263 static int
8264find_internal_func(name)
8265 char_u *name; /* name of the function */
8266{
8267 int first = 0;
8268 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8269 int cmp;
8270 int x;
8271
8272 /*
8273 * Find the function name in the table. Binary search.
8274 */
8275 while (first <= last)
8276 {
8277 x = first + ((unsigned)(last - first) >> 1);
8278 cmp = STRCMP(name, functions[x].f_name);
8279 if (cmp < 0)
8280 last = x - 1;
8281 else if (cmp > 0)
8282 first = x + 1;
8283 else
8284 return x;
8285 }
8286 return -1;
8287}
8288
8289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8291 * name it contains, otherwise return "name".
8292 */
8293 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008294deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008295 char_u *name;
8296 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008297 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298{
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008300 int cc;
8301
8302 cc = name[*lenp];
8303 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008304 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008305 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008307 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 {
8310 *lenp = 0;
8311 return (char_u *)""; /* just in case */
8312 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008313 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 }
8316
8317 return name;
8318}
8319
8320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 * Allocate a variable for the result of a function.
8322 * Return OK or FAIL.
8323 */
8324 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008325get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8326 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 char_u *name; /* name of the function */
8328 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 char_u **arg; /* argument, pointing to the '(' */
8331 linenr_T firstline; /* first line of range */
8332 linenr_T lastline; /* last line of range */
8333 int *doesrange; /* return: function handled range */
8334 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 char_u *argp;
8338 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008339 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 int argcount = 0; /* number of arguments found */
8341
8342 /*
8343 * Get the arguments.
8344 */
8345 argp = *arg;
8346 while (argcount < MAX_FUNC_ARGS)
8347 {
8348 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8349 if (*argp == ')' || *argp == ',' || *argp == NUL)
8350 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8352 {
8353 ret = FAIL;
8354 break;
8355 }
8356 ++argcount;
8357 if (*argp != ',')
8358 break;
8359 }
8360 if (*argp == ')')
8361 ++argp;
8362 else
8363 ret = FAIL;
8364
8365 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008366 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008367 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 {
8370 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008371 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008373 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375
8376 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008377 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 *arg = skipwhite(argp);
8380 return ret;
8381}
8382
8383
8384/*
8385 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008386 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008387 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 */
8389 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008390call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008391 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008392 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008394 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008396 typval_T *argvars; /* vars for arguments, must have "argcount"
8397 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 linenr_T firstline; /* first line of range */
8399 linenr_T lastline; /* last line of range */
8400 int *doesrange; /* return: function handled range */
8401 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405#define ERROR_UNKNOWN 0
8406#define ERROR_TOOMANY 1
8407#define ERROR_TOOFEW 2
8408#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008409#define ERROR_DICT 4
8410#define ERROR_NONE 5
8411#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 int error = ERROR_NONE;
8413 int i;
8414 int llen;
8415 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#define FLEN_FIXED 40
8417 char_u fname_buf[FLEN_FIXED + 1];
8418 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008419 char_u *name;
8420
8421 /* Make a copy of the name, if it comes from a funcref variable it could
8422 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008423 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008424 if (name == NULL)
8425 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
8427 /*
8428 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8429 * Change <SNR>123_name() to K_SNR 123_name().
8430 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 llen = eval_fname_script(name);
8433 if (llen > 0)
8434 {
8435 fname_buf[0] = K_SPECIAL;
8436 fname_buf[1] = KS_EXTRA;
8437 fname_buf[2] = (int)KE_SNR;
8438 i = 3;
8439 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8440 {
8441 if (current_SID <= 0)
8442 error = ERROR_SCRIPT;
8443 else
8444 {
8445 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8446 i = (int)STRLEN(fname_buf);
8447 }
8448 }
8449 if (i + STRLEN(name + llen) < FLEN_FIXED)
8450 {
8451 STRCPY(fname_buf + i, name + llen);
8452 fname = fname_buf;
8453 }
8454 else
8455 {
8456 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8457 if (fname == NULL)
8458 error = ERROR_OTHER;
8459 else
8460 {
8461 mch_memmove(fname, fname_buf, (size_t)i);
8462 STRCPY(fname + i, name + llen);
8463 }
8464 }
8465 }
8466 else
8467 fname = name;
8468
8469 *doesrange = FALSE;
8470
8471
8472 /* execute the function if no errors detected and executing */
8473 if (evaluate && error == ERROR_NONE)
8474 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008475 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8476 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 error = ERROR_UNKNOWN;
8478
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008479 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
8481 /*
8482 * User defined function.
8483 */
8484 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008485
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008487 /* Trigger FuncUndefined event, may load the function. */
8488 if (fp == NULL
8489 && apply_autocmds(EVENT_FUNCUNDEFINED,
8490 fname, fname, TRUE, NULL)
8491 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008493 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 fp = find_func(fname);
8495 }
8496#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008497 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008498 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008499 {
8500 /* loaded a package, search for the function again */
8501 fp = find_func(fname);
8502 }
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (fp != NULL)
8505 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008506 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008508 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008510 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008512 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008513 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 else
8515 {
8516 /*
8517 * Call the user function.
8518 * Save and restore search patterns, script variables and
8519 * redo buffer.
8520 */
8521 save_search_patterns();
8522 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008523 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008524 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008525 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008526 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8527 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8528 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008529 /* Function was unreferenced while being used, free it
8530 * now. */
8531 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 restoreRedobuff();
8533 restore_search_patterns();
8534 error = ERROR_NONE;
8535 }
8536 }
8537 }
8538 else
8539 {
8540 /*
8541 * Find the function name in the table, call its implementation.
8542 */
8543 i = find_internal_func(fname);
8544 if (i >= 0)
8545 {
8546 if (argcount < functions[i].f_min_argc)
8547 error = ERROR_TOOFEW;
8548 else if (argcount > functions[i].f_max_argc)
8549 error = ERROR_TOOMANY;
8550 else
8551 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 error = ERROR_NONE;
8555 }
8556 }
8557 }
8558 /*
8559 * The function call (or "FuncUndefined" autocommand sequence) might
8560 * have been aborted by an error, an interrupt, or an explicitly thrown
8561 * exception that has not been caught so far. This situation can be
8562 * tested for by calling aborting(). For an error in an internal
8563 * function or for the "E132" error in call_user_func(), however, the
8564 * throw point at which the "force_abort" flag (temporarily reset by
8565 * emsg()) is normally updated has not been reached yet. We need to
8566 * update that flag first to make aborting() reliable.
8567 */
8568 update_force_abort();
8569 }
8570 if (error == ERROR_NONE)
8571 ret = OK;
8572
8573 /*
8574 * Report an error unless the argument evaluation or function call has been
8575 * cancelled due to an aborting error, an interrupt, or an exception.
8576 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008577 if (!aborting())
8578 {
8579 switch (error)
8580 {
8581 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008582 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008583 break;
8584 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008585 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008586 break;
8587 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008588 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008589 name);
8590 break;
8591 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008592 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008593 name);
8594 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008596 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 name);
8598 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 }
8600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (fname != name && fname != fname_buf)
8603 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008604 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
8606 return ret;
8607}
8608
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008609/*
8610 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008611 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008612 */
8613 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008614emsg_funcname(ermsg, name)
8615 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008616 char_u *name;
8617{
8618 char_u *p;
8619
8620 if (*name == K_SPECIAL)
8621 p = concat_str((char_u *)"<SNR>", name + 3);
8622 else
8623 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008624 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008625 if (p != name)
8626 vim_free(p);
8627}
8628
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008629/*
8630 * Return TRUE for a non-zero Number and a non-empty String.
8631 */
8632 static int
8633non_zero_arg(argvars)
8634 typval_T *argvars;
8635{
8636 return ((argvars[0].v_type == VAR_NUMBER
8637 && argvars[0].vval.v_number != 0)
8638 || (argvars[0].v_type == VAR_STRING
8639 && argvars[0].vval.v_string != NULL
8640 && *argvars[0].vval.v_string != NUL));
8641}
8642
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643/*********************************************
8644 * Implementation of the built-in functions
8645 */
8646
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008647#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008648static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8649
8650/*
8651 * Get the float value of "argvars[0]" into "f".
8652 * Returns FAIL when the argument is not a Number or Float.
8653 */
8654 static int
8655get_float_arg(argvars, f)
8656 typval_T *argvars;
8657 float_T *f;
8658{
8659 if (argvars[0].v_type == VAR_FLOAT)
8660 {
8661 *f = argvars[0].vval.v_float;
8662 return OK;
8663 }
8664 if (argvars[0].v_type == VAR_NUMBER)
8665 {
8666 *f = (float_T)argvars[0].vval.v_number;
8667 return OK;
8668 }
8669 EMSG(_("E808: Number or Float required"));
8670 return FAIL;
8671}
8672
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008673/*
8674 * "abs(expr)" function
8675 */
8676 static void
8677f_abs(argvars, rettv)
8678 typval_T *argvars;
8679 typval_T *rettv;
8680{
8681 if (argvars[0].v_type == VAR_FLOAT)
8682 {
8683 rettv->v_type = VAR_FLOAT;
8684 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8685 }
8686 else
8687 {
8688 varnumber_T n;
8689 int error = FALSE;
8690
8691 n = get_tv_number_chk(&argvars[0], &error);
8692 if (error)
8693 rettv->vval.v_number = -1;
8694 else if (n > 0)
8695 rettv->vval.v_number = n;
8696 else
8697 rettv->vval.v_number = -n;
8698 }
8699}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008700
8701/*
8702 * "acos()" function
8703 */
8704 static void
8705f_acos(argvars, rettv)
8706 typval_T *argvars;
8707 typval_T *rettv;
8708{
8709 float_T f;
8710
8711 rettv->v_type = VAR_FLOAT;
8712 if (get_float_arg(argvars, &f) == OK)
8713 rettv->vval.v_float = acos(f);
8714 else
8715 rettv->vval.v_float = 0.0;
8716}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008717#endif
8718
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 */
8722 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008723f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008730 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008732 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008733 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008734 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008736 }
8737 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738 EMSG(_(e_listreq));
8739}
8740
8741/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008742 * "and(expr, expr)" function
8743 */
8744 static void
8745f_and(argvars, rettv)
8746 typval_T *argvars;
8747 typval_T *rettv;
8748{
8749 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8750 & get_tv_number_chk(&argvars[1], NULL);
8751}
8752
8753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008754 * "append(lnum, string/list)" function
8755 */
8756 static void
8757f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008758 typval_T *argvars;
8759 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760{
8761 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008762 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008763 list_T *l = NULL;
8764 listitem_T *li = NULL;
8765 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766 long added = 0;
8767
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008768 /* When coming here from Insert mode, sync undo, so that this can be
8769 * undone separately from what was previously inserted. */
8770 if (u_sync_once == 2)
8771 {
8772 u_sync_once = 1; /* notify that u_sync() was called */
8773 u_sync(TRUE);
8774 }
8775
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776 lnum = get_tv_lnum(argvars);
8777 if (lnum >= 0
8778 && lnum <= curbuf->b_ml.ml_line_count
8779 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008780 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008781 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008782 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008783 l = argvars[1].vval.v_list;
8784 if (l == NULL)
8785 return;
8786 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008787 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788 for (;;)
8789 {
8790 if (l == NULL)
8791 tv = &argvars[1]; /* append a string */
8792 else if (li == NULL)
8793 break; /* end of list */
8794 else
8795 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 line = get_tv_string_chk(tv);
8797 if (line == NULL) /* type error */
8798 {
8799 rettv->vval.v_number = 1; /* Failed */
8800 break;
8801 }
8802 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008803 ++added;
8804 if (l == NULL)
8805 break;
8806 li = li->li_next;
8807 }
8808
8809 appended_lines_mark(lnum, added);
8810 if (curwin->w_cursor.lnum > lnum)
8811 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008813 else
8814 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * "argc()" function
8819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008822 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828/*
8829 * "argidx()" function
8830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008833 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837}
8838
8839/*
8840 * "argv(nr)" function
8841 */
8842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 typval_T *argvars;
8845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 int idx;
8848
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008849 if (argvars[0].v_type != VAR_UNKNOWN)
8850 {
8851 idx = get_tv_number_chk(&argvars[0], NULL);
8852 if (idx >= 0 && idx < ARGCOUNT)
8853 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8854 else
8855 rettv->vval.v_string = NULL;
8856 rettv->v_type = VAR_STRING;
8857 }
8858 else if (rettv_list_alloc(rettv) == OK)
8859 for (idx = 0; idx < ARGCOUNT; ++idx)
8860 list_append_string(rettv->vval.v_list,
8861 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862}
8863
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008864#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008865/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008866 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008867 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008868 static void
8869f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008870 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008871 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008873 float_T f;
8874
8875 rettv->v_type = VAR_FLOAT;
8876 if (get_float_arg(argvars, &f) == OK)
8877 rettv->vval.v_float = asin(f);
8878 else
8879 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880}
8881
8882/*
8883 * "atan()" function
8884 */
8885 static void
8886f_atan(argvars, rettv)
8887 typval_T *argvars;
8888 typval_T *rettv;
8889{
8890 float_T f;
8891
8892 rettv->v_type = VAR_FLOAT;
8893 if (get_float_arg(argvars, &f) == OK)
8894 rettv->vval.v_float = atan(f);
8895 else
8896 rettv->vval.v_float = 0.0;
8897}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008898
8899/*
8900 * "atan2()" function
8901 */
8902 static void
8903f_atan2(argvars, rettv)
8904 typval_T *argvars;
8905 typval_T *rettv;
8906{
8907 float_T fx, fy;
8908
8909 rettv->v_type = VAR_FLOAT;
8910 if (get_float_arg(argvars, &fx) == OK
8911 && get_float_arg(&argvars[1], &fy) == OK)
8912 rettv->vval.v_float = atan2(fx, fy);
8913 else
8914 rettv->vval.v_float = 0.0;
8915}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008916#endif
8917
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918/*
8919 * "browse(save, title, initdir, default)" function
8920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926#ifdef FEAT_BROWSE
8927 int save;
8928 char_u *title;
8929 char_u *initdir;
8930 char_u *defname;
8931 char_u buf[NUMBUFLEN];
8932 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 save = get_tv_number_chk(&argvars[0], &error);
8936 title = get_tv_string_chk(&argvars[1]);
8937 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8938 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940 if (error || title == NULL || initdir == NULL || defname == NULL)
8941 rettv->vval.v_string = NULL;
8942 else
8943 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008944 do_browse(save ? BROWSE_SAVE : 0,
8945 title, defname, NULL, initdir, NULL, curbuf);
8946#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008950}
8951
8952/*
8953 * "browsedir(title, initdir)" function
8954 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008959{
8960#ifdef FEAT_BROWSE
8961 char_u *title;
8962 char_u *initdir;
8963 char_u buf[NUMBUFLEN];
8964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965 title = get_tv_string_chk(&argvars[0]);
8966 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 if (title == NULL || initdir == NULL)
8969 rettv->vval.v_string = NULL;
8970 else
8971 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977}
8978
Bram Moolenaar33570922005-01-25 22:26:29 +00008979static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008980
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981/*
8982 * Find a buffer by number or exact name.
8983 */
8984 static buf_T *
8985find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 if (avar->v_type == VAR_NUMBER)
8991 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008992 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008995 if (buf == NULL)
8996 {
8997 /* No full path name match, try a match with a URL or a "nofile"
8998 * buffer, these don't use the full path. */
8999 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9000 if (buf->b_fname != NULL
9001 && (path_with_url(buf->b_fname)
9002#ifdef FEAT_QUICKFIX
9003 || bt_nofile(buf)
9004#endif
9005 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009006 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009007 break;
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 }
9010 return buf;
9011}
9012
9013/*
9014 * "bufexists(expr)" function
9015 */
9016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 typval_T *argvars;
9019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022}
9023
9024/*
9025 * "buflisted(expr)" function
9026 */
9027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *argvars;
9030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
9032 buf_T *buf;
9033
9034 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036}
9037
9038/*
9039 * "bufloaded(expr)" function
9040 */
9041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009043 typval_T *argvars;
9044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
9046 buf_T *buf;
9047
9048 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050}
9051
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009052static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009053
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054/*
9055 * Get buffer by number or pattern.
9056 */
9057 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009058get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009060 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 int save_magic;
9064 char_u *save_cpo;
9065 buf_T *buf;
9066
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 if (tv->v_type == VAR_NUMBER)
9068 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009069 if (tv->v_type != VAR_STRING)
9070 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (name == NULL || *name == NUL)
9072 return curbuf;
9073 if (name[0] == '$' && name[1] == NUL)
9074 return lastbuf;
9075
9076 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9077 save_magic = p_magic;
9078 p_magic = TRUE;
9079 save_cpo = p_cpo;
9080 p_cpo = (char_u *)"";
9081
9082 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009083 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084
9085 p_magic = save_magic;
9086 p_cpo = save_cpo;
9087
9088 /* If not found, try expanding the name, like done for bufexists(). */
9089 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
9092 return buf;
9093}
9094
9095/*
9096 * "bufname(expr)" function
9097 */
9098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 typval_T *argvars;
9101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
9103 buf_T *buf;
9104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 --emsg_off;
9114}
9115
9116/*
9117 * "bufnr(expr)" function
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009125 int error = FALSE;
9126 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009130 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009131 --emsg_off;
9132
9133 /* If the buffer isn't found and the second argument is not zero create a
9134 * new buffer. */
9135 if (buf == NULL
9136 && argvars[1].v_type != VAR_UNKNOWN
9137 && get_tv_number_chk(&argvars[1], &error) != 0
9138 && !error
9139 && (name = get_tv_string_chk(&argvars[0])) != NULL
9140 && !error)
9141 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9142
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
9149/*
9150 * "bufwinnr(nr)" function
9151 */
9152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009154 typval_T *argvars;
9155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157#ifdef FEAT_WINDOWS
9158 win_T *wp;
9159 int winnr = 0;
9160#endif
9161 buf_T *buf;
9162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009163 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009165 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166#ifdef FEAT_WINDOWS
9167 for (wp = firstwin; wp; wp = wp->w_next)
9168 {
9169 ++winnr;
9170 if (wp->w_buffer == buf)
9171 break;
9172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176#endif
9177 --emsg_off;
9178}
9179
9180/*
9181 * "byte2line(byte)" function
9182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009185 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190#else
9191 long boff = 0;
9192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 (linenr_T)0, &boff);
9199#endif
9200}
9201
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009202 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009203byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009204 typval_T *argvars;
9205 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009206 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009207{
9208#ifdef FEAT_MBYTE
9209 char_u *t;
9210#endif
9211 char_u *str;
9212 long idx;
9213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 str = get_tv_string_chk(&argvars[0]);
9215 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009217 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009218 return;
9219
9220#ifdef FEAT_MBYTE
9221 t = str;
9222 for ( ; idx > 0; idx--)
9223 {
9224 if (*t == NUL) /* EOL reached */
9225 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009226 if (enc_utf8 && comp)
9227 t += utf_ptr2len(t);
9228 else
9229 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009230 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009231 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009232#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009233 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009235#endif
9236}
9237
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009238/*
9239 * "byteidx()" function
9240 */
9241 static void
9242f_byteidx(argvars, rettv)
9243 typval_T *argvars;
9244 typval_T *rettv;
9245{
9246 byteidx(argvars, rettv, FALSE);
9247}
9248
9249/*
9250 * "byteidxcomp()" function
9251 */
9252 static void
9253f_byteidxcomp(argvars, rettv)
9254 typval_T *argvars;
9255 typval_T *rettv;
9256{
9257 byteidx(argvars, rettv, TRUE);
9258}
9259
Bram Moolenaardb913952012-06-29 12:54:53 +02009260 int
9261func_call(name, args, selfdict, rettv)
9262 char_u *name;
9263 typval_T *args;
9264 dict_T *selfdict;
9265 typval_T *rettv;
9266{
9267 listitem_T *item;
9268 typval_T argv[MAX_FUNC_ARGS + 1];
9269 int argc = 0;
9270 int dummy;
9271 int r = 0;
9272
9273 for (item = args->vval.v_list->lv_first; item != NULL;
9274 item = item->li_next)
9275 {
9276 if (argc == MAX_FUNC_ARGS)
9277 {
9278 EMSG(_("E699: Too many arguments"));
9279 break;
9280 }
9281 /* Make a copy of each argument. This is needed to be able to set
9282 * v_lock to VAR_FIXED in the copy without changing the original list.
9283 */
9284 copy_tv(&item->li_tv, &argv[argc++]);
9285 }
9286
9287 if (item == NULL)
9288 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9289 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9290 &dummy, TRUE, selfdict);
9291
9292 /* Free the arguments. */
9293 while (argc > 0)
9294 clear_tv(&argv[--argc]);
9295
9296 return r;
9297}
9298
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009299/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009300 * "call(func, arglist)" function
9301 */
9302 static void
9303f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 typval_T *argvars;
9305 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009306{
9307 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009308 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009309
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009310 if (argvars[1].v_type != VAR_LIST)
9311 {
9312 EMSG(_(e_listreq));
9313 return;
9314 }
9315 if (argvars[1].vval.v_list == NULL)
9316 return;
9317
9318 if (argvars[0].v_type == VAR_FUNC)
9319 func = argvars[0].vval.v_string;
9320 else
9321 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009322 if (*func == NUL)
9323 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009324
Bram Moolenaare9a41262005-01-15 22:18:47 +00009325 if (argvars[2].v_type != VAR_UNKNOWN)
9326 {
9327 if (argvars[2].v_type != VAR_DICT)
9328 {
9329 EMSG(_(e_dictreq));
9330 return;
9331 }
9332 selfdict = argvars[2].vval.v_dict;
9333 }
9334
Bram Moolenaardb913952012-06-29 12:54:53 +02009335 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009336}
9337
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009338#ifdef FEAT_FLOAT
9339/*
9340 * "ceil({float})" function
9341 */
9342 static void
9343f_ceil(argvars, rettv)
9344 typval_T *argvars;
9345 typval_T *rettv;
9346{
9347 float_T f;
9348
9349 rettv->v_type = VAR_FLOAT;
9350 if (get_float_arg(argvars, &f) == OK)
9351 rettv->vval.v_float = ceil(f);
9352 else
9353 rettv->vval.v_float = 0.0;
9354}
9355#endif
9356
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009357/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009358 * "changenr()" function
9359 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009360 static void
9361f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009362 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009363 typval_T *rettv;
9364{
9365 rettv->vval.v_number = curbuf->b_u_seq_cur;
9366}
9367
9368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 * "char2nr(string)" function
9370 */
9371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *argvars;
9374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376#ifdef FEAT_MBYTE
9377 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009378 {
9379 int utf8 = 0;
9380
9381 if (argvars[1].v_type != VAR_UNKNOWN)
9382 utf8 = get_tv_number_chk(&argvars[1], NULL);
9383
9384 if (utf8)
9385 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9386 else
9387 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 else
9390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392}
9393
9394/*
9395 * "cindent(lnum)" function
9396 */
9397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
9402#ifdef FEAT_CINDENT
9403 pos_T pos;
9404 linenr_T lnum;
9405
9406 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9409 {
9410 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 curwin->w_cursor = pos;
9413 }
9414 else
9415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417}
9418
9419/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009420 * "clearmatches()" function
9421 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009422 static void
9423f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009424 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009425 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009426{
9427#ifdef FEAT_SEARCH_EXTRA
9428 clear_matches(curwin);
9429#endif
9430}
9431
9432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 * "col(string)" function
9434 */
9435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 typval_T *argvars;
9438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440 colnr_T col = 0;
9441 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009442 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009444 fp = var2fpos(&argvars[0], FALSE, &fnum);
9445 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 if (fp->col == MAXCOL)
9448 {
9449 /* '> can be MAXCOL, get the length of the line then */
9450 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009451 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
9453 col = MAXCOL;
9454 }
9455 else
9456 {
9457 col = fp->col + 1;
9458#ifdef FEAT_VIRTUALEDIT
9459 /* col(".") when the cursor is on the NUL at the end of the line
9460 * because of "coladd" can be seen as an extra column. */
9461 if (virtual_active() && fp == &curwin->w_cursor)
9462 {
9463 char_u *p = ml_get_cursor();
9464
9465 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9466 curwin->w_virtcol - curwin->w_cursor.coladd))
9467 {
9468# ifdef FEAT_MBYTE
9469 int l;
9470
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009471 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 col += l;
9473# else
9474 if (*p != NUL && p[1] == NUL)
9475 ++col;
9476# endif
9477 }
9478 }
9479#endif
9480 }
9481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
Bram Moolenaar572cb562005-08-05 21:35:02 +00009485#if defined(FEAT_INS_EXPAND)
9486/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009487 * "complete()" function
9488 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009489 static void
9490f_complete(argvars, rettv)
9491 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009492 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009493{
9494 int startcol;
9495
9496 if ((State & INSERT) == 0)
9497 {
9498 EMSG(_("E785: complete() can only be used in Insert mode"));
9499 return;
9500 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009501
9502 /* Check for undo allowed here, because if something was already inserted
9503 * the line was already saved for undo and this check isn't done. */
9504 if (!undo_allowed())
9505 return;
9506
Bram Moolenaarade00832006-03-10 21:46:58 +00009507 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9508 {
9509 EMSG(_(e_invarg));
9510 return;
9511 }
9512
9513 startcol = get_tv_number_chk(&argvars[0], NULL);
9514 if (startcol <= 0)
9515 return;
9516
9517 set_completion(startcol - 1, argvars[1].vval.v_list);
9518}
9519
9520/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009521 * "complete_add()" function
9522 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009523 static void
9524f_complete_add(argvars, rettv)
9525 typval_T *argvars;
9526 typval_T *rettv;
9527{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009528 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009529}
9530
9531/*
9532 * "complete_check()" function
9533 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009534 static void
9535f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009536 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009537 typval_T *rettv;
9538{
9539 int saved = RedrawingDisabled;
9540
9541 RedrawingDisabled = 0;
9542 ins_compl_check_keys(0);
9543 rettv->vval.v_number = compl_interrupted;
9544 RedrawingDisabled = saved;
9545}
9546#endif
9547
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548/*
9549 * "confirm(message, buttons[, default [, type]])" function
9550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009553 typval_T *argvars UNUSED;
9554 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9557 char_u *message;
9558 char_u *buttons = NULL;
9559 char_u buf[NUMBUFLEN];
9560 char_u buf2[NUMBUFLEN];
9561 int def = 1;
9562 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 char_u *typestr;
9564 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 message = get_tv_string_chk(&argvars[0]);
9567 if (message == NULL)
9568 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9572 if (buttons == NULL)
9573 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009574 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009577 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9580 if (typestr == NULL)
9581 error = TRUE;
9582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 switch (TOUPPER_ASC(*typestr))
9585 {
9586 case 'E': type = VIM_ERROR; break;
9587 case 'Q': type = VIM_QUESTION; break;
9588 case 'I': type = VIM_INFO; break;
9589 case 'W': type = VIM_WARNING; break;
9590 case 'G': type = VIM_GENERIC; break;
9591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 }
9593 }
9594 }
9595 }
9596
9597 if (buttons == NULL || *buttons == NUL)
9598 buttons = (char_u *)_("&Ok");
9599
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009600 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009602 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#endif
9604}
9605
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009606/*
9607 * "copy()" function
9608 */
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009614 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009617#ifdef FEAT_FLOAT
9618/*
9619 * "cos()" function
9620 */
9621 static void
9622f_cos(argvars, rettv)
9623 typval_T *argvars;
9624 typval_T *rettv;
9625{
9626 float_T f;
9627
9628 rettv->v_type = VAR_FLOAT;
9629 if (get_float_arg(argvars, &f) == OK)
9630 rettv->vval.v_float = cos(f);
9631 else
9632 rettv->vval.v_float = 0.0;
9633}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009634
9635/*
9636 * "cosh()" function
9637 */
9638 static void
9639f_cosh(argvars, rettv)
9640 typval_T *argvars;
9641 typval_T *rettv;
9642{
9643 float_T f;
9644
9645 rettv->v_type = VAR_FLOAT;
9646 if (get_float_arg(argvars, &f) == OK)
9647 rettv->vval.v_float = cosh(f);
9648 else
9649 rettv->vval.v_float = 0.0;
9650}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009651#endif
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009654 * "count()" function
9655 */
9656 static void
9657f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009658 typval_T *argvars;
9659 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009660{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009661 long n = 0;
9662 int ic = FALSE;
9663
Bram Moolenaare9a41262005-01-15 22:18:47 +00009664 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009666 listitem_T *li;
9667 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009669
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 if ((l = argvars[0].vval.v_list) != NULL)
9671 {
9672 li = l->lv_first;
9673 if (argvars[2].v_type != VAR_UNKNOWN)
9674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009675 int error = FALSE;
9676
9677 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 if (argvars[3].v_type != VAR_UNKNOWN)
9679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009680 idx = get_tv_number_chk(&argvars[3], &error);
9681 if (!error)
9682 {
9683 li = list_find(l, idx);
9684 if (li == NULL)
9685 EMSGN(_(e_listidx), idx);
9686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009687 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 if (error)
9689 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 }
9691
9692 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009693 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009694 ++n;
9695 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009696 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009697 else if (argvars[0].v_type == VAR_DICT)
9698 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009699 int todo;
9700 dict_T *d;
9701 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009702
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009703 if ((d = argvars[0].vval.v_dict) != NULL)
9704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009705 int error = FALSE;
9706
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 if (argvars[2].v_type != VAR_UNKNOWN)
9708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009710 if (argvars[3].v_type != VAR_UNKNOWN)
9711 EMSG(_(e_invarg));
9712 }
9713
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009714 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009716 {
9717 if (!HASHITEM_EMPTY(hi))
9718 {
9719 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009720 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009721 ++n;
9722 }
9723 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009724 }
9725 }
9726 else
9727 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009728 rettv->vval.v_number = n;
9729}
9730
9731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9733 *
9734 * Checks the existence of a cscope connection.
9735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009738 typval_T *argvars UNUSED;
9739 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741#ifdef FEAT_CSCOPE
9742 int num = 0;
9743 char_u *dbpath = NULL;
9744 char_u *prepend = NULL;
9745 char_u buf[NUMBUFLEN];
9746
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009747 if (argvars[0].v_type != VAR_UNKNOWN
9748 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 num = (int)get_tv_number(&argvars[0]);
9751 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009752 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 }
9755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#endif
9758}
9759
9760/*
9761 * "cursor(lnum, col)" function
9762 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009763 * Moves the cursor to the specified line and column.
9764 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 typval_T *argvars;
9769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009772#ifdef FEAT_VIRTUALEDIT
9773 long coladd = 0;
9774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009776 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009777 if (argvars[1].v_type == VAR_UNKNOWN)
9778 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009779 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009780
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009781 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009782 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009783 line = pos.lnum;
9784 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009785#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009786 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009787#endif
9788 }
9789 else
9790 {
9791 line = get_tv_lnum(argvars);
9792 col = get_tv_number_chk(&argvars[1], NULL);
9793#ifdef FEAT_VIRTUALEDIT
9794 if (argvars[2].v_type != VAR_UNKNOWN)
9795 coladd = get_tv_number_chk(&argvars[2], NULL);
9796#endif
9797 }
9798 if (line < 0 || col < 0
9799#ifdef FEAT_VIRTUALEDIT
9800 || coladd < 0
9801#endif
9802 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (line > 0)
9805 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (col > 0)
9807 curwin->w_cursor.col = col - 1;
9808#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009809 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#endif
9811
9812 /* Make sure the cursor is in a valid position. */
9813 check_cursor();
9814#ifdef FEAT_MBYTE
9815 /* Correct cursor for multi-byte character. */
9816 if (has_mbyte)
9817 mb_adjust_cursor();
9818#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009819
9820 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009821 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822}
9823
9824/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009825 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 */
9827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009829 typval_T *argvars;
9830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009832 int noref = 0;
9833
9834 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009835 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009836 if (noref < 0 || noref > 1)
9837 EMSG(_(e_invarg));
9838 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009839 {
9840 current_copyID += COPYID_INC;
9841 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843}
9844
9845/*
9846 * "delete()" function
9847 */
9848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009850 typval_T *argvars;
9851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857}
9858
9859/*
9860 * "did_filetype()" function
9861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009864 typval_T *argvars UNUSED;
9865 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869#endif
9870}
9871
9872/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009873 * "diff_filler()" function
9874 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009877 typval_T *argvars UNUSED;
9878 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009879{
9880#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882#endif
9883}
9884
9885/*
9886 * "diff_hlID()" function
9887 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009890 typval_T *argvars UNUSED;
9891 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009892{
9893#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009895 static linenr_T prev_lnum = 0;
9896 static int changedtick = 0;
9897 static int fnum = 0;
9898 static int change_start = 0;
9899 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009900 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901 int filler_lines;
9902 int col;
9903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 if (lnum < 0) /* ignore type error in {lnum} arg */
9905 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009906 if (lnum != prev_lnum
9907 || changedtick != curbuf->b_changedtick
9908 || fnum != curbuf->b_fnum)
9909 {
9910 /* New line, buffer, change: need to get the values. */
9911 filler_lines = diff_check(curwin, lnum);
9912 if (filler_lines < 0)
9913 {
9914 if (filler_lines == -1)
9915 {
9916 change_start = MAXCOL;
9917 change_end = -1;
9918 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9919 hlID = HLF_ADD; /* added line */
9920 else
9921 hlID = HLF_CHD; /* changed line */
9922 }
9923 else
9924 hlID = HLF_ADD; /* added line */
9925 }
9926 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009927 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009928 prev_lnum = lnum;
9929 changedtick = curbuf->b_changedtick;
9930 fnum = curbuf->b_fnum;
9931 }
9932
9933 if (hlID == HLF_CHD || hlID == HLF_TXD)
9934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009936 if (col >= change_start && col <= change_end)
9937 hlID = HLF_TXD; /* changed text */
9938 else
9939 hlID = HLF_CHD; /* changed line */
9940 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009941 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009942#endif
9943}
9944
9945/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009946 * "empty({expr})" function
9947 */
9948 static void
9949f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009950 typval_T *argvars;
9951 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009952{
9953 int n;
9954
9955 switch (argvars[0].v_type)
9956 {
9957 case VAR_STRING:
9958 case VAR_FUNC:
9959 n = argvars[0].vval.v_string == NULL
9960 || *argvars[0].vval.v_string == NUL;
9961 break;
9962 case VAR_NUMBER:
9963 n = argvars[0].vval.v_number == 0;
9964 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009965#ifdef FEAT_FLOAT
9966 case VAR_FLOAT:
9967 n = argvars[0].vval.v_float == 0.0;
9968 break;
9969#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009970 case VAR_LIST:
9971 n = argvars[0].vval.v_list == NULL
9972 || argvars[0].vval.v_list->lv_first == NULL;
9973 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 case VAR_DICT:
9975 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009978 default:
9979 EMSG2(_(e_intern2), "f_empty()");
9980 n = 0;
9981 }
9982
9983 rettv->vval.v_number = n;
9984}
9985
9986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 * "escape({string}, {chars})" function
9988 */
9989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009991 typval_T *argvars;
9992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 char_u buf[NUMBUFLEN];
9995
Bram Moolenaar758711c2005-02-02 23:11:38 +00009996 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9997 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010002 * "eval()" function
10003 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010004 static void
10005f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010006 typval_T *argvars;
10007 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010008{
10009 char_u *s;
10010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010011 s = get_tv_string_chk(&argvars[0]);
10012 if (s != NULL)
10013 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10016 {
10017 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010018 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010020 else if (*s != NUL)
10021 EMSG(_(e_trailing));
10022}
10023
10024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 * "eventhandler()" function
10026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010029 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033}
10034
10035/*
10036 * "executable()" function
10037 */
10038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *argvars;
10041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044}
10045
10046/*
10047 * "exists()" function
10048 */
10049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010051 typval_T *argvars;
10052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 char_u *p;
10055 char_u *name;
10056 int n = FALSE;
10057 int len = 0;
10058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 if (*p == '$') /* environment variable */
10061 {
10062 /* first try "normal" environment variables (fast) */
10063 if (mch_getenv(p + 1) != NULL)
10064 n = TRUE;
10065 else
10066 {
10067 /* try expanding things like $VIM and ${HOME} */
10068 p = expand_env_save(p);
10069 if (p != NULL && *p != '$')
10070 n = TRUE;
10071 vim_free(p);
10072 }
10073 }
10074 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010076 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010077 if (*skipwhite(p) != NUL)
10078 n = FALSE; /* trailing garbage */
10079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 else if (*p == '*') /* internal or user defined function */
10081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010082 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 }
10084 else if (*p == ':')
10085 {
10086 n = cmd_exists(p + 1);
10087 }
10088 else if (*p == '#')
10089 {
10090#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010091 if (p[1] == '#')
10092 n = autocmd_supported(p + 2);
10093 else
10094 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095#endif
10096 }
10097 else /* internal variable */
10098 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010099 char_u *tofree;
10100 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010102 /* get_name_len() takes care of expanding curly braces */
10103 name = p;
10104 len = get_name_len(&p, &tofree, TRUE, FALSE);
10105 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010107 if (tofree != NULL)
10108 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010109 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010110 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010112 /* handle d.key, l[idx], f(expr) */
10113 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10114 if (n)
10115 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 }
10117 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010118 if (*p != NUL)
10119 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010121 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 }
10123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125}
10126
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010127#ifdef FEAT_FLOAT
10128/*
10129 * "exp()" function
10130 */
10131 static void
10132f_exp(argvars, rettv)
10133 typval_T *argvars;
10134 typval_T *rettv;
10135{
10136 float_T f;
10137
10138 rettv->v_type = VAR_FLOAT;
10139 if (get_float_arg(argvars, &f) == OK)
10140 rettv->vval.v_float = exp(f);
10141 else
10142 rettv->vval.v_float = 0.0;
10143}
10144#endif
10145
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146/*
10147 * "expand()" function
10148 */
10149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 typval_T *argvars;
10152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
10154 char_u *s;
10155 int len;
10156 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010157 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010160 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010163 if (argvars[1].v_type != VAR_UNKNOWN
10164 && argvars[2].v_type != VAR_UNKNOWN
10165 && get_tv_number_chk(&argvars[2], &error)
10166 && !error)
10167 {
10168 rettv->v_type = VAR_LIST;
10169 rettv->vval.v_list = NULL;
10170 }
10171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 if (*s == '%' || *s == '#' || *s == '<')
10174 {
10175 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010176 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010178 if (rettv->v_type == VAR_LIST)
10179 {
10180 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10181 list_append_string(rettv->vval.v_list, result, -1);
10182 else
10183 vim_free(result);
10184 }
10185 else
10186 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 }
10188 else
10189 {
10190 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010191 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 if (argvars[1].v_type != VAR_UNKNOWN
10193 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010194 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 if (!error)
10196 {
10197 ExpandInit(&xpc);
10198 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010199 if (p_wic)
10200 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010201 if (rettv->v_type == VAR_STRING)
10202 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10203 options, WILD_ALL);
10204 else if (rettv_list_alloc(rettv) != FAIL)
10205 {
10206 int i;
10207
10208 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10209 for (i = 0; i < xpc.xp_numfiles; i++)
10210 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10211 ExpandCleanup(&xpc);
10212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 }
10214 else
10215 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 }
10217}
10218
10219/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010220 * Go over all entries in "d2" and add them to "d1".
10221 * When "action" is "error" then a duplicate key is an error.
10222 * When "action" is "force" then a duplicate key is overwritten.
10223 * Otherwise duplicate keys are ignored ("action" is "keep").
10224 */
10225 void
10226dict_extend(d1, d2, action)
10227 dict_T *d1;
10228 dict_T *d2;
10229 char_u *action;
10230{
10231 dictitem_T *di1;
10232 hashitem_T *hi2;
10233 int todo;
10234
10235 todo = (int)d2->dv_hashtab.ht_used;
10236 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10237 {
10238 if (!HASHITEM_EMPTY(hi2))
10239 {
10240 --todo;
10241 di1 = dict_find(d1, hi2->hi_key, -1);
10242 if (d1->dv_scope != 0)
10243 {
10244 /* Disallow replacing a builtin function in l: and g:.
10245 * Check the key to be valid when adding to any
10246 * scope. */
10247 if (d1->dv_scope == VAR_DEF_SCOPE
10248 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10249 && var_check_func_name(hi2->hi_key,
10250 di1 == NULL))
10251 break;
10252 if (!valid_varname(hi2->hi_key))
10253 break;
10254 }
10255 if (di1 == NULL)
10256 {
10257 di1 = dictitem_copy(HI2DI(hi2));
10258 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10259 dictitem_free(di1);
10260 }
10261 else if (*action == 'e')
10262 {
10263 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10264 break;
10265 }
10266 else if (*action == 'f' && HI2DI(hi2) != di1)
10267 {
10268 clear_tv(&di1->di_tv);
10269 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10270 }
10271 }
10272 }
10273}
10274
10275/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010278 */
10279 static void
10280f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010281 typval_T *argvars;
10282 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010283{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010284 char *arg_errmsg = N_("extend() argument");
10285
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010287 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 list_T *l1, *l2;
10289 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010292
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 l1 = argvars[0].vval.v_list;
10294 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010295 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010296 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010297 {
10298 if (argvars[2].v_type != VAR_UNKNOWN)
10299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 before = get_tv_number_chk(&argvars[2], &error);
10301 if (error)
10302 return; /* type error; errmsg already given */
10303
Bram Moolenaar758711c2005-02-02 23:11:38 +000010304 if (before == l1->lv_len)
10305 item = NULL;
10306 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010308 item = list_find(l1, before);
10309 if (item == NULL)
10310 {
10311 EMSGN(_(e_listidx), before);
10312 return;
10313 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 }
10315 }
10316 else
10317 item = NULL;
10318 list_extend(l1, l2, item);
10319
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 copy_tv(&argvars[0], rettv);
10321 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10324 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010325 dict_T *d1, *d2;
10326 char_u *action;
10327 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010328
10329 d1 = argvars[0].vval.v_dict;
10330 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010331 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010332 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010333 {
10334 /* Check the third argument. */
10335 if (argvars[2].v_type != VAR_UNKNOWN)
10336 {
10337 static char *(av[]) = {"keep", "force", "error"};
10338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 action = get_tv_string_chk(&argvars[2]);
10340 if (action == NULL)
10341 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 for (i = 0; i < 3; ++i)
10343 if (STRCMP(action, av[i]) == 0)
10344 break;
10345 if (i == 3)
10346 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010347 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 return;
10349 }
10350 }
10351 else
10352 action = (char_u *)"force";
10353
Bram Moolenaara9922d62013-05-30 13:01:18 +020010354 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356 copy_tv(&argvars[0], rettv);
10357 }
10358 }
10359 else
10360 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010361}
10362
10363/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010364 * "feedkeys()" function
10365 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010366 static void
10367f_feedkeys(argvars, rettv)
10368 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010369 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010370{
10371 int remap = TRUE;
10372 char_u *keys, *flags;
10373 char_u nbuf[NUMBUFLEN];
10374 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010375 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010376
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010377 /* This is not allowed in the sandbox. If the commands would still be
10378 * executed in the sandbox it would be OK, but it probably happens later,
10379 * when "sandbox" is no longer set. */
10380 if (check_secure())
10381 return;
10382
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010383 keys = get_tv_string(&argvars[0]);
10384 if (*keys != NUL)
10385 {
10386 if (argvars[1].v_type != VAR_UNKNOWN)
10387 {
10388 flags = get_tv_string_buf(&argvars[1], nbuf);
10389 for ( ; *flags != NUL; ++flags)
10390 {
10391 switch (*flags)
10392 {
10393 case 'n': remap = FALSE; break;
10394 case 'm': remap = TRUE; break;
10395 case 't': typed = TRUE; break;
10396 }
10397 }
10398 }
10399
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010400 /* Need to escape K_SPECIAL and CSI before putting the string in the
10401 * typeahead buffer. */
10402 keys_esc = vim_strsave_escape_csi(keys);
10403 if (keys_esc != NULL)
10404 {
10405 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010406 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010407 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010408 if (vgetc_busy)
10409 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010410 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010411 }
10412}
10413
10414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 * "filereadable()" function
10416 */
10417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 typval_T *argvars;
10420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010422 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 char_u *p;
10424 int n;
10425
Bram Moolenaarc236c162008-07-13 17:41:49 +000010426#ifndef O_NONBLOCK
10427# define O_NONBLOCK 0
10428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010430 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10431 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 {
10433 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010434 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 }
10436 else
10437 n = FALSE;
10438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440}
10441
10442/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010443 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 * rights to write into.
10445 */
10446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010448 typval_T *argvars;
10449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010451 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452}
10453
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010454static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010455
10456 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010457findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010458 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010459 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010460 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010461{
10462#ifdef FEAT_SEARCHPATH
10463 char_u *fname;
10464 char_u *fresult = NULL;
10465 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10466 char_u *p;
10467 char_u pathbuf[NUMBUFLEN];
10468 int count = 1;
10469 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010470 int error = FALSE;
10471#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010472
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010473 rettv->vval.v_string = NULL;
10474 rettv->v_type = VAR_STRING;
10475
10476#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010479 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10482 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010483 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 else
10485 {
10486 if (*p != NUL)
10487 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010490 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010491 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010492 }
10493
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010494 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10495 error = TRUE;
10496
10497 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 do
10500 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010501 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010502 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 fresult = find_file_in_path_option(first ? fname : NULL,
10504 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010505 0, first, path,
10506 find_what,
10507 curbuf->b_ffname,
10508 find_what == FINDFILE_DIR
10509 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010511
10512 if (fresult != NULL && rettv->v_type == VAR_LIST)
10513 list_append_string(rettv->vval.v_list, fresult, -1);
10514
10515 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010517
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010518 if (rettv->v_type == VAR_STRING)
10519 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010520#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010521}
10522
Bram Moolenaar33570922005-01-25 22:26:29 +000010523static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10524static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010525
10526/*
10527 * Implementation of map() and filter().
10528 */
10529 static void
10530filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010531 typval_T *argvars;
10532 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010533 int map;
10534{
10535 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010536 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 listitem_T *li, *nli;
10538 list_T *l = NULL;
10539 dictitem_T *di;
10540 hashtab_T *ht;
10541 hashitem_T *hi;
10542 dict_T *d = NULL;
10543 typval_T save_val;
10544 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010545 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010546 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010547 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10548 char *arg_errmsg = (map ? N_("map() argument")
10549 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010550 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010551 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010552
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010554 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010555 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010556 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 return;
10558 }
10559 else if (argvars[0].v_type == VAR_DICT)
10560 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010561 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010562 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 return;
10564 }
10565 else
10566 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010567 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 return;
10569 }
10570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 expr = get_tv_string_buf_chk(&argvars[1], buf);
10572 /* On type errors, the preceding call has already displayed an error
10573 * message. Avoid a misleading error message for an empty string that
10574 * was not passed as argument. */
10575 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 prepare_vimvar(VV_VAL, &save_val);
10578 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010579
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010580 /* We reset "did_emsg" to be able to detect whether an error
10581 * occurred during evaluation of the expression. */
10582 save_did_emsg = did_emsg;
10583 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010584
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010585 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010588 vimvars[VV_KEY].vv_type = VAR_STRING;
10589
10590 ht = &d->dv_hashtab;
10591 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010592 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 if (!HASHITEM_EMPTY(hi))
10596 {
10597 --todo;
10598 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010599 if (tv_check_lock(di->di_tv.v_lock,
10600 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 break;
10602 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010603 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010604 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 break;
10606 if (!map && rem)
10607 dictitem_remove(d, di);
10608 clear_tv(&vimvars[VV_KEY].vv_tv);
10609 }
10610 }
10611 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 }
10613 else
10614 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010615 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 for (li = l->lv_first; li != NULL; li = nli)
10618 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010619 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010620 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010622 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010623 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010624 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010625 break;
10626 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010628 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010629 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010631
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010632 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010634
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010635 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637
10638 copy_tv(&argvars[0], rettv);
10639}
10640
10641 static int
10642filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010643 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010644 char_u *expr;
10645 int map;
10646 int *remp;
10647{
Bram Moolenaar33570922005-01-25 22:26:29 +000010648 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010649 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010650 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010651
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 s = expr;
10654 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010655 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010656 if (*s != NUL) /* check for trailing chars after expr */
10657 {
10658 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010659 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010660 }
10661 if (map)
10662 {
10663 /* map(): replace the list item value */
10664 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010665 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 *tv = rettv;
10667 }
10668 else
10669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 int error = FALSE;
10671
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 /* On type error, nothing has been removed; return FAIL to stop the
10676 * loop. The error message was given by get_tv_number_chk(). */
10677 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010678 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010680 retval = OK;
10681theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010682 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010683 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010684}
10685
10686/*
10687 * "filter()" function
10688 */
10689 static void
10690f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 typval_T *argvars;
10692 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010693{
10694 filter_map(argvars, rettv, FALSE);
10695}
10696
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010698 * "finddir({fname}[, {path}[, {count}]])" function
10699 */
10700 static void
10701f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010702 typval_T *argvars;
10703 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010705 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010706}
10707
10708/*
10709 * "findfile({fname}[, {path}[, {count}]])" function
10710 */
10711 static void
10712f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *argvars;
10714 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010715{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010716 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010717}
10718
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010719#ifdef FEAT_FLOAT
10720/*
10721 * "float2nr({float})" function
10722 */
10723 static void
10724f_float2nr(argvars, rettv)
10725 typval_T *argvars;
10726 typval_T *rettv;
10727{
10728 float_T f;
10729
10730 if (get_float_arg(argvars, &f) == OK)
10731 {
10732 if (f < -0x7fffffff)
10733 rettv->vval.v_number = -0x7fffffff;
10734 else if (f > 0x7fffffff)
10735 rettv->vval.v_number = 0x7fffffff;
10736 else
10737 rettv->vval.v_number = (varnumber_T)f;
10738 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010739}
10740
10741/*
10742 * "floor({float})" function
10743 */
10744 static void
10745f_floor(argvars, rettv)
10746 typval_T *argvars;
10747 typval_T *rettv;
10748{
10749 float_T f;
10750
10751 rettv->v_type = VAR_FLOAT;
10752 if (get_float_arg(argvars, &f) == OK)
10753 rettv->vval.v_float = floor(f);
10754 else
10755 rettv->vval.v_float = 0.0;
10756}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010757
10758/*
10759 * "fmod()" function
10760 */
10761 static void
10762f_fmod(argvars, rettv)
10763 typval_T *argvars;
10764 typval_T *rettv;
10765{
10766 float_T fx, fy;
10767
10768 rettv->v_type = VAR_FLOAT;
10769 if (get_float_arg(argvars, &fx) == OK
10770 && get_float_arg(&argvars[1], &fy) == OK)
10771 rettv->vval.v_float = fmod(fx, fy);
10772 else
10773 rettv->vval.v_float = 0.0;
10774}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010775#endif
10776
Bram Moolenaar0d660222005-01-07 21:51:51 +000010777/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010778 * "fnameescape({string})" function
10779 */
10780 static void
10781f_fnameescape(argvars, rettv)
10782 typval_T *argvars;
10783 typval_T *rettv;
10784{
10785 rettv->vval.v_string = vim_strsave_fnameescape(
10786 get_tv_string(&argvars[0]), FALSE);
10787 rettv->v_type = VAR_STRING;
10788}
10789
10790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 * "fnamemodify({fname}, {mods})" function
10792 */
10793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010795 typval_T *argvars;
10796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797{
10798 char_u *fname;
10799 char_u *mods;
10800 int usedlen = 0;
10801 int len;
10802 char_u *fbuf = NULL;
10803 char_u buf[NUMBUFLEN];
10804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 fname = get_tv_string_chk(&argvars[0]);
10806 mods = get_tv_string_buf_chk(&argvars[1], buf);
10807 if (fname == NULL || mods == NULL)
10808 fname = NULL;
10809 else
10810 {
10811 len = (int)STRLEN(fname);
10812 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 vim_free(fbuf);
10821}
10822
Bram Moolenaar33570922005-01-25 22:26:29 +000010823static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824
10825/*
10826 * "foldclosed()" function
10827 */
10828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010832 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834#ifdef FEAT_FOLDING
10835 linenr_T lnum;
10836 linenr_T first, last;
10837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10840 {
10841 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10842 {
10843 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 return;
10848 }
10849 }
10850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852}
10853
10854/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010855 * "foldclosed()" function
10856 */
10857 static void
10858f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010859 typval_T *argvars;
10860 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010861{
10862 foldclosed_both(argvars, rettv, FALSE);
10863}
10864
10865/*
10866 * "foldclosedend()" function
10867 */
10868 static void
10869f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 typval_T *argvars;
10871 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010872{
10873 foldclosed_both(argvars, rettv, TRUE);
10874}
10875
10876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 * "foldlevel()" function
10878 */
10879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010881 typval_T *argvars UNUSED;
10882 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883{
10884#ifdef FEAT_FOLDING
10885 linenr_T lnum;
10886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010889 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892
10893/*
10894 * "foldtext()" function
10895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010898 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901#ifdef FEAT_FOLDING
10902 linenr_T lnum;
10903 char_u *s;
10904 char_u *r;
10905 int len;
10906 char *txt;
10907#endif
10908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->v_type = VAR_STRING;
10910 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010912 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10913 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10914 <= curbuf->b_ml.ml_line_count
10915 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 {
10917 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010918 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10919 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 {
10921 if (!linewhite(lnum))
10922 break;
10923 ++lnum;
10924 }
10925
10926 /* Find interesting text in this line. */
10927 s = skipwhite(ml_get(lnum));
10928 /* skip C comment-start */
10929 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010932 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010933 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010934 {
10935 s = skipwhite(ml_get(lnum + 1));
10936 if (*s == '*')
10937 s = skipwhite(s + 1);
10938 }
10939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 txt = _("+-%s%3ld lines: ");
10941 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010942 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 + 20 /* for %3ld */
10944 + STRLEN(s))); /* concatenated */
10945 if (r != NULL)
10946 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10948 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10949 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 len = (int)STRLEN(r);
10951 STRCAT(r, s);
10952 /* remove 'foldmarker' and 'commentstring' */
10953 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 }
10956 }
10957#endif
10958}
10959
10960/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010961 * "foldtextresult(lnum)" function
10962 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010967{
10968#ifdef FEAT_FOLDING
10969 linenr_T lnum;
10970 char_u *text;
10971 char_u buf[51];
10972 foldinfo_T foldinfo;
10973 int fold_count;
10974#endif
10975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976 rettv->v_type = VAR_STRING;
10977 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010978#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 /* treat illegal types and illegal string values for {lnum} the same */
10981 if (lnum < 0)
10982 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010983 fold_count = foldedCount(curwin, lnum, &foldinfo);
10984 if (fold_count > 0)
10985 {
10986 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10987 &foldinfo, buf);
10988 if (text == buf)
10989 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010991 }
10992#endif
10993}
10994
10995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 * "foreground()" function
10997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011000 typval_T *argvars UNUSED;
11001 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003#ifdef FEAT_GUI
11004 if (gui.in_use)
11005 gui_mch_set_foreground();
11006#else
11007# ifdef WIN32
11008 win32_set_foreground();
11009# endif
11010#endif
11011}
11012
11013/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011014 * "function()" function
11015 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011018 typval_T *argvars;
11019 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011020{
11021 char_u *s;
11022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011024 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011025 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011026 /* Don't check an autoload name for existence here. */
11027 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011028 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011029 else
11030 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011031 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011032 {
11033 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011034 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011035
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011036 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11037 * also be called from another script. Using trans_function_name()
11038 * would also work, but some plugins depend on the name being
11039 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011040 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011041 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011042 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011043 if (rettv->vval.v_string != NULL)
11044 {
11045 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011046 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011047 }
11048 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011049 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011050 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011052 }
11053}
11054
11055/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011056 * "garbagecollect()" function
11057 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011058 static void
11059f_garbagecollect(argvars, rettv)
11060 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011061 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011062{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011063 /* This is postponed until we are back at the toplevel, because we may be
11064 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11065 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011066
11067 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11068 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011069}
11070
11071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011072 * "get()" function
11073 */
11074 static void
11075f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011076 typval_T *argvars;
11077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078{
Bram Moolenaar33570922005-01-25 22:26:29 +000011079 listitem_T *li;
11080 list_T *l;
11081 dictitem_T *di;
11082 dict_T *d;
11083 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011084
Bram Moolenaare9a41262005-01-15 22:18:47 +000011085 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011086 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011087 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 int error = FALSE;
11090
11091 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11092 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011094 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011095 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011096 else if (argvars[0].v_type == VAR_DICT)
11097 {
11098 if ((d = argvars[0].vval.v_dict) != NULL)
11099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011100 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 if (di != NULL)
11102 tv = &di->di_tv;
11103 }
11104 }
11105 else
11106 EMSG2(_(e_listdictarg), "get()");
11107
11108 if (tv == NULL)
11109 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011110 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 copy_tv(&argvars[2], rettv);
11112 }
11113 else
11114 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115}
11116
Bram Moolenaar342337a2005-07-21 21:11:17 +000011117static 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 +000011118
11119/*
11120 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011121 * Return a range (from start to end) of lines in rettv from the specified
11122 * buffer.
11123 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011124 */
11125 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011126get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011127 buf_T *buf;
11128 linenr_T start;
11129 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011130 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011131 typval_T *rettv;
11132{
11133 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011134
Bram Moolenaar959a1432013-12-14 12:17:38 +010011135 rettv->v_type = VAR_STRING;
11136 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011137 if (retlist && rettv_list_alloc(rettv) == FAIL)
11138 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011139
11140 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11141 return;
11142
11143 if (!retlist)
11144 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011145 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11146 p = ml_get_buf(buf, start, FALSE);
11147 else
11148 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011149 rettv->vval.v_string = vim_strsave(p);
11150 }
11151 else
11152 {
11153 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154 return;
11155
11156 if (start < 1)
11157 start = 1;
11158 if (end > buf->b_ml.ml_line_count)
11159 end = buf->b_ml.ml_line_count;
11160 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011161 if (list_append_string(rettv->vval.v_list,
11162 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011163 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011164 }
11165}
11166
11167/*
11168 * "getbufline()" function
11169 */
11170 static void
11171f_getbufline(argvars, rettv)
11172 typval_T *argvars;
11173 typval_T *rettv;
11174{
11175 linenr_T lnum;
11176 linenr_T end;
11177 buf_T *buf;
11178
11179 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11180 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011181 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011182 --emsg_off;
11183
Bram Moolenaar661b1822005-07-28 22:36:45 +000011184 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011185 if (argvars[2].v_type == VAR_UNKNOWN)
11186 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011187 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011188 end = get_tv_lnum_buf(&argvars[2], buf);
11189
Bram Moolenaar342337a2005-07-21 21:11:17 +000011190 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011191}
11192
Bram Moolenaar0d660222005-01-07 21:51:51 +000011193/*
11194 * "getbufvar()" function
11195 */
11196 static void
11197f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011198 typval_T *argvars;
11199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011200{
11201 buf_T *buf;
11202 buf_T *save_curbuf;
11203 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011204 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011205 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11208 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011209 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011210 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011212 rettv->v_type = VAR_STRING;
11213 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011214
11215 if (buf != NULL && varname != NULL)
11216 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011217 /* set curbuf to be our buf, temporarily */
11218 save_curbuf = curbuf;
11219 curbuf = buf;
11220
Bram Moolenaar0d660222005-01-07 21:51:51 +000011221 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011222 {
11223 if (get_option_tv(&varname, rettv, TRUE) == OK)
11224 done = TRUE;
11225 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011226 else if (STRCMP(varname, "changedtick") == 0)
11227 {
11228 rettv->v_type = VAR_NUMBER;
11229 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011230 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011231 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011232 else
11233 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011234 /* Look up the variable. */
11235 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11236 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11237 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011238 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011239 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011241 done = TRUE;
11242 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011244
11245 /* restore previous notion of curbuf */
11246 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247 }
11248
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011249 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11250 /* use the default value */
11251 copy_tv(&argvars[2], rettv);
11252
Bram Moolenaar0d660222005-01-07 21:51:51 +000011253 --emsg_off;
11254}
11255
11256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 * "getchar()" function
11258 */
11259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011261 typval_T *argvars;
11262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263{
11264 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011267 /* Position the cursor. Needed after a message that ends in a space. */
11268 windgoto(msg_row, msg_col);
11269
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 ++no_mapping;
11271 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011272 for (;;)
11273 {
11274 if (argvars[0].v_type == VAR_UNKNOWN)
11275 /* getchar(): blocking wait. */
11276 n = safe_vgetc();
11277 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11278 /* getchar(1): only check if char avail */
11279 n = vpeekc();
11280 else if (error || vpeekc() == NUL)
11281 /* illegal argument or getchar(0) and no char avail: return zero */
11282 n = 0;
11283 else
11284 /* getchar(0) and char avail: return char */
11285 n = safe_vgetc();
11286 if (n == K_IGNORE)
11287 continue;
11288 break;
11289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 --no_mapping;
11291 --allow_keys;
11292
Bram Moolenaar219b8702006-11-01 14:32:36 +000011293 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11294 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11295 vimvars[VV_MOUSE_COL].vv_nr = 0;
11296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 if (IS_SPECIAL(n) || mod_mask != 0)
11299 {
11300 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11301 int i = 0;
11302
11303 /* Turn a special key into three bytes, plus modifier. */
11304 if (mod_mask != 0)
11305 {
11306 temp[i++] = K_SPECIAL;
11307 temp[i++] = KS_MODIFIER;
11308 temp[i++] = mod_mask;
11309 }
11310 if (IS_SPECIAL(n))
11311 {
11312 temp[i++] = K_SPECIAL;
11313 temp[i++] = K_SECOND(n);
11314 temp[i++] = K_THIRD(n);
11315 }
11316#ifdef FEAT_MBYTE
11317 else if (has_mbyte)
11318 i += (*mb_char2bytes)(n, temp + i);
11319#endif
11320 else
11321 temp[i++] = n;
11322 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->v_type = VAR_STRING;
11324 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011325
11326#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011327 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011328 {
11329 int row = mouse_row;
11330 int col = mouse_col;
11331 win_T *win;
11332 linenr_T lnum;
11333# ifdef FEAT_WINDOWS
11334 win_T *wp;
11335# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011336 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011337
11338 if (row >= 0 && col >= 0)
11339 {
11340 /* Find the window at the mouse coordinates and compute the
11341 * text position. */
11342 win = mouse_find_win(&row, &col);
11343 (void)mouse_comp_pos(win, &row, &col, &lnum);
11344# ifdef FEAT_WINDOWS
11345 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011346 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011347# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011348 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011349 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11350 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11351 }
11352 }
11353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 }
11355}
11356
11357/*
11358 * "getcharmod()" function
11359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366}
11367
11368/*
11369 * "getcmdline()" function
11370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011373 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->v_type = VAR_STRING;
11377 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378}
11379
11380/*
11381 * "getcmdpos()" function
11382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011385 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389}
11390
11391/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011392 * "getcmdtype()" function
11393 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011394 static void
11395f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011397 typval_T *rettv;
11398{
11399 rettv->v_type = VAR_STRING;
11400 rettv->vval.v_string = alloc(2);
11401 if (rettv->vval.v_string != NULL)
11402 {
11403 rettv->vval.v_string[0] = get_cmdline_type();
11404 rettv->vval.v_string[1] = NUL;
11405 }
11406}
11407
11408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 * "getcwd()" function
11410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011416 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011419 rettv->vval.v_string = NULL;
11420 cwd = alloc(MAXPATHL);
11421 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011423 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11424 {
11425 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011427 if (rettv->vval.v_string != NULL)
11428 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011430 }
11431 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 }
11433}
11434
11435/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011436 * "getfontname()" function
11437 */
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011440 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_STRING;
11444 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011445#ifdef FEAT_GUI
11446 if (gui.in_use)
11447 {
11448 GuiFont font;
11449 char_u *name = NULL;
11450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011451 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011452 {
11453 /* Get the "Normal" font. Either the name saved by
11454 * hl_set_font_name() or from the font ID. */
11455 font = gui.norm_font;
11456 name = hl_get_font_name();
11457 }
11458 else
11459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011461 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11462 return;
11463 font = gui_mch_get_font(name, FALSE);
11464 if (font == NOFONT)
11465 return; /* Invalid font name, return empty string. */
11466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011469 gui_mch_free_font(font);
11470 }
11471#endif
11472}
11473
11474/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011475 * "getfperm({fname})" function
11476 */
11477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011479 typval_T *argvars;
11480 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481{
11482 char_u *fname;
11483 struct stat st;
11484 char_u *perm = NULL;
11485 char_u flags[] = "rwx";
11486 int i;
11487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011491 if (mch_stat((char *)fname, &st) >= 0)
11492 {
11493 perm = vim_strsave((char_u *)"---------");
11494 if (perm != NULL)
11495 {
11496 for (i = 0; i < 9; i++)
11497 {
11498 if (st.st_mode & (1 << (8 - i)))
11499 perm[i] = flags[i % 3];
11500 }
11501 }
11502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011504}
11505
11506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 * "getfsize({fname})" function
11508 */
11509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *argvars;
11512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
11514 char_u *fname;
11515 struct stat st;
11516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520
11521 if (mch_stat((char *)fname, &st) >= 0)
11522 {
11523 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011524 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011528
11529 /* non-perfect check for overflow */
11530 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11531 rettv->vval.v_number = -2;
11532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 }
11534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536}
11537
11538/*
11539 * "getftime({fname})" function
11540 */
11541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011543 typval_T *argvars;
11544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545{
11546 char_u *fname;
11547 struct stat st;
11548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550
11551 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555}
11556
11557/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011558 * "getftype({fname})" function
11559 */
11560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011564{
11565 char_u *fname;
11566 struct stat st;
11567 char_u *type = NULL;
11568 char *t;
11569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011573 if (mch_lstat((char *)fname, &st) >= 0)
11574 {
11575#ifdef S_ISREG
11576 if (S_ISREG(st.st_mode))
11577 t = "file";
11578 else if (S_ISDIR(st.st_mode))
11579 t = "dir";
11580# ifdef S_ISLNK
11581 else if (S_ISLNK(st.st_mode))
11582 t = "link";
11583# endif
11584# ifdef S_ISBLK
11585 else if (S_ISBLK(st.st_mode))
11586 t = "bdev";
11587# endif
11588# ifdef S_ISCHR
11589 else if (S_ISCHR(st.st_mode))
11590 t = "cdev";
11591# endif
11592# ifdef S_ISFIFO
11593 else if (S_ISFIFO(st.st_mode))
11594 t = "fifo";
11595# endif
11596# ifdef S_ISSOCK
11597 else if (S_ISSOCK(st.st_mode))
11598 t = "fifo";
11599# endif
11600 else
11601 t = "other";
11602#else
11603# ifdef S_IFMT
11604 switch (st.st_mode & S_IFMT)
11605 {
11606 case S_IFREG: t = "file"; break;
11607 case S_IFDIR: t = "dir"; break;
11608# ifdef S_IFLNK
11609 case S_IFLNK: t = "link"; break;
11610# endif
11611# ifdef S_IFBLK
11612 case S_IFBLK: t = "bdev"; break;
11613# endif
11614# ifdef S_IFCHR
11615 case S_IFCHR: t = "cdev"; break;
11616# endif
11617# ifdef S_IFIFO
11618 case S_IFIFO: t = "fifo"; break;
11619# endif
11620# ifdef S_IFSOCK
11621 case S_IFSOCK: t = "socket"; break;
11622# endif
11623 default: t = "other";
11624 }
11625# else
11626 if (mch_isdir(fname))
11627 t = "dir";
11628 else
11629 t = "file";
11630# endif
11631#endif
11632 type = vim_strsave((char_u *)t);
11633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011635}
11636
11637/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011638 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011639 */
11640 static void
11641f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011642 typval_T *argvars;
11643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644{
11645 linenr_T lnum;
11646 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011647 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011648
11649 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011650 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011651 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011652 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011653 retlist = FALSE;
11654 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011655 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011656 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011657 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011658 retlist = TRUE;
11659 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011660
Bram Moolenaar342337a2005-07-21 21:11:17 +000011661 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662}
11663
11664/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011665 * "getmatches()" function
11666 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011667 static void
11668f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011669 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011670 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011671{
11672#ifdef FEAT_SEARCH_EXTRA
11673 dict_T *dict;
11674 matchitem_T *cur = curwin->w_match_head;
11675
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011676 if (rettv_list_alloc(rettv) == OK)
11677 {
11678 while (cur != NULL)
11679 {
11680 dict = dict_alloc();
11681 if (dict == NULL)
11682 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011683 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11684 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11685 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11686 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11687 list_append_dict(rettv->vval.v_list, dict);
11688 cur = cur->next;
11689 }
11690 }
11691#endif
11692}
11693
11694/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011695 * "getpid()" function
11696 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011697 static void
11698f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011699 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011700 typval_T *rettv;
11701{
11702 rettv->vval.v_number = mch_get_pid();
11703}
11704
11705/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011706 * "getpos(string)" function
11707 */
11708 static void
11709f_getpos(argvars, rettv)
11710 typval_T *argvars;
11711 typval_T *rettv;
11712{
11713 pos_T *fp;
11714 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011715 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011716
11717 if (rettv_list_alloc(rettv) == OK)
11718 {
11719 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011720 fp = var2fpos(&argvars[0], TRUE, &fnum);
11721 if (fnum != -1)
11722 list_append_number(l, (varnumber_T)fnum);
11723 else
11724 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011725 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11726 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011727 list_append_number(l, (fp != NULL)
11728 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011729 : (varnumber_T)0);
11730 list_append_number(l,
11731#ifdef FEAT_VIRTUALEDIT
11732 (fp != NULL) ? (varnumber_T)fp->coladd :
11733#endif
11734 (varnumber_T)0);
11735 }
11736 else
11737 rettv->vval.v_number = FALSE;
11738}
11739
11740/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011741 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011742 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011743 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011744f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011745 typval_T *argvars UNUSED;
11746 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011747{
11748#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011749 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011750#endif
11751
Bram Moolenaar2641f772005-03-25 21:58:17 +000011752#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011753 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011754 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011755 wp = NULL;
11756 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11757 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011758 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011759 if (wp == NULL)
11760 return;
11761 }
11762
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011763 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011764 }
11765#endif
11766}
11767
11768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 * "getreg()" function
11770 */
11771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011773 typval_T *argvars;
11774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775{
11776 char_u *strregname;
11777 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011778 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011781 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 strregname = get_tv_string_chk(&argvars[0]);
11784 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011785 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011789 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 regname = (strregname == NULL ? '"' : *strregname);
11791 if (regname == 0)
11792 regname = '"';
11793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 rettv->vval.v_string = error ? NULL :
11796 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797}
11798
11799/*
11800 * "getregtype()" function
11801 */
11802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806{
11807 char_u *strregname;
11808 int regname;
11809 char_u buf[NUMBUFLEN + 2];
11810 long reglen = 0;
11811
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 {
11814 strregname = get_tv_string_chk(&argvars[0]);
11815 if (strregname == NULL) /* type error; errmsg already given */
11816 {
11817 rettv->v_type = VAR_STRING;
11818 rettv->vval.v_string = NULL;
11819 return;
11820 }
11821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 else
11823 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011824 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825
11826 regname = (strregname == NULL ? '"' : *strregname);
11827 if (regname == 0)
11828 regname = '"';
11829
11830 buf[0] = NUL;
11831 buf[1] = NUL;
11832 switch (get_reg_type(regname, &reglen))
11833 {
11834 case MLINE: buf[0] = 'V'; break;
11835 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 case MBLOCK:
11837 buf[0] = Ctrl_V;
11838 sprintf((char *)buf + 1, "%ld", reglen + 1);
11839 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 rettv->v_type = VAR_STRING;
11842 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843}
11844
11845/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011846 * "gettabvar()" function
11847 */
11848 static void
11849f_gettabvar(argvars, rettv)
11850 typval_T *argvars;
11851 typval_T *rettv;
11852{
11853 tabpage_T *tp;
11854 dictitem_T *v;
11855 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011856 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011857
11858 rettv->v_type = VAR_STRING;
11859 rettv->vval.v_string = NULL;
11860
11861 varname = get_tv_string_chk(&argvars[1]);
11862 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11863 if (tp != NULL && varname != NULL)
11864 {
11865 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011866 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011867 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011868 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011869 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011870 done = TRUE;
11871 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011872 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011873
11874 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011875 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011876}
11877
11878/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011879 * "gettabwinvar()" function
11880 */
11881 static void
11882f_gettabwinvar(argvars, rettv)
11883 typval_T *argvars;
11884 typval_T *rettv;
11885{
11886 getwinvar(argvars, rettv, 1);
11887}
11888
11889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890 * "getwinposx()" function
11891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011894 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898#ifdef FEAT_GUI
11899 if (gui.in_use)
11900 {
11901 int x, y;
11902
11903 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 }
11906#endif
11907}
11908
11909/*
11910 * "getwinposy()" function
11911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011914 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011917 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918#ifdef FEAT_GUI
11919 if (gui.in_use)
11920 {
11921 int x, y;
11922
11923 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 }
11926#endif
11927}
11928
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011929/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011930 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011931 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011932 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011933find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011934 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011935 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011936{
11937#ifdef FEAT_WINDOWS
11938 win_T *wp;
11939#endif
11940 int nr;
11941
11942 nr = get_tv_number_chk(vp, NULL);
11943
11944#ifdef FEAT_WINDOWS
11945 if (nr < 0)
11946 return NULL;
11947 if (nr == 0)
11948 return curwin;
11949
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011950 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11951 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011952 if (--nr <= 0)
11953 break;
11954 return wp;
11955#else
11956 if (nr == 0 || nr == 1)
11957 return curwin;
11958 return NULL;
11959#endif
11960}
11961
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962/*
11963 * "getwinvar()" function
11964 */
11965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011967 typval_T *argvars;
11968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011970 getwinvar(argvars, rettv, 0);
11971}
11972
11973/*
11974 * getwinvar() and gettabwinvar()
11975 */
11976 static void
11977getwinvar(argvars, rettv, off)
11978 typval_T *argvars;
11979 typval_T *rettv;
11980 int off; /* 1 for gettabwinvar() */
11981{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982 win_T *win, *oldcurwin;
11983 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011984 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011985 tabpage_T *tp = NULL;
11986 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011987 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011989#ifdef FEAT_WINDOWS
11990 if (off == 1)
11991 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11992 else
11993 tp = curtab;
11994#endif
11995 win = find_win_by_nr(&argvars[off], tp);
11996 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011999 rettv->v_type = VAR_STRING;
12000 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001
12002 if (win != NULL && varname != NULL)
12003 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012004 /* Set curwin to be our win, temporarily. Also set the tabpage,
12005 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012006 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012007
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012009 {
12010 if (get_option_tv(&varname, rettv, 1) == OK)
12011 done = TRUE;
12012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 else
12014 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012015 /* Look up the variable. */
12016 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12017 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012019 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012021 done = TRUE;
12022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012024
12025 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012026 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 }
12028
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012029 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12030 /* use the default return value */
12031 copy_tv(&argvars[off + 2], rettv);
12032
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 --emsg_off;
12034}
12035
12036/*
12037 * "glob()" function
12038 */
12039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012040f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012041 typval_T *argvars;
12042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012044 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012046 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012048 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012049 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012051 if (argvars[1].v_type != VAR_UNKNOWN)
12052 {
12053 if (get_tv_number_chk(&argvars[1], &error))
12054 options |= WILD_KEEP_ALL;
12055 if (argvars[2].v_type != VAR_UNKNOWN
12056 && get_tv_number_chk(&argvars[2], &error))
12057 {
12058 rettv->v_type = VAR_LIST;
12059 rettv->vval.v_list = NULL;
12060 }
12061 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012062 if (!error)
12063 {
12064 ExpandInit(&xpc);
12065 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012066 if (p_wic)
12067 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012068 if (rettv->v_type == VAR_STRING)
12069 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012070 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012071 else if (rettv_list_alloc(rettv) != FAIL)
12072 {
12073 int i;
12074
12075 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12076 NULL, options, WILD_ALL_KEEP);
12077 for (i = 0; i < xpc.xp_numfiles; i++)
12078 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12079
12080 ExpandCleanup(&xpc);
12081 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012082 }
12083 else
12084 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085}
12086
12087/*
12088 * "globpath()" function
12089 */
12090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012091f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012092 typval_T *argvars;
12093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012095 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012097 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012098 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012100 /* When the optional second argument is non-zero, don't remove matches
12101 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12102 if (argvars[2].v_type != VAR_UNKNOWN
12103 && get_tv_number_chk(&argvars[2], &error))
12104 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012106 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012107 rettv->vval.v_string = NULL;
12108 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012109 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12110 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111}
12112
12113/*
12114 * "has()" function
12115 */
12116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012117f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012118 typval_T *argvars;
12119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120{
12121 int i;
12122 char_u *name;
12123 int n = FALSE;
12124 static char *(has_list[]) =
12125 {
12126#ifdef AMIGA
12127 "amiga",
12128# ifdef FEAT_ARP
12129 "arp",
12130# endif
12131#endif
12132#ifdef __BEOS__
12133 "beos",
12134#endif
12135#ifdef MSDOS
12136# ifdef DJGPP
12137 "dos32",
12138# else
12139 "dos16",
12140# endif
12141#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012142#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 "mac",
12144#endif
12145#if defined(MACOS_X_UNIX)
12146 "macunix",
12147#endif
12148#ifdef OS2
12149 "os2",
12150#endif
12151#ifdef __QNX__
12152 "qnx",
12153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154#ifdef UNIX
12155 "unix",
12156#endif
12157#ifdef VMS
12158 "vms",
12159#endif
12160#ifdef WIN16
12161 "win16",
12162#endif
12163#ifdef WIN32
12164 "win32",
12165#endif
12166#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12167 "win32unix",
12168#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012169#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170 "win64",
12171#endif
12172#ifdef EBCDIC
12173 "ebcdic",
12174#endif
12175#ifndef CASE_INSENSITIVE_FILENAME
12176 "fname_case",
12177#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012178#ifdef HAVE_ACL
12179 "acl",
12180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181#ifdef FEAT_ARABIC
12182 "arabic",
12183#endif
12184#ifdef FEAT_AUTOCMD
12185 "autocmd",
12186#endif
12187#ifdef FEAT_BEVAL
12188 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012189# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12190 "balloon_multiline",
12191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192#endif
12193#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12194 "builtin_terms",
12195# ifdef ALL_BUILTIN_TCAPS
12196 "all_builtin_terms",
12197# endif
12198#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012199#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12200 || defined(FEAT_GUI_W32) \
12201 || defined(FEAT_GUI_MOTIF))
12202 "browsefilter",
12203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef FEAT_BYTEOFF
12205 "byte_offset",
12206#endif
12207#ifdef FEAT_CINDENT
12208 "cindent",
12209#endif
12210#ifdef FEAT_CLIENTSERVER
12211 "clientserver",
12212#endif
12213#ifdef FEAT_CLIPBOARD
12214 "clipboard",
12215#endif
12216#ifdef FEAT_CMDL_COMPL
12217 "cmdline_compl",
12218#endif
12219#ifdef FEAT_CMDHIST
12220 "cmdline_hist",
12221#endif
12222#ifdef FEAT_COMMENTS
12223 "comments",
12224#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012225#ifdef FEAT_CONCEAL
12226 "conceal",
12227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228#ifdef FEAT_CRYPT
12229 "cryptv",
12230#endif
12231#ifdef FEAT_CSCOPE
12232 "cscope",
12233#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012234#ifdef FEAT_CURSORBIND
12235 "cursorbind",
12236#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012237#ifdef CURSOR_SHAPE
12238 "cursorshape",
12239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240#ifdef DEBUG
12241 "debug",
12242#endif
12243#ifdef FEAT_CON_DIALOG
12244 "dialog_con",
12245#endif
12246#ifdef FEAT_GUI_DIALOG
12247 "dialog_gui",
12248#endif
12249#ifdef FEAT_DIFF
12250 "diff",
12251#endif
12252#ifdef FEAT_DIGRAPHS
12253 "digraphs",
12254#endif
12255#ifdef FEAT_DND
12256 "dnd",
12257#endif
12258#ifdef FEAT_EMACS_TAGS
12259 "emacs_tags",
12260#endif
12261 "eval", /* always present, of course! */
12262#ifdef FEAT_EX_EXTRA
12263 "ex_extra",
12264#endif
12265#ifdef FEAT_SEARCH_EXTRA
12266 "extra_search",
12267#endif
12268#ifdef FEAT_FKMAP
12269 "farsi",
12270#endif
12271#ifdef FEAT_SEARCHPATH
12272 "file_in_path",
12273#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012274#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012275 "filterpipe",
12276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277#ifdef FEAT_FIND_ID
12278 "find_in_path",
12279#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012280#ifdef FEAT_FLOAT
12281 "float",
12282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283#ifdef FEAT_FOLDING
12284 "folding",
12285#endif
12286#ifdef FEAT_FOOTER
12287 "footer",
12288#endif
12289#if !defined(USE_SYSTEM) && defined(UNIX)
12290 "fork",
12291#endif
12292#ifdef FEAT_GETTEXT
12293 "gettext",
12294#endif
12295#ifdef FEAT_GUI
12296 "gui",
12297#endif
12298#ifdef FEAT_GUI_ATHENA
12299# ifdef FEAT_GUI_NEXTAW
12300 "gui_neXtaw",
12301# else
12302 "gui_athena",
12303# endif
12304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305#ifdef FEAT_GUI_GTK
12306 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012309#ifdef FEAT_GUI_GNOME
12310 "gui_gnome",
12311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312#ifdef FEAT_GUI_MAC
12313 "gui_mac",
12314#endif
12315#ifdef FEAT_GUI_MOTIF
12316 "gui_motif",
12317#endif
12318#ifdef FEAT_GUI_PHOTON
12319 "gui_photon",
12320#endif
12321#ifdef FEAT_GUI_W16
12322 "gui_win16",
12323#endif
12324#ifdef FEAT_GUI_W32
12325 "gui_win32",
12326#endif
12327#ifdef FEAT_HANGULIN
12328 "hangul_input",
12329#endif
12330#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12331 "iconv",
12332#endif
12333#ifdef FEAT_INS_EXPAND
12334 "insert_expand",
12335#endif
12336#ifdef FEAT_JUMPLIST
12337 "jumplist",
12338#endif
12339#ifdef FEAT_KEYMAP
12340 "keymap",
12341#endif
12342#ifdef FEAT_LANGMAP
12343 "langmap",
12344#endif
12345#ifdef FEAT_LIBCALL
12346 "libcall",
12347#endif
12348#ifdef FEAT_LINEBREAK
12349 "linebreak",
12350#endif
12351#ifdef FEAT_LISP
12352 "lispindent",
12353#endif
12354#ifdef FEAT_LISTCMDS
12355 "listcmds",
12356#endif
12357#ifdef FEAT_LOCALMAP
12358 "localmap",
12359#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012360#ifdef FEAT_LUA
12361# ifndef DYNAMIC_LUA
12362 "lua",
12363# endif
12364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365#ifdef FEAT_MENU
12366 "menu",
12367#endif
12368#ifdef FEAT_SESSION
12369 "mksession",
12370#endif
12371#ifdef FEAT_MODIFY_FNAME
12372 "modify_fname",
12373#endif
12374#ifdef FEAT_MOUSE
12375 "mouse",
12376#endif
12377#ifdef FEAT_MOUSESHAPE
12378 "mouseshape",
12379#endif
12380#if defined(UNIX) || defined(VMS)
12381# ifdef FEAT_MOUSE_DEC
12382 "mouse_dec",
12383# endif
12384# ifdef FEAT_MOUSE_GPM
12385 "mouse_gpm",
12386# endif
12387# ifdef FEAT_MOUSE_JSB
12388 "mouse_jsbterm",
12389# endif
12390# ifdef FEAT_MOUSE_NET
12391 "mouse_netterm",
12392# endif
12393# ifdef FEAT_MOUSE_PTERM
12394 "mouse_pterm",
12395# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012396# ifdef FEAT_MOUSE_SGR
12397 "mouse_sgr",
12398# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012399# ifdef FEAT_SYSMOUSE
12400 "mouse_sysmouse",
12401# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012402# ifdef FEAT_MOUSE_URXVT
12403 "mouse_urxvt",
12404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405# ifdef FEAT_MOUSE_XTERM
12406 "mouse_xterm",
12407# endif
12408#endif
12409#ifdef FEAT_MBYTE
12410 "multi_byte",
12411#endif
12412#ifdef FEAT_MBYTE_IME
12413 "multi_byte_ime",
12414#endif
12415#ifdef FEAT_MULTI_LANG
12416 "multi_lang",
12417#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012418#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012419#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012420 "mzscheme",
12421#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423#ifdef FEAT_OLE
12424 "ole",
12425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426#ifdef FEAT_PATH_EXTRA
12427 "path_extra",
12428#endif
12429#ifdef FEAT_PERL
12430#ifndef DYNAMIC_PERL
12431 "perl",
12432#endif
12433#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012434#ifdef FEAT_PERSISTENT_UNDO
12435 "persistent_undo",
12436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437#ifdef FEAT_PYTHON
12438#ifndef DYNAMIC_PYTHON
12439 "python",
12440#endif
12441#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012442#ifdef FEAT_PYTHON3
12443#ifndef DYNAMIC_PYTHON3
12444 "python3",
12445#endif
12446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447#ifdef FEAT_POSTSCRIPT
12448 "postscript",
12449#endif
12450#ifdef FEAT_PRINTER
12451 "printer",
12452#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012453#ifdef FEAT_PROFILE
12454 "profile",
12455#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012456#ifdef FEAT_RELTIME
12457 "reltime",
12458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459#ifdef FEAT_QUICKFIX
12460 "quickfix",
12461#endif
12462#ifdef FEAT_RIGHTLEFT
12463 "rightleft",
12464#endif
12465#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12466 "ruby",
12467#endif
12468#ifdef FEAT_SCROLLBIND
12469 "scrollbind",
12470#endif
12471#ifdef FEAT_CMDL_INFO
12472 "showcmd",
12473 "cmdline_info",
12474#endif
12475#ifdef FEAT_SIGNS
12476 "signs",
12477#endif
12478#ifdef FEAT_SMARTINDENT
12479 "smartindent",
12480#endif
12481#ifdef FEAT_SNIFF
12482 "sniff",
12483#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012484#ifdef STARTUPTIME
12485 "startuptime",
12486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487#ifdef FEAT_STL_OPT
12488 "statusline",
12489#endif
12490#ifdef FEAT_SUN_WORKSHOP
12491 "sun_workshop",
12492#endif
12493#ifdef FEAT_NETBEANS_INTG
12494 "netbeans_intg",
12495#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012496#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012497 "spell",
12498#endif
12499#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 "syntax",
12501#endif
12502#if defined(USE_SYSTEM) || !defined(UNIX)
12503 "system",
12504#endif
12505#ifdef FEAT_TAG_BINS
12506 "tag_binary",
12507#endif
12508#ifdef FEAT_TAG_OLDSTATIC
12509 "tag_old_static",
12510#endif
12511#ifdef FEAT_TAG_ANYWHITE
12512 "tag_any_white",
12513#endif
12514#ifdef FEAT_TCL
12515# ifndef DYNAMIC_TCL
12516 "tcl",
12517# endif
12518#endif
12519#ifdef TERMINFO
12520 "terminfo",
12521#endif
12522#ifdef FEAT_TERMRESPONSE
12523 "termresponse",
12524#endif
12525#ifdef FEAT_TEXTOBJ
12526 "textobjects",
12527#endif
12528#ifdef HAVE_TGETENT
12529 "tgetent",
12530#endif
12531#ifdef FEAT_TITLE
12532 "title",
12533#endif
12534#ifdef FEAT_TOOLBAR
12535 "toolbar",
12536#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012537#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12538 "unnamedplus",
12539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540#ifdef FEAT_USR_CMDS
12541 "user-commands", /* was accidentally included in 5.4 */
12542 "user_commands",
12543#endif
12544#ifdef FEAT_VIMINFO
12545 "viminfo",
12546#endif
12547#ifdef FEAT_VERTSPLIT
12548 "vertsplit",
12549#endif
12550#ifdef FEAT_VIRTUALEDIT
12551 "virtualedit",
12552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554#ifdef FEAT_VISUALEXTRA
12555 "visualextra",
12556#endif
12557#ifdef FEAT_VREPLACE
12558 "vreplace",
12559#endif
12560#ifdef FEAT_WILDIGN
12561 "wildignore",
12562#endif
12563#ifdef FEAT_WILDMENU
12564 "wildmenu",
12565#endif
12566#ifdef FEAT_WINDOWS
12567 "windows",
12568#endif
12569#ifdef FEAT_WAK
12570 "winaltkeys",
12571#endif
12572#ifdef FEAT_WRITEBACKUP
12573 "writebackup",
12574#endif
12575#ifdef FEAT_XIM
12576 "xim",
12577#endif
12578#ifdef FEAT_XFONTSET
12579 "xfontset",
12580#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012581#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012582 "xpm",
12583 "xpm_w32", /* for backward compatibility */
12584#else
12585# if defined(HAVE_XPM)
12586 "xpm",
12587# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589#ifdef USE_XSMP
12590 "xsmp",
12591#endif
12592#ifdef USE_XSMP_INTERACT
12593 "xsmp_interact",
12594#endif
12595#ifdef FEAT_XCLIPBOARD
12596 "xterm_clipboard",
12597#endif
12598#ifdef FEAT_XTERM_SAVE
12599 "xterm_save",
12600#endif
12601#if defined(UNIX) && defined(FEAT_X11)
12602 "X11",
12603#endif
12604 NULL
12605 };
12606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012607 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608 for (i = 0; has_list[i] != NULL; ++i)
12609 if (STRICMP(name, has_list[i]) == 0)
12610 {
12611 n = TRUE;
12612 break;
12613 }
12614
12615 if (n == FALSE)
12616 {
12617 if (STRNICMP(name, "patch", 5) == 0)
12618 n = has_patch(atoi((char *)name + 5));
12619 else if (STRICMP(name, "vim_starting") == 0)
12620 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012621#ifdef FEAT_MBYTE
12622 else if (STRICMP(name, "multi_byte_encoding") == 0)
12623 n = has_mbyte;
12624#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012625#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12626 else if (STRICMP(name, "balloon_multiline") == 0)
12627 n = multiline_balloon_available();
12628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629#ifdef DYNAMIC_TCL
12630 else if (STRICMP(name, "tcl") == 0)
12631 n = tcl_enabled(FALSE);
12632#endif
12633#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12634 else if (STRICMP(name, "iconv") == 0)
12635 n = iconv_enabled(FALSE);
12636#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012637#ifdef DYNAMIC_LUA
12638 else if (STRICMP(name, "lua") == 0)
12639 n = lua_enabled(FALSE);
12640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012641#ifdef DYNAMIC_MZSCHEME
12642 else if (STRICMP(name, "mzscheme") == 0)
12643 n = mzscheme_enabled(FALSE);
12644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645#ifdef DYNAMIC_RUBY
12646 else if (STRICMP(name, "ruby") == 0)
12647 n = ruby_enabled(FALSE);
12648#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012649#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650#ifdef DYNAMIC_PYTHON
12651 else if (STRICMP(name, "python") == 0)
12652 n = python_enabled(FALSE);
12653#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012654#endif
12655#ifdef FEAT_PYTHON3
12656#ifdef DYNAMIC_PYTHON3
12657 else if (STRICMP(name, "python3") == 0)
12658 n = python3_enabled(FALSE);
12659#endif
12660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661#ifdef DYNAMIC_PERL
12662 else if (STRICMP(name, "perl") == 0)
12663 n = perl_enabled(FALSE);
12664#endif
12665#ifdef FEAT_GUI
12666 else if (STRICMP(name, "gui_running") == 0)
12667 n = (gui.in_use || gui.starting);
12668# ifdef FEAT_GUI_W32
12669 else if (STRICMP(name, "gui_win32s") == 0)
12670 n = gui_is_win32s();
12671# endif
12672# ifdef FEAT_BROWSE
12673 else if (STRICMP(name, "browse") == 0)
12674 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12675# endif
12676#endif
12677#ifdef FEAT_SYN_HL
12678 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012679 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680#endif
12681#if defined(WIN3264)
12682 else if (STRICMP(name, "win95") == 0)
12683 n = mch_windows95();
12684#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012685#ifdef FEAT_NETBEANS_INTG
12686 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012687 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 }
12690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692}
12693
12694/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012695 * "has_key()" function
12696 */
12697 static void
12698f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012699 typval_T *argvars;
12700 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012701{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012702 if (argvars[0].v_type != VAR_DICT)
12703 {
12704 EMSG(_(e_dictreq));
12705 return;
12706 }
12707 if (argvars[0].vval.v_dict == NULL)
12708 return;
12709
12710 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012711 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012712}
12713
12714/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012715 * "haslocaldir()" function
12716 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012717 static void
12718f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012719 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012720 typval_T *rettv;
12721{
12722 rettv->vval.v_number = (curwin->w_localdir != NULL);
12723}
12724
12725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 * "hasmapto()" function
12727 */
12728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012730 typval_T *argvars;
12731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732{
12733 char_u *name;
12734 char_u *mode;
12735 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012736 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012739 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 mode = (char_u *)"nvo";
12741 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012744 if (argvars[2].v_type != VAR_UNKNOWN)
12745 abbr = get_tv_number(&argvars[2]);
12746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747
Bram Moolenaar2c932302006-03-18 21:42:09 +000012748 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752}
12753
12754/*
12755 * "histadd()" function
12756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761{
12762#ifdef FEAT_CMDHIST
12763 int histype;
12764 char_u *str;
12765 char_u buf[NUMBUFLEN];
12766#endif
12767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 if (check_restricted() || check_secure())
12770 return;
12771#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12773 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 if (histype >= 0)
12775 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 if (*str != NUL)
12778 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012779 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 return;
12783 }
12784 }
12785#endif
12786}
12787
12788/*
12789 * "histdel()" function
12790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012793 typval_T *argvars UNUSED;
12794 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795{
12796#ifdef FEAT_CMDHIST
12797 int n;
12798 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012801 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12802 if (str == NULL)
12803 n = 0;
12804 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012806 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012807 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012809 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 else
12812 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012813 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 get_tv_string_buf(&argvars[1], buf));
12815 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816#endif
12817}
12818
12819/*
12820 * "histget()" function
12821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_histget(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#ifdef FEAT_CMDHIST
12828 int type;
12829 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12833 if (str == NULL)
12834 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 {
12837 type = get_histtype(str);
12838 if (argvars[1].v_type == VAR_UNKNOWN)
12839 idx = get_history_idx(type);
12840 else
12841 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12842 /* -1 on type error */
12843 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849}
12850
12851/*
12852 * "histnr()" function
12853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012856 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858{
12859 int i;
12860
12861#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012862 char_u *history = get_tv_string_chk(&argvars[0]);
12863
12864 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 if (i >= HIST_CMD && i < HIST_COUNT)
12866 i = get_history_idx(i);
12867 else
12868#endif
12869 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012870 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871}
12872
12873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 * "highlightID(name)" function
12875 */
12876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012878 typval_T *argvars;
12879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882}
12883
12884/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012885 * "highlight_exists()" function
12886 */
12887 static void
12888f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012889 typval_T *argvars;
12890 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012891{
12892 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12893}
12894
12895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 * "hostname()" function
12897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012900 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902{
12903 char_u hostname[256];
12904
12905 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906 rettv->v_type = VAR_STRING;
12907 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908}
12909
12910/*
12911 * iconv() function
12912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917{
12918#ifdef FEAT_MBYTE
12919 char_u buf1[NUMBUFLEN];
12920 char_u buf2[NUMBUFLEN];
12921 char_u *from, *to, *str;
12922 vimconv_T vimconv;
12923#endif
12924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012925 rettv->v_type = VAR_STRING;
12926 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927
12928#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 str = get_tv_string(&argvars[0]);
12930 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12931 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 vimconv.vc_type = CONV_NONE;
12933 convert_setup(&vimconv, from, to);
12934
12935 /* If the encodings are equal, no conversion needed. */
12936 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012937 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940
12941 convert_setup(&vimconv, NULL, NULL);
12942 vim_free(from);
12943 vim_free(to);
12944#endif
12945}
12946
12947/*
12948 * "indent()" function
12949 */
12950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012952 typval_T *argvars;
12953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954{
12955 linenr_T lnum;
12956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962}
12963
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012964/*
12965 * "index()" function
12966 */
12967 static void
12968f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012969 typval_T *argvars;
12970 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012971{
Bram Moolenaar33570922005-01-25 22:26:29 +000012972 list_T *l;
12973 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012974 long idx = 0;
12975 int ic = FALSE;
12976
12977 rettv->vval.v_number = -1;
12978 if (argvars[0].v_type != VAR_LIST)
12979 {
12980 EMSG(_(e_listreq));
12981 return;
12982 }
12983 l = argvars[0].vval.v_list;
12984 if (l != NULL)
12985 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012986 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012987 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012989 int error = FALSE;
12990
Bram Moolenaar758711c2005-02-02 23:11:38 +000012991 /* Start at specified item. Use the cached index that list_find()
12992 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012993 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012994 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012995 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012996 ic = get_tv_number_chk(&argvars[3], &error);
12997 if (error)
12998 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012999 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013000
Bram Moolenaar758711c2005-02-02 23:11:38 +000013001 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013002 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013003 {
13004 rettv->vval.v_number = idx;
13005 break;
13006 }
13007 }
13008}
13009
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010static int inputsecret_flag = 0;
13011
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013012static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13013
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013015 * This function is used by f_input() and f_inputdialog() functions. The third
13016 * argument to f_input() specifies the type of completion to use at the
13017 * prompt. The third argument to f_inputdialog() specifies the value to return
13018 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 */
13020 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013021get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013022 typval_T *argvars;
13023 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013024 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013026 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027 char_u *p = NULL;
13028 int c;
13029 char_u buf[NUMBUFLEN];
13030 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013031 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013032 int xp_type = EXPAND_NOTHING;
13033 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013036 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037
13038#ifdef NO_CONSOLE_INPUT
13039 /* While starting up, there is no place to enter text. */
13040 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042#endif
13043
13044 cmd_silent = FALSE; /* Want to see the prompt. */
13045 if (prompt != NULL)
13046 {
13047 /* Only the part of the message after the last NL is considered as
13048 * prompt for the command line */
13049 p = vim_strrchr(prompt, '\n');
13050 if (p == NULL)
13051 p = prompt;
13052 else
13053 {
13054 ++p;
13055 c = *p;
13056 *p = NUL;
13057 msg_start();
13058 msg_clr_eos();
13059 msg_puts_attr(prompt, echo_attr);
13060 msg_didout = FALSE;
13061 msg_starthere();
13062 *p = c;
13063 }
13064 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013066 if (argvars[1].v_type != VAR_UNKNOWN)
13067 {
13068 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13069 if (defstr != NULL)
13070 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013072 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013073 {
13074 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013075 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013076 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013077
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013078 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013079 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013080
Bram Moolenaar4463f292005-09-25 22:20:24 +000013081 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13082 if (xp_name == NULL)
13083 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013085 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013086
Bram Moolenaar4463f292005-09-25 22:20:24 +000013087 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13088 &xp_arg) == FAIL)
13089 return;
13090 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013091 }
13092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013094 {
13095# ifdef FEAT_EX_EXTRA
13096 int save_ex_normal_busy = ex_normal_busy;
13097 ex_normal_busy = 0;
13098# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013099 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013100 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13101 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013102# ifdef FEAT_EX_EXTRA
13103 ex_normal_busy = save_ex_normal_busy;
13104# endif
13105 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013106 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013107 && argvars[1].v_type != VAR_UNKNOWN
13108 && argvars[2].v_type != VAR_UNKNOWN)
13109 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13110 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013111
13112 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013114 /* since the user typed this, no need to wait for return */
13115 need_wait_return = FALSE;
13116 msg_didout = FALSE;
13117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118 cmd_silent = cmd_silent_save;
13119}
13120
13121/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013122 * "input()" function
13123 * Also handles inputsecret() when inputsecret is set.
13124 */
13125 static void
13126f_input(argvars, rettv)
13127 typval_T *argvars;
13128 typval_T *rettv;
13129{
13130 get_user_input(argvars, rettv, FALSE);
13131}
13132
13133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 * "inputdialog()" function
13135 */
13136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013138 typval_T *argvars;
13139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140{
13141#if defined(FEAT_GUI_TEXTDIALOG)
13142 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13143 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13144 {
13145 char_u *message;
13146 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013147 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013149 message = get_tv_string_chk(&argvars[0]);
13150 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013151 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013152 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 else
13154 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 if (message != NULL && defstr != NULL
13156 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013157 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 else
13160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 if (message != NULL && defstr != NULL
13162 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013163 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164 rettv->vval.v_string = vim_strsave(
13165 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 }
13171 else
13172#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013173 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174}
13175
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013176/*
13177 * "inputlist()" function
13178 */
13179 static void
13180f_inputlist(argvars, rettv)
13181 typval_T *argvars;
13182 typval_T *rettv;
13183{
13184 listitem_T *li;
13185 int selected;
13186 int mouse_used;
13187
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013188#ifdef NO_CONSOLE_INPUT
13189 /* While starting up, there is no place to enter text. */
13190 if (no_console_input())
13191 return;
13192#endif
13193 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13194 {
13195 EMSG2(_(e_listarg), "inputlist()");
13196 return;
13197 }
13198
13199 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013200 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013201 lines_left = Rows; /* avoid more prompt */
13202 msg_scroll = TRUE;
13203 msg_clr_eos();
13204
13205 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13206 {
13207 msg_puts(get_tv_string(&li->li_tv));
13208 msg_putchar('\n');
13209 }
13210
13211 /* Ask for choice. */
13212 selected = prompt_for_number(&mouse_used);
13213 if (mouse_used)
13214 selected -= lines_left;
13215
13216 rettv->vval.v_number = selected;
13217}
13218
13219
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13221
13222/*
13223 * "inputrestore()" function
13224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013226f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013227 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229{
13230 if (ga_userinput.ga_len > 0)
13231 {
13232 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13234 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013235 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 }
13237 else if (p_verbose > 1)
13238 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013239 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 }
13242}
13243
13244/*
13245 * "inputsave()" function
13246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013249 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013252 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 if (ga_grow(&ga_userinput, 1) == OK)
13254 {
13255 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13256 + ga_userinput.ga_len);
13257 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013258 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 }
13260 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262}
13263
13264/*
13265 * "inputsecret()" function
13266 */
13267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013269 typval_T *argvars;
13270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271{
13272 ++cmdline_star;
13273 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 --cmdline_star;
13276 --inputsecret_flag;
13277}
13278
13279/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013280 * "insert()" function
13281 */
13282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013283f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013284 typval_T *argvars;
13285 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013286{
13287 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 listitem_T *item;
13289 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013290 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013291
13292 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013293 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013294 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013295 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013296 {
13297 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013298 before = get_tv_number_chk(&argvars[2], &error);
13299 if (error)
13300 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013301
Bram Moolenaar758711c2005-02-02 23:11:38 +000013302 if (before == l->lv_len)
13303 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013304 else
13305 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013306 item = list_find(l, before);
13307 if (item == NULL)
13308 {
13309 EMSGN(_(e_listidx), before);
13310 l = NULL;
13311 }
13312 }
13313 if (l != NULL)
13314 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013315 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013316 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013317 }
13318 }
13319}
13320
13321/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013322 * "invert(expr)" function
13323 */
13324 static void
13325f_invert(argvars, rettv)
13326 typval_T *argvars;
13327 typval_T *rettv;
13328{
13329 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13330}
13331
13332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 * "isdirectory()" function
13334 */
13335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013336f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013337 typval_T *argvars;
13338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341}
13342
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013343/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013344 * "islocked()" function
13345 */
13346 static void
13347f_islocked(argvars, rettv)
13348 typval_T *argvars;
13349 typval_T *rettv;
13350{
13351 lval_T lv;
13352 char_u *end;
13353 dictitem_T *di;
13354
13355 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013356 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13357 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013358 if (end != NULL && lv.ll_name != NULL)
13359 {
13360 if (*end != NUL)
13361 EMSG(_(e_trailing));
13362 else
13363 {
13364 if (lv.ll_tv == NULL)
13365 {
13366 if (check_changedtick(lv.ll_name))
13367 rettv->vval.v_number = 1; /* always locked */
13368 else
13369 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013370 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013371 if (di != NULL)
13372 {
13373 /* Consider a variable locked when:
13374 * 1. the variable itself is locked
13375 * 2. the value of the variable is locked.
13376 * 3. the List or Dict value is locked.
13377 */
13378 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13379 || tv_islocked(&di->di_tv));
13380 }
13381 }
13382 }
13383 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013384 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013385 else if (lv.ll_newkey != NULL)
13386 EMSG2(_(e_dictkey), lv.ll_newkey);
13387 else if (lv.ll_list != NULL)
13388 /* List item. */
13389 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13390 else
13391 /* Dictionary item. */
13392 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13393 }
13394 }
13395
13396 clear_lval(&lv);
13397}
13398
Bram Moolenaar33570922005-01-25 22:26:29 +000013399static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013400
13401/*
13402 * Turn a dict into a list:
13403 * "what" == 0: list of keys
13404 * "what" == 1: list of values
13405 * "what" == 2: list of items
13406 */
13407 static void
13408dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *argvars;
13410 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013411 int what;
13412{
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 list_T *l2;
13414 dictitem_T *di;
13415 hashitem_T *hi;
13416 listitem_T *li;
13417 listitem_T *li2;
13418 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013419 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013420
Bram Moolenaar8c711452005-01-14 21:53:12 +000013421 if (argvars[0].v_type != VAR_DICT)
13422 {
13423 EMSG(_(e_dictreq));
13424 return;
13425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013426 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013427 return;
13428
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013429 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013430 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013431
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013432 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013433 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013435 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013437 --todo;
13438 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013439
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013440 li = listitem_alloc();
13441 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013442 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013443 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013444
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013445 if (what == 0)
13446 {
13447 /* keys() */
13448 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013449 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013450 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13451 }
13452 else if (what == 1)
13453 {
13454 /* values() */
13455 copy_tv(&di->di_tv, &li->li_tv);
13456 }
13457 else
13458 {
13459 /* items() */
13460 l2 = list_alloc();
13461 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013462 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013463 li->li_tv.vval.v_list = l2;
13464 if (l2 == NULL)
13465 break;
13466 ++l2->lv_refcount;
13467
13468 li2 = listitem_alloc();
13469 if (li2 == NULL)
13470 break;
13471 list_append(l2, li2);
13472 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013473 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013474 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13475
13476 li2 = listitem_alloc();
13477 if (li2 == NULL)
13478 break;
13479 list_append(l2, li2);
13480 copy_tv(&di->di_tv, &li2->li_tv);
13481 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013482 }
13483 }
13484}
13485
13486/*
13487 * "items(dict)" function
13488 */
13489 static void
13490f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013491 typval_T *argvars;
13492 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013493{
13494 dict_list(argvars, rettv, 2);
13495}
13496
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013498 * "join()" function
13499 */
13500 static void
13501f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013502 typval_T *argvars;
13503 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013504{
13505 garray_T ga;
13506 char_u *sep;
13507
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013508 if (argvars[0].v_type != VAR_LIST)
13509 {
13510 EMSG(_(e_listreq));
13511 return;
13512 }
13513 if (argvars[0].vval.v_list == NULL)
13514 return;
13515 if (argvars[1].v_type == VAR_UNKNOWN)
13516 sep = (char_u *)" ";
13517 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013518 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013519
13520 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521
13522 if (sep != NULL)
13523 {
13524 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013525 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013526 ga_append(&ga, NUL);
13527 rettv->vval.v_string = (char_u *)ga.ga_data;
13528 }
13529 else
13530 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013531}
13532
13533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013534 * "keys()" function
13535 */
13536 static void
13537f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013538 typval_T *argvars;
13539 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013540{
13541 dict_list(argvars, rettv, 0);
13542}
13543
13544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 * "last_buffer_nr()" function.
13546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013549 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551{
13552 int n = 0;
13553 buf_T *buf;
13554
13555 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13556 if (n < buf->b_fnum)
13557 n = buf->b_fnum;
13558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013560}
13561
13562/*
13563 * "len()" function
13564 */
13565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013567 typval_T *argvars;
13568 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013569{
13570 switch (argvars[0].v_type)
13571 {
13572 case VAR_STRING:
13573 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 rettv->vval.v_number = (varnumber_T)STRLEN(
13575 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013576 break;
13577 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013578 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013580 case VAR_DICT:
13581 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13582 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013583 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013584 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013585 break;
13586 }
13587}
13588
Bram Moolenaar33570922005-01-25 22:26:29 +000013589static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013590
13591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 typval_T *argvars;
13594 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013595 int type;
13596{
13597#ifdef FEAT_LIBCALL
13598 char_u *string_in;
13599 char_u **string_result;
13600 int nr_result;
13601#endif
13602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013604 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013606
13607 if (check_restricted() || check_secure())
13608 return;
13609
13610#ifdef FEAT_LIBCALL
13611 /* The first two args must be strings, otherwise its meaningless */
13612 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13613 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013614 string_in = NULL;
13615 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013616 string_in = argvars[2].vval.v_string;
13617 if (type == VAR_NUMBER)
13618 string_result = NULL;
13619 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013621 if (mch_libcall(argvars[0].vval.v_string,
13622 argvars[1].vval.v_string,
13623 string_in,
13624 argvars[2].vval.v_number,
13625 string_result,
13626 &nr_result) == OK
13627 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013629 }
13630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631}
13632
13633/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013634 * "libcall()" function
13635 */
13636 static void
13637f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 typval_T *argvars;
13639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013640{
13641 libcall_common(argvars, rettv, VAR_STRING);
13642}
13643
13644/*
13645 * "libcallnr()" function
13646 */
13647 static void
13648f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013649 typval_T *argvars;
13650 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013651{
13652 libcall_common(argvars, rettv, VAR_NUMBER);
13653}
13654
13655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 * "line(string)" function
13657 */
13658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013660 typval_T *argvars;
13661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662{
13663 linenr_T lnum = 0;
13664 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013665 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013667 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 if (fp != NULL)
13669 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671}
13672
13673/*
13674 * "line2byte(lnum)" function
13675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013678 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680{
13681#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683#else
13684 linenr_T lnum;
13685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13691 if (rettv->vval.v_number >= 0)
13692 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693#endif
13694}
13695
13696/*
13697 * "lispindent(lnum)" function
13698 */
13699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013701 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703{
13704#ifdef FEAT_LISP
13705 pos_T pos;
13706 linenr_T lnum;
13707
13708 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13711 {
13712 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 curwin->w_cursor = pos;
13715 }
13716 else
13717#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719}
13720
13721/*
13722 * "localtime()" function
13723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013725f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013726 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730}
13731
Bram Moolenaar33570922005-01-25 22:26:29 +000013732static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733
13734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013735get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013736 typval_T *argvars;
13737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 int exact;
13739{
13740 char_u *keys;
13741 char_u *which;
13742 char_u buf[NUMBUFLEN];
13743 char_u *keys_buf = NULL;
13744 char_u *rhs;
13745 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013746 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013747 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013748 mapblock_T *mp;
13749 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750
13751 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 rettv->v_type = VAR_STRING;
13753 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 if (*keys == NUL)
13757 return;
13758
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013759 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013761 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013762 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013763 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013764 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013765 if (argvars[3].v_type != VAR_UNKNOWN)
13766 get_dict = get_tv_number(&argvars[3]);
13767 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 else
13770 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 if (which == NULL)
13772 return;
13773
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 mode = get_map_mode(&which, 0);
13775
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013776 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013777 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013779
13780 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013782 /* Return a string. */
13783 if (rhs != NULL)
13784 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785
Bram Moolenaarbd743252010-10-20 21:23:33 +020013786 }
13787 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13788 {
13789 /* Return a dictionary. */
13790 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13791 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13792 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793
Bram Moolenaarbd743252010-10-20 21:23:33 +020013794 dict_add_nr_str(dict, "lhs", 0L, lhs);
13795 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13796 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13797 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13798 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13799 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13800 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013801 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013802 dict_add_nr_str(dict, "mode", 0L, mapmode);
13803
13804 vim_free(lhs);
13805 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 }
13807}
13808
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013809#ifdef FEAT_FLOAT
13810/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013811 * "log()" function
13812 */
13813 static void
13814f_log(argvars, rettv)
13815 typval_T *argvars;
13816 typval_T *rettv;
13817{
13818 float_T f;
13819
13820 rettv->v_type = VAR_FLOAT;
13821 if (get_float_arg(argvars, &f) == OK)
13822 rettv->vval.v_float = log(f);
13823 else
13824 rettv->vval.v_float = 0.0;
13825}
13826
13827/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013828 * "log10()" function
13829 */
13830 static void
13831f_log10(argvars, rettv)
13832 typval_T *argvars;
13833 typval_T *rettv;
13834{
13835 float_T f;
13836
13837 rettv->v_type = VAR_FLOAT;
13838 if (get_float_arg(argvars, &f) == OK)
13839 rettv->vval.v_float = log10(f);
13840 else
13841 rettv->vval.v_float = 0.0;
13842}
13843#endif
13844
Bram Moolenaar1dced572012-04-05 16:54:08 +020013845#ifdef FEAT_LUA
13846/*
13847 * "luaeval()" function
13848 */
13849 static void
13850f_luaeval(argvars, rettv)
13851 typval_T *argvars;
13852 typval_T *rettv;
13853{
13854 char_u *str;
13855 char_u buf[NUMBUFLEN];
13856
13857 str = get_tv_string_buf(&argvars[0], buf);
13858 do_luaeval(str, argvars + 1, rettv);
13859}
13860#endif
13861
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013863 * "map()" function
13864 */
13865 static void
13866f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013867 typval_T *argvars;
13868 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013869{
13870 filter_map(argvars, rettv, TRUE);
13871}
13872
13873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013874 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 */
13876 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013877f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013878 typval_T *argvars;
13879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013881 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882}
13883
13884/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013885 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 */
13887 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013888f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013889 typval_T *argvars;
13890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013892 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893}
13894
Bram Moolenaar33570922005-01-25 22:26:29 +000013895static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896
13897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013898find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013899 typval_T *argvars;
13900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 int type;
13902{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013903 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013904 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 char_u *pat;
13907 regmatch_T regmatch;
13908 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013909 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 char_u *save_cpo;
13911 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013912 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013913 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013914 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013915 list_T *l = NULL;
13916 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013917 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013918 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919
13920 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13921 save_cpo = p_cpo;
13922 p_cpo = (char_u *)"";
13923
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013924 rettv->vval.v_number = -1;
13925 if (type == 3)
13926 {
13927 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013928 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013929 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013930 }
13931 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013933 rettv->v_type = VAR_STRING;
13934 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013937 if (argvars[0].v_type == VAR_LIST)
13938 {
13939 if ((l = argvars[0].vval.v_list) == NULL)
13940 goto theend;
13941 li = l->lv_first;
13942 }
13943 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013944 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013945 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013946 len = (long)STRLEN(str);
13947 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13950 if (pat == NULL)
13951 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013952
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013953 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 int error = FALSE;
13956
13957 start = get_tv_number_chk(&argvars[2], &error);
13958 if (error)
13959 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013960 if (l != NULL)
13961 {
13962 li = list_find(l, start);
13963 if (li == NULL)
13964 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013965 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013966 }
13967 else
13968 {
13969 if (start < 0)
13970 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013971 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013972 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013973 /* When "count" argument is there ignore matches before "start",
13974 * otherwise skip part of the string. Differs when pattern is "^"
13975 * or "\<". */
13976 if (argvars[3].v_type != VAR_UNKNOWN)
13977 startcol = start;
13978 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013979 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013980 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013981 len -= start;
13982 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013983 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013984
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013985 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 nth = get_tv_number_chk(&argvars[3], &error);
13987 if (error)
13988 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 }
13990
13991 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13992 if (regmatch.regprog != NULL)
13993 {
13994 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013995
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013996 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013997 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013998 if (l != NULL)
13999 {
14000 if (li == NULL)
14001 {
14002 match = FALSE;
14003 break;
14004 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014005 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014006 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014007 if (str == NULL)
14008 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014009 }
14010
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014011 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014012
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014013 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014014 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014015 if (l == NULL && !match)
14016 break;
14017
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014018 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014019 if (l != NULL)
14020 {
14021 li = li->li_next;
14022 ++idx;
14023 }
14024 else
14025 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014026#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014027 startcol = (colnr_T)(regmatch.startp[0]
14028 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014029#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014030 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014031#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014032 if (startcol > (colnr_T)len
14033 || str + startcol <= regmatch.startp[0])
14034 {
14035 match = FALSE;
14036 break;
14037 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014038 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014039 }
14040
14041 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014043 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014044 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014045 int i;
14046
14047 /* return list with matched string and submatches */
14048 for (i = 0; i < NSUBEXP; ++i)
14049 {
14050 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014051 {
14052 if (list_append_string(rettv->vval.v_list,
14053 (char_u *)"", 0) == FAIL)
14054 break;
14055 }
14056 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014057 regmatch.startp[i],
14058 (int)(regmatch.endp[i] - regmatch.startp[i]))
14059 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014060 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014061 }
14062 }
14063 else if (type == 2)
14064 {
14065 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014066 if (l != NULL)
14067 copy_tv(&li->li_tv, rettv);
14068 else
14069 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014071 }
14072 else if (l != NULL)
14073 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 else
14075 {
14076 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 (varnumber_T)(regmatch.startp[0] - str);
14079 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014080 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014082 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 }
14084 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014085 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 }
14087
14088theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014089 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 p_cpo = save_cpo;
14091}
14092
14093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094 * "match()" function
14095 */
14096 static void
14097f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014098 typval_T *argvars;
14099 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100{
14101 find_some_match(argvars, rettv, 1);
14102}
14103
14104/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014105 * "matchadd()" function
14106 */
14107 static void
14108f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014109 typval_T *argvars UNUSED;
14110 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014111{
14112#ifdef FEAT_SEARCH_EXTRA
14113 char_u buf[NUMBUFLEN];
14114 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14115 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14116 int prio = 10; /* default priority */
14117 int id = -1;
14118 int error = FALSE;
14119
14120 rettv->vval.v_number = -1;
14121
14122 if (grp == NULL || pat == NULL)
14123 return;
14124 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014125 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014126 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014127 if (argvars[3].v_type != VAR_UNKNOWN)
14128 id = get_tv_number_chk(&argvars[3], &error);
14129 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014130 if (error == TRUE)
14131 return;
14132 if (id >= 1 && id <= 3)
14133 {
14134 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14135 return;
14136 }
14137
14138 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14139#endif
14140}
14141
14142/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014143 * "matcharg()" function
14144 */
14145 static void
14146f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014147 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014148 typval_T *rettv;
14149{
14150 if (rettv_list_alloc(rettv) == OK)
14151 {
14152#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014153 int id = get_tv_number(&argvars[0]);
14154 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014155
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014156 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014157 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014158 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14159 {
14160 list_append_string(rettv->vval.v_list,
14161 syn_id2name(m->hlg_id), -1);
14162 list_append_string(rettv->vval.v_list, m->pattern, -1);
14163 }
14164 else
14165 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014166 list_append_string(rettv->vval.v_list, NULL, -1);
14167 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014168 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014169 }
14170#endif
14171 }
14172}
14173
14174/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014175 * "matchdelete()" function
14176 */
14177 static void
14178f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014179 typval_T *argvars UNUSED;
14180 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014181{
14182#ifdef FEAT_SEARCH_EXTRA
14183 rettv->vval.v_number = match_delete(curwin,
14184 (int)get_tv_number(&argvars[0]), TRUE);
14185#endif
14186}
14187
14188/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014189 * "matchend()" function
14190 */
14191 static void
14192f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 typval_T *argvars;
14194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195{
14196 find_some_match(argvars, rettv, 0);
14197}
14198
14199/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014200 * "matchlist()" function
14201 */
14202 static void
14203f_matchlist(argvars, rettv)
14204 typval_T *argvars;
14205 typval_T *rettv;
14206{
14207 find_some_match(argvars, rettv, 3);
14208}
14209
14210/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014211 * "matchstr()" function
14212 */
14213 static void
14214f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014215 typval_T *argvars;
14216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014217{
14218 find_some_match(argvars, rettv, 2);
14219}
14220
Bram Moolenaar33570922005-01-25 22:26:29 +000014221static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014222
14223 static void
14224max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014225 typval_T *argvars;
14226 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014227 int domax;
14228{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014229 long n = 0;
14230 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014232
14233 if (argvars[0].v_type == VAR_LIST)
14234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014235 list_T *l;
14236 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014237
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014238 l = argvars[0].vval.v_list;
14239 if (l != NULL)
14240 {
14241 li = l->lv_first;
14242 if (li != NULL)
14243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014244 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014245 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014246 {
14247 li = li->li_next;
14248 if (li == NULL)
14249 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014251 if (domax ? i > n : i < n)
14252 n = i;
14253 }
14254 }
14255 }
14256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014257 else if (argvars[0].v_type == VAR_DICT)
14258 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014259 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014260 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014261 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014262 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014263
14264 d = argvars[0].vval.v_dict;
14265 if (d != NULL)
14266 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014267 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014268 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014269 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014270 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014271 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014272 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014274 if (first)
14275 {
14276 n = i;
14277 first = FALSE;
14278 }
14279 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014280 n = i;
14281 }
14282 }
14283 }
14284 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014285 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014286 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014288}
14289
14290/*
14291 * "max()" function
14292 */
14293 static void
14294f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014295 typval_T *argvars;
14296 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014297{
14298 max_min(argvars, rettv, TRUE);
14299}
14300
14301/*
14302 * "min()" function
14303 */
14304 static void
14305f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014306 typval_T *argvars;
14307 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014308{
14309 max_min(argvars, rettv, FALSE);
14310}
14311
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014312static int mkdir_recurse __ARGS((char_u *dir, int prot));
14313
14314/*
14315 * Create the directory in which "dir" is located, and higher levels when
14316 * needed.
14317 */
14318 static int
14319mkdir_recurse(dir, prot)
14320 char_u *dir;
14321 int prot;
14322{
14323 char_u *p;
14324 char_u *updir;
14325 int r = FAIL;
14326
14327 /* Get end of directory name in "dir".
14328 * We're done when it's "/" or "c:/". */
14329 p = gettail_sep(dir);
14330 if (p <= get_past_head(dir))
14331 return OK;
14332
14333 /* If the directory exists we're done. Otherwise: create it.*/
14334 updir = vim_strnsave(dir, (int)(p - dir));
14335 if (updir == NULL)
14336 return FAIL;
14337 if (mch_isdir(updir))
14338 r = OK;
14339 else if (mkdir_recurse(updir, prot) == OK)
14340 r = vim_mkdir_emsg(updir, prot);
14341 vim_free(updir);
14342 return r;
14343}
14344
14345#ifdef vim_mkdir
14346/*
14347 * "mkdir()" function
14348 */
14349 static void
14350f_mkdir(argvars, rettv)
14351 typval_T *argvars;
14352 typval_T *rettv;
14353{
14354 char_u *dir;
14355 char_u buf[NUMBUFLEN];
14356 int prot = 0755;
14357
14358 rettv->vval.v_number = FAIL;
14359 if (check_restricted() || check_secure())
14360 return;
14361
14362 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014363 if (*dir == NUL)
14364 rettv->vval.v_number = FAIL;
14365 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014366 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014367 if (*gettail(dir) == NUL)
14368 /* remove trailing slashes */
14369 *gettail_sep(dir) = NUL;
14370
14371 if (argvars[1].v_type != VAR_UNKNOWN)
14372 {
14373 if (argvars[2].v_type != VAR_UNKNOWN)
14374 prot = get_tv_number_chk(&argvars[2], NULL);
14375 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14376 mkdir_recurse(dir, prot);
14377 }
14378 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014379 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014380}
14381#endif
14382
Bram Moolenaar0d660222005-01-07 21:51:51 +000014383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 * "mode()" function
14385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014388 typval_T *argvars;
14389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014391 char_u buf[3];
14392
14393 buf[1] = NUL;
14394 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 if (VIsual_active)
14397 {
14398 if (VIsual_select)
14399 buf[0] = VIsual_mode + 's' - 'v';
14400 else
14401 buf[0] = VIsual_mode;
14402 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014403 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014404 || State == CONFIRM)
14405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014407 if (State == ASKMORE)
14408 buf[1] = 'm';
14409 else if (State == CONFIRM)
14410 buf[1] = '?';
14411 }
14412 else if (State == EXTERNCMD)
14413 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 else if (State & INSERT)
14415 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014416#ifdef FEAT_VREPLACE
14417 if (State & VREPLACE_FLAG)
14418 {
14419 buf[0] = 'R';
14420 buf[1] = 'v';
14421 }
14422 else
14423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 if (State & REPLACE_FLAG)
14425 buf[0] = 'R';
14426 else
14427 buf[0] = 'i';
14428 }
14429 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014432 if (exmode_active)
14433 buf[1] = 'v';
14434 }
14435 else if (exmode_active)
14436 {
14437 buf[0] = 'c';
14438 buf[1] = 'e';
14439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014443 if (finish_op)
14444 buf[1] = 'o';
14445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014447 /* Clear out the minor mode when the argument is not a non-zero number or
14448 * non-empty string. */
14449 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014450 buf[1] = NUL;
14451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452 rettv->vval.v_string = vim_strsave(buf);
14453 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454}
14455
Bram Moolenaar429fa852013-04-15 12:27:36 +020014456#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014457/*
14458 * "mzeval()" function
14459 */
14460 static void
14461f_mzeval(argvars, rettv)
14462 typval_T *argvars;
14463 typval_T *rettv;
14464{
14465 char_u *str;
14466 char_u buf[NUMBUFLEN];
14467
14468 str = get_tv_string_buf(&argvars[0], buf);
14469 do_mzeval(str, rettv);
14470}
Bram Moolenaar75676462013-01-30 14:55:42 +010014471
14472 void
14473mzscheme_call_vim(name, args, rettv)
14474 char_u *name;
14475 typval_T *args;
14476 typval_T *rettv;
14477{
14478 typval_T argvars[3];
14479
14480 argvars[0].v_type = VAR_STRING;
14481 argvars[0].vval.v_string = name;
14482 copy_tv(args, &argvars[1]);
14483 argvars[2].v_type = VAR_UNKNOWN;
14484 f_call(argvars, rettv);
14485 clear_tv(&argvars[1]);
14486}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014487#endif
14488
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014490 * "nextnonblank()" function
14491 */
14492 static void
14493f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 typval_T *argvars;
14495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496{
14497 linenr_T lnum;
14498
14499 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014501 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014502 {
14503 lnum = 0;
14504 break;
14505 }
14506 if (*skipwhite(ml_get(lnum)) != NUL)
14507 break;
14508 }
14509 rettv->vval.v_number = lnum;
14510}
14511
14512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 * "nr2char()" function
14514 */
14515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014516f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 typval_T *argvars;
14518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519{
14520 char_u buf[NUMBUFLEN];
14521
14522#ifdef FEAT_MBYTE
14523 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014524 {
14525 int utf8 = 0;
14526
14527 if (argvars[1].v_type != VAR_UNKNOWN)
14528 utf8 = get_tv_number_chk(&argvars[1], NULL);
14529 if (utf8)
14530 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14531 else
14532 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 else
14535#endif
14536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 buf[1] = NUL;
14539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540 rettv->v_type = VAR_STRING;
14541 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014542}
14543
14544/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014545 * "or(expr, expr)" function
14546 */
14547 static void
14548f_or(argvars, rettv)
14549 typval_T *argvars;
14550 typval_T *rettv;
14551{
14552 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14553 | get_tv_number_chk(&argvars[1], NULL);
14554}
14555
14556/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014557 * "pathshorten()" function
14558 */
14559 static void
14560f_pathshorten(argvars, rettv)
14561 typval_T *argvars;
14562 typval_T *rettv;
14563{
14564 char_u *p;
14565
14566 rettv->v_type = VAR_STRING;
14567 p = get_tv_string_chk(&argvars[0]);
14568 if (p == NULL)
14569 rettv->vval.v_string = NULL;
14570 else
14571 {
14572 p = vim_strsave(p);
14573 rettv->vval.v_string = p;
14574 if (p != NULL)
14575 shorten_dir(p);
14576 }
14577}
14578
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014579#ifdef FEAT_FLOAT
14580/*
14581 * "pow()" function
14582 */
14583 static void
14584f_pow(argvars, rettv)
14585 typval_T *argvars;
14586 typval_T *rettv;
14587{
14588 float_T fx, fy;
14589
14590 rettv->v_type = VAR_FLOAT;
14591 if (get_float_arg(argvars, &fx) == OK
14592 && get_float_arg(&argvars[1], &fy) == OK)
14593 rettv->vval.v_float = pow(fx, fy);
14594 else
14595 rettv->vval.v_float = 0.0;
14596}
14597#endif
14598
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014599/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600 * "prevnonblank()" function
14601 */
14602 static void
14603f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 typval_T *argvars;
14605 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014606{
14607 linenr_T lnum;
14608
14609 lnum = get_tv_lnum(argvars);
14610 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14611 lnum = 0;
14612 else
14613 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14614 --lnum;
14615 rettv->vval.v_number = lnum;
14616}
14617
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014618#ifdef HAVE_STDARG_H
14619/* This dummy va_list is here because:
14620 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14621 * - locally in the function results in a "used before set" warning
14622 * - using va_start() to initialize it gives "function with fixed args" error */
14623static va_list ap;
14624#endif
14625
Bram Moolenaar8c711452005-01-14 21:53:12 +000014626/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014627 * "printf()" function
14628 */
14629 static void
14630f_printf(argvars, rettv)
14631 typval_T *argvars;
14632 typval_T *rettv;
14633{
14634 rettv->v_type = VAR_STRING;
14635 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014636#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014637 {
14638 char_u buf[NUMBUFLEN];
14639 int len;
14640 char_u *s;
14641 int saved_did_emsg = did_emsg;
14642 char *fmt;
14643
14644 /* Get the required length, allocate the buffer and do it for real. */
14645 did_emsg = FALSE;
14646 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014647 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014648 if (!did_emsg)
14649 {
14650 s = alloc(len + 1);
14651 if (s != NULL)
14652 {
14653 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014654 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014655 }
14656 }
14657 did_emsg |= saved_did_emsg;
14658 }
14659#endif
14660}
14661
14662/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014663 * "pumvisible()" function
14664 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014665 static void
14666f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014667 typval_T *argvars UNUSED;
14668 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014669{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014670#ifdef FEAT_INS_EXPAND
14671 if (pum_visible())
14672 rettv->vval.v_number = 1;
14673#endif
14674}
14675
Bram Moolenaardb913952012-06-29 12:54:53 +020014676#ifdef FEAT_PYTHON3
14677/*
14678 * "py3eval()" function
14679 */
14680 static void
14681f_py3eval(argvars, rettv)
14682 typval_T *argvars;
14683 typval_T *rettv;
14684{
14685 char_u *str;
14686 char_u buf[NUMBUFLEN];
14687
14688 str = get_tv_string_buf(&argvars[0], buf);
14689 do_py3eval(str, rettv);
14690}
14691#endif
14692
14693#ifdef FEAT_PYTHON
14694/*
14695 * "pyeval()" function
14696 */
14697 static void
14698f_pyeval(argvars, rettv)
14699 typval_T *argvars;
14700 typval_T *rettv;
14701{
14702 char_u *str;
14703 char_u buf[NUMBUFLEN];
14704
14705 str = get_tv_string_buf(&argvars[0], buf);
14706 do_pyeval(str, rettv);
14707}
14708#endif
14709
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014710/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014711 * "range()" function
14712 */
14713 static void
14714f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014715 typval_T *argvars;
14716 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014717{
14718 long start;
14719 long end;
14720 long stride = 1;
14721 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014722 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014725 if (argvars[1].v_type == VAR_UNKNOWN)
14726 {
14727 end = start - 1;
14728 start = 0;
14729 }
14730 else
14731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014732 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014733 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014734 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014735 }
14736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014737 if (error)
14738 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014739 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014740 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014741 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014742 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014743 else
14744 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014745 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014746 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014747 if (list_append_number(rettv->vval.v_list,
14748 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014749 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014750 }
14751}
14752
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014753/*
14754 * "readfile()" function
14755 */
14756 static void
14757f_readfile(argvars, rettv)
14758 typval_T *argvars;
14759 typval_T *rettv;
14760{
14761 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014762 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014763 char_u *fname;
14764 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014765 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14766 int io_size = sizeof(buf);
14767 int readlen; /* size of last fread() */
14768 char_u *prev = NULL; /* previously read bytes, if any */
14769 long prevlen = 0; /* length of data in prev */
14770 long prevsize = 0; /* size of prev buffer */
14771 long maxline = MAXLNUM;
14772 long cnt = 0;
14773 char_u *p; /* position in buf */
14774 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014775
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014776 if (argvars[1].v_type != VAR_UNKNOWN)
14777 {
14778 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14779 binary = TRUE;
14780 if (argvars[2].v_type != VAR_UNKNOWN)
14781 maxline = get_tv_number(&argvars[2]);
14782 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014783
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014784 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014785 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014786
14787 /* Always open the file in binary mode, library functions have a mind of
14788 * their own about CR-LF conversion. */
14789 fname = get_tv_string(&argvars[0]);
14790 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14791 {
14792 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14793 return;
14794 }
14795
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014796 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014797 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014798 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014799
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014800 /* This for loop processes what was read, but is also entered at end
14801 * of file so that either:
14802 * - an incomplete line gets written
14803 * - a "binary" file gets an empty line at the end if it ends in a
14804 * newline. */
14805 for (p = buf, start = buf;
14806 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14807 ++p)
14808 {
14809 if (*p == '\n' || readlen <= 0)
14810 {
14811 listitem_T *li;
14812 char_u *s = NULL;
14813 long_u len = p - start;
14814
14815 /* Finished a line. Remove CRs before NL. */
14816 if (readlen > 0 && !binary)
14817 {
14818 while (len > 0 && start[len - 1] == '\r')
14819 --len;
14820 /* removal may cross back to the "prev" string */
14821 if (len == 0)
14822 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14823 --prevlen;
14824 }
14825 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014826 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014827 else
14828 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014829 /* Change "prev" buffer to be the right size. This way
14830 * the bytes are only copied once, and very long lines are
14831 * allocated only once. */
14832 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014833 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014834 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014835 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014836 prev = NULL; /* the list will own the string */
14837 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014838 }
14839 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014840 if (s == NULL)
14841 {
14842 do_outofmem_msg((long_u) prevlen + len + 1);
14843 failed = TRUE;
14844 break;
14845 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014846
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014847 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014848 {
14849 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014850 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014851 break;
14852 }
14853 li->li_tv.v_type = VAR_STRING;
14854 li->li_tv.v_lock = 0;
14855 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014856 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014858 start = p + 1; /* step over newline */
14859 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014860 break;
14861 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014862 else if (*p == NUL)
14863 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014864#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014865 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14866 * when finding the BF and check the previous two bytes. */
14867 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014868 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014869 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14870 * + 1, these may be in the "prev" string. */
14871 char_u back1 = p >= buf + 1 ? p[-1]
14872 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14873 char_u back2 = p >= buf + 2 ? p[-2]
14874 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14875 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014876
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014877 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014878 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014879 char_u *dest = p - 2;
14880
14881 /* Usually a BOM is at the beginning of a file, and so at
14882 * the beginning of a line; then we can just step over it.
14883 */
14884 if (start == dest)
14885 start = p + 1;
14886 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014887 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014888 /* have to shuffle buf to close gap */
14889 int adjust_prevlen = 0;
14890
14891 if (dest < buf)
14892 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014893 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014894 dest = buf;
14895 }
14896 if (readlen > p - buf + 1)
14897 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14898 readlen -= 3 - adjust_prevlen;
14899 prevlen -= adjust_prevlen;
14900 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014901 }
14902 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014903 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014904#endif
14905 } /* for */
14906
14907 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14908 break;
14909 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014910 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014911 /* There's part of a line in buf, store it in "prev". */
14912 if (p - start + prevlen >= prevsize)
14913 {
14914 /* need bigger "prev" buffer */
14915 char_u *newprev;
14916
14917 /* A common use case is ordinary text files and "prev" gets a
14918 * fragment of a line, so the first allocation is made
14919 * small, to avoid repeatedly 'allocing' large and
14920 * 'reallocing' small. */
14921 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014922 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014923 else
14924 {
14925 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014926 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014927 prevsize = grow50pc > growmin ? grow50pc : growmin;
14928 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014929 newprev = prev == NULL ? alloc(prevsize)
14930 : vim_realloc(prev, prevsize);
14931 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014932 {
14933 do_outofmem_msg((long_u)prevsize);
14934 failed = TRUE;
14935 break;
14936 }
14937 prev = newprev;
14938 }
14939 /* Add the line part to end of "prev". */
14940 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014941 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014942 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014943 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014944
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014945 /*
14946 * For a negative line count use only the lines at the end of the file,
14947 * free the rest.
14948 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014949 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014950 while (cnt > -maxline)
14951 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014952 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014953 --cnt;
14954 }
14955
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014956 if (failed)
14957 {
14958 list_free(rettv->vval.v_list, TRUE);
14959 /* readfile doc says an empty list is returned on error */
14960 rettv->vval.v_list = list_alloc();
14961 }
14962
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014963 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014964 fclose(fd);
14965}
14966
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014967#if defined(FEAT_RELTIME)
14968static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14969
14970/*
14971 * Convert a List to proftime_T.
14972 * Return FAIL when there is something wrong.
14973 */
14974 static int
14975list2proftime(arg, tm)
14976 typval_T *arg;
14977 proftime_T *tm;
14978{
14979 long n1, n2;
14980 int error = FALSE;
14981
14982 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14983 || arg->vval.v_list->lv_len != 2)
14984 return FAIL;
14985 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14986 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14987# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014988 tm->HighPart = n1;
14989 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014990# else
14991 tm->tv_sec = n1;
14992 tm->tv_usec = n2;
14993# endif
14994 return error ? FAIL : OK;
14995}
14996#endif /* FEAT_RELTIME */
14997
14998/*
14999 * "reltime()" function
15000 */
15001 static void
15002f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015003 typval_T *argvars UNUSED;
15004 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015005{
15006#ifdef FEAT_RELTIME
15007 proftime_T res;
15008 proftime_T start;
15009
15010 if (argvars[0].v_type == VAR_UNKNOWN)
15011 {
15012 /* No arguments: get current time. */
15013 profile_start(&res);
15014 }
15015 else if (argvars[1].v_type == VAR_UNKNOWN)
15016 {
15017 if (list2proftime(&argvars[0], &res) == FAIL)
15018 return;
15019 profile_end(&res);
15020 }
15021 else
15022 {
15023 /* Two arguments: compute the difference. */
15024 if (list2proftime(&argvars[0], &start) == FAIL
15025 || list2proftime(&argvars[1], &res) == FAIL)
15026 return;
15027 profile_sub(&res, &start);
15028 }
15029
15030 if (rettv_list_alloc(rettv) == OK)
15031 {
15032 long n1, n2;
15033
15034# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015035 n1 = res.HighPart;
15036 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015037# else
15038 n1 = res.tv_sec;
15039 n2 = res.tv_usec;
15040# endif
15041 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15042 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15043 }
15044#endif
15045}
15046
15047/*
15048 * "reltimestr()" function
15049 */
15050 static void
15051f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015052 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015053 typval_T *rettv;
15054{
15055#ifdef FEAT_RELTIME
15056 proftime_T tm;
15057#endif
15058
15059 rettv->v_type = VAR_STRING;
15060 rettv->vval.v_string = NULL;
15061#ifdef FEAT_RELTIME
15062 if (list2proftime(&argvars[0], &tm) == OK)
15063 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15064#endif
15065}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015066
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15068static void make_connection __ARGS((void));
15069static int check_connection __ARGS((void));
15070
15071 static void
15072make_connection()
15073{
15074 if (X_DISPLAY == NULL
15075# ifdef FEAT_GUI
15076 && !gui.in_use
15077# endif
15078 )
15079 {
15080 x_force_connect = TRUE;
15081 setup_term_clip();
15082 x_force_connect = FALSE;
15083 }
15084}
15085
15086 static int
15087check_connection()
15088{
15089 make_connection();
15090 if (X_DISPLAY == NULL)
15091 {
15092 EMSG(_("E240: No connection to Vim server"));
15093 return FAIL;
15094 }
15095 return OK;
15096}
15097#endif
15098
15099#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015100static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015101
15102 static void
15103remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 typval_T *argvars;
15105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106 int expr;
15107{
15108 char_u *server_name;
15109 char_u *keys;
15110 char_u *r = NULL;
15111 char_u buf[NUMBUFLEN];
15112# ifdef WIN32
15113 HWND w;
15114# else
15115 Window w;
15116# endif
15117
15118 if (check_restricted() || check_secure())
15119 return;
15120
15121# ifdef FEAT_X11
15122 if (check_connection() == FAIL)
15123 return;
15124# endif
15125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015126 server_name = get_tv_string_chk(&argvars[0]);
15127 if (server_name == NULL)
15128 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129 keys = get_tv_string_buf(&argvars[1], buf);
15130# ifdef WIN32
15131 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15132# else
15133 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15134 < 0)
15135# endif
15136 {
15137 if (r != NULL)
15138 EMSG(r); /* sending worked but evaluation failed */
15139 else
15140 EMSG2(_("E241: Unable to send to %s"), server_name);
15141 return;
15142 }
15143
15144 rettv->vval.v_string = r;
15145
15146 if (argvars[2].v_type != VAR_UNKNOWN)
15147 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015148 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015149 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015150 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015151
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015152 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 v.di_tv.v_type = VAR_STRING;
15154 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015155 idvar = get_tv_string_chk(&argvars[2]);
15156 if (idvar != NULL)
15157 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015158 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 }
15160}
15161#endif
15162
15163/*
15164 * "remote_expr()" function
15165 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166 static void
15167f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015170{
15171 rettv->v_type = VAR_STRING;
15172 rettv->vval.v_string = NULL;
15173#ifdef FEAT_CLIENTSERVER
15174 remote_common(argvars, rettv, TRUE);
15175#endif
15176}
15177
15178/*
15179 * "remote_foreground()" function
15180 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181 static void
15182f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015183 typval_T *argvars UNUSED;
15184 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015185{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186#ifdef FEAT_CLIENTSERVER
15187# ifdef WIN32
15188 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015189 {
15190 char_u *server_name = get_tv_string_chk(&argvars[0]);
15191
15192 if (server_name != NULL)
15193 serverForeground(server_name);
15194 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015195# else
15196 /* Send a foreground() expression to the server. */
15197 argvars[1].v_type = VAR_STRING;
15198 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15199 argvars[2].v_type = VAR_UNKNOWN;
15200 remote_common(argvars, rettv, TRUE);
15201 vim_free(argvars[1].vval.v_string);
15202# endif
15203#endif
15204}
15205
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206 static void
15207f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015208 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015209 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015210{
15211#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015212 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213 char_u *s = NULL;
15214# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015215 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015217 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015218
15219 if (check_restricted() || check_secure())
15220 {
15221 rettv->vval.v_number = -1;
15222 return;
15223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 serverid = get_tv_string_chk(&argvars[0]);
15225 if (serverid == NULL)
15226 {
15227 rettv->vval.v_number = -1;
15228 return; /* type error; errmsg already given */
15229 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015230# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015231 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015232 if (n == 0)
15233 rettv->vval.v_number = -1;
15234 else
15235 {
15236 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15237 rettv->vval.v_number = (s != NULL);
15238 }
15239# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015240 if (check_connection() == FAIL)
15241 return;
15242
15243 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015245# endif
15246
15247 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 char_u *retvar;
15250
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 v.di_tv.v_type = VAR_STRING;
15252 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015253 retvar = get_tv_string_chk(&argvars[1]);
15254 if (retvar != NULL)
15255 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015256 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257 }
15258#else
15259 rettv->vval.v_number = -1;
15260#endif
15261}
15262
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263 static void
15264f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015265 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015267{
15268 char_u *r = NULL;
15269
15270#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015271 char_u *serverid = get_tv_string_chk(&argvars[0]);
15272
15273 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015274 {
15275# ifdef WIN32
15276 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015277 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015278
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015279 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280 if (n != 0)
15281 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15282 if (r == NULL)
15283# else
15284 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015285 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286# endif
15287 EMSG(_("E277: Unable to read a server reply"));
15288 }
15289#endif
15290 rettv->v_type = VAR_STRING;
15291 rettv->vval.v_string = r;
15292}
15293
15294/*
15295 * "remote_send()" function
15296 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015297 static void
15298f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015299 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015300 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015301{
15302 rettv->v_type = VAR_STRING;
15303 rettv->vval.v_string = NULL;
15304#ifdef FEAT_CLIENTSERVER
15305 remote_common(argvars, rettv, FALSE);
15306#endif
15307}
15308
15309/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015310 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015311 */
15312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015313f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015314 typval_T *argvars;
15315 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015316{
Bram Moolenaar33570922005-01-25 22:26:29 +000015317 list_T *l;
15318 listitem_T *item, *item2;
15319 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015320 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015321 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015322 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015323 dict_T *d;
15324 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015325 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015326
Bram Moolenaar8c711452005-01-14 21:53:12 +000015327 if (argvars[0].v_type == VAR_DICT)
15328 {
15329 if (argvars[2].v_type != VAR_UNKNOWN)
15330 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015331 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015332 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015334 key = get_tv_string_chk(&argvars[1]);
15335 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015337 di = dict_find(d, key, -1);
15338 if (di == NULL)
15339 EMSG2(_(e_dictkey), key);
15340 else
15341 {
15342 *rettv = di->di_tv;
15343 init_tv(&di->di_tv);
15344 dictitem_remove(d, di);
15345 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015347 }
15348 }
15349 else if (argvars[0].v_type != VAR_LIST)
15350 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015351 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015352 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015354 int error = FALSE;
15355
15356 idx = get_tv_number_chk(&argvars[1], &error);
15357 if (error)
15358 ; /* type error: do nothing, errmsg already given */
15359 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015360 EMSGN(_(e_listidx), idx);
15361 else
15362 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015363 if (argvars[2].v_type == VAR_UNKNOWN)
15364 {
15365 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015366 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015367 *rettv = item->li_tv;
15368 vim_free(item);
15369 }
15370 else
15371 {
15372 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015373 end = get_tv_number_chk(&argvars[2], &error);
15374 if (error)
15375 ; /* type error: do nothing */
15376 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015377 EMSGN(_(e_listidx), end);
15378 else
15379 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015380 int cnt = 0;
15381
15382 for (li = item; li != NULL; li = li->li_next)
15383 {
15384 ++cnt;
15385 if (li == item2)
15386 break;
15387 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015388 if (li == NULL) /* didn't find "item2" after "item" */
15389 EMSG(_(e_invrange));
15390 else
15391 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015392 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015393 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015394 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015395 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015396 l->lv_first = item;
15397 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015398 item->li_prev = NULL;
15399 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015400 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015401 }
15402 }
15403 }
15404 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015405 }
15406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407}
15408
15409/*
15410 * "rename({from}, {to})" function
15411 */
15412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015414 typval_T *argvars;
15415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416{
15417 char_u buf[NUMBUFLEN];
15418
15419 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015422 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15423 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424}
15425
15426/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015427 * "repeat()" function
15428 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015429 static void
15430f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015431 typval_T *argvars;
15432 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015433{
15434 char_u *p;
15435 int n;
15436 int slen;
15437 int len;
15438 char_u *r;
15439 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015440
15441 n = get_tv_number(&argvars[1]);
15442 if (argvars[0].v_type == VAR_LIST)
15443 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015444 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015445 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015446 if (list_extend(rettv->vval.v_list,
15447 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015448 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015449 }
15450 else
15451 {
15452 p = get_tv_string(&argvars[0]);
15453 rettv->v_type = VAR_STRING;
15454 rettv->vval.v_string = NULL;
15455
15456 slen = (int)STRLEN(p);
15457 len = slen * n;
15458 if (len <= 0)
15459 return;
15460
15461 r = alloc(len + 1);
15462 if (r != NULL)
15463 {
15464 for (i = 0; i < n; i++)
15465 mch_memmove(r + i * slen, p, (size_t)slen);
15466 r[len] = NUL;
15467 }
15468
15469 rettv->vval.v_string = r;
15470 }
15471}
15472
15473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474 * "resolve()" function
15475 */
15476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015478 typval_T *argvars;
15479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480{
15481 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015482#ifdef HAVE_READLINK
15483 char_u *buf = NULL;
15484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487#ifdef FEAT_SHORTCUT
15488 {
15489 char_u *v = NULL;
15490
15491 v = mch_resolve_shortcut(p);
15492 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015495 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 }
15497#else
15498# ifdef HAVE_READLINK
15499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 char_u *cpy;
15501 int len;
15502 char_u *remain = NULL;
15503 char_u *q;
15504 int is_relative_to_current = FALSE;
15505 int has_trailing_pathsep = FALSE;
15506 int limit = 100;
15507
15508 p = vim_strsave(p);
15509
15510 if (p[0] == '.' && (vim_ispathsep(p[1])
15511 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15512 is_relative_to_current = TRUE;
15513
15514 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015515 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015518 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520
15521 q = getnextcomp(p);
15522 if (*q != NUL)
15523 {
15524 /* Separate the first path component in "p", and keep the
15525 * remainder (beginning with the path separator). */
15526 remain = vim_strsave(q - 1);
15527 q[-1] = NUL;
15528 }
15529
Bram Moolenaard9462e32011-04-11 21:35:11 +020015530 buf = alloc(MAXPATHL + 1);
15531 if (buf == NULL)
15532 goto fail;
15533
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 for (;;)
15535 {
15536 for (;;)
15537 {
15538 len = readlink((char *)p, (char *)buf, MAXPATHL);
15539 if (len <= 0)
15540 break;
15541 buf[len] = NUL;
15542
15543 if (limit-- == 0)
15544 {
15545 vim_free(p);
15546 vim_free(remain);
15547 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 goto fail;
15550 }
15551
15552 /* Ensure that the result will have a trailing path separator
15553 * if the argument has one. */
15554 if (remain == NULL && has_trailing_pathsep)
15555 add_pathsep(buf);
15556
15557 /* Separate the first path component in the link value and
15558 * concatenate the remainders. */
15559 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15560 if (*q != NUL)
15561 {
15562 if (remain == NULL)
15563 remain = vim_strsave(q - 1);
15564 else
15565 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015566 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 if (cpy != NULL)
15568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569 vim_free(remain);
15570 remain = cpy;
15571 }
15572 }
15573 q[-1] = NUL;
15574 }
15575
15576 q = gettail(p);
15577 if (q > p && *q == NUL)
15578 {
15579 /* Ignore trailing path separator. */
15580 q[-1] = NUL;
15581 q = gettail(p);
15582 }
15583 if (q > p && !mch_isFullName(buf))
15584 {
15585 /* symlink is relative to directory of argument */
15586 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15587 if (cpy != NULL)
15588 {
15589 STRCPY(cpy, p);
15590 STRCPY(gettail(cpy), buf);
15591 vim_free(p);
15592 p = cpy;
15593 }
15594 }
15595 else
15596 {
15597 vim_free(p);
15598 p = vim_strsave(buf);
15599 }
15600 }
15601
15602 if (remain == NULL)
15603 break;
15604
15605 /* Append the first path component of "remain" to "p". */
15606 q = getnextcomp(remain + 1);
15607 len = q - remain - (*q != NUL);
15608 cpy = vim_strnsave(p, STRLEN(p) + len);
15609 if (cpy != NULL)
15610 {
15611 STRNCAT(cpy, remain, len);
15612 vim_free(p);
15613 p = cpy;
15614 }
15615 /* Shorten "remain". */
15616 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015617 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618 else
15619 {
15620 vim_free(remain);
15621 remain = NULL;
15622 }
15623 }
15624
15625 /* If the result is a relative path name, make it explicitly relative to
15626 * the current directory if and only if the argument had this form. */
15627 if (!vim_ispathsep(*p))
15628 {
15629 if (is_relative_to_current
15630 && *p != NUL
15631 && !(p[0] == '.'
15632 && (p[1] == NUL
15633 || vim_ispathsep(p[1])
15634 || (p[1] == '.'
15635 && (p[2] == NUL
15636 || vim_ispathsep(p[2]))))))
15637 {
15638 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015639 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 if (cpy != NULL)
15641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 vim_free(p);
15643 p = cpy;
15644 }
15645 }
15646 else if (!is_relative_to_current)
15647 {
15648 /* Strip leading "./". */
15649 q = p;
15650 while (q[0] == '.' && vim_ispathsep(q[1]))
15651 q += 2;
15652 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015653 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 }
15655 }
15656
15657 /* Ensure that the result will have no trailing path separator
15658 * if the argument had none. But keep "/" or "//". */
15659 if (!has_trailing_pathsep)
15660 {
15661 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015662 if (after_pathsep(p, q))
15663 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 }
15665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015666 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 }
15668# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015669 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670# endif
15671#endif
15672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674
15675#ifdef HAVE_READLINK
15676fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015677 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680}
15681
15682/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015683 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 */
15685 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015686f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015687 typval_T *argvars;
15688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689{
Bram Moolenaar33570922005-01-25 22:26:29 +000015690 list_T *l;
15691 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693 if (argvars[0].v_type != VAR_LIST)
15694 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015695 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015696 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015697 {
15698 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015699 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015700 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015701 while (li != NULL)
15702 {
15703 ni = li->li_prev;
15704 list_append(l, li);
15705 li = ni;
15706 }
15707 rettv->vval.v_list = l;
15708 rettv->v_type = VAR_LIST;
15709 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015710 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712}
15713
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015714#define SP_NOMOVE 0x01 /* don't move cursor */
15715#define SP_REPEAT 0x02 /* repeat to find outer pair */
15716#define SP_RETCOUNT 0x04 /* return matchcount */
15717#define SP_SETPCMARK 0x08 /* set previous context mark */
15718#define SP_START 0x10 /* accept match at start position */
15719#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15720#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015721
Bram Moolenaar33570922005-01-25 22:26:29 +000015722static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015723
15724/*
15725 * Get flags for a search function.
15726 * Possibly sets "p_ws".
15727 * Returns BACKWARD, FORWARD or zero (for an error).
15728 */
15729 static int
15730get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015731 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015732 int *flagsp;
15733{
15734 int dir = FORWARD;
15735 char_u *flags;
15736 char_u nbuf[NUMBUFLEN];
15737 int mask;
15738
15739 if (varp->v_type != VAR_UNKNOWN)
15740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015741 flags = get_tv_string_buf_chk(varp, nbuf);
15742 if (flags == NULL)
15743 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015744 while (*flags != NUL)
15745 {
15746 switch (*flags)
15747 {
15748 case 'b': dir = BACKWARD; break;
15749 case 'w': p_ws = TRUE; break;
15750 case 'W': p_ws = FALSE; break;
15751 default: mask = 0;
15752 if (flagsp != NULL)
15753 switch (*flags)
15754 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015755 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015756 case 'e': mask = SP_END; break;
15757 case 'm': mask = SP_RETCOUNT; break;
15758 case 'n': mask = SP_NOMOVE; break;
15759 case 'p': mask = SP_SUBPAT; break;
15760 case 'r': mask = SP_REPEAT; break;
15761 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762 }
15763 if (mask == 0)
15764 {
15765 EMSG2(_(e_invarg2), flags);
15766 dir = 0;
15767 }
15768 else
15769 *flagsp |= mask;
15770 }
15771 if (dir == 0)
15772 break;
15773 ++flags;
15774 }
15775 }
15776 return dir;
15777}
15778
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015780 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015782 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015783search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015784 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015785 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015786 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015788 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 char_u *pat;
15790 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015791 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 int save_p_ws = p_ws;
15793 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015794 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015795 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015796 proftime_T tm;
15797#ifdef FEAT_RELTIME
15798 long time_limit = 0;
15799#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015800 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015801 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015803 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015804 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015805 if (dir == 0)
15806 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015807 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015808 if (flags & SP_START)
15809 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015810 if (flags & SP_END)
15811 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015812
Bram Moolenaar76929292008-01-06 19:07:36 +000015813 /* Optional arguments: line number to stop searching and timeout. */
15814 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015815 {
15816 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15817 if (lnum_stop < 0)
15818 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015819#ifdef FEAT_RELTIME
15820 if (argvars[3].v_type != VAR_UNKNOWN)
15821 {
15822 time_limit = get_tv_number_chk(&argvars[3], NULL);
15823 if (time_limit < 0)
15824 goto theend;
15825 }
15826#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015827 }
15828
Bram Moolenaar76929292008-01-06 19:07:36 +000015829#ifdef FEAT_RELTIME
15830 /* Set the time limit, if there is one. */
15831 profile_setlimit(time_limit, &tm);
15832#endif
15833
Bram Moolenaar231334e2005-07-25 20:46:57 +000015834 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015835 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015836 * Check to make sure only those flags are set.
15837 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15838 * flags cannot be set. Check for that condition also.
15839 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015840 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015841 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015843 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015844 goto theend;
15845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015847 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015848 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015849 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015850 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015852 if (flags & SP_SUBPAT)
15853 retval = subpatnum;
15854 else
15855 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015856 if (flags & SP_SETPCMARK)
15857 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015859 if (match_pos != NULL)
15860 {
15861 /* Store the match cursor position */
15862 match_pos->lnum = pos.lnum;
15863 match_pos->col = pos.col + 1;
15864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 /* "/$" will put the cursor after the end of the line, may need to
15866 * correct that here */
15867 check_cursor();
15868 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015869
15870 /* If 'n' flag is used: restore cursor position. */
15871 if (flags & SP_NOMOVE)
15872 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015873 else
15874 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015875theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015877
15878 return retval;
15879}
15880
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015881#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015882
15883/*
15884 * round() is not in C90, use ceil() or floor() instead.
15885 */
15886 float_T
15887vim_round(f)
15888 float_T f;
15889{
15890 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15891}
15892
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015893/*
15894 * "round({float})" function
15895 */
15896 static void
15897f_round(argvars, rettv)
15898 typval_T *argvars;
15899 typval_T *rettv;
15900{
15901 float_T f;
15902
15903 rettv->v_type = VAR_FLOAT;
15904 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015905 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015906 else
15907 rettv->vval.v_float = 0.0;
15908}
15909#endif
15910
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015911/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015912 * "screenattr()" function
15913 */
15914 static void
15915f_screenattr(argvars, rettv)
15916 typval_T *argvars UNUSED;
15917 typval_T *rettv;
15918{
15919 int row;
15920 int col;
15921 int c;
15922
15923 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15924 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15925 if (row < 0 || row >= screen_Rows
15926 || col < 0 || col >= screen_Columns)
15927 c = -1;
15928 else
15929 c = ScreenAttrs[LineOffset[row] + col];
15930 rettv->vval.v_number = c;
15931}
15932
15933/*
15934 * "screenchar()" function
15935 */
15936 static void
15937f_screenchar(argvars, rettv)
15938 typval_T *argvars UNUSED;
15939 typval_T *rettv;
15940{
15941 int row;
15942 int col;
15943 int off;
15944 int c;
15945
15946 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15947 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15948 if (row < 0 || row >= screen_Rows
15949 || col < 0 || col >= screen_Columns)
15950 c = -1;
15951 else
15952 {
15953 off = LineOffset[row] + col;
15954#ifdef FEAT_MBYTE
15955 if (enc_utf8 && ScreenLinesUC[off] != 0)
15956 c = ScreenLinesUC[off];
15957 else
15958#endif
15959 c = ScreenLines[off];
15960 }
15961 rettv->vval.v_number = c;
15962}
15963
15964/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015965 * "screencol()" function
15966 *
15967 * First column is 1 to be consistent with virtcol().
15968 */
15969 static void
15970f_screencol(argvars, rettv)
15971 typval_T *argvars UNUSED;
15972 typval_T *rettv;
15973{
15974 rettv->vval.v_number = screen_screencol() + 1;
15975}
15976
15977/*
15978 * "screenrow()" function
15979 */
15980 static void
15981f_screenrow(argvars, rettv)
15982 typval_T *argvars UNUSED;
15983 typval_T *rettv;
15984{
15985 rettv->vval.v_number = screen_screenrow() + 1;
15986}
15987
15988/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015989 * "search()" function
15990 */
15991 static void
15992f_search(argvars, rettv)
15993 typval_T *argvars;
15994 typval_T *rettv;
15995{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015996 int flags = 0;
15997
15998 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999}
16000
Bram Moolenaar071d4272004-06-13 20:20:40 +000016001/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016002 * "searchdecl()" function
16003 */
16004 static void
16005f_searchdecl(argvars, rettv)
16006 typval_T *argvars;
16007 typval_T *rettv;
16008{
16009 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016010 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016011 int error = FALSE;
16012 char_u *name;
16013
16014 rettv->vval.v_number = 1; /* default: FAIL */
16015
16016 name = get_tv_string_chk(&argvars[0]);
16017 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016018 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016019 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016020 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16021 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16022 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016023 if (!error && name != NULL)
16024 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016025 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016026}
16027
16028/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016029 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016031 static int
16032searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016033 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016034 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035{
16036 char_u *spat, *mpat, *epat;
16037 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 int dir;
16040 int flags = 0;
16041 char_u nbuf1[NUMBUFLEN];
16042 char_u nbuf2[NUMBUFLEN];
16043 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016044 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016045 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016046 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016049 spat = get_tv_string_chk(&argvars[0]);
16050 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16051 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16052 if (spat == NULL || mpat == NULL || epat == NULL)
16053 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 /* Handle the optional fourth argument: flags */
16056 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016057 if (dir == 0)
16058 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016059
16060 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016061 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16062 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016063 if ((flags & (SP_END | SP_SUBPAT)) != 0
16064 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016065 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016066 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016067 goto theend;
16068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016070 /* Using 'r' implies 'W', otherwise it doesn't work. */
16071 if (flags & SP_REPEAT)
16072 p_ws = FALSE;
16073
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016074 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016075 if (argvars[3].v_type == VAR_UNKNOWN
16076 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 skip = (char_u *)"";
16078 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016081 if (argvars[5].v_type != VAR_UNKNOWN)
16082 {
16083 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16084 if (lnum_stop < 0)
16085 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016086#ifdef FEAT_RELTIME
16087 if (argvars[6].v_type != VAR_UNKNOWN)
16088 {
16089 time_limit = get_tv_number_chk(&argvars[6], NULL);
16090 if (time_limit < 0)
16091 goto theend;
16092 }
16093#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016094 }
16095 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016096 if (skip == NULL)
16097 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016099 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016100 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016101
16102theend:
16103 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016104
16105 return retval;
16106}
16107
16108/*
16109 * "searchpair()" function
16110 */
16111 static void
16112f_searchpair(argvars, rettv)
16113 typval_T *argvars;
16114 typval_T *rettv;
16115{
16116 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16117}
16118
16119/*
16120 * "searchpairpos()" function
16121 */
16122 static void
16123f_searchpairpos(argvars, rettv)
16124 typval_T *argvars;
16125 typval_T *rettv;
16126{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127 pos_T match_pos;
16128 int lnum = 0;
16129 int col = 0;
16130
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016131 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016132 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016133
16134 if (searchpair_cmn(argvars, &match_pos) > 0)
16135 {
16136 lnum = match_pos.lnum;
16137 col = match_pos.col;
16138 }
16139
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016140 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16141 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016142}
16143
16144/*
16145 * Search for a start/middle/end thing.
16146 * Used by searchpair(), see its documentation for the details.
16147 * Returns 0 or -1 for no match,
16148 */
16149 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016150do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16151 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016152 char_u *spat; /* start pattern */
16153 char_u *mpat; /* middle pattern */
16154 char_u *epat; /* end pattern */
16155 int dir; /* BACKWARD or FORWARD */
16156 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016157 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016158 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016159 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016160 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016161{
16162 char_u *save_cpo;
16163 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16164 long retval = 0;
16165 pos_T pos;
16166 pos_T firstpos;
16167 pos_T foundpos;
16168 pos_T save_cursor;
16169 pos_T save_pos;
16170 int n;
16171 int r;
16172 int nest = 1;
16173 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016174 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016175 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016176
16177 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16178 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016179 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016180
Bram Moolenaar76929292008-01-06 19:07:36 +000016181#ifdef FEAT_RELTIME
16182 /* Set the time limit, if there is one. */
16183 profile_setlimit(time_limit, &tm);
16184#endif
16185
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016186 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16187 * start/middle/end (pat3, for the top pair). */
16188 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16189 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16190 if (pat2 == NULL || pat3 == NULL)
16191 goto theend;
16192 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16193 if (*mpat == NUL)
16194 STRCPY(pat3, pat2);
16195 else
16196 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16197 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016198 if (flags & SP_START)
16199 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016200
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 save_cursor = curwin->w_cursor;
16202 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016203 clearpos(&firstpos);
16204 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 pat = pat3;
16206 for (;;)
16207 {
16208 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016209 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16211 /* didn't find it or found the first match again: FAIL */
16212 break;
16213
16214 if (firstpos.lnum == 0)
16215 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016216 if (equalpos(pos, foundpos))
16217 {
16218 /* Found the same position again. Can happen with a pattern that
16219 * has "\zs" at the end and searching backwards. Advance one
16220 * character and try again. */
16221 if (dir == BACKWARD)
16222 decl(&pos);
16223 else
16224 incl(&pos);
16225 }
16226 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016228 /* clear the start flag to avoid getting stuck here */
16229 options &= ~SEARCH_START;
16230
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 /* If the skip pattern matches, ignore this match. */
16232 if (*skip != NUL)
16233 {
16234 save_pos = curwin->w_cursor;
16235 curwin->w_cursor = pos;
16236 r = eval_to_bool(skip, &err, NULL, FALSE);
16237 curwin->w_cursor = save_pos;
16238 if (err)
16239 {
16240 /* Evaluating {skip} caused an error, break here. */
16241 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016242 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 break;
16244 }
16245 if (r)
16246 continue;
16247 }
16248
16249 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16250 {
16251 /* Found end when searching backwards or start when searching
16252 * forward: nested pair. */
16253 ++nest;
16254 pat = pat2; /* nested, don't search for middle */
16255 }
16256 else
16257 {
16258 /* Found end when searching forward or start when searching
16259 * backward: end of (nested) pair; or found middle in outer pair. */
16260 if (--nest == 1)
16261 pat = pat3; /* outer level, search for middle */
16262 }
16263
16264 if (nest == 0)
16265 {
16266 /* Found the match: return matchcount or line number. */
16267 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016268 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016270 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016271 if (flags & SP_SETPCMARK)
16272 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 curwin->w_cursor = pos;
16274 if (!(flags & SP_REPEAT))
16275 break;
16276 nest = 1; /* search for next unmatched */
16277 }
16278 }
16279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016280 if (match_pos != NULL)
16281 {
16282 /* Store the match cursor position */
16283 match_pos->lnum = curwin->w_cursor.lnum;
16284 match_pos->col = curwin->w_cursor.col + 1;
16285 }
16286
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016288 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 curwin->w_cursor = save_cursor;
16290
16291theend:
16292 vim_free(pat2);
16293 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016294 if (p_cpo == empty_option)
16295 p_cpo = save_cpo;
16296 else
16297 /* Darn, evaluating the {skip} expression changed the value. */
16298 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016299
16300 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301}
16302
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016303/*
16304 * "searchpos()" function
16305 */
16306 static void
16307f_searchpos(argvars, rettv)
16308 typval_T *argvars;
16309 typval_T *rettv;
16310{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016311 pos_T match_pos;
16312 int lnum = 0;
16313 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016314 int n;
16315 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016316
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016317 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016318 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016319
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016320 n = search_cmn(argvars, &match_pos, &flags);
16321 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016322 {
16323 lnum = match_pos.lnum;
16324 col = match_pos.col;
16325 }
16326
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016327 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16328 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016329 if (flags & SP_SUBPAT)
16330 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016331}
16332
16333
Bram Moolenaar0d660222005-01-07 21:51:51 +000016334 static void
16335f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016336 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016339#ifdef FEAT_CLIENTSERVER
16340 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016341 char_u *server = get_tv_string_chk(&argvars[0]);
16342 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016345 if (server == NULL || reply == NULL)
16346 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347 if (check_restricted() || check_secure())
16348 return;
16349# ifdef FEAT_X11
16350 if (check_connection() == FAIL)
16351 return;
16352# endif
16353
16354 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356 EMSG(_("E258: Unable to send to client"));
16357 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359 rettv->vval.v_number = 0;
16360#else
16361 rettv->vval.v_number = -1;
16362#endif
16363}
16364
Bram Moolenaar0d660222005-01-07 21:51:51 +000016365 static void
16366f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016367 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369{
16370 char_u *r = NULL;
16371
16372#ifdef FEAT_CLIENTSERVER
16373# ifdef WIN32
16374 r = serverGetVimNames();
16375# else
16376 make_connection();
16377 if (X_DISPLAY != NULL)
16378 r = serverGetVimNames(X_DISPLAY);
16379# endif
16380#endif
16381 rettv->v_type = VAR_STRING;
16382 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383}
16384
16385/*
16386 * "setbufvar()" function
16387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016389f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016391 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392{
16393 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016396 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 char_u nbuf[NUMBUFLEN];
16398
16399 if (check_restricted() || check_secure())
16400 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016401 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16402 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016403 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 varp = &argvars[2];
16405
16406 if (buf != NULL && varname != NULL && varp != NULL)
16407 {
16408 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410
16411 if (*varname == '&')
16412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016413 long numval;
16414 char_u *strval;
16415 int error = FALSE;
16416
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016418 numval = get_tv_number_chk(varp, &error);
16419 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016420 if (!error && strval != NULL)
16421 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422 }
16423 else
16424 {
16425 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16426 if (bufvarname != NULL)
16427 {
16428 STRCPY(bufvarname, "b:");
16429 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016430 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 vim_free(bufvarname);
16432 }
16433 }
16434
16435 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438}
16439
16440/*
16441 * "setcmdpos()" function
16442 */
16443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016444f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016445 typval_T *argvars;
16446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016448 int pos = (int)get_tv_number(&argvars[0]) - 1;
16449
16450 if (pos >= 0)
16451 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452}
16453
16454/*
16455 * "setline()" function
16456 */
16457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016458f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016459 typval_T *argvars;
16460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461{
16462 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016463 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016464 list_T *l = NULL;
16465 listitem_T *li = NULL;
16466 long added = 0;
16467 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016469 lnum = get_tv_lnum(&argvars[0]);
16470 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016472 l = argvars[1].vval.v_list;
16473 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016475 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016476 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016477
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016478 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016479 for (;;)
16480 {
16481 if (l != NULL)
16482 {
16483 /* list argument, get next string */
16484 if (li == NULL)
16485 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016486 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016487 li = li->li_next;
16488 }
16489
16490 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016491 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016492 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016493
16494 /* When coming here from Insert mode, sync undo, so that this can be
16495 * undone separately from what was previously inserted. */
16496 if (u_sync_once == 2)
16497 {
16498 u_sync_once = 1; /* notify that u_sync() was called */
16499 u_sync(TRUE);
16500 }
16501
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016502 if (lnum <= curbuf->b_ml.ml_line_count)
16503 {
16504 /* existing line, replace it */
16505 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16506 {
16507 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016508 if (lnum == curwin->w_cursor.lnum)
16509 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016510 rettv->vval.v_number = 0; /* OK */
16511 }
16512 }
16513 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16514 {
16515 /* lnum is one past the last line, append the line */
16516 ++added;
16517 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16518 rettv->vval.v_number = 0; /* OK */
16519 }
16520
16521 if (l == NULL) /* only one string argument */
16522 break;
16523 ++lnum;
16524 }
16525
16526 if (added > 0)
16527 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528}
16529
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016530static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16531
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016533 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016534 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016535 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016536set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016537 win_T *wp UNUSED;
16538 typval_T *list_arg UNUSED;
16539 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016540 typval_T *rettv;
16541{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016542#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016543 char_u *act;
16544 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016545#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016546
Bram Moolenaar2641f772005-03-25 21:58:17 +000016547 rettv->vval.v_number = -1;
16548
16549#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016550 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016551 EMSG(_(e_listreq));
16552 else
16553 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016554 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016555
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016556 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016557 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016558 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016559 if (act == NULL)
16560 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016561 if (*act == 'a' || *act == 'r')
16562 action = *act;
16563 }
16564
Bram Moolenaar81484f42012-12-05 15:16:47 +010016565 if (l != NULL && set_errorlist(wp, l, action,
16566 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016567 rettv->vval.v_number = 0;
16568 }
16569#endif
16570}
16571
16572/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016573 * "setloclist()" function
16574 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016575 static void
16576f_setloclist(argvars, rettv)
16577 typval_T *argvars;
16578 typval_T *rettv;
16579{
16580 win_T *win;
16581
16582 rettv->vval.v_number = -1;
16583
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016584 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016585 if (win != NULL)
16586 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16587}
16588
16589/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016590 * "setmatches()" function
16591 */
16592 static void
16593f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016594 typval_T *argvars UNUSED;
16595 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016596{
16597#ifdef FEAT_SEARCH_EXTRA
16598 list_T *l;
16599 listitem_T *li;
16600 dict_T *d;
16601
16602 rettv->vval.v_number = -1;
16603 if (argvars[0].v_type != VAR_LIST)
16604 {
16605 EMSG(_(e_listreq));
16606 return;
16607 }
16608 if ((l = argvars[0].vval.v_list) != NULL)
16609 {
16610
16611 /* To some extent make sure that we are dealing with a list from
16612 * "getmatches()". */
16613 li = l->lv_first;
16614 while (li != NULL)
16615 {
16616 if (li->li_tv.v_type != VAR_DICT
16617 || (d = li->li_tv.vval.v_dict) == NULL)
16618 {
16619 EMSG(_(e_invarg));
16620 return;
16621 }
16622 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16623 && dict_find(d, (char_u *)"pattern", -1) != NULL
16624 && dict_find(d, (char_u *)"priority", -1) != NULL
16625 && dict_find(d, (char_u *)"id", -1) != NULL))
16626 {
16627 EMSG(_(e_invarg));
16628 return;
16629 }
16630 li = li->li_next;
16631 }
16632
16633 clear_matches(curwin);
16634 li = l->lv_first;
16635 while (li != NULL)
16636 {
16637 d = li->li_tv.vval.v_dict;
16638 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16639 get_dict_string(d, (char_u *)"pattern", FALSE),
16640 (int)get_dict_number(d, (char_u *)"priority"),
16641 (int)get_dict_number(d, (char_u *)"id"));
16642 li = li->li_next;
16643 }
16644 rettv->vval.v_number = 0;
16645 }
16646#endif
16647}
16648
16649/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016650 * "setpos()" function
16651 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016652 static void
16653f_setpos(argvars, rettv)
16654 typval_T *argvars;
16655 typval_T *rettv;
16656{
16657 pos_T pos;
16658 int fnum;
16659 char_u *name;
16660
Bram Moolenaar08250432008-02-13 11:42:46 +000016661 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016662 name = get_tv_string_chk(argvars);
16663 if (name != NULL)
16664 {
16665 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16666 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016667 if (--pos.col < 0)
16668 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016669 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016670 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016671 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016672 if (fnum == curbuf->b_fnum)
16673 {
16674 curwin->w_cursor = pos;
16675 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016676 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016677 }
16678 else
16679 EMSG(_(e_invarg));
16680 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016681 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16682 {
16683 /* set mark */
16684 if (setmark_pos(name[1], &pos, fnum) == OK)
16685 rettv->vval.v_number = 0;
16686 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016687 else
16688 EMSG(_(e_invarg));
16689 }
16690 }
16691}
16692
16693/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016694 * "setqflist()" function
16695 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016696 static void
16697f_setqflist(argvars, rettv)
16698 typval_T *argvars;
16699 typval_T *rettv;
16700{
16701 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16702}
16703
16704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705 * "setreg()" function
16706 */
16707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016708f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016709 typval_T *argvars;
16710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711{
16712 int regname;
16713 char_u *strregname;
16714 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016715 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 int append;
16717 char_u yank_type;
16718 long block_len;
16719
16720 block_len = -1;
16721 yank_type = MAUTO;
16722 append = FALSE;
16723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016724 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016727 if (strregname == NULL)
16728 return; /* type error; errmsg already given */
16729 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 if (regname == 0 || regname == '@')
16731 regname = '"';
16732 else if (regname == '=')
16733 return;
16734
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016735 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016737 stropt = get_tv_string_chk(&argvars[2]);
16738 if (stropt == NULL)
16739 return; /* type error */
16740 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 switch (*stropt)
16742 {
16743 case 'a': case 'A': /* append */
16744 append = TRUE;
16745 break;
16746 case 'v': case 'c': /* character-wise selection */
16747 yank_type = MCHAR;
16748 break;
16749 case 'V': case 'l': /* line-wise selection */
16750 yank_type = MLINE;
16751 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 case 'b': case Ctrl_V: /* block-wise selection */
16753 yank_type = MBLOCK;
16754 if (VIM_ISDIGIT(stropt[1]))
16755 {
16756 ++stropt;
16757 block_len = getdigits(&stropt) - 1;
16758 --stropt;
16759 }
16760 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 }
16762 }
16763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016764 strval = get_tv_string_chk(&argvars[1]);
16765 if (strval != NULL)
16766 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016768 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769}
16770
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016771/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016772 * "settabvar()" function
16773 */
16774 static void
16775f_settabvar(argvars, rettv)
16776 typval_T *argvars;
16777 typval_T *rettv;
16778{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016779#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016780 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016781 tabpage_T *tp;
16782#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016783 char_u *varname, *tabvarname;
16784 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016785
16786 rettv->vval.v_number = 0;
16787
16788 if (check_restricted() || check_secure())
16789 return;
16790
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016791#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016792 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016793#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016794 varname = get_tv_string_chk(&argvars[1]);
16795 varp = &argvars[2];
16796
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016797 if (varname != NULL && varp != NULL
16798#ifdef FEAT_WINDOWS
16799 && tp != NULL
16800#endif
16801 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016802 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016803#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016804 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016805 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016806#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016807
16808 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16809 if (tabvarname != NULL)
16810 {
16811 STRCPY(tabvarname, "t:");
16812 STRCPY(tabvarname + 2, varname);
16813 set_var(tabvarname, varp, TRUE);
16814 vim_free(tabvarname);
16815 }
16816
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016817#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016818 /* Restore current tabpage */
16819 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016820 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016821#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016822 }
16823}
16824
16825/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016826 * "settabwinvar()" function
16827 */
16828 static void
16829f_settabwinvar(argvars, rettv)
16830 typval_T *argvars;
16831 typval_T *rettv;
16832{
16833 setwinvar(argvars, rettv, 1);
16834}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835
16836/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016837 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016840f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016841 typval_T *argvars;
16842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016844 setwinvar(argvars, rettv, 0);
16845}
16846
16847/*
16848 * "setwinvar()" and "settabwinvar()" functions
16849 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016850
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016851 static void
16852setwinvar(argvars, rettv, off)
16853 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016854 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016855 int off;
16856{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 win_T *win;
16858#ifdef FEAT_WINDOWS
16859 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016860 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861#endif
16862 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016865 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866
16867 if (check_restricted() || check_secure())
16868 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016869
16870#ifdef FEAT_WINDOWS
16871 if (off == 1)
16872 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16873 else
16874 tp = curtab;
16875#endif
16876 win = find_win_by_nr(&argvars[off], tp);
16877 varname = get_tv_string_chk(&argvars[off + 1]);
16878 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879
16880 if (win != NULL && varname != NULL && varp != NULL)
16881 {
16882#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016883 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016884 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885#endif
16886
16887 if (*varname == '&')
16888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016889 long numval;
16890 char_u *strval;
16891 int error = FALSE;
16892
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016894 numval = get_tv_number_chk(varp, &error);
16895 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016896 if (!error && strval != NULL)
16897 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 }
16899 else
16900 {
16901 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16902 if (winvarname != NULL)
16903 {
16904 STRCPY(winvarname, "w:");
16905 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016906 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 vim_free(winvarname);
16908 }
16909 }
16910
16911#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016912 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913#endif
16914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915}
16916
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016917#ifdef FEAT_CRYPT
16918/*
16919 * "sha256({string})" function
16920 */
16921 static void
16922f_sha256(argvars, rettv)
16923 typval_T *argvars;
16924 typval_T *rettv;
16925{
16926 char_u *p;
16927
16928 p = get_tv_string(&argvars[0]);
16929 rettv->vval.v_string = vim_strsave(
16930 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16931 rettv->v_type = VAR_STRING;
16932}
16933#endif /* FEAT_CRYPT */
16934
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016936 * "shellescape({string})" function
16937 */
16938 static void
16939f_shellescape(argvars, rettv)
16940 typval_T *argvars;
16941 typval_T *rettv;
16942{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016943 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010016944 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016945 rettv->v_type = VAR_STRING;
16946}
16947
16948/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016949 * shiftwidth() function
16950 */
16951 static void
16952f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016953 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016954 typval_T *rettv;
16955{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016956 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016957}
16958
16959/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 */
16962 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016964 typval_T *argvars;
16965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 p = get_tv_string(&argvars[0]);
16970 rettv->vval.v_string = vim_strsave(p);
16971 simplify_filename(rettv->vval.v_string); /* simplify in place */
16972 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973}
16974
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016975#ifdef FEAT_FLOAT
16976/*
16977 * "sin()" function
16978 */
16979 static void
16980f_sin(argvars, rettv)
16981 typval_T *argvars;
16982 typval_T *rettv;
16983{
16984 float_T f;
16985
16986 rettv->v_type = VAR_FLOAT;
16987 if (get_float_arg(argvars, &f) == OK)
16988 rettv->vval.v_float = sin(f);
16989 else
16990 rettv->vval.v_float = 0.0;
16991}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016992
16993/*
16994 * "sinh()" function
16995 */
16996 static void
16997f_sinh(argvars, rettv)
16998 typval_T *argvars;
16999 typval_T *rettv;
17000{
17001 float_T f;
17002
17003 rettv->v_type = VAR_FLOAT;
17004 if (get_float_arg(argvars, &f) == OK)
17005 rettv->vval.v_float = sinh(f);
17006 else
17007 rettv->vval.v_float = 0.0;
17008}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017009#endif
17010
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011static int
17012#ifdef __BORLANDC__
17013 _RTLENTRYF
17014#endif
17015 item_compare __ARGS((const void *s1, const void *s2));
17016static int
17017#ifdef __BORLANDC__
17018 _RTLENTRYF
17019#endif
17020 item_compare2 __ARGS((const void *s1, const void *s2));
17021
17022static int item_compare_ic;
17023static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017024static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017025static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026#define ITEM_COMPARE_FAIL 999
17027
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017029 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 static int
17032#ifdef __BORLANDC__
17033_RTLENTRYF
17034#endif
17035item_compare(s1, s2)
17036 const void *s1;
17037 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039 char_u *p1, *p2;
17040 char_u *tofree1, *tofree2;
17041 int res;
17042 char_u numbuf1[NUMBUFLEN];
17043 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017045 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17046 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017047 if (p1 == NULL)
17048 p1 = (char_u *)"";
17049 if (p2 == NULL)
17050 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051 if (item_compare_ic)
17052 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054 res = STRCMP(p1, p2);
17055 vim_free(tofree1);
17056 vim_free(tofree2);
17057 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058}
17059
17060 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061#ifdef __BORLANDC__
17062_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064item_compare2(s1, s2)
17065 const void *s1;
17066 const void *s2;
17067{
17068 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017069 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017070 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017073 /* shortcut after failure in previous call; compare all items equal */
17074 if (item_compare_func_err)
17075 return 0;
17076
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017077 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17078 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17080 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081
17082 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017083 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017084 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17085 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086 clear_tv(&argv[0]);
17087 clear_tv(&argv[1]);
17088
17089 if (res == FAIL)
17090 res = ITEM_COMPARE_FAIL;
17091 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017092 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17093 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017094 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 clear_tv(&rettv);
17096 return res;
17097}
17098
17099/*
17100 * "sort({list})" function
17101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017103f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017104 typval_T *argvars;
17105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106{
Bram Moolenaar33570922005-01-25 22:26:29 +000017107 list_T *l;
17108 listitem_T *li;
17109 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110 long len;
17111 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113 if (argvars[0].v_type != VAR_LIST)
17114 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 else
17116 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017117 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017118 if (l == NULL || tv_check_lock(l->lv_lock,
17119 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017120 return;
17121 rettv->vval.v_list = l;
17122 rettv->v_type = VAR_LIST;
17123 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125 len = list_len(l);
17126 if (len <= 1)
17127 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129 item_compare_ic = FALSE;
17130 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017131 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132 if (argvars[1].v_type != VAR_UNKNOWN)
17133 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017134 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137 else
17138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017139 int error = FALSE;
17140
17141 i = get_tv_number_chk(&argvars[1], &error);
17142 if (error)
17143 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 if (i == 1)
17145 item_compare_ic = TRUE;
17146 else
17147 item_compare_func = get_tv_string(&argvars[1]);
17148 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017149
17150 if (argvars[2].v_type != VAR_UNKNOWN)
17151 {
17152 /* optional third argument: {dict} */
17153 if (argvars[2].v_type != VAR_DICT)
17154 {
17155 EMSG(_(e_dictreq));
17156 return;
17157 }
17158 item_compare_selfdict = argvars[2].vval.v_dict;
17159 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017163 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017164 if (ptrs == NULL)
17165 return;
17166 i = 0;
17167 for (li = l->lv_first; li != NULL; li = li->li_next)
17168 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017170 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017171 /* test the compare function */
17172 if (item_compare_func != NULL
17173 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17174 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017175 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017177 {
17178 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017179 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 item_compare_func == NULL ? item_compare : item_compare2);
17181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 if (!item_compare_func_err)
17183 {
17184 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017185 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017186 l->lv_len = 0;
17187 for (i = 0; i < len; ++i)
17188 list_append(l, ptrs[i]);
17189 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017190 }
17191
17192 vim_free(ptrs);
17193 }
17194}
17195
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017196/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017197 * "soundfold({word})" function
17198 */
17199 static void
17200f_soundfold(argvars, rettv)
17201 typval_T *argvars;
17202 typval_T *rettv;
17203{
17204 char_u *s;
17205
17206 rettv->v_type = VAR_STRING;
17207 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017208#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017209 rettv->vval.v_string = eval_soundfold(s);
17210#else
17211 rettv->vval.v_string = vim_strsave(s);
17212#endif
17213}
17214
17215/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017216 * "spellbadword()" function
17217 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017218 static void
17219f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017220 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017221 typval_T *rettv;
17222{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017223 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017224 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017225 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017226
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017227 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017228 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017229
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017230#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017231 if (argvars[0].v_type == VAR_UNKNOWN)
17232 {
17233 /* Find the start and length of the badly spelled word. */
17234 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17235 if (len != 0)
17236 word = ml_get_cursor();
17237 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017238 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017239 {
17240 char_u *str = get_tv_string_chk(&argvars[0]);
17241 int capcol = -1;
17242
17243 if (str != NULL)
17244 {
17245 /* Check the argument for spelling. */
17246 while (*str != NUL)
17247 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017248 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017249 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017250 {
17251 word = str;
17252 break;
17253 }
17254 str += len;
17255 }
17256 }
17257 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017258#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017259
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017260 list_append_string(rettv->vval.v_list, word, len);
17261 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017262 attr == HLF_SPB ? "bad" :
17263 attr == HLF_SPR ? "rare" :
17264 attr == HLF_SPL ? "local" :
17265 attr == HLF_SPC ? "caps" :
17266 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017267}
17268
17269/*
17270 * "spellsuggest()" function
17271 */
17272 static void
17273f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017274 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017275 typval_T *rettv;
17276{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017277#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017278 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017279 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017280 int maxcount;
17281 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017282 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017283 listitem_T *li;
17284 int need_capital = FALSE;
17285#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017286
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017287 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017288 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017289
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017290#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017291 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017292 {
17293 str = get_tv_string(&argvars[0]);
17294 if (argvars[1].v_type != VAR_UNKNOWN)
17295 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017296 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017297 if (maxcount <= 0)
17298 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017299 if (argvars[2].v_type != VAR_UNKNOWN)
17300 {
17301 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17302 if (typeerr)
17303 return;
17304 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017305 }
17306 else
17307 maxcount = 25;
17308
Bram Moolenaar4770d092006-01-12 23:22:24 +000017309 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017310
17311 for (i = 0; i < ga.ga_len; ++i)
17312 {
17313 str = ((char_u **)ga.ga_data)[i];
17314
17315 li = listitem_alloc();
17316 if (li == NULL)
17317 vim_free(str);
17318 else
17319 {
17320 li->li_tv.v_type = VAR_STRING;
17321 li->li_tv.v_lock = 0;
17322 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017323 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017324 }
17325 }
17326 ga_clear(&ga);
17327 }
17328#endif
17329}
17330
Bram Moolenaar0d660222005-01-07 21:51:51 +000017331 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017332f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017333 typval_T *argvars;
17334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017335{
17336 char_u *str;
17337 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017338 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017339 regmatch_T regmatch;
17340 char_u patbuf[NUMBUFLEN];
17341 char_u *save_cpo;
17342 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017343 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017344 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017345 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017346
17347 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17348 save_cpo = p_cpo;
17349 p_cpo = (char_u *)"";
17350
17351 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017352 if (argvars[1].v_type != VAR_UNKNOWN)
17353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017354 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17355 if (pat == NULL)
17356 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017357 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017358 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017359 }
17360 if (pat == NULL || *pat == NUL)
17361 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017362
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017363 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365 if (typeerr)
17366 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367
Bram Moolenaar0d660222005-01-07 21:51:51 +000017368 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17369 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017371 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017372 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017373 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017374 if (*str == NUL)
17375 match = FALSE; /* empty item at the end */
17376 else
17377 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017378 if (match)
17379 end = regmatch.startp[0];
17380 else
17381 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017382 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17383 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017384 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017385 if (list_append_string(rettv->vval.v_list, str,
17386 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017387 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017388 }
17389 if (!match)
17390 break;
17391 /* Advance to just after the match. */
17392 if (regmatch.endp[0] > str)
17393 col = 0;
17394 else
17395 {
17396 /* Don't get stuck at the same match. */
17397#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017398 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017399#else
17400 col = 1;
17401#endif
17402 }
17403 str = regmatch.endp[0];
17404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405
Bram Moolenaar473de612013-06-08 18:19:48 +020017406 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408
Bram Moolenaar0d660222005-01-07 21:51:51 +000017409 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410}
17411
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017412#ifdef FEAT_FLOAT
17413/*
17414 * "sqrt()" function
17415 */
17416 static void
17417f_sqrt(argvars, rettv)
17418 typval_T *argvars;
17419 typval_T *rettv;
17420{
17421 float_T f;
17422
17423 rettv->v_type = VAR_FLOAT;
17424 if (get_float_arg(argvars, &f) == OK)
17425 rettv->vval.v_float = sqrt(f);
17426 else
17427 rettv->vval.v_float = 0.0;
17428}
17429
17430/*
17431 * "str2float()" function
17432 */
17433 static void
17434f_str2float(argvars, rettv)
17435 typval_T *argvars;
17436 typval_T *rettv;
17437{
17438 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17439
17440 if (*p == '+')
17441 p = skipwhite(p + 1);
17442 (void)string2float(p, &rettv->vval.v_float);
17443 rettv->v_type = VAR_FLOAT;
17444}
17445#endif
17446
Bram Moolenaar2c932302006-03-18 21:42:09 +000017447/*
17448 * "str2nr()" function
17449 */
17450 static void
17451f_str2nr(argvars, rettv)
17452 typval_T *argvars;
17453 typval_T *rettv;
17454{
17455 int base = 10;
17456 char_u *p;
17457 long n;
17458
17459 if (argvars[1].v_type != VAR_UNKNOWN)
17460 {
17461 base = get_tv_number(&argvars[1]);
17462 if (base != 8 && base != 10 && base != 16)
17463 {
17464 EMSG(_(e_invarg));
17465 return;
17466 }
17467 }
17468
17469 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017470 if (*p == '+')
17471 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017472 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17473 rettv->vval.v_number = n;
17474}
17475
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476#ifdef HAVE_STRFTIME
17477/*
17478 * "strftime({format}[, {time}])" function
17479 */
17480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017481f_strftime(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 result_buf[256];
17486 struct tm *curtime;
17487 time_t seconds;
17488 char_u *p;
17489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017493 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 seconds = time(NULL);
17495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 curtime = localtime(&seconds);
17498 /* MSVC returns NULL for an invalid value of seconds. */
17499 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017500 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 else
17502 {
17503# ifdef FEAT_MBYTE
17504 vimconv_T conv;
17505 char_u *enc;
17506
17507 conv.vc_type = CONV_NONE;
17508 enc = enc_locale();
17509 convert_setup(&conv, p_enc, enc);
17510 if (conv.vc_type != CONV_NONE)
17511 p = string_convert(&conv, p, NULL);
17512# endif
17513 if (p != NULL)
17514 (void)strftime((char *)result_buf, sizeof(result_buf),
17515 (char *)p, curtime);
17516 else
17517 result_buf[0] = NUL;
17518
17519# ifdef FEAT_MBYTE
17520 if (conv.vc_type != CONV_NONE)
17521 vim_free(p);
17522 convert_setup(&conv, enc, p_enc);
17523 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017524 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 else
17526# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528
17529# ifdef FEAT_MBYTE
17530 /* Release conversion descriptors */
17531 convert_setup(&conv, NULL, NULL);
17532 vim_free(enc);
17533# endif
17534 }
17535}
17536#endif
17537
17538/*
17539 * "stridx()" function
17540 */
17541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017542f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 typval_T *argvars;
17544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545{
17546 char_u buf[NUMBUFLEN];
17547 char_u *needle;
17548 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017551 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017553 needle = get_tv_string_chk(&argvars[1]);
17554 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017556 if (needle == NULL || haystack == NULL)
17557 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558
Bram Moolenaar33570922005-01-25 22:26:29 +000017559 if (argvars[2].v_type != VAR_UNKNOWN)
17560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017561 int error = FALSE;
17562
17563 start_idx = get_tv_number_chk(&argvars[2], &error);
17564 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017565 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017566 if (start_idx >= 0)
17567 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017568 }
17569
17570 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17571 if (pos != NULL)
17572 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573}
17574
17575/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017576 * "string()" function
17577 */
17578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017579f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 typval_T *argvars;
17581 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017582{
17583 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017584 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017587 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017588 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017589 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017590 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591}
17592
17593/*
17594 * "strlen()" function
17595 */
17596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017597f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 typval_T *argvars;
17599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017601 rettv->vval.v_number = (varnumber_T)(STRLEN(
17602 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603}
17604
17605/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017606 * "strchars()" function
17607 */
17608 static void
17609f_strchars(argvars, rettv)
17610 typval_T *argvars;
17611 typval_T *rettv;
17612{
17613 char_u *s = get_tv_string(&argvars[0]);
17614#ifdef FEAT_MBYTE
17615 varnumber_T len = 0;
17616
17617 while (*s != NUL)
17618 {
17619 mb_cptr2char_adv(&s);
17620 ++len;
17621 }
17622 rettv->vval.v_number = len;
17623#else
17624 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17625#endif
17626}
17627
17628/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017629 * "strdisplaywidth()" function
17630 */
17631 static void
17632f_strdisplaywidth(argvars, rettv)
17633 typval_T *argvars;
17634 typval_T *rettv;
17635{
17636 char_u *s = get_tv_string(&argvars[0]);
17637 int col = 0;
17638
17639 if (argvars[1].v_type != VAR_UNKNOWN)
17640 col = get_tv_number(&argvars[1]);
17641
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017642 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017643}
17644
17645/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017646 * "strwidth()" function
17647 */
17648 static void
17649f_strwidth(argvars, rettv)
17650 typval_T *argvars;
17651 typval_T *rettv;
17652{
17653 char_u *s = get_tv_string(&argvars[0]);
17654
17655 rettv->vval.v_number = (varnumber_T)(
17656#ifdef FEAT_MBYTE
17657 mb_string2cells(s, -1)
17658#else
17659 STRLEN(s)
17660#endif
17661 );
17662}
17663
17664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 * "strpart()" function
17666 */
17667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017669 typval_T *argvars;
17670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671{
17672 char_u *p;
17673 int n;
17674 int len;
17675 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017676 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017678 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 slen = (int)STRLEN(p);
17680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681 n = get_tv_number_chk(&argvars[1], &error);
17682 if (error)
17683 len = 0;
17684 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 else
17687 len = slen - n; /* default len: all bytes that are available. */
17688
17689 /*
17690 * Only return the overlap between the specified part and the actual
17691 * string.
17692 */
17693 if (n < 0)
17694 {
17695 len += n;
17696 n = 0;
17697 }
17698 else if (n > slen)
17699 n = slen;
17700 if (len < 0)
17701 len = 0;
17702 else if (n + len > slen)
17703 len = slen - n;
17704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017705 rettv->v_type = VAR_STRING;
17706 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707}
17708
17709/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017710 * "strridx()" function
17711 */
17712 static void
17713f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 typval_T *argvars;
17715 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017716{
17717 char_u buf[NUMBUFLEN];
17718 char_u *needle;
17719 char_u *haystack;
17720 char_u *rest;
17721 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017722 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017724 needle = get_tv_string_chk(&argvars[1]);
17725 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017726
17727 rettv->vval.v_number = -1;
17728 if (needle == NULL || haystack == NULL)
17729 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017730
17731 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017732 if (argvars[2].v_type != VAR_UNKNOWN)
17733 {
17734 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017735 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017736 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017737 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017738 }
17739 else
17740 end_idx = haystack_len;
17741
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017743 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017744 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017745 lastmatch = haystack + end_idx;
17746 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017747 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017748 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017749 for (rest = haystack; *rest != '\0'; ++rest)
17750 {
17751 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017752 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017753 break;
17754 lastmatch = rest;
17755 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017756 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017757
17758 if (lastmatch == NULL)
17759 rettv->vval.v_number = -1;
17760 else
17761 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17762}
17763
17764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 * "strtrans()" function
17766 */
17767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017769 typval_T *argvars;
17770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017772 rettv->v_type = VAR_STRING;
17773 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774}
17775
17776/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017777 * "submatch()" function
17778 */
17779 static void
17780f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017781 typval_T *argvars;
17782 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017783{
17784 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017785 rettv->vval.v_string =
17786 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017787}
17788
17789/*
17790 * "substitute()" function
17791 */
17792 static void
17793f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017794 typval_T *argvars;
17795 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017796{
17797 char_u patbuf[NUMBUFLEN];
17798 char_u subbuf[NUMBUFLEN];
17799 char_u flagsbuf[NUMBUFLEN];
17800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017801 char_u *str = get_tv_string_chk(&argvars[0]);
17802 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17803 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17804 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17805
Bram Moolenaar0d660222005-01-07 21:51:51 +000017806 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017807 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17808 rettv->vval.v_string = NULL;
17809 else
17810 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017811}
17812
17813/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017814 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017817f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017818 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820{
17821 int id = 0;
17822#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017823 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 long col;
17825 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017826 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017828 lnum = get_tv_lnum(argvars); /* -1 on type error */
17829 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17830 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017832 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017833 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017834 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835#endif
17836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017837 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838}
17839
17840/*
17841 * "synIDattr(id, what [, mode])" function
17842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017844f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017845 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847{
17848 char_u *p = NULL;
17849#ifdef FEAT_SYN_HL
17850 int id;
17851 char_u *what;
17852 char_u *mode;
17853 char_u modebuf[NUMBUFLEN];
17854 int modec;
17855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017856 id = get_tv_number(&argvars[0]);
17857 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017858 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017860 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017862 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 modec = 0; /* replace invalid with current */
17864 }
17865 else
17866 {
17867#ifdef FEAT_GUI
17868 if (gui.in_use)
17869 modec = 'g';
17870 else
17871#endif
17872 if (t_colors > 1)
17873 modec = 'c';
17874 else
17875 modec = 't';
17876 }
17877
17878
17879 switch (TOLOWER_ASC(what[0]))
17880 {
17881 case 'b':
17882 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17883 p = highlight_color(id, what, modec);
17884 else /* bold */
17885 p = highlight_has_attr(id, HL_BOLD, modec);
17886 break;
17887
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017888 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 p = highlight_color(id, what, modec);
17890 break;
17891
17892 case 'i':
17893 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17894 p = highlight_has_attr(id, HL_INVERSE, modec);
17895 else /* italic */
17896 p = highlight_has_attr(id, HL_ITALIC, modec);
17897 break;
17898
17899 case 'n': /* name */
17900 p = get_highlight_name(NULL, id - 1);
17901 break;
17902
17903 case 'r': /* reverse */
17904 p = highlight_has_attr(id, HL_INVERSE, modec);
17905 break;
17906
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017907 case 's':
17908 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17909 p = highlight_color(id, what, modec);
17910 else /* standout */
17911 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 break;
17913
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017914 case 'u':
17915 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17916 /* underline */
17917 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17918 else
17919 /* undercurl */
17920 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 break;
17922 }
17923
17924 if (p != NULL)
17925 p = vim_strsave(p);
17926#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017927 rettv->v_type = VAR_STRING;
17928 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929}
17930
17931/*
17932 * "synIDtrans(id)" function
17933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017935f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017936 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938{
17939 int id;
17940
17941#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017942 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943
17944 if (id > 0)
17945 id = syn_get_final_id(id);
17946 else
17947#endif
17948 id = 0;
17949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017950 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951}
17952
17953/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017954 * "synconcealed(lnum, col)" function
17955 */
17956 static void
17957f_synconcealed(argvars, rettv)
17958 typval_T *argvars UNUSED;
17959 typval_T *rettv;
17960{
17961#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17962 long lnum;
17963 long col;
17964 int syntax_flags = 0;
17965 int cchar;
17966 int matchid = 0;
17967 char_u str[NUMBUFLEN];
17968#endif
17969
17970 rettv->v_type = VAR_LIST;
17971 rettv->vval.v_list = NULL;
17972
17973#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17974 lnum = get_tv_lnum(argvars); /* -1 on type error */
17975 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17976
17977 vim_memset(str, NUL, sizeof(str));
17978
17979 if (rettv_list_alloc(rettv) != FAIL)
17980 {
17981 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17982 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17983 && curwin->w_p_cole > 0)
17984 {
17985 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17986 syntax_flags = get_syntax_info(&matchid);
17987
17988 /* get the conceal character */
17989 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17990 {
17991 cchar = syn_get_sub_char();
17992 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17993 cchar = lcs_conceal;
17994 if (cchar != NUL)
17995 {
17996# ifdef FEAT_MBYTE
17997 if (has_mbyte)
17998 (*mb_char2bytes)(cchar, str);
17999 else
18000# endif
18001 str[0] = cchar;
18002 }
18003 }
18004 }
18005
18006 list_append_number(rettv->vval.v_list,
18007 (syntax_flags & HL_CONCEAL) != 0);
18008 /* -1 to auto-determine strlen */
18009 list_append_string(rettv->vval.v_list, str, -1);
18010 list_append_number(rettv->vval.v_list, matchid);
18011 }
18012#endif
18013}
18014
18015/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018016 * "synstack(lnum, col)" function
18017 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018018 static void
18019f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018020 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018021 typval_T *rettv;
18022{
18023#ifdef FEAT_SYN_HL
18024 long lnum;
18025 long col;
18026 int i;
18027 int id;
18028#endif
18029
18030 rettv->v_type = VAR_LIST;
18031 rettv->vval.v_list = NULL;
18032
18033#ifdef FEAT_SYN_HL
18034 lnum = get_tv_lnum(argvars); /* -1 on type error */
18035 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18036
18037 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018038 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018039 && rettv_list_alloc(rettv) != FAIL)
18040 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018041 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018042 for (i = 0; ; ++i)
18043 {
18044 id = syn_get_stack_item(i);
18045 if (id < 0)
18046 break;
18047 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18048 break;
18049 }
18050 }
18051#endif
18052}
18053
18054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 * "system()" function
18056 */
18057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018058f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018059 typval_T *argvars;
18060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018062 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018064 char_u *infile = NULL;
18065 char_u buf[NUMBUFLEN];
18066 int err = FALSE;
18067 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018069 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018070 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018071
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018072 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018073 {
18074 /*
18075 * Write the string to a temp file, to be used for input of the shell
18076 * command.
18077 */
18078 if ((infile = vim_tempname('i')) == NULL)
18079 {
18080 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018081 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018082 }
18083
18084 fd = mch_fopen((char *)infile, WRITEBIN);
18085 if (fd == NULL)
18086 {
18087 EMSG2(_(e_notopen), infile);
18088 goto done;
18089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018090 p = get_tv_string_buf_chk(&argvars[1], buf);
18091 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018092 {
18093 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018094 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018095 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018096 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18097 err = TRUE;
18098 if (fclose(fd) != 0)
18099 err = TRUE;
18100 if (err)
18101 {
18102 EMSG(_("E677: Error writing temp file"));
18103 goto done;
18104 }
18105 }
18106
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018107 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18108 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018109
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110#ifdef USE_CR
18111 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018112 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 {
18114 char_u *s;
18115
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018116 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 {
18118 if (*s == CAR)
18119 *s = NL;
18120 }
18121 }
18122#else
18123# ifdef USE_CRNL
18124 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018125 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 {
18127 char_u *s, *d;
18128
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018129 d = res;
18130 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 {
18132 if (s[0] == CAR && s[1] == NL)
18133 ++s;
18134 *d++ = *s;
18135 }
18136 *d = NUL;
18137 }
18138# endif
18139#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018140
18141done:
18142 if (infile != NULL)
18143 {
18144 mch_remove(infile);
18145 vim_free(infile);
18146 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018147 rettv->v_type = VAR_STRING;
18148 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149}
18150
18151/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018152 * "tabpagebuflist()" function
18153 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018154 static void
18155f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018156 typval_T *argvars UNUSED;
18157 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018158{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018159#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018160 tabpage_T *tp;
18161 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018162
18163 if (argvars[0].v_type == VAR_UNKNOWN)
18164 wp = firstwin;
18165 else
18166 {
18167 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18168 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018169 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018170 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018171 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018172 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018173 for (; wp != NULL; wp = wp->w_next)
18174 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018175 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018176 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018177 }
18178#endif
18179}
18180
18181
18182/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018183 * "tabpagenr()" function
18184 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018185 static void
18186f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018187 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018188 typval_T *rettv;
18189{
18190 int nr = 1;
18191#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018192 char_u *arg;
18193
18194 if (argvars[0].v_type != VAR_UNKNOWN)
18195 {
18196 arg = get_tv_string_chk(&argvars[0]);
18197 nr = 0;
18198 if (arg != NULL)
18199 {
18200 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018201 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018202 else
18203 EMSG2(_(e_invexpr2), arg);
18204 }
18205 }
18206 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018207 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018208#endif
18209 rettv->vval.v_number = nr;
18210}
18211
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018212
18213#ifdef FEAT_WINDOWS
18214static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18215
18216/*
18217 * Common code for tabpagewinnr() and winnr().
18218 */
18219 static int
18220get_winnr(tp, argvar)
18221 tabpage_T *tp;
18222 typval_T *argvar;
18223{
18224 win_T *twin;
18225 int nr = 1;
18226 win_T *wp;
18227 char_u *arg;
18228
18229 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18230 if (argvar->v_type != VAR_UNKNOWN)
18231 {
18232 arg = get_tv_string_chk(argvar);
18233 if (arg == NULL)
18234 nr = 0; /* type error; errmsg already given */
18235 else if (STRCMP(arg, "$") == 0)
18236 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18237 else if (STRCMP(arg, "#") == 0)
18238 {
18239 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18240 if (twin == NULL)
18241 nr = 0;
18242 }
18243 else
18244 {
18245 EMSG2(_(e_invexpr2), arg);
18246 nr = 0;
18247 }
18248 }
18249
18250 if (nr > 0)
18251 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18252 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018253 {
18254 if (wp == NULL)
18255 {
18256 /* didn't find it in this tabpage */
18257 nr = 0;
18258 break;
18259 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018260 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018261 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018262 return nr;
18263}
18264#endif
18265
18266/*
18267 * "tabpagewinnr()" function
18268 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018269 static void
18270f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018271 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018272 typval_T *rettv;
18273{
18274 int nr = 1;
18275#ifdef FEAT_WINDOWS
18276 tabpage_T *tp;
18277
18278 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18279 if (tp == NULL)
18280 nr = 0;
18281 else
18282 nr = get_winnr(tp, &argvars[1]);
18283#endif
18284 rettv->vval.v_number = nr;
18285}
18286
18287
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018288/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018289 * "tagfiles()" function
18290 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018291 static void
18292f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018293 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018294 typval_T *rettv;
18295{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018296 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018297 tagname_T tn;
18298 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018300 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018301 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018302 fname = alloc(MAXPATHL);
18303 if (fname == NULL)
18304 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018305
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018306 for (first = TRUE; ; first = FALSE)
18307 if (get_tagfname(&tn, first, fname) == FAIL
18308 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018309 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018310 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018311 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018312}
18313
18314/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018315 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018316 */
18317 static void
18318f_taglist(argvars, rettv)
18319 typval_T *argvars;
18320 typval_T *rettv;
18321{
18322 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018323
18324 tag_pattern = get_tv_string(&argvars[0]);
18325
18326 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018327 if (*tag_pattern == NUL)
18328 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018329
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018330 if (rettv_list_alloc(rettv) == OK)
18331 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018332}
18333
18334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 * "tempname()" function
18336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018338f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018339 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341{
18342 static int x = 'A';
18343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018344 rettv->v_type = VAR_STRING;
18345 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346
18347 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18348 * names. Skip 'I' and 'O', they are used for shell redirection. */
18349 do
18350 {
18351 if (x == 'Z')
18352 x = '0';
18353 else if (x == '9')
18354 x = 'A';
18355 else
18356 {
18357#ifdef EBCDIC
18358 if (x == 'I')
18359 x = 'J';
18360 else if (x == 'R')
18361 x = 'S';
18362 else
18363#endif
18364 ++x;
18365 }
18366 } while (x == 'I' || x == 'O');
18367}
18368
18369/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018370 * "test(list)" function: Just checking the walls...
18371 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018372 static void
18373f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018374 typval_T *argvars UNUSED;
18375 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018376{
18377 /* Used for unit testing. Change the code below to your liking. */
18378#if 0
18379 listitem_T *li;
18380 list_T *l;
18381 char_u *bad, *good;
18382
18383 if (argvars[0].v_type != VAR_LIST)
18384 return;
18385 l = argvars[0].vval.v_list;
18386 if (l == NULL)
18387 return;
18388 li = l->lv_first;
18389 if (li == NULL)
18390 return;
18391 bad = get_tv_string(&li->li_tv);
18392 li = li->li_next;
18393 if (li == NULL)
18394 return;
18395 good = get_tv_string(&li->li_tv);
18396 rettv->vval.v_number = test_edit_score(bad, good);
18397#endif
18398}
18399
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018400#ifdef FEAT_FLOAT
18401/*
18402 * "tan()" function
18403 */
18404 static void
18405f_tan(argvars, rettv)
18406 typval_T *argvars;
18407 typval_T *rettv;
18408{
18409 float_T f;
18410
18411 rettv->v_type = VAR_FLOAT;
18412 if (get_float_arg(argvars, &f) == OK)
18413 rettv->vval.v_float = tan(f);
18414 else
18415 rettv->vval.v_float = 0.0;
18416}
18417
18418/*
18419 * "tanh()" function
18420 */
18421 static void
18422f_tanh(argvars, rettv)
18423 typval_T *argvars;
18424 typval_T *rettv;
18425{
18426 float_T f;
18427
18428 rettv->v_type = VAR_FLOAT;
18429 if (get_float_arg(argvars, &f) == OK)
18430 rettv->vval.v_float = tanh(f);
18431 else
18432 rettv->vval.v_float = 0.0;
18433}
18434#endif
18435
Bram Moolenaard52d9742005-08-21 22:20:28 +000018436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 * "tolower(string)" function
18438 */
18439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018440f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018441 typval_T *argvars;
18442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443{
18444 char_u *p;
18445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018446 p = vim_strsave(get_tv_string(&argvars[0]));
18447 rettv->v_type = VAR_STRING;
18448 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449
18450 if (p != NULL)
18451 while (*p != NUL)
18452 {
18453#ifdef FEAT_MBYTE
18454 int l;
18455
18456 if (enc_utf8)
18457 {
18458 int c, lc;
18459
18460 c = utf_ptr2char(p);
18461 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018462 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 /* TODO: reallocate string when byte count changes. */
18464 if (utf_char2len(lc) == l)
18465 utf_char2bytes(lc, p);
18466 p += l;
18467 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018468 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 p += l; /* skip multi-byte character */
18470 else
18471#endif
18472 {
18473 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18474 ++p;
18475 }
18476 }
18477}
18478
18479/*
18480 * "toupper(string)" function
18481 */
18482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018484 typval_T *argvars;
18485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018487 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018488 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489}
18490
18491/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018492 * "tr(string, fromstr, tostr)" function
18493 */
18494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018495f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018496 typval_T *argvars;
18497 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018498{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018499 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018500 char_u *fromstr;
18501 char_u *tostr;
18502 char_u *p;
18503#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018504 int inlen;
18505 int fromlen;
18506 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018507 int idx;
18508 char_u *cpstr;
18509 int cplen;
18510 int first = TRUE;
18511#endif
18512 char_u buf[NUMBUFLEN];
18513 char_u buf2[NUMBUFLEN];
18514 garray_T ga;
18515
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018516 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018517 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18518 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018519
18520 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018521 rettv->v_type = VAR_STRING;
18522 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018523 if (fromstr == NULL || tostr == NULL)
18524 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018525 ga_init2(&ga, (int)sizeof(char), 80);
18526
18527#ifdef FEAT_MBYTE
18528 if (!has_mbyte)
18529#endif
18530 /* not multi-byte: fromstr and tostr must be the same length */
18531 if (STRLEN(fromstr) != STRLEN(tostr))
18532 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018533#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018534error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018535#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018536 EMSG2(_(e_invarg2), fromstr);
18537 ga_clear(&ga);
18538 return;
18539 }
18540
18541 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018542 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018543 {
18544#ifdef FEAT_MBYTE
18545 if (has_mbyte)
18546 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018547 inlen = (*mb_ptr2len)(in_str);
18548 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018549 cplen = inlen;
18550 idx = 0;
18551 for (p = fromstr; *p != NUL; p += fromlen)
18552 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018553 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018554 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018555 {
18556 for (p = tostr; *p != NUL; p += tolen)
18557 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018558 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018559 if (idx-- == 0)
18560 {
18561 cplen = tolen;
18562 cpstr = p;
18563 break;
18564 }
18565 }
18566 if (*p == NUL) /* tostr is shorter than fromstr */
18567 goto error;
18568 break;
18569 }
18570 ++idx;
18571 }
18572
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018573 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018574 {
18575 /* Check that fromstr and tostr have the same number of
18576 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018577 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018578 first = FALSE;
18579 for (p = tostr; *p != NUL; p += tolen)
18580 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018581 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018582 --idx;
18583 }
18584 if (idx != 0)
18585 goto error;
18586 }
18587
18588 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018589 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018590 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018591
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018592 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018593 }
18594 else
18595#endif
18596 {
18597 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018598 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018599 if (p != NULL)
18600 ga_append(&ga, tostr[p - fromstr]);
18601 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018602 ga_append(&ga, *in_str);
18603 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018604 }
18605 }
18606
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018607 /* add a terminating NUL */
18608 ga_grow(&ga, 1);
18609 ga_append(&ga, NUL);
18610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018611 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018612}
18613
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018614#ifdef FEAT_FLOAT
18615/*
18616 * "trunc({float})" function
18617 */
18618 static void
18619f_trunc(argvars, rettv)
18620 typval_T *argvars;
18621 typval_T *rettv;
18622{
18623 float_T f;
18624
18625 rettv->v_type = VAR_FLOAT;
18626 if (get_float_arg(argvars, &f) == OK)
18627 /* trunc() is not in C90, use floor() or ceil() instead. */
18628 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18629 else
18630 rettv->vval.v_float = 0.0;
18631}
18632#endif
18633
Bram Moolenaar8299df92004-07-10 09:47:34 +000018634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 * "type(expr)" function
18636 */
18637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018638f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018639 typval_T *argvars;
18640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018642 int n;
18643
18644 switch (argvars[0].v_type)
18645 {
18646 case VAR_NUMBER: n = 0; break;
18647 case VAR_STRING: n = 1; break;
18648 case VAR_FUNC: n = 2; break;
18649 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018650 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018651#ifdef FEAT_FLOAT
18652 case VAR_FLOAT: n = 5; break;
18653#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018654 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18655 }
18656 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657}
18658
18659/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018660 * "undofile(name)" function
18661 */
18662 static void
18663f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018664 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018665 typval_T *rettv;
18666{
18667 rettv->v_type = VAR_STRING;
18668#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018669 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018670 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018671
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018672 if (*fname == NUL)
18673 {
18674 /* If there is no file name there will be no undo file. */
18675 rettv->vval.v_string = NULL;
18676 }
18677 else
18678 {
18679 char_u *ffname = FullName_save(fname, FALSE);
18680
18681 if (ffname != NULL)
18682 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18683 vim_free(ffname);
18684 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018685 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018686#else
18687 rettv->vval.v_string = NULL;
18688#endif
18689}
18690
18691/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018692 * "undotree()" function
18693 */
18694 static void
18695f_undotree(argvars, rettv)
18696 typval_T *argvars UNUSED;
18697 typval_T *rettv;
18698{
18699 if (rettv_dict_alloc(rettv) == OK)
18700 {
18701 dict_T *dict = rettv->vval.v_dict;
18702 list_T *list;
18703
Bram Moolenaar730cde92010-06-27 05:18:54 +020018704 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018705 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018706 dict_add_nr_str(dict, "save_last",
18707 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018708 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18709 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018710 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018711
18712 list = list_alloc();
18713 if (list != NULL)
18714 {
18715 u_eval_tree(curbuf->b_u_oldhead, list);
18716 dict_add_list(dict, "entries", list);
18717 }
18718 }
18719}
18720
18721/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018722 * "values(dict)" function
18723 */
18724 static void
18725f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018726 typval_T *argvars;
18727 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018728{
18729 dict_list(argvars, rettv, 1);
18730}
18731
18732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 * "virtcol(string)" function
18734 */
18735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018736f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018737 typval_T *argvars;
18738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739{
18740 colnr_T vcol = 0;
18741 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018742 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018744 fp = var2fpos(&argvars[0], FALSE, &fnum);
18745 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18746 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 {
18748 getvvcol(curwin, fp, NULL, NULL, &vcol);
18749 ++vcol;
18750 }
18751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753}
18754
18755/*
18756 * "visualmode()" function
18757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018760 typval_T *argvars UNUSED;
18761 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 char_u str[2];
18764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018765 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 str[0] = curbuf->b_visual_mode_eval;
18767 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018768 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769
18770 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018771 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773}
18774
18775/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018776 * "wildmenumode()" function
18777 */
18778 static void
18779f_wildmenumode(argvars, rettv)
18780 typval_T *argvars UNUSED;
18781 typval_T *rettv UNUSED;
18782{
18783#ifdef FEAT_WILDMENU
18784 if (wild_menu_showing)
18785 rettv->vval.v_number = 1;
18786#endif
18787}
18788
18789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 * "winbufnr(nr)" function
18791 */
18792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793f_winbufnr(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
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804}
18805
18806/*
18807 * "wincol()" function
18808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018810f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018811 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813{
18814 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018815 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816}
18817
18818/*
18819 * "winheight(nr)" function
18820 */
18821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018822f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018823 typval_T *argvars;
18824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825{
18826 win_T *wp;
18827
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018828 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018830 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018832 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833}
18834
18835/*
18836 * "winline()" function
18837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018839f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018840 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842{
18843 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018844 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845}
18846
18847/*
18848 * "winnr()" function
18849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018851f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018852 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854{
18855 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018856
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018858 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018860 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861}
18862
18863/*
18864 * "winrestcmd()" function
18865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018867f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018868 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870{
18871#ifdef FEAT_WINDOWS
18872 win_T *wp;
18873 int winnr = 1;
18874 garray_T ga;
18875 char_u buf[50];
18876
18877 ga_init2(&ga, (int)sizeof(char), 70);
18878 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18879 {
18880 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18881 ga_concat(&ga, buf);
18882# ifdef FEAT_VERTSPLIT
18883 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18884 ga_concat(&ga, buf);
18885# endif
18886 ++winnr;
18887 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018888 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018890 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018892 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018894 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895}
18896
18897/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018898 * "winrestview()" function
18899 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018900 static void
18901f_winrestview(argvars, rettv)
18902 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018903 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018904{
18905 dict_T *dict;
18906
18907 if (argvars[0].v_type != VAR_DICT
18908 || (dict = argvars[0].vval.v_dict) == NULL)
18909 EMSG(_(e_invarg));
18910 else
18911 {
18912 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18913 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18914#ifdef FEAT_VIRTUALEDIT
18915 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18916#endif
18917 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018918 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018919
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018920 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018921#ifdef FEAT_DIFF
18922 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18923#endif
18924 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18925 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18926
18927 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018928 win_new_height(curwin, curwin->w_height);
18929# ifdef FEAT_VERTSPLIT
18930 win_new_width(curwin, W_WIDTH(curwin));
18931# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018932 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018933
18934 if (curwin->w_topline == 0)
18935 curwin->w_topline = 1;
18936 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18937 curwin->w_topline = curbuf->b_ml.ml_line_count;
18938#ifdef FEAT_DIFF
18939 check_topfill(curwin, TRUE);
18940#endif
18941 }
18942}
18943
18944/*
18945 * "winsaveview()" function
18946 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018947 static void
18948f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018949 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018950 typval_T *rettv;
18951{
18952 dict_T *dict;
18953
Bram Moolenaara800b422010-06-27 01:15:55 +020018954 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018955 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018956 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018957
18958 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18959 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18960#ifdef FEAT_VIRTUALEDIT
18961 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18962#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018963 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018964 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18965
18966 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18967#ifdef FEAT_DIFF
18968 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18969#endif
18970 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18971 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18972}
18973
18974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 * "winwidth(nr)" function
18976 */
18977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018978f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018979 typval_T *argvars;
18980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981{
18982 win_T *wp;
18983
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018984 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 else
18988#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018989 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018991 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992#endif
18993}
18994
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018996 * "writefile()" function
18997 */
18998 static void
18999f_writefile(argvars, rettv)
19000 typval_T *argvars;
19001 typval_T *rettv;
19002{
19003 int binary = FALSE;
19004 char_u *fname;
19005 FILE *fd;
19006 listitem_T *li;
19007 char_u *s;
19008 int ret = 0;
19009 int c;
19010
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019011 if (check_restricted() || check_secure())
19012 return;
19013
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019014 if (argvars[0].v_type != VAR_LIST)
19015 {
19016 EMSG2(_(e_listarg), "writefile()");
19017 return;
19018 }
19019 if (argvars[0].vval.v_list == NULL)
19020 return;
19021
19022 if (argvars[2].v_type != VAR_UNKNOWN
19023 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19024 binary = TRUE;
19025
19026 /* Always open the file in binary mode, library functions have a mind of
19027 * their own about CR-LF conversion. */
19028 fname = get_tv_string(&argvars[1]);
19029 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19030 {
19031 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19032 ret = -1;
19033 }
19034 else
19035 {
19036 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19037 li = li->li_next)
19038 {
19039 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19040 {
19041 if (*s == '\n')
19042 c = putc(NUL, fd);
19043 else
19044 c = putc(*s, fd);
19045 if (c == EOF)
19046 {
19047 ret = -1;
19048 break;
19049 }
19050 }
19051 if (!binary || li->li_next != NULL)
19052 if (putc('\n', fd) == EOF)
19053 {
19054 ret = -1;
19055 break;
19056 }
19057 if (ret < 0)
19058 {
19059 EMSG(_(e_write));
19060 break;
19061 }
19062 }
19063 fclose(fd);
19064 }
19065
19066 rettv->vval.v_number = ret;
19067}
19068
19069/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019070 * "xor(expr, expr)" function
19071 */
19072 static void
19073f_xor(argvars, rettv)
19074 typval_T *argvars;
19075 typval_T *rettv;
19076{
19077 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19078 ^ get_tv_number_chk(&argvars[1], NULL);
19079}
19080
19081
19082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019084 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 */
19086 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019087var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019089 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019090 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019092 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019094 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095
Bram Moolenaara5525202006-03-02 22:52:09 +000019096 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019097 if (varp->v_type == VAR_LIST)
19098 {
19099 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019100 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019101 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019102 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019103
19104 l = varp->vval.v_list;
19105 if (l == NULL)
19106 return NULL;
19107
19108 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019109 pos.lnum = list_find_nr(l, 0L, &error);
19110 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019111 return NULL; /* invalid line number */
19112
19113 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019114 pos.col = list_find_nr(l, 1L, &error);
19115 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019116 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019117 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019118
19119 /* We accept "$" for the column number: last column. */
19120 li = list_find(l, 1L);
19121 if (li != NULL && li->li_tv.v_type == VAR_STRING
19122 && li->li_tv.vval.v_string != NULL
19123 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19124 pos.col = len + 1;
19125
Bram Moolenaara5525202006-03-02 22:52:09 +000019126 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019127 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019128 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019129 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019130
Bram Moolenaara5525202006-03-02 22:52:09 +000019131#ifdef FEAT_VIRTUALEDIT
19132 /* Get the virtual offset. Defaults to zero. */
19133 pos.coladd = list_find_nr(l, 2L, &error);
19134 if (error)
19135 pos.coladd = 0;
19136#endif
19137
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019138 return &pos;
19139 }
19140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019141 name = get_tv_string_chk(varp);
19142 if (name == NULL)
19143 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019144 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019146 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19147 {
19148 if (VIsual_active)
19149 return &VIsual;
19150 return &curwin->w_cursor;
19151 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019152 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019154 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19156 return NULL;
19157 return pp;
19158 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019159
19160#ifdef FEAT_VIRTUALEDIT
19161 pos.coladd = 0;
19162#endif
19163
Bram Moolenaar477933c2007-07-17 14:32:23 +000019164 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019165 {
19166 pos.col = 0;
19167 if (name[1] == '0') /* "w0": first visible line */
19168 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019169 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019170 pos.lnum = curwin->w_topline;
19171 return &pos;
19172 }
19173 else if (name[1] == '$') /* "w$": last visible line */
19174 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019175 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019176 pos.lnum = curwin->w_botline - 1;
19177 return &pos;
19178 }
19179 }
19180 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019182 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 {
19184 pos.lnum = curbuf->b_ml.ml_line_count;
19185 pos.col = 0;
19186 }
19187 else
19188 {
19189 pos.lnum = curwin->w_cursor.lnum;
19190 pos.col = (colnr_T)STRLEN(ml_get_curline());
19191 }
19192 return &pos;
19193 }
19194 return NULL;
19195}
19196
19197/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019198 * Convert list in "arg" into a position and optional file number.
19199 * When "fnump" is NULL there is no file number, only 3 items.
19200 * Note that the column is passed on as-is, the caller may want to decrement
19201 * it to use 1 for the first column.
19202 * Return FAIL when conversion is not possible, doesn't check the position for
19203 * validity.
19204 */
19205 static int
19206list2fpos(arg, posp, fnump)
19207 typval_T *arg;
19208 pos_T *posp;
19209 int *fnump;
19210{
19211 list_T *l = arg->vval.v_list;
19212 long i = 0;
19213 long n;
19214
Bram Moolenaarbde35262006-07-23 20:12:24 +000019215 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19216 * when "fnump" isn't NULL and "coladd" is optional. */
19217 if (arg->v_type != VAR_LIST
19218 || l == NULL
19219 || l->lv_len < (fnump == NULL ? 2 : 3)
19220 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019221 return FAIL;
19222
19223 if (fnump != NULL)
19224 {
19225 n = list_find_nr(l, i++, NULL); /* fnum */
19226 if (n < 0)
19227 return FAIL;
19228 if (n == 0)
19229 n = curbuf->b_fnum; /* current buffer */
19230 *fnump = n;
19231 }
19232
19233 n = list_find_nr(l, i++, NULL); /* lnum */
19234 if (n < 0)
19235 return FAIL;
19236 posp->lnum = n;
19237
19238 n = list_find_nr(l, i++, NULL); /* col */
19239 if (n < 0)
19240 return FAIL;
19241 posp->col = n;
19242
19243#ifdef FEAT_VIRTUALEDIT
19244 n = list_find_nr(l, i, NULL);
19245 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019246 posp->coladd = 0;
19247 else
19248 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019249#endif
19250
19251 return OK;
19252}
19253
19254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 * Get the length of an environment variable name.
19256 * Advance "arg" to the first character after the name.
19257 * Return 0 for error.
19258 */
19259 static int
19260get_env_len(arg)
19261 char_u **arg;
19262{
19263 char_u *p;
19264 int len;
19265
19266 for (p = *arg; vim_isIDc(*p); ++p)
19267 ;
19268 if (p == *arg) /* no name found */
19269 return 0;
19270
19271 len = (int)(p - *arg);
19272 *arg = p;
19273 return len;
19274}
19275
19276/*
19277 * Get the length of the name of a function or internal variable.
19278 * "arg" is advanced to the first non-white character after the name.
19279 * Return 0 if something is wrong.
19280 */
19281 static int
19282get_id_len(arg)
19283 char_u **arg;
19284{
19285 char_u *p;
19286 int len;
19287
19288 /* Find the end of the name. */
19289 for (p = *arg; eval_isnamec(*p); ++p)
19290 ;
19291 if (p == *arg) /* no name found */
19292 return 0;
19293
19294 len = (int)(p - *arg);
19295 *arg = skipwhite(p);
19296
19297 return len;
19298}
19299
19300/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019301 * Get the length of the name of a variable or function.
19302 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019304 * Return -1 if curly braces expansion failed.
19305 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 * If the name contains 'magic' {}'s, expand them and return the
19307 * expanded name in an allocated string via 'alias' - caller must free.
19308 */
19309 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019310get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 char_u **arg;
19312 char_u **alias;
19313 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019314 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315{
19316 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 char_u *p;
19318 char_u *expr_start;
19319 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320
19321 *alias = NULL; /* default to no alias */
19322
19323 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19324 && (*arg)[2] == (int)KE_SNR)
19325 {
19326 /* hard coded <SNR>, already translated */
19327 *arg += 3;
19328 return get_id_len(arg) + 3;
19329 }
19330 len = eval_fname_script(*arg);
19331 if (len > 0)
19332 {
19333 /* literal "<SID>", "s:" or "<SNR>" */
19334 *arg += len;
19335 }
19336
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019338 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019340 p = find_name_end(*arg, &expr_start, &expr_end,
19341 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 if (expr_start != NULL)
19343 {
19344 char_u *temp_string;
19345
19346 if (!evaluate)
19347 {
19348 len += (int)(p - *arg);
19349 *arg = skipwhite(p);
19350 return len;
19351 }
19352
19353 /*
19354 * Include any <SID> etc in the expanded string:
19355 * Thus the -len here.
19356 */
19357 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19358 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019359 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 *alias = temp_string;
19361 *arg = skipwhite(p);
19362 return (int)STRLEN(temp_string);
19363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364
19365 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019366 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 EMSG2(_(e_invexpr2), *arg);
19368
19369 return len;
19370}
19371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019372/*
19373 * Find the end of a variable or function name, taking care of magic braces.
19374 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19375 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019376 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377 * Return a pointer to just after the name. Equal to "arg" if there is no
19378 * valid name.
19379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019381find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 char_u *arg;
19383 char_u **expr_start;
19384 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019385 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019387 int mb_nest = 0;
19388 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 char_u *p;
19390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019391 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019393 *expr_start = NULL;
19394 *expr_end = NULL;
19395 }
19396
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019397 /* Quick check for valid starting character. */
19398 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19399 return arg;
19400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019401 for (p = arg; *p != NUL
19402 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019403 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019404 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019405 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019406 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019407 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019408 if (*p == '\'')
19409 {
19410 /* skip over 'string' to avoid counting [ and ] inside it. */
19411 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19412 ;
19413 if (*p == NUL)
19414 break;
19415 }
19416 else if (*p == '"')
19417 {
19418 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19419 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19420 if (*p == '\\' && p[1] != NUL)
19421 ++p;
19422 if (*p == NUL)
19423 break;
19424 }
19425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019426 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 if (*p == '[')
19429 ++br_nest;
19430 else if (*p == ']')
19431 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019434 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019436 if (*p == '{')
19437 {
19438 mb_nest++;
19439 if (expr_start != NULL && *expr_start == NULL)
19440 *expr_start = p;
19441 }
19442 else if (*p == '}')
19443 {
19444 mb_nest--;
19445 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19446 *expr_end = p;
19447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 }
19450
19451 return p;
19452}
19453
19454/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019455 * Expands out the 'magic' {}'s in a variable/function name.
19456 * Note that this can call itself recursively, to deal with
19457 * constructs like foo{bar}{baz}{bam}
19458 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19459 * "in_start" ^
19460 * "expr_start" ^
19461 * "expr_end" ^
19462 * "in_end" ^
19463 *
19464 * Returns a new allocated string, which the caller must free.
19465 * Returns NULL for failure.
19466 */
19467 static char_u *
19468make_expanded_name(in_start, expr_start, expr_end, in_end)
19469 char_u *in_start;
19470 char_u *expr_start;
19471 char_u *expr_end;
19472 char_u *in_end;
19473{
19474 char_u c1;
19475 char_u *retval = NULL;
19476 char_u *temp_result;
19477 char_u *nextcmd = NULL;
19478
19479 if (expr_end == NULL || in_end == NULL)
19480 return NULL;
19481 *expr_start = NUL;
19482 *expr_end = NUL;
19483 c1 = *in_end;
19484 *in_end = NUL;
19485
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019486 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019487 if (temp_result != NULL && nextcmd == NULL)
19488 {
19489 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19490 + (in_end - expr_end) + 1));
19491 if (retval != NULL)
19492 {
19493 STRCPY(retval, in_start);
19494 STRCAT(retval, temp_result);
19495 STRCAT(retval, expr_end + 1);
19496 }
19497 }
19498 vim_free(temp_result);
19499
19500 *in_end = c1; /* put char back for error messages */
19501 *expr_start = '{';
19502 *expr_end = '}';
19503
19504 if (retval != NULL)
19505 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019506 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019507 if (expr_start != NULL)
19508 {
19509 /* Further expansion! */
19510 temp_result = make_expanded_name(retval, expr_start,
19511 expr_end, temp_result);
19512 vim_free(retval);
19513 retval = temp_result;
19514 }
19515 }
19516
19517 return retval;
19518}
19519
19520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019522 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 */
19524 static int
19525eval_isnamec(c)
19526 int c;
19527{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019528 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19529}
19530
19531/*
19532 * Return TRUE if character "c" can be used as the first character in a
19533 * variable or function name (excluding '{' and '}').
19534 */
19535 static int
19536eval_isnamec1(c)
19537 int c;
19538{
19539 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540}
19541
19542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 * Set number v: variable to "val".
19544 */
19545 void
19546set_vim_var_nr(idx, val)
19547 int idx;
19548 long val;
19549{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019550 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551}
19552
19553/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019554 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 */
19556 long
19557get_vim_var_nr(idx)
19558 int idx;
19559{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019560 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561}
19562
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019563/*
19564 * Get string v: variable value. Uses a static buffer, can only be used once.
19565 */
19566 char_u *
19567get_vim_var_str(idx)
19568 int idx;
19569{
19570 return get_tv_string(&vimvars[idx].vv_tv);
19571}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019572
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019574 * Get List v: variable value. Caller must take care of reference count when
19575 * needed.
19576 */
19577 list_T *
19578get_vim_var_list(idx)
19579 int idx;
19580{
19581 return vimvars[idx].vv_list;
19582}
19583
19584/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019585 * Set v:char to character "c".
19586 */
19587 void
19588set_vim_var_char(c)
19589 int c;
19590{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019591 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019592
19593#ifdef FEAT_MBYTE
19594 if (has_mbyte)
19595 buf[(*mb_char2bytes)(c, buf)] = NUL;
19596 else
19597#endif
19598 {
19599 buf[0] = c;
19600 buf[1] = NUL;
19601 }
19602 set_vim_var_string(VV_CHAR, buf, -1);
19603}
19604
19605/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019606 * Set v:count to "count" and v:count1 to "count1".
19607 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 */
19609 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019610set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 long count;
19612 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019613 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019615 if (set_prevcount)
19616 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019617 vimvars[VV_COUNT].vv_nr = count;
19618 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619}
19620
19621/*
19622 * Set string v: variable to a copy of "val".
19623 */
19624 void
19625set_vim_var_string(idx, val, len)
19626 int idx;
19627 char_u *val;
19628 int len; /* length of "val" to use or -1 (whole string) */
19629{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019630 /* Need to do this (at least) once, since we can't initialize a union.
19631 * Will always be invoked when "v:progname" is set. */
19632 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19633
Bram Moolenaare9a41262005-01-15 22:18:47 +000019634 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019636 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019638 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019640 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641}
19642
19643/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019644 * Set List v: variable to "val".
19645 */
19646 void
19647set_vim_var_list(idx, val)
19648 int idx;
19649 list_T *val;
19650{
19651 list_unref(vimvars[idx].vv_list);
19652 vimvars[idx].vv_list = val;
19653 if (val != NULL)
19654 ++val->lv_refcount;
19655}
19656
19657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 * Set v:register if needed.
19659 */
19660 void
19661set_reg_var(c)
19662 int c;
19663{
19664 char_u regname;
19665
19666 if (c == 0 || c == ' ')
19667 regname = '"';
19668 else
19669 regname = c;
19670 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019671 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 set_vim_var_string(VV_REG, &regname, 1);
19673}
19674
19675/*
19676 * Get or set v:exception. If "oldval" == NULL, return the current value.
19677 * Otherwise, restore the value to "oldval" and return NULL.
19678 * Must always be called in pairs to save and restore v:exception! Does not
19679 * take care of memory allocations.
19680 */
19681 char_u *
19682v_exception(oldval)
19683 char_u *oldval;
19684{
19685 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019686 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687
Bram Moolenaare9a41262005-01-15 22:18:47 +000019688 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 return NULL;
19690}
19691
19692/*
19693 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19694 * Otherwise, restore the value to "oldval" and return NULL.
19695 * Must always be called in pairs to save and restore v:throwpoint! Does not
19696 * take care of memory allocations.
19697 */
19698 char_u *
19699v_throwpoint(oldval)
19700 char_u *oldval;
19701{
19702 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019703 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704
Bram Moolenaare9a41262005-01-15 22:18:47 +000019705 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 return NULL;
19707}
19708
19709#if defined(FEAT_AUTOCMD) || defined(PROTO)
19710/*
19711 * Set v:cmdarg.
19712 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19713 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19714 * Must always be called in pairs!
19715 */
19716 char_u *
19717set_cmdarg(eap, oldarg)
19718 exarg_T *eap;
19719 char_u *oldarg;
19720{
19721 char_u *oldval;
19722 char_u *newval;
19723 unsigned len;
19724
Bram Moolenaare9a41262005-01-15 22:18:47 +000019725 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019726 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019728 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019729 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019730 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 }
19732
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019733 if (eap->force_bin == FORCE_BIN)
19734 len = 6;
19735 else if (eap->force_bin == FORCE_NOBIN)
19736 len = 8;
19737 else
19738 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019739
19740 if (eap->read_edit)
19741 len += 7;
19742
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019743 if (eap->force_ff != 0)
19744 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19745# ifdef FEAT_MBYTE
19746 if (eap->force_enc != 0)
19747 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019748 if (eap->bad_char != 0)
19749 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019750# endif
19751
19752 newval = alloc(len + 1);
19753 if (newval == NULL)
19754 return NULL;
19755
19756 if (eap->force_bin == FORCE_BIN)
19757 sprintf((char *)newval, " ++bin");
19758 else if (eap->force_bin == FORCE_NOBIN)
19759 sprintf((char *)newval, " ++nobin");
19760 else
19761 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019762
19763 if (eap->read_edit)
19764 STRCAT(newval, " ++edit");
19765
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019766 if (eap->force_ff != 0)
19767 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19768 eap->cmd + eap->force_ff);
19769# ifdef FEAT_MBYTE
19770 if (eap->force_enc != 0)
19771 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19772 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019773 if (eap->bad_char == BAD_KEEP)
19774 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19775 else if (eap->bad_char == BAD_DROP)
19776 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19777 else if (eap->bad_char != 0)
19778 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019779# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019780 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019781 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782}
19783#endif
19784
19785/*
19786 * Get the value of internal variable "name".
19787 * Return OK or FAIL.
19788 */
19789 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019790get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 char_u *name;
19792 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019793 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019794 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019795 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796{
19797 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019798 typval_T *tv = NULL;
19799 typval_T atv;
19800 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802
19803 /* truncate the name, so that we can use strcmp() */
19804 cc = name[len];
19805 name[len] = NUL;
19806
19807 /*
19808 * Check for "b:changedtick".
19809 */
19810 if (STRCMP(name, "b:changedtick") == 0)
19811 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019812 atv.v_type = VAR_NUMBER;
19813 atv.vval.v_number = curbuf->b_changedtick;
19814 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 }
19816
19817 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 * Check for user-defined variables.
19819 */
19820 else
19821 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019822 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 }
19826
Bram Moolenaare9a41262005-01-15 22:18:47 +000019827 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019829 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 ret = FAIL;
19832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019834 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835
19836 name[len] = cc;
19837
19838 return ret;
19839}
19840
19841/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019842 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19843 * Also handle function call with Funcref variable: func(expr)
19844 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19845 */
19846 static int
19847handle_subscript(arg, rettv, evaluate, verbose)
19848 char_u **arg;
19849 typval_T *rettv;
19850 int evaluate; /* do more than finding the end */
19851 int verbose; /* give error messages */
19852{
19853 int ret = OK;
19854 dict_T *selfdict = NULL;
19855 char_u *s;
19856 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019857 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019858
19859 while (ret == OK
19860 && (**arg == '['
19861 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019862 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019863 && !vim_iswhite(*(*arg - 1)))
19864 {
19865 if (**arg == '(')
19866 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019867 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019868 if (evaluate)
19869 {
19870 functv = *rettv;
19871 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019872
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019873 /* Invoke the function. Recursive! */
19874 s = functv.vval.v_string;
19875 }
19876 else
19877 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019878 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019879 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19880 &len, evaluate, selfdict);
19881
19882 /* Clear the funcref afterwards, so that deleting it while
19883 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019884 if (evaluate)
19885 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019886
19887 /* Stop the expression evaluation when immediately aborting on
19888 * error, or when an interrupt occurred or an exception was thrown
19889 * but not caught. */
19890 if (aborting())
19891 {
19892 if (ret == OK)
19893 clear_tv(rettv);
19894 ret = FAIL;
19895 }
19896 dict_unref(selfdict);
19897 selfdict = NULL;
19898 }
19899 else /* **arg == '[' || **arg == '.' */
19900 {
19901 dict_unref(selfdict);
19902 if (rettv->v_type == VAR_DICT)
19903 {
19904 selfdict = rettv->vval.v_dict;
19905 if (selfdict != NULL)
19906 ++selfdict->dv_refcount;
19907 }
19908 else
19909 selfdict = NULL;
19910 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19911 {
19912 clear_tv(rettv);
19913 ret = FAIL;
19914 }
19915 }
19916 }
19917 dict_unref(selfdict);
19918 return ret;
19919}
19920
19921/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019922 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019923 * value).
19924 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019925 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019926alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019927{
Bram Moolenaar33570922005-01-25 22:26:29 +000019928 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019929}
19930
19931/*
19932 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 * The string "s" must have been allocated, it is consumed.
19934 * Return NULL for out of memory, the variable otherwise.
19935 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019936 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 char_u *s;
19939{
Bram Moolenaar33570922005-01-25 22:26:29 +000019940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 rettv = alloc_tv();
19943 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019945 rettv->v_type = VAR_STRING;
19946 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 }
19948 else
19949 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019950 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951}
19952
19953/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019956 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019957free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019958 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959{
19960 if (varp != NULL)
19961 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019962 switch (varp->v_type)
19963 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019964 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019965 func_unref(varp->vval.v_string);
19966 /*FALLTHROUGH*/
19967 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019968 vim_free(varp->vval.v_string);
19969 break;
19970 case VAR_LIST:
19971 list_unref(varp->vval.v_list);
19972 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019973 case VAR_DICT:
19974 dict_unref(varp->vval.v_dict);
19975 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019976 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019977#ifdef FEAT_FLOAT
19978 case VAR_FLOAT:
19979#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019980 case VAR_UNKNOWN:
19981 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019982 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019983 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019984 break;
19985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 vim_free(varp);
19987 }
19988}
19989
19990/*
19991 * Free the memory for a variable value and set the value to NULL or 0.
19992 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019993 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019995 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996{
19997 if (varp != NULL)
19998 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019999 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020001 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020002 func_unref(varp->vval.v_string);
20003 /*FALLTHROUGH*/
20004 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020005 vim_free(varp->vval.v_string);
20006 varp->vval.v_string = NULL;
20007 break;
20008 case VAR_LIST:
20009 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020010 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020011 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020012 case VAR_DICT:
20013 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020014 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020015 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020017 varp->vval.v_number = 0;
20018 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020019#ifdef FEAT_FLOAT
20020 case VAR_FLOAT:
20021 varp->vval.v_float = 0.0;
20022 break;
20023#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 case VAR_UNKNOWN:
20025 break;
20026 default:
20027 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020029 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 }
20031}
20032
20033/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 * Set the value of a variable to NULL without freeing items.
20035 */
20036 static void
20037init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020038 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020039{
20040 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042}
20043
20044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 * Get the number value of a variable.
20046 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020047 * For incompatible types, return 0.
20048 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20049 * caller of incompatible types: it sets *denote to TRUE if "denote"
20050 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 */
20052 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020053get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020056 int error = FALSE;
20057
20058 return get_tv_number_chk(varp, &error); /* return 0L on error */
20059}
20060
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020061 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020062get_tv_number_chk(varp, denote)
20063 typval_T *varp;
20064 int *denote;
20065{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020066 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020068 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020070 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020071 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020072#ifdef FEAT_FLOAT
20073 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020074 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020075 break;
20076#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020077 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020078 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020079 break;
20080 case VAR_STRING:
20081 if (varp->vval.v_string != NULL)
20082 vim_str2nr(varp->vval.v_string, NULL, NULL,
20083 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020084 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020085 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020086 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020087 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020088 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020089 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020090 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020091 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020092 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020093 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020095 if (denote == NULL) /* useful for values that must be unsigned */
20096 n = -1;
20097 else
20098 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020099 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100}
20101
20102/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020103 * Get the lnum from the first argument.
20104 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020105 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 */
20107 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020108get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110{
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 linenr_T lnum;
20113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020114 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 if (lnum == 0) /* no valid number, try using line() */
20116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020117 rettv.v_type = VAR_NUMBER;
20118 f_line(argvars, &rettv);
20119 lnum = rettv.vval.v_number;
20120 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 }
20122 return lnum;
20123}
20124
20125/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020126 * Get the lnum from the first argument.
20127 * Also accepts "$", then "buf" is used.
20128 * Returns 0 on error.
20129 */
20130 static linenr_T
20131get_tv_lnum_buf(argvars, buf)
20132 typval_T *argvars;
20133 buf_T *buf;
20134{
20135 if (argvars[0].v_type == VAR_STRING
20136 && argvars[0].vval.v_string != NULL
20137 && argvars[0].vval.v_string[0] == '$'
20138 && buf != NULL)
20139 return buf->b_ml.ml_line_count;
20140 return get_tv_number_chk(&argvars[0], NULL);
20141}
20142
20143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 * Get the string value of a variable.
20145 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020146 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20147 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 * If the String variable has never been set, return an empty string.
20149 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020150 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20151 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 */
20153 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020154get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020155 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156{
20157 static char_u mybuf[NUMBUFLEN];
20158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020159 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160}
20161
20162 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020165 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020167 char_u *res = get_tv_string_buf_chk(varp, buf);
20168
20169 return res != NULL ? res : (char_u *)"";
20170}
20171
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020172 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020173get_tv_string_chk(varp)
20174 typval_T *varp;
20175{
20176 static char_u mybuf[NUMBUFLEN];
20177
20178 return get_tv_string_buf_chk(varp, mybuf);
20179}
20180
20181 static char_u *
20182get_tv_string_buf_chk(varp, buf)
20183 typval_T *varp;
20184 char_u *buf;
20185{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020186 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020188 case VAR_NUMBER:
20189 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20190 return buf;
20191 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020192 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020193 break;
20194 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020195 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020196 break;
20197 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020198 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020199 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020200#ifdef FEAT_FLOAT
20201 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020202 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020203 break;
20204#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020205 case VAR_STRING:
20206 if (varp->vval.v_string != NULL)
20207 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020208 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020209 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020210 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020213 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214}
20215
20216/*
20217 * Find variable "name" in the list of variables.
20218 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020219 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020220 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020224find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020227 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231
Bram Moolenaara7043832005-01-21 11:56:39 +000020232 ht = find_var_ht(name, &varname);
20233 if (htp != NULL)
20234 *htp = ht;
20235 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020237 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238}
20239
20240/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020241 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020242 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020244 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020245find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020246 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020247 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020248 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020249 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020250{
Bram Moolenaar33570922005-01-25 22:26:29 +000020251 hashitem_T *hi;
20252
20253 if (*varname == NUL)
20254 {
20255 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020256 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020257 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020258 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 case 'g': return &globvars_var;
20260 case 'v': return &vimvars_var;
20261 case 'b': return &curbuf->b_bufvar;
20262 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020263#ifdef FEAT_WINDOWS
20264 case 't': return &curtab->tp_winvar;
20265#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020266 case 'l': return current_funccal == NULL
20267 ? NULL : &current_funccal->l_vars_var;
20268 case 'a': return current_funccal == NULL
20269 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020270 }
20271 return NULL;
20272 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020273
20274 hi = hash_find(ht, varname);
20275 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020276 {
20277 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020278 * worked find the variable again. Don't auto-load a script if it was
20279 * loaded already, otherwise it would be loaded every time when
20280 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020281 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020282 {
20283 /* Note: script_autoload() may make "hi" invalid. It must either
20284 * be obtained again or not used. */
20285 if (!script_autoload(varname, FALSE) || aborting())
20286 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020287 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020288 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020289 if (HASHITEM_EMPTY(hi))
20290 return NULL;
20291 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020293}
20294
20295/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020296 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020297 * Set "varname" to the start of name without ':'.
20298 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020299 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020300find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 char_u *name;
20302 char_u **varname;
20303{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020304 hashitem_T *hi;
20305
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 if (name[1] != ':')
20307 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020308 /* The name must not start with a colon or #. */
20309 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 return NULL;
20311 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020312
20313 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020314 hi = hash_find(&compat_hashtab, name);
20315 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020316 return &compat_hashtab;
20317
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 return &globvarht; /* global variable */
20320 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 }
20322 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020323 if (*name == 'g') /* global variable */
20324 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020325 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20326 */
20327 if (vim_strchr(name + 2, ':') != NULL
20328 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020329 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020331 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020333 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020334#ifdef FEAT_WINDOWS
20335 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020336 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020337#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020338 if (*name == 'v') /* v: variable */
20339 return &vimvarht;
20340 if (*name == 'a' && current_funccal != NULL) /* function argument */
20341 return &current_funccal->l_avars.dv_hashtab;
20342 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20343 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 if (*name == 's' /* script variable */
20345 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20346 return &SCRIPT_VARS(current_SID);
20347 return NULL;
20348}
20349
20350/*
20351 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020352 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 * Returns NULL when it doesn't exist.
20354 */
20355 char_u *
20356get_var_value(name)
20357 char_u *name;
20358{
Bram Moolenaar33570922005-01-25 22:26:29 +000020359 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020361 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 if (v == NULL)
20363 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020364 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365}
20366
20367/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020368 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 * sourcing this script and when executing functions defined in the script.
20370 */
20371 void
20372new_script_vars(id)
20373 scid_T id;
20374{
Bram Moolenaara7043832005-01-21 11:56:39 +000020375 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020376 hashtab_T *ht;
20377 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020378
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20380 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020381 /* Re-allocating ga_data means that an ht_array pointing to
20382 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020384 for (i = 1; i <= ga_scripts.ga_len; ++i)
20385 {
20386 ht = &SCRIPT_VARS(i);
20387 if (ht->ht_mask == HT_INIT_SIZE - 1)
20388 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020389 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020391 }
20392
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 while (ga_scripts.ga_len < id)
20394 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020395 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020396 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020397 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 }
20400 }
20401}
20402
20403/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020404 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20405 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 */
20407 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020408init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 dict_T *dict;
20410 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020411 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412{
Bram Moolenaar33570922005-01-25 22:26:29 +000020413 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020414 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020415 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020416 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020417 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 dict_var->di_tv.vval.v_dict = dict;
20419 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020420 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020421 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20422 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423}
20424
20425/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020426 * Unreference a dictionary initialized by init_var_dict().
20427 */
20428 void
20429unref_var_dict(dict)
20430 dict_T *dict;
20431{
20432 /* Now the dict needs to be freed if no one else is using it, go back to
20433 * normal reference counting. */
20434 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20435 dict_unref(dict);
20436}
20437
20438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020440 * Frees all allocated variables and the value they contain.
20441 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 */
20443 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020444vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020445 hashtab_T *ht;
20446{
20447 vars_clear_ext(ht, TRUE);
20448}
20449
20450/*
20451 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20452 */
20453 static void
20454vars_clear_ext(ht, free_val)
20455 hashtab_T *ht;
20456 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457{
Bram Moolenaara7043832005-01-21 11:56:39 +000020458 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020459 hashitem_T *hi;
20460 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461
Bram Moolenaar33570922005-01-25 22:26:29 +000020462 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020463 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020464 for (hi = ht->ht_array; todo > 0; ++hi)
20465 {
20466 if (!HASHITEM_EMPTY(hi))
20467 {
20468 --todo;
20469
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020471 * ht_array might change then. hash_clear() takes care of it
20472 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 v = HI2DI(hi);
20474 if (free_val)
20475 clear_tv(&v->di_tv);
20476 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20477 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020478 }
20479 }
20480 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020481 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482}
20483
Bram Moolenaara7043832005-01-21 11:56:39 +000020484/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 * Delete a variable from hashtab "ht" at item "hi".
20486 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020489delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020490 hashtab_T *ht;
20491 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492{
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020494
20495 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020496 clear_tv(&di->di_tv);
20497 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498}
20499
20500/*
20501 * List the value of one internal variable.
20502 */
20503 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020504list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020505 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020507 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020509 char_u *tofree;
20510 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020511 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020512
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020513 current_copyID += COPYID_INC;
20514 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020515 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020516 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020517 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518}
20519
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020521list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 char_u *prefix;
20523 char_u *name;
20524 int type;
20525 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020526 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527{
Bram Moolenaar31859182007-08-14 20:41:13 +000020528 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20529 msg_start();
20530 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 if (name != NULL) /* "a:" vars don't have a name stored */
20532 msg_puts(name);
20533 msg_putchar(' ');
20534 msg_advance(22);
20535 if (type == VAR_NUMBER)
20536 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020537 else if (type == VAR_FUNC)
20538 msg_putchar('*');
20539 else if (type == VAR_LIST)
20540 {
20541 msg_putchar('[');
20542 if (*string == '[')
20543 ++string;
20544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020545 else if (type == VAR_DICT)
20546 {
20547 msg_putchar('{');
20548 if (*string == '{')
20549 ++string;
20550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 else
20552 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020553
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020555
20556 if (type == VAR_FUNC)
20557 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020558 if (*first)
20559 {
20560 msg_clr_eos();
20561 *first = FALSE;
20562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563}
20564
20565/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020566 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 * If the variable already exists, the value is updated.
20568 * Otherwise the variable is created.
20569 */
20570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020571set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020573 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020574 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575{
Bram Moolenaar33570922005-01-25 22:26:29 +000020576 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020580 ht = find_var_ht(name, &varname);
20581 if (ht == NULL || *varname == NUL)
20582 {
20583 EMSG2(_(e_illvar), name);
20584 return;
20585 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020586 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020587
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020588 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20589 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020590
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020593 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020594 if (var_check_ro(v->di_flags, name)
20595 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020596 return;
20597 if (v->di_tv.v_type != tv->v_type
20598 && !((v->di_tv.v_type == VAR_STRING
20599 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020601 || tv->v_type == VAR_NUMBER))
20602#ifdef FEAT_FLOAT
20603 && !((v->di_tv.v_type == VAR_NUMBER
20604 || v->di_tv.v_type == VAR_FLOAT)
20605 && (tv->v_type == VAR_NUMBER
20606 || tv->v_type == VAR_FLOAT))
20607#endif
20608 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020609 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020610 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020611 return;
20612 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020613
20614 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020615 * Handle setting internal v: variables separately: we don't change
20616 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 */
20618 if (ht == &vimvarht)
20619 {
20620 if (v->di_tv.v_type == VAR_STRING)
20621 {
20622 vim_free(v->di_tv.vval.v_string);
20623 if (copy || tv->v_type != VAR_STRING)
20624 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20625 else
20626 {
20627 /* Take over the string to avoid an extra alloc/free. */
20628 v->di_tv.vval.v_string = tv->vval.v_string;
20629 tv->vval.v_string = NULL;
20630 }
20631 }
20632 else if (v->di_tv.v_type != VAR_NUMBER)
20633 EMSG2(_(e_intern2), "set_var()");
20634 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020635 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020636 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020637 if (STRCMP(varname, "searchforward") == 0)
20638 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020639#ifdef FEAT_SEARCH_EXTRA
20640 else if (STRCMP(varname, "hlsearch") == 0)
20641 {
20642 no_hlsearch = !v->di_tv.vval.v_number;
20643 redraw_all_later(SOME_VALID);
20644 }
20645#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020646 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020647 return;
20648 }
20649
20650 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 }
20652 else /* add a new variable */
20653 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020654 /* Can't add "v:" variable. */
20655 if (ht == &vimvarht)
20656 {
20657 EMSG2(_(e_illvar), name);
20658 return;
20659 }
20660
Bram Moolenaar92124a32005-06-17 22:03:40 +000020661 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020662 if (!valid_varname(varname))
20663 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020664
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020665 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20666 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020667 if (v == NULL)
20668 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020669 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020670 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020672 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 return;
20674 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020675 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020677
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020678 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020679 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020680 else
20681 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020682 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020683 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020684 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686}
20687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020688/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020689 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020690 * Also give an error message.
20691 */
20692 static int
20693var_check_ro(flags, name)
20694 int flags;
20695 char_u *name;
20696{
20697 if (flags & DI_FLAGS_RO)
20698 {
20699 EMSG2(_(e_readonlyvar), name);
20700 return TRUE;
20701 }
20702 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20703 {
20704 EMSG2(_(e_readonlysbx), name);
20705 return TRUE;
20706 }
20707 return FALSE;
20708}
20709
20710/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020711 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20712 * Also give an error message.
20713 */
20714 static int
20715var_check_fixed(flags, name)
20716 int flags;
20717 char_u *name;
20718{
20719 if (flags & DI_FLAGS_FIX)
20720 {
20721 EMSG2(_("E795: Cannot delete variable %s"), name);
20722 return TRUE;
20723 }
20724 return FALSE;
20725}
20726
20727/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020728 * Check if a funcref is assigned to a valid variable name.
20729 * Return TRUE and give an error if not.
20730 */
20731 static int
20732var_check_func_name(name, new_var)
20733 char_u *name; /* points to start of variable name */
20734 int new_var; /* TRUE when creating the variable */
20735{
20736 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20737 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20738 ? name[2] : name[0]))
20739 {
20740 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20741 name);
20742 return TRUE;
20743 }
20744 /* Don't allow hiding a function. When "v" is not NULL we might be
20745 * assigning another function to the same var, the type is checked
20746 * below. */
20747 if (new_var && function_exists(name))
20748 {
20749 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20750 name);
20751 return TRUE;
20752 }
20753 return FALSE;
20754}
20755
20756/*
20757 * Check if a variable name is valid.
20758 * Return FALSE and give an error if not.
20759 */
20760 static int
20761valid_varname(varname)
20762 char_u *varname;
20763{
20764 char_u *p;
20765
20766 for (p = varname; *p != NUL; ++p)
20767 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20768 && *p != AUTOLOAD_CHAR)
20769 {
20770 EMSG2(_(e_illvar), varname);
20771 return FALSE;
20772 }
20773 return TRUE;
20774}
20775
20776/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020777 * Return TRUE if typeval "tv" is set to be locked (immutable).
20778 * Also give an error message, using "name".
20779 */
20780 static int
20781tv_check_lock(lock, name)
20782 int lock;
20783 char_u *name;
20784{
20785 if (lock & VAR_LOCKED)
20786 {
20787 EMSG2(_("E741: Value is locked: %s"),
20788 name == NULL ? (char_u *)_("Unknown") : name);
20789 return TRUE;
20790 }
20791 if (lock & VAR_FIXED)
20792 {
20793 EMSG2(_("E742: Cannot change value of %s"),
20794 name == NULL ? (char_u *)_("Unknown") : name);
20795 return TRUE;
20796 }
20797 return FALSE;
20798}
20799
20800/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020801 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020802 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020803 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020804 * It is OK for "from" and "to" to point to the same item. This is used to
20805 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020806 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020807 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020808copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020809 typval_T *from;
20810 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020812 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020813 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020814 switch (from->v_type)
20815 {
20816 case VAR_NUMBER:
20817 to->vval.v_number = from->vval.v_number;
20818 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020819#ifdef FEAT_FLOAT
20820 case VAR_FLOAT:
20821 to->vval.v_float = from->vval.v_float;
20822 break;
20823#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020824 case VAR_STRING:
20825 case VAR_FUNC:
20826 if (from->vval.v_string == NULL)
20827 to->vval.v_string = NULL;
20828 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020829 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020830 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020831 if (from->v_type == VAR_FUNC)
20832 func_ref(to->vval.v_string);
20833 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020834 break;
20835 case VAR_LIST:
20836 if (from->vval.v_list == NULL)
20837 to->vval.v_list = NULL;
20838 else
20839 {
20840 to->vval.v_list = from->vval.v_list;
20841 ++to->vval.v_list->lv_refcount;
20842 }
20843 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020844 case VAR_DICT:
20845 if (from->vval.v_dict == NULL)
20846 to->vval.v_dict = NULL;
20847 else
20848 {
20849 to->vval.v_dict = from->vval.v_dict;
20850 ++to->vval.v_dict->dv_refcount;
20851 }
20852 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020853 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020854 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020855 break;
20856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857}
20858
20859/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020860 * Make a copy of an item.
20861 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020862 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20863 * reference to an already copied list/dict can be used.
20864 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020865 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020866 static int
20867item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 typval_T *from;
20869 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020870 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020871 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020872{
20873 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020874 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020875
Bram Moolenaar33570922005-01-25 22:26:29 +000020876 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020877 {
20878 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020879 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020880 }
20881 ++recurse;
20882
20883 switch (from->v_type)
20884 {
20885 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020886#ifdef FEAT_FLOAT
20887 case VAR_FLOAT:
20888#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020889 case VAR_STRING:
20890 case VAR_FUNC:
20891 copy_tv(from, to);
20892 break;
20893 case VAR_LIST:
20894 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020895 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020896 if (from->vval.v_list == NULL)
20897 to->vval.v_list = NULL;
20898 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20899 {
20900 /* use the copy made earlier */
20901 to->vval.v_list = from->vval.v_list->lv_copylist;
20902 ++to->vval.v_list->lv_refcount;
20903 }
20904 else
20905 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20906 if (to->vval.v_list == NULL)
20907 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020908 break;
20909 case VAR_DICT:
20910 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020911 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020912 if (from->vval.v_dict == NULL)
20913 to->vval.v_dict = NULL;
20914 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20915 {
20916 /* use the copy made earlier */
20917 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20918 ++to->vval.v_dict->dv_refcount;
20919 }
20920 else
20921 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20922 if (to->vval.v_dict == NULL)
20923 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020924 break;
20925 default:
20926 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020927 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020928 }
20929 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020930 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020931}
20932
20933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 * ":echo expr1 ..." print each argument separated with a space, add a
20935 * newline at the end.
20936 * ":echon expr1 ..." print each argument plain.
20937 */
20938 void
20939ex_echo(eap)
20940 exarg_T *eap;
20941{
20942 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020943 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020944 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 char_u *p;
20946 int needclr = TRUE;
20947 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020948 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949
20950 if (eap->skip)
20951 ++emsg_skip;
20952 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020954 /* If eval1() causes an error message the text from the command may
20955 * still need to be cleared. E.g., "echo 22,44". */
20956 need_clr_eos = needclr;
20957
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020959 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 {
20961 /*
20962 * Report the invalid expression unless the expression evaluation
20963 * has been cancelled due to an aborting error, an interrupt, or an
20964 * exception.
20965 */
20966 if (!aborting())
20967 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020968 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 break;
20970 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020971 need_clr_eos = FALSE;
20972
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 if (!eap->skip)
20974 {
20975 if (atstart)
20976 {
20977 atstart = FALSE;
20978 /* Call msg_start() after eval1(), evaluating the expression
20979 * may cause a message to appear. */
20980 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020981 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020982 /* Mark the saved text as finishing the line, so that what
20983 * follows is displayed on a new line when scrolling back
20984 * at the more prompt. */
20985 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 }
20989 else if (eap->cmdidx == CMD_echo)
20990 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020991 current_copyID += COPYID_INC;
20992 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020993 if (p != NULL)
20994 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020996 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020998 if (*p != TAB && needclr)
20999 {
21000 /* remove any text still there from the command */
21001 msg_clr_eos();
21002 needclr = FALSE;
21003 }
21004 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 }
21006 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021007 {
21008#ifdef FEAT_MBYTE
21009 if (has_mbyte)
21010 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021011 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021012
21013 (void)msg_outtrans_len_attr(p, i, echo_attr);
21014 p += i - 1;
21015 }
21016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021018 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021021 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 arg = skipwhite(arg);
21025 }
21026 eap->nextcmd = check_nextcmd(arg);
21027
21028 if (eap->skip)
21029 --emsg_skip;
21030 else
21031 {
21032 /* remove text that may still be there from the command */
21033 if (needclr)
21034 msg_clr_eos();
21035 if (eap->cmdidx == CMD_echo)
21036 msg_end();
21037 }
21038}
21039
21040/*
21041 * ":echohl {name}".
21042 */
21043 void
21044ex_echohl(eap)
21045 exarg_T *eap;
21046{
21047 int id;
21048
21049 id = syn_name2id(eap->arg);
21050 if (id == 0)
21051 echo_attr = 0;
21052 else
21053 echo_attr = syn_id2attr(id);
21054}
21055
21056/*
21057 * ":execute expr1 ..." execute the result of an expression.
21058 * ":echomsg expr1 ..." Print a message
21059 * ":echoerr expr1 ..." Print an error
21060 * Each gets spaces around each argument and a newline at the end for
21061 * echo commands
21062 */
21063 void
21064ex_execute(eap)
21065 exarg_T *eap;
21066{
21067 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021068 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 int ret = OK;
21070 char_u *p;
21071 garray_T ga;
21072 int len;
21073 int save_did_emsg;
21074
21075 ga_init2(&ga, 1, 80);
21076
21077 if (eap->skip)
21078 ++emsg_skip;
21079 while (*arg != NUL && *arg != '|' && *arg != '\n')
21080 {
21081 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021082 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 {
21084 /*
21085 * Report the invalid expression unless the expression evaluation
21086 * has been cancelled due to an aborting error, an interrupt, or an
21087 * exception.
21088 */
21089 if (!aborting())
21090 EMSG2(_(e_invexpr2), p);
21091 ret = FAIL;
21092 break;
21093 }
21094
21095 if (!eap->skip)
21096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021097 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 len = (int)STRLEN(p);
21099 if (ga_grow(&ga, len + 2) == FAIL)
21100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021101 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 ret = FAIL;
21103 break;
21104 }
21105 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 ga.ga_len += len;
21109 }
21110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021111 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 arg = skipwhite(arg);
21113 }
21114
21115 if (ret != FAIL && ga.ga_data != NULL)
21116 {
21117 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021120 out_flush();
21121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 else if (eap->cmdidx == CMD_echoerr)
21123 {
21124 /* We don't want to abort following commands, restore did_emsg. */
21125 save_did_emsg = did_emsg;
21126 EMSG((char_u *)ga.ga_data);
21127 if (!force_abort)
21128 did_emsg = save_did_emsg;
21129 }
21130 else if (eap->cmdidx == CMD_execute)
21131 do_cmdline((char_u *)ga.ga_data,
21132 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21133 }
21134
21135 ga_clear(&ga);
21136
21137 if (eap->skip)
21138 --emsg_skip;
21139
21140 eap->nextcmd = check_nextcmd(arg);
21141}
21142
21143/*
21144 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21145 * "arg" points to the "&" or '+' when called, to "option" when returning.
21146 * Returns NULL when no option name found. Otherwise pointer to the char
21147 * after the option name.
21148 */
21149 static char_u *
21150find_option_end(arg, opt_flags)
21151 char_u **arg;
21152 int *opt_flags;
21153{
21154 char_u *p = *arg;
21155
21156 ++p;
21157 if (*p == 'g' && p[1] == ':')
21158 {
21159 *opt_flags = OPT_GLOBAL;
21160 p += 2;
21161 }
21162 else if (*p == 'l' && p[1] == ':')
21163 {
21164 *opt_flags = OPT_LOCAL;
21165 p += 2;
21166 }
21167 else
21168 *opt_flags = 0;
21169
21170 if (!ASCII_ISALPHA(*p))
21171 return NULL;
21172 *arg = p;
21173
21174 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21175 p += 4; /* termcap option */
21176 else
21177 while (ASCII_ISALPHA(*p))
21178 ++p;
21179 return p;
21180}
21181
21182/*
21183 * ":function"
21184 */
21185 void
21186ex_function(eap)
21187 exarg_T *eap;
21188{
21189 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021190 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 int j;
21192 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021194 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 char_u *name = NULL;
21196 char_u *p;
21197 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021198 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 garray_T newargs;
21200 garray_T newlines;
21201 int varargs = FALSE;
21202 int mustend = FALSE;
21203 int flags = 0;
21204 ufunc_T *fp;
21205 int indent;
21206 int nesting;
21207 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021208 dictitem_T *v;
21209 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021210 static int func_nr = 0; /* number for nameless function */
21211 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021212 hashtab_T *ht;
21213 int todo;
21214 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021215 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216
21217 /*
21218 * ":function" without argument: list functions.
21219 */
21220 if (ends_excmd(*eap->arg))
21221 {
21222 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021223 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021224 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021225 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021226 {
21227 if (!HASHITEM_EMPTY(hi))
21228 {
21229 --todo;
21230 fp = HI2UF(hi);
21231 if (!isdigit(*fp->uf_name))
21232 list_func_head(fp, FALSE);
21233 }
21234 }
21235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 eap->nextcmd = check_nextcmd(eap->arg);
21237 return;
21238 }
21239
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021240 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021241 * ":function /pat": list functions matching pattern.
21242 */
21243 if (*eap->arg == '/')
21244 {
21245 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21246 if (!eap->skip)
21247 {
21248 regmatch_T regmatch;
21249
21250 c = *p;
21251 *p = NUL;
21252 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21253 *p = c;
21254 if (regmatch.regprog != NULL)
21255 {
21256 regmatch.rm_ic = p_ic;
21257
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021258 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021259 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21260 {
21261 if (!HASHITEM_EMPTY(hi))
21262 {
21263 --todo;
21264 fp = HI2UF(hi);
21265 if (!isdigit(*fp->uf_name)
21266 && vim_regexec(&regmatch, fp->uf_name, 0))
21267 list_func_head(fp, FALSE);
21268 }
21269 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021270 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021271 }
21272 }
21273 if (*p == '/')
21274 ++p;
21275 eap->nextcmd = check_nextcmd(p);
21276 return;
21277 }
21278
21279 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021280 * Get the function name. There are these situations:
21281 * func normal function name
21282 * "name" == func, "fudi.fd_dict" == NULL
21283 * dict.func new dictionary entry
21284 * "name" == NULL, "fudi.fd_dict" set,
21285 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21286 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021287 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021288 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21289 * dict.func existing dict entry that's not a Funcref
21290 * "name" == NULL, "fudi.fd_dict" set,
21291 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021294 name = trans_function_name(&p, eap->skip, 0, &fudi);
21295 paren = (vim_strchr(p, '(') != NULL);
21296 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 {
21298 /*
21299 * Return on an invalid expression in braces, unless the expression
21300 * evaluation has been cancelled due to an aborting error, an
21301 * interrupt, or an exception.
21302 */
21303 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021305 if (!eap->skip && fudi.fd_newkey != NULL)
21306 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021307 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 else
21311 eap->skip = TRUE;
21312 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021313
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 /* An error in a function call during evaluation of an expression in magic
21315 * braces should not cause the function not to be defined. */
21316 saved_did_emsg = did_emsg;
21317 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318
21319 /*
21320 * ":function func" with only function name: list function.
21321 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021322 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 {
21324 if (!ends_excmd(*skipwhite(p)))
21325 {
21326 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021327 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 }
21329 eap->nextcmd = check_nextcmd(p);
21330 if (eap->nextcmd != NULL)
21331 *p = NUL;
21332 if (!eap->skip && !got_int)
21333 {
21334 fp = find_func(name);
21335 if (fp != NULL)
21336 {
21337 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021338 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021340 if (FUNCLINE(fp, j) == NULL)
21341 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 msg_putchar('\n');
21343 msg_outnum((long)(j + 1));
21344 if (j < 9)
21345 msg_putchar(' ');
21346 if (j < 99)
21347 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021348 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 out_flush(); /* show a line at a time */
21350 ui_breakcheck();
21351 }
21352 if (!got_int)
21353 {
21354 msg_putchar('\n');
21355 msg_puts((char_u *)" endfunction");
21356 }
21357 }
21358 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021359 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021361 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 }
21363
21364 /*
21365 * ":function name(arg1, arg2)" Define function.
21366 */
21367 p = skipwhite(p);
21368 if (*p != '(')
21369 {
21370 if (!eap->skip)
21371 {
21372 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021373 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 }
21375 /* attempt to continue by skipping some text */
21376 if (vim_strchr(p, '(') != NULL)
21377 p = vim_strchr(p, '(');
21378 }
21379 p = skipwhite(p + 1);
21380
21381 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21382 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21383
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021384 if (!eap->skip)
21385 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021386 /* Check the name of the function. Unless it's a dictionary function
21387 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021388 if (name != NULL)
21389 arg = name;
21390 else
21391 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021392 if (arg != NULL && (fudi.fd_di == NULL
21393 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021394 {
21395 if (*arg == K_SPECIAL)
21396 j = 3;
21397 else
21398 j = 0;
21399 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21400 : eval_isnamec(arg[j])))
21401 ++j;
21402 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021403 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021404 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021405 /* Disallow using the g: dict. */
21406 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21407 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021408 }
21409
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 /*
21411 * Isolate the arguments: "arg1, arg2, ...)"
21412 */
21413 while (*p != ')')
21414 {
21415 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21416 {
21417 varargs = TRUE;
21418 p += 3;
21419 mustend = TRUE;
21420 }
21421 else
21422 {
21423 arg = p;
21424 while (ASCII_ISALNUM(*p) || *p == '_')
21425 ++p;
21426 if (arg == p || isdigit(*arg)
21427 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21428 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21429 {
21430 if (!eap->skip)
21431 EMSG2(_("E125: Illegal argument: %s"), arg);
21432 break;
21433 }
21434 if (ga_grow(&newargs, 1) == FAIL)
21435 goto erret;
21436 c = *p;
21437 *p = NUL;
21438 arg = vim_strsave(arg);
21439 if (arg == NULL)
21440 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021441
21442 /* Check for duplicate argument name. */
21443 for (i = 0; i < newargs.ga_len; ++i)
21444 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21445 {
21446 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021447 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021448 goto erret;
21449 }
21450
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21452 *p = c;
21453 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 if (*p == ',')
21455 ++p;
21456 else
21457 mustend = TRUE;
21458 }
21459 p = skipwhite(p);
21460 if (mustend && *p != ')')
21461 {
21462 if (!eap->skip)
21463 EMSG2(_(e_invarg2), eap->arg);
21464 break;
21465 }
21466 }
21467 ++p; /* skip the ')' */
21468
Bram Moolenaare9a41262005-01-15 22:18:47 +000021469 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 for (;;)
21471 {
21472 p = skipwhite(p);
21473 if (STRNCMP(p, "range", 5) == 0)
21474 {
21475 flags |= FC_RANGE;
21476 p += 5;
21477 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021478 else if (STRNCMP(p, "dict", 4) == 0)
21479 {
21480 flags |= FC_DICT;
21481 p += 4;
21482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 else if (STRNCMP(p, "abort", 5) == 0)
21484 {
21485 flags |= FC_ABORT;
21486 p += 5;
21487 }
21488 else
21489 break;
21490 }
21491
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021492 /* When there is a line break use what follows for the function body.
21493 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21494 if (*p == '\n')
21495 line_arg = p + 1;
21496 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 EMSG(_(e_trailing));
21498
21499 /*
21500 * Read the body of the function, until ":endfunction" is found.
21501 */
21502 if (KeyTyped)
21503 {
21504 /* Check if the function already exists, don't let the user type the
21505 * whole function before telling him it doesn't work! For a script we
21506 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021507 if (!eap->skip && !eap->forceit)
21508 {
21509 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21510 EMSG(_(e_funcdict));
21511 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021512 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021515 if (!eap->skip && did_emsg)
21516 goto erret;
21517
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 msg_putchar('\n'); /* don't overwrite the function name */
21519 cmdline_row = msg_row;
21520 }
21521
21522 indent = 2;
21523 nesting = 0;
21524 for (;;)
21525 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021526 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021527 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021528 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021529 saved_wait_return = FALSE;
21530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021532 sourcing_lnum_off = sourcing_lnum;
21533
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021534 if (line_arg != NULL)
21535 {
21536 /* Use eap->arg, split up in parts by line breaks. */
21537 theline = line_arg;
21538 p = vim_strchr(theline, '\n');
21539 if (p == NULL)
21540 line_arg += STRLEN(line_arg);
21541 else
21542 {
21543 *p = NUL;
21544 line_arg = p + 1;
21545 }
21546 }
21547 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 theline = getcmdline(':', 0L, indent);
21549 else
21550 theline = eap->getline(':', eap->cookie, indent);
21551 if (KeyTyped)
21552 lines_left = Rows - 1;
21553 if (theline == NULL)
21554 {
21555 EMSG(_("E126: Missing :endfunction"));
21556 goto erret;
21557 }
21558
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021559 /* Detect line continuation: sourcing_lnum increased more than one. */
21560 if (sourcing_lnum > sourcing_lnum_off + 1)
21561 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21562 else
21563 sourcing_lnum_off = 0;
21564
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 if (skip_until != NULL)
21566 {
21567 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21568 * don't check for ":endfunc". */
21569 if (STRCMP(theline, skip_until) == 0)
21570 {
21571 vim_free(skip_until);
21572 skip_until = NULL;
21573 }
21574 }
21575 else
21576 {
21577 /* skip ':' and blanks*/
21578 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21579 ;
21580
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021581 /* Check for "endfunction". */
21582 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021584 if (line_arg == NULL)
21585 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 break;
21587 }
21588
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021589 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 * at "end". */
21591 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21592 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021593 else if (STRNCMP(p, "if", 2) == 0
21594 || STRNCMP(p, "wh", 2) == 0
21595 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 || STRNCMP(p, "try", 3) == 0)
21597 indent += 2;
21598
21599 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021600 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021602 if (*p == '!')
21603 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 p += eval_fname_script(p);
21605 if (ASCII_ISALPHA(*p))
21606 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021607 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 if (*skipwhite(p) == '(')
21609 {
21610 ++nesting;
21611 indent += 2;
21612 }
21613 }
21614 }
21615
21616 /* Check for ":append" or ":insert". */
21617 p = skip_range(p, NULL);
21618 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21619 || (p[0] == 'i'
21620 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21621 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21622 skip_until = vim_strsave((char_u *)".");
21623
21624 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21625 arg = skipwhite(skiptowhite(p));
21626 if (arg[0] == '<' && arg[1] =='<'
21627 && ((p[0] == 'p' && p[1] == 'y'
21628 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21629 || (p[0] == 'p' && p[1] == 'e'
21630 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21631 || (p[0] == 't' && p[1] == 'c'
21632 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021633 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21634 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21636 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021637 || (p[0] == 'm' && p[1] == 'z'
21638 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 ))
21640 {
21641 /* ":python <<" continues until a dot, like ":append" */
21642 p = skipwhite(arg + 2);
21643 if (*p == NUL)
21644 skip_until = vim_strsave((char_u *)".");
21645 else
21646 skip_until = vim_strsave(p);
21647 }
21648 }
21649
21650 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021651 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021652 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021653 if (line_arg == NULL)
21654 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021656 }
21657
21658 /* Copy the line to newly allocated memory. get_one_sourceline()
21659 * allocates 250 bytes per line, this saves 80% on average. The cost
21660 * is an extra alloc/free. */
21661 p = vim_strsave(theline);
21662 if (p != NULL)
21663 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021664 if (line_arg == NULL)
21665 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021666 theline = p;
21667 }
21668
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021669 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21670
21671 /* Add NULL lines for continuation lines, so that the line count is
21672 * equal to the index in the growarray. */
21673 while (sourcing_lnum_off-- > 0)
21674 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021675
21676 /* Check for end of eap->arg. */
21677 if (line_arg != NULL && *line_arg == NUL)
21678 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 }
21680
21681 /* Don't define the function when skipping commands or when an error was
21682 * detected. */
21683 if (eap->skip || did_emsg)
21684 goto erret;
21685
21686 /*
21687 * If there are no errors, add the function
21688 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021689 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021690 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021691 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021692 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021693 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021694 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021695 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021696 goto erret;
21697 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021698
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021699 fp = find_func(name);
21700 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021702 if (!eap->forceit)
21703 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021704 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021705 goto erret;
21706 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021707 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021709 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021710 name);
21711 goto erret;
21712 }
21713 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021714 ga_clear_strings(&(fp->uf_args));
21715 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021716 vim_free(name);
21717 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 }
21720 else
21721 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021722 char numbuf[20];
21723
21724 fp = NULL;
21725 if (fudi.fd_newkey == NULL && !eap->forceit)
21726 {
21727 EMSG(_(e_funcdict));
21728 goto erret;
21729 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021730 if (fudi.fd_di == NULL)
21731 {
21732 /* Can't add a function to a locked dictionary */
21733 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21734 goto erret;
21735 }
21736 /* Can't change an existing function if it is locked */
21737 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21738 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021739
21740 /* Give the function a sequential number. Can only be used with a
21741 * Funcref! */
21742 vim_free(name);
21743 sprintf(numbuf, "%d", ++func_nr);
21744 name = vim_strsave((char_u *)numbuf);
21745 if (name == NULL)
21746 goto erret;
21747 }
21748
21749 if (fp == NULL)
21750 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021751 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021752 {
21753 int slen, plen;
21754 char_u *scriptname;
21755
21756 /* Check that the autoload name matches the script name. */
21757 j = FAIL;
21758 if (sourcing_name != NULL)
21759 {
21760 scriptname = autoload_name(name);
21761 if (scriptname != NULL)
21762 {
21763 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021764 plen = (int)STRLEN(p);
21765 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021766 if (slen > plen && fnamecmp(p,
21767 sourcing_name + slen - plen) == 0)
21768 j = OK;
21769 vim_free(scriptname);
21770 }
21771 }
21772 if (j == FAIL)
21773 {
21774 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21775 goto erret;
21776 }
21777 }
21778
21779 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 if (fp == NULL)
21781 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021782
21783 if (fudi.fd_dict != NULL)
21784 {
21785 if (fudi.fd_di == NULL)
21786 {
21787 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021788 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021789 if (fudi.fd_di == NULL)
21790 {
21791 vim_free(fp);
21792 goto erret;
21793 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021794 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21795 {
21796 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021797 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021798 goto erret;
21799 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021800 }
21801 else
21802 /* overwrite existing dict entry */
21803 clear_tv(&fudi.fd_di->di_tv);
21804 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021805 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021806 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021807 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021808
21809 /* behave like "dict" was used */
21810 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021811 }
21812
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021814 STRCPY(fp->uf_name, name);
21815 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021817 fp->uf_args = newargs;
21818 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021819#ifdef FEAT_PROFILE
21820 fp->uf_tml_count = NULL;
21821 fp->uf_tml_total = NULL;
21822 fp->uf_tml_self = NULL;
21823 fp->uf_profiling = FALSE;
21824 if (prof_def_func())
21825 func_do_profile(fp);
21826#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021827 fp->uf_varargs = varargs;
21828 fp->uf_flags = flags;
21829 fp->uf_calls = 0;
21830 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021831 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832
21833erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834 ga_clear_strings(&newargs);
21835 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021836ret_free:
21837 vim_free(skip_until);
21838 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021841 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842}
21843
21844/*
21845 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021846 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021848 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021849 * TFN_INT: internal function name OK
21850 * TFN_QUIET: be quiet
21851 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 * Advances "pp" to just after the function name (if no error).
21853 */
21854 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021855trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 char_u **pp;
21857 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021858 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021861 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862 char_u *start;
21863 char_u *end;
21864 int lead;
21865 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021868
21869 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021870 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021872
21873 /* Check for hard coded <SNR>: already translated function ID (from a user
21874 * command). */
21875 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21876 && (*pp)[2] == (int)KE_SNR)
21877 {
21878 *pp += 3;
21879 len = get_id_len(pp) + 3;
21880 return vim_strnsave(start, len);
21881 }
21882
21883 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21884 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021886 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021888
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021889 /* Note that TFN_ flags use the same values as GLV_ flags. */
21890 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021891 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021892 if (end == start)
21893 {
21894 if (!skip)
21895 EMSG(_("E129: Function name required"));
21896 goto theend;
21897 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021898 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021899 {
21900 /*
21901 * Report an invalid expression in braces, unless the expression
21902 * evaluation has been cancelled due to an aborting error, an
21903 * interrupt, or an exception.
21904 */
21905 if (!aborting())
21906 {
21907 if (end != NULL)
21908 EMSG2(_(e_invarg2), start);
21909 }
21910 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021911 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021912 goto theend;
21913 }
21914
21915 if (lv.ll_tv != NULL)
21916 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021917 if (fdp != NULL)
21918 {
21919 fdp->fd_dict = lv.ll_dict;
21920 fdp->fd_newkey = lv.ll_newkey;
21921 lv.ll_newkey = NULL;
21922 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021923 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021924 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21925 {
21926 name = vim_strsave(lv.ll_tv->vval.v_string);
21927 *pp = end;
21928 }
21929 else
21930 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021931 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21932 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021933 EMSG(_(e_funcref));
21934 else
21935 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021936 name = NULL;
21937 }
21938 goto theend;
21939 }
21940
21941 if (lv.ll_name == NULL)
21942 {
21943 /* Error found, but continue after the function name. */
21944 *pp = end;
21945 goto theend;
21946 }
21947
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021948 /* Check if the name is a Funcref. If so, use the value. */
21949 if (lv.ll_exp_name != NULL)
21950 {
21951 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010021952 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021953 if (name == lv.ll_exp_name)
21954 name = NULL;
21955 }
21956 else
21957 {
21958 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010021959 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021960 if (name == *pp)
21961 name = NULL;
21962 }
21963 if (name != NULL)
21964 {
21965 name = vim_strsave(name);
21966 *pp = end;
21967 goto theend;
21968 }
21969
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021970 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021971 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021972 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021973 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21974 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21975 {
21976 /* When there was "s:" already or the name expanded to get a
21977 * leading "s:" then remove it. */
21978 lv.ll_name += 2;
21979 len -= 2;
21980 lead = 2;
21981 }
21982 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021983 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021984 {
21985 if (lead == 2) /* skip over "s:" */
21986 lv.ll_name += 2;
21987 len = (int)(end - lv.ll_name);
21988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021989
21990 /*
21991 * Copy the function name to allocated memory.
21992 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21993 * Accept <SNR>123_name() outside a script.
21994 */
21995 if (skip)
21996 lead = 0; /* do nothing */
21997 else if (lead > 0)
21998 {
21999 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022000 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22001 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022002 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022003 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022004 if (current_SID <= 0)
22005 {
22006 EMSG(_(e_usingsid));
22007 goto theend;
22008 }
22009 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22010 lead += (int)STRLEN(sid_buf);
22011 }
22012 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022013 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022014 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022015 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022016 goto theend;
22017 }
22018 name = alloc((unsigned)(len + lead + 1));
22019 if (name != NULL)
22020 {
22021 if (lead > 0)
22022 {
22023 name[0] = K_SPECIAL;
22024 name[1] = KS_EXTRA;
22025 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022026 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022027 STRCPY(name + 3, sid_buf);
22028 }
22029 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22030 name[len + lead] = NUL;
22031 }
22032 *pp = end;
22033
22034theend:
22035 clear_lval(&lv);
22036 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037}
22038
22039/*
22040 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22041 * Return 2 if "p" starts with "s:".
22042 * Return 0 otherwise.
22043 */
22044 static int
22045eval_fname_script(p)
22046 char_u *p;
22047{
22048 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22049 || STRNICMP(p + 1, "SNR>", 4) == 0))
22050 return 5;
22051 if (p[0] == 's' && p[1] == ':')
22052 return 2;
22053 return 0;
22054}
22055
22056/*
22057 * Return TRUE if "p" starts with "<SID>" or "s:".
22058 * Only works if eval_fname_script() returned non-zero for "p"!
22059 */
22060 static int
22061eval_fname_sid(p)
22062 char_u *p;
22063{
22064 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22065}
22066
22067/*
22068 * List the head of the function: "name(arg1, arg2)".
22069 */
22070 static void
22071list_func_head(fp, indent)
22072 ufunc_T *fp;
22073 int indent;
22074{
22075 int j;
22076
22077 msg_start();
22078 if (indent)
22079 MSG_PUTS(" ");
22080 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022081 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 {
22083 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022084 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 }
22086 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022087 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022089 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 {
22091 if (j)
22092 MSG_PUTS(", ");
22093 msg_puts(FUNCARG(fp, j));
22094 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022095 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 {
22097 if (j)
22098 MSG_PUTS(", ");
22099 MSG_PUTS("...");
22100 }
22101 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022102 if (fp->uf_flags & FC_ABORT)
22103 MSG_PUTS(" abort");
22104 if (fp->uf_flags & FC_RANGE)
22105 MSG_PUTS(" range");
22106 if (fp->uf_flags & FC_DICT)
22107 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022108 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022109 if (p_verbose > 0)
22110 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111}
22112
22113/*
22114 * Find a function by name, return pointer to it in ufuncs.
22115 * Return NULL for unknown function.
22116 */
22117 static ufunc_T *
22118find_func(name)
22119 char_u *name;
22120{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022121 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022123 hi = hash_find(&func_hashtab, name);
22124 if (!HASHITEM_EMPTY(hi))
22125 return HI2UF(hi);
22126 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127}
22128
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022129#if defined(EXITFREE) || defined(PROTO)
22130 void
22131free_all_functions()
22132{
22133 hashitem_T *hi;
22134
22135 /* Need to start all over every time, because func_free() may change the
22136 * hash table. */
22137 while (func_hashtab.ht_used > 0)
22138 for (hi = func_hashtab.ht_array; ; ++hi)
22139 if (!HASHITEM_EMPTY(hi))
22140 {
22141 func_free(HI2UF(hi));
22142 break;
22143 }
22144}
22145#endif
22146
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022147 int
22148translated_function_exists(name)
22149 char_u *name;
22150{
22151 if (builtin_function(name))
22152 return find_internal_func(name) >= 0;
22153 return find_func(name) != NULL;
22154}
22155
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022156/*
22157 * Return TRUE if a function "name" exists.
22158 */
22159 static int
22160function_exists(name)
22161 char_u *name;
22162{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022163 char_u *nm = name;
22164 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022165 int n = FALSE;
22166
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022167 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22168 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022169 nm = skipwhite(nm);
22170
22171 /* Only accept "funcname", "funcname ", "funcname (..." and
22172 * "funcname(...", not "funcname!...". */
22173 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022174 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022175 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022176 return n;
22177}
22178
Bram Moolenaara1544c02013-05-30 12:35:52 +020022179 char_u *
22180get_expanded_name(name, check)
22181 char_u *name;
22182 int check;
22183{
22184 char_u *nm = name;
22185 char_u *p;
22186
22187 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22188
22189 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022190 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022191 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022192
Bram Moolenaara1544c02013-05-30 12:35:52 +020022193 vim_free(p);
22194 return NULL;
22195}
22196
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022197/*
22198 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022199 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022200 */
22201 static int
22202builtin_function(name)
22203 char_u *name;
22204{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022205 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22206 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022207}
22208
Bram Moolenaar05159a02005-02-26 23:04:13 +000022209#if defined(FEAT_PROFILE) || defined(PROTO)
22210/*
22211 * Start profiling function "fp".
22212 */
22213 static void
22214func_do_profile(fp)
22215 ufunc_T *fp;
22216{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022217 int len = fp->uf_lines.ga_len;
22218
22219 if (len == 0)
22220 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022221 fp->uf_tm_count = 0;
22222 profile_zero(&fp->uf_tm_self);
22223 profile_zero(&fp->uf_tm_total);
22224 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022225 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022226 if (fp->uf_tml_total == NULL)
22227 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022228 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022229 if (fp->uf_tml_self == NULL)
22230 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022231 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022232 fp->uf_tml_idx = -1;
22233 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22234 || fp->uf_tml_self == NULL)
22235 return; /* out of memory */
22236
22237 fp->uf_profiling = TRUE;
22238}
22239
22240/*
22241 * Dump the profiling results for all functions in file "fd".
22242 */
22243 void
22244func_dump_profile(fd)
22245 FILE *fd;
22246{
22247 hashitem_T *hi;
22248 int todo;
22249 ufunc_T *fp;
22250 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022251 ufunc_T **sorttab;
22252 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022253
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022254 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022255 if (todo == 0)
22256 return; /* nothing to dump */
22257
Bram Moolenaar73830342005-02-28 22:48:19 +000022258 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22259
Bram Moolenaar05159a02005-02-26 23:04:13 +000022260 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22261 {
22262 if (!HASHITEM_EMPTY(hi))
22263 {
22264 --todo;
22265 fp = HI2UF(hi);
22266 if (fp->uf_profiling)
22267 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022268 if (sorttab != NULL)
22269 sorttab[st_len++] = fp;
22270
Bram Moolenaar05159a02005-02-26 23:04:13 +000022271 if (fp->uf_name[0] == K_SPECIAL)
22272 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22273 else
22274 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22275 if (fp->uf_tm_count == 1)
22276 fprintf(fd, "Called 1 time\n");
22277 else
22278 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22279 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22280 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22281 fprintf(fd, "\n");
22282 fprintf(fd, "count total (s) self (s)\n");
22283
22284 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22285 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022286 if (FUNCLINE(fp, i) == NULL)
22287 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022288 prof_func_line(fd, fp->uf_tml_count[i],
22289 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022290 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22291 }
22292 fprintf(fd, "\n");
22293 }
22294 }
22295 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022296
22297 if (sorttab != NULL && st_len > 0)
22298 {
22299 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22300 prof_total_cmp);
22301 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22302 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22303 prof_self_cmp);
22304 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22305 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022306
22307 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022308}
Bram Moolenaar73830342005-02-28 22:48:19 +000022309
22310 static void
22311prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22312 FILE *fd;
22313 ufunc_T **sorttab;
22314 int st_len;
22315 char *title;
22316 int prefer_self; /* when equal print only self time */
22317{
22318 int i;
22319 ufunc_T *fp;
22320
22321 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22322 fprintf(fd, "count total (s) self (s) function\n");
22323 for (i = 0; i < 20 && i < st_len; ++i)
22324 {
22325 fp = sorttab[i];
22326 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22327 prefer_self);
22328 if (fp->uf_name[0] == K_SPECIAL)
22329 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22330 else
22331 fprintf(fd, " %s()\n", fp->uf_name);
22332 }
22333 fprintf(fd, "\n");
22334}
22335
22336/*
22337 * Print the count and times for one function or function line.
22338 */
22339 static void
22340prof_func_line(fd, count, total, self, prefer_self)
22341 FILE *fd;
22342 int count;
22343 proftime_T *total;
22344 proftime_T *self;
22345 int prefer_self; /* when equal print only self time */
22346{
22347 if (count > 0)
22348 {
22349 fprintf(fd, "%5d ", count);
22350 if (prefer_self && profile_equal(total, self))
22351 fprintf(fd, " ");
22352 else
22353 fprintf(fd, "%s ", profile_msg(total));
22354 if (!prefer_self && profile_equal(total, self))
22355 fprintf(fd, " ");
22356 else
22357 fprintf(fd, "%s ", profile_msg(self));
22358 }
22359 else
22360 fprintf(fd, " ");
22361}
22362
22363/*
22364 * Compare function for total time sorting.
22365 */
22366 static int
22367#ifdef __BORLANDC__
22368_RTLENTRYF
22369#endif
22370prof_total_cmp(s1, s2)
22371 const void *s1;
22372 const void *s2;
22373{
22374 ufunc_T *p1, *p2;
22375
22376 p1 = *(ufunc_T **)s1;
22377 p2 = *(ufunc_T **)s2;
22378 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22379}
22380
22381/*
22382 * Compare function for self time sorting.
22383 */
22384 static int
22385#ifdef __BORLANDC__
22386_RTLENTRYF
22387#endif
22388prof_self_cmp(s1, s2)
22389 const void *s1;
22390 const void *s2;
22391{
22392 ufunc_T *p1, *p2;
22393
22394 p1 = *(ufunc_T **)s1;
22395 p2 = *(ufunc_T **)s2;
22396 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22397}
22398
Bram Moolenaar05159a02005-02-26 23:04:13 +000022399#endif
22400
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022401/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022402 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022403 * Return TRUE if a package was loaded.
22404 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022405 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022406script_autoload(name, reload)
22407 char_u *name;
22408 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022409{
22410 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022411 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022412 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022413 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022414
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022415 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022416 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022417 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022418 return FALSE;
22419
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022420 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022421
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022422 /* Find the name in the list of previously loaded package names. Skip
22423 * "autoload/", it's always the same. */
22424 for (i = 0; i < ga_loaded.ga_len; ++i)
22425 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22426 break;
22427 if (!reload && i < ga_loaded.ga_len)
22428 ret = FALSE; /* was loaded already */
22429 else
22430 {
22431 /* Remember the name if it wasn't loaded already. */
22432 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22433 {
22434 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22435 tofree = NULL;
22436 }
22437
22438 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022439 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022440 ret = TRUE;
22441 }
22442
22443 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022444 return ret;
22445}
22446
22447/*
22448 * Return the autoload script name for a function or variable name.
22449 * Returns NULL when out of memory.
22450 */
22451 static char_u *
22452autoload_name(name)
22453 char_u *name;
22454{
22455 char_u *p;
22456 char_u *scriptname;
22457
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022458 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022459 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22460 if (scriptname == NULL)
22461 return FALSE;
22462 STRCPY(scriptname, "autoload/");
22463 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022464 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022465 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022466 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022467 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022468 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022469}
22470
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22472
22473/*
22474 * Function given to ExpandGeneric() to obtain the list of user defined
22475 * function names.
22476 */
22477 char_u *
22478get_user_func_name(xp, idx)
22479 expand_T *xp;
22480 int idx;
22481{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022482 static long_u done;
22483 static hashitem_T *hi;
22484 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485
22486 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022488 done = 0;
22489 hi = func_hashtab.ht_array;
22490 }
22491 if (done < func_hashtab.ht_used)
22492 {
22493 if (done++ > 0)
22494 ++hi;
22495 while (HASHITEM_EMPTY(hi))
22496 ++hi;
22497 fp = HI2UF(hi);
22498
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022499 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022500 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022501
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022502 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22503 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504
22505 cat_func_name(IObuff, fp);
22506 if (xp->xp_context != EXPAND_USER_FUNC)
22507 {
22508 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022509 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 STRCAT(IObuff, ")");
22511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 return IObuff;
22513 }
22514 return NULL;
22515}
22516
22517#endif /* FEAT_CMDL_COMPL */
22518
22519/*
22520 * Copy the function name of "fp" to buffer "buf".
22521 * "buf" must be able to hold the function name plus three bytes.
22522 * Takes care of script-local function names.
22523 */
22524 static void
22525cat_func_name(buf, fp)
22526 char_u *buf;
22527 ufunc_T *fp;
22528{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022529 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 {
22531 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022532 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 }
22534 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022535 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536}
22537
22538/*
22539 * ":delfunction {name}"
22540 */
22541 void
22542ex_delfunction(eap)
22543 exarg_T *eap;
22544{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022545 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 char_u *p;
22547 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022548 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549
22550 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022551 name = trans_function_name(&p, eap->skip, 0, &fudi);
22552 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022554 {
22555 if (fudi.fd_dict != NULL && !eap->skip)
22556 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 if (!ends_excmd(*skipwhite(p)))
22560 {
22561 vim_free(name);
22562 EMSG(_(e_trailing));
22563 return;
22564 }
22565 eap->nextcmd = check_nextcmd(p);
22566 if (eap->nextcmd != NULL)
22567 *p = NUL;
22568
22569 if (!eap->skip)
22570 fp = find_func(name);
22571 vim_free(name);
22572
22573 if (!eap->skip)
22574 {
22575 if (fp == NULL)
22576 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022577 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 return;
22579 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022580 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 {
22582 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22583 return;
22584 }
22585
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022586 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022588 /* Delete the dict item that refers to the function, it will
22589 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022590 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022592 else
22593 func_free(fp);
22594 }
22595}
22596
22597/*
22598 * Free a function and remove it from the list of functions.
22599 */
22600 static void
22601func_free(fp)
22602 ufunc_T *fp;
22603{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022604 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022605
22606 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022607 ga_clear_strings(&(fp->uf_args));
22608 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022609#ifdef FEAT_PROFILE
22610 vim_free(fp->uf_tml_count);
22611 vim_free(fp->uf_tml_total);
22612 vim_free(fp->uf_tml_self);
22613#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022614
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022615 /* remove the function from the function hashtable */
22616 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22617 if (HASHITEM_EMPTY(hi))
22618 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022619 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022620 hash_remove(&func_hashtab, hi);
22621
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022622 vim_free(fp);
22623}
22624
22625/*
22626 * Unreference a Function: decrement the reference count and free it when it
22627 * becomes zero. Only for numbered functions.
22628 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022629 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022630func_unref(name)
22631 char_u *name;
22632{
22633 ufunc_T *fp;
22634
22635 if (name != NULL && isdigit(*name))
22636 {
22637 fp = find_func(name);
22638 if (fp == NULL)
22639 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022640 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022641 {
22642 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022643 * when "uf_calls" becomes zero. */
22644 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022645 func_free(fp);
22646 }
22647 }
22648}
22649
22650/*
22651 * Count a reference to a Function.
22652 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022653 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022654func_ref(name)
22655 char_u *name;
22656{
22657 ufunc_T *fp;
22658
22659 if (name != NULL && isdigit(*name))
22660 {
22661 fp = find_func(name);
22662 if (fp == NULL)
22663 EMSG2(_(e_intern2), "func_ref()");
22664 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022665 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 }
22667}
22668
22669/*
22670 * Call a user function.
22671 */
22672 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022673call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674 ufunc_T *fp; /* pointer to function */
22675 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 typval_T *argvars; /* arguments */
22677 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 linenr_T firstline; /* first line of range */
22679 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022680 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681{
Bram Moolenaar33570922005-01-25 22:26:29 +000022682 char_u *save_sourcing_name;
22683 linenr_T save_sourcing_lnum;
22684 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022685 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022686 int save_did_emsg;
22687 static int depth = 0;
22688 dictitem_T *v;
22689 int fixvar_idx = 0; /* index in fixvar[] */
22690 int i;
22691 int ai;
22692 char_u numbuf[NUMBUFLEN];
22693 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022694#ifdef FEAT_PROFILE
22695 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022696 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698
22699 /* If depth of calling is getting too high, don't execute the function */
22700 if (depth >= p_mfd)
22701 {
22702 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022703 rettv->v_type = VAR_NUMBER;
22704 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 return;
22706 }
22707 ++depth;
22708
22709 line_breakcheck(); /* check for CTRL-C hit */
22710
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022711 fc = (funccall_T *)alloc(sizeof(funccall_T));
22712 fc->caller = current_funccal;
22713 current_funccal = fc;
22714 fc->func = fp;
22715 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022716 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022717 fc->linenr = 0;
22718 fc->returned = FALSE;
22719 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022721 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22722 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723
Bram Moolenaar33570922005-01-25 22:26:29 +000022724 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022725 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022726 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22727 * each argument variable and saves a lot of time.
22728 */
22729 /*
22730 * Init l: variables.
22731 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022732 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022733 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022734 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022735 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22736 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022737 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022738 name = v->di_key;
22739 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022741 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022742 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022743 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022744 v->di_tv.vval.v_dict = selfdict;
22745 ++selfdict->dv_refcount;
22746 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022747
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 /*
22749 * Init a: variables.
22750 * Set a:0 to "argcount".
22751 * Set a:000 to a list with room for the "..." arguments.
22752 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022753 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022754 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022755 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022756 /* Use "name" to avoid a warning from some compiler that checks the
22757 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022758 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022759 name = v->di_key;
22760 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022761 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022762 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022763 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022764 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022765 v->di_tv.vval.v_list = &fc->l_varlist;
22766 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22767 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22768 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022769
22770 /*
22771 * Set a:firstline to "firstline" and a:lastline to "lastline".
22772 * Set a:name to named arguments.
22773 * Set a:N to the "..." arguments.
22774 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022775 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022776 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022777 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022778 (varnumber_T)lastline);
22779 for (i = 0; i < argcount; ++i)
22780 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022781 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022782 if (ai < 0)
22783 /* named argument a:name */
22784 name = FUNCARG(fp, i);
22785 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022786 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 /* "..." argument a:1, a:2, etc. */
22788 sprintf((char *)numbuf, "%d", ai + 1);
22789 name = numbuf;
22790 }
22791 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22792 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022793 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22795 }
22796 else
22797 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022798 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22799 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022800 if (v == NULL)
22801 break;
22802 v->di_flags = DI_FLAGS_RO;
22803 }
22804 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022805 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022806
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022807 /* Note: the values are copied directly to avoid alloc/free.
22808 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022809 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022810 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022811
22812 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22813 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022814 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22815 fc->l_listitems[ai].li_tv = argvars[i];
22816 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022817 }
22818 }
22819
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 /* Don't redraw while executing the function. */
22821 ++RedrawingDisabled;
22822 save_sourcing_name = sourcing_name;
22823 save_sourcing_lnum = sourcing_lnum;
22824 sourcing_lnum = 1;
22825 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022826 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 if (sourcing_name != NULL)
22828 {
22829 if (save_sourcing_name != NULL
22830 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22831 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22832 else
22833 STRCPY(sourcing_name, "function ");
22834 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22835
22836 if (p_verbose >= 12)
22837 {
22838 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022839 verbose_enter_scroll();
22840
Bram Moolenaar555b2802005-05-19 21:08:39 +000022841 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 if (p_verbose >= 14)
22843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022845 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022846 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022847 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848
22849 msg_puts((char_u *)"(");
22850 for (i = 0; i < argcount; ++i)
22851 {
22852 if (i > 0)
22853 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022854 if (argvars[i].v_type == VAR_NUMBER)
22855 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 else
22857 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022858 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22859 if (s != NULL)
22860 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022861 if (vim_strsize(s) > MSG_BUF_CLEN)
22862 {
22863 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22864 s = buf;
22865 }
22866 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022867 vim_free(tofree);
22868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 }
22870 }
22871 msg_puts((char_u *)")");
22872 }
22873 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022874
22875 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 --no_wait_return;
22877 }
22878 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022879#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022880 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022881 {
22882 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22883 func_do_profile(fp);
22884 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022885 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022886 {
22887 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022888 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022889 profile_zero(&fp->uf_tm_children);
22890 }
22891 script_prof_save(&wait_start);
22892 }
22893#endif
22894
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022896 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 save_did_emsg = did_emsg;
22898 did_emsg = FALSE;
22899
22900 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022901 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22903
22904 --RedrawingDisabled;
22905
22906 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022907 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022909 clear_tv(rettv);
22910 rettv->v_type = VAR_NUMBER;
22911 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 }
22913
Bram Moolenaar05159a02005-02-26 23:04:13 +000022914#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022915 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022916 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022917 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022918 profile_end(&call_start);
22919 profile_sub_wait(&wait_start, &call_start);
22920 profile_add(&fp->uf_tm_total, &call_start);
22921 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022922 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022923 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022924 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22925 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022926 }
22927 }
22928#endif
22929
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930 /* when being verbose, mention the return value */
22931 if (p_verbose >= 12)
22932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022934 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022937 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022938 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022939 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022940 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022943 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022944 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022945 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022946 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022947
Bram Moolenaar555b2802005-05-19 21:08:39 +000022948 /* The value may be very long. Skip the middle part, so that we
22949 * have some idea how it starts and ends. smsg() would always
22950 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022951 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022952 if (s != NULL)
22953 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022954 if (vim_strsize(s) > MSG_BUF_CLEN)
22955 {
22956 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22957 s = buf;
22958 }
22959 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022960 vim_free(tofree);
22961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 }
22963 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022964
22965 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 --no_wait_return;
22967 }
22968
22969 vim_free(sourcing_name);
22970 sourcing_name = save_sourcing_name;
22971 sourcing_lnum = save_sourcing_lnum;
22972 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022973#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022974 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022975 script_prof_restore(&wait_start);
22976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977
22978 if (p_verbose >= 12 && sourcing_name != NULL)
22979 {
22980 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022981 verbose_enter_scroll();
22982
Bram Moolenaar555b2802005-05-19 21:08:39 +000022983 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022985
22986 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 --no_wait_return;
22988 }
22989
22990 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022991 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022993
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022994 /* If the a:000 list and the l: and a: dicts are not referenced we can
22995 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022996 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22997 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22998 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22999 {
23000 free_funccal(fc, FALSE);
23001 }
23002 else
23003 {
23004 hashitem_T *hi;
23005 listitem_T *li;
23006 int todo;
23007
23008 /* "fc" is still in use. This can happen when returning "a:000" or
23009 * assigning "l:" to a global variable.
23010 * Link "fc" in the list for garbage collection later. */
23011 fc->caller = previous_funccal;
23012 previous_funccal = fc;
23013
23014 /* Make a copy of the a: variables, since we didn't do that above. */
23015 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23016 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23017 {
23018 if (!HASHITEM_EMPTY(hi))
23019 {
23020 --todo;
23021 v = HI2DI(hi);
23022 copy_tv(&v->di_tv, &v->di_tv);
23023 }
23024 }
23025
23026 /* Make a copy of the a:000 items, since we didn't do that above. */
23027 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23028 copy_tv(&li->li_tv, &li->li_tv);
23029 }
23030}
23031
23032/*
23033 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023034 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023035 */
23036 static int
23037can_free_funccal(fc, copyID)
23038 funccall_T *fc;
23039 int copyID;
23040{
23041 return (fc->l_varlist.lv_copyID != copyID
23042 && fc->l_vars.dv_copyID != copyID
23043 && fc->l_avars.dv_copyID != copyID);
23044}
23045
23046/*
23047 * Free "fc" and what it contains.
23048 */
23049 static void
23050free_funccal(fc, free_val)
23051 funccall_T *fc;
23052 int free_val; /* a: vars were allocated */
23053{
23054 listitem_T *li;
23055
23056 /* The a: variables typevals may not have been allocated, only free the
23057 * allocated variables. */
23058 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23059
23060 /* free all l: variables */
23061 vars_clear(&fc->l_vars.dv_hashtab);
23062
23063 /* Free the a:000 variables if they were allocated. */
23064 if (free_val)
23065 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23066 clear_tv(&li->li_tv);
23067
23068 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069}
23070
23071/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023072 * Add a number variable "name" to dict "dp" with value "nr".
23073 */
23074 static void
23075add_nr_var(dp, v, name, nr)
23076 dict_T *dp;
23077 dictitem_T *v;
23078 char *name;
23079 varnumber_T nr;
23080{
23081 STRCPY(v->di_key, name);
23082 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23083 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23084 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023085 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 v->di_tv.vval.v_number = nr;
23087}
23088
23089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 * ":return [expr]"
23091 */
23092 void
23093ex_return(eap)
23094 exarg_T *eap;
23095{
23096 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023097 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098 int returning = FALSE;
23099
23100 if (current_funccal == NULL)
23101 {
23102 EMSG(_("E133: :return not inside a function"));
23103 return;
23104 }
23105
23106 if (eap->skip)
23107 ++emsg_skip;
23108
23109 eap->nextcmd = NULL;
23110 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023111 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 {
23113 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023114 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023116 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 }
23118 /* It's safer to return also on error. */
23119 else if (!eap->skip)
23120 {
23121 /*
23122 * Return unless the expression evaluation has been cancelled due to an
23123 * aborting error, an interrupt, or an exception.
23124 */
23125 if (!aborting())
23126 returning = do_return(eap, FALSE, TRUE, NULL);
23127 }
23128
23129 /* When skipping or the return gets pending, advance to the next command
23130 * in this line (!returning). Otherwise, ignore the rest of the line.
23131 * Following lines will be ignored by get_func_line(). */
23132 if (returning)
23133 eap->nextcmd = NULL;
23134 else if (eap->nextcmd == NULL) /* no argument */
23135 eap->nextcmd = check_nextcmd(arg);
23136
23137 if (eap->skip)
23138 --emsg_skip;
23139}
23140
23141/*
23142 * Return from a function. Possibly makes the return pending. Also called
23143 * for a pending return at the ":endtry" or after returning from an extra
23144 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023145 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023146 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 * FALSE when the return gets pending.
23148 */
23149 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023150do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 exarg_T *eap;
23152 int reanimate;
23153 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023154 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155{
23156 int idx;
23157 struct condstack *cstack = eap->cstack;
23158
23159 if (reanimate)
23160 /* Undo the return. */
23161 current_funccal->returned = FALSE;
23162
23163 /*
23164 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23165 * not in its finally clause (which then is to be executed next) is found.
23166 * In this case, make the ":return" pending for execution at the ":endtry".
23167 * Otherwise, return normally.
23168 */
23169 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23170 if (idx >= 0)
23171 {
23172 cstack->cs_pending[idx] = CSTP_RETURN;
23173
23174 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023175 /* A pending return again gets pending. "rettv" points to an
23176 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023178 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 else
23180 {
23181 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023182 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023184 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023186 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 {
23188 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023189 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023190 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 else
23192 EMSG(_(e_outofmem));
23193 }
23194 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023195 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196
23197 if (reanimate)
23198 {
23199 /* The pending return value could be overwritten by a ":return"
23200 * without argument in a finally clause; reset the default
23201 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023202 current_funccal->rettv->v_type = VAR_NUMBER;
23203 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 }
23205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023206 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 }
23208 else
23209 {
23210 current_funccal->returned = TRUE;
23211
23212 /* If the return is carried out now, store the return value. For
23213 * a return immediately after reanimation, the value is already
23214 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023215 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023217 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023218 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023220 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 }
23222 }
23223
23224 return idx < 0;
23225}
23226
23227/*
23228 * Free the variable with a pending return value.
23229 */
23230 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023231discard_pending_return(rettv)
23232 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233{
Bram Moolenaar33570922005-01-25 22:26:29 +000023234 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235}
23236
23237/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023238 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239 * is an allocated string. Used by report_pending() for verbose messages.
23240 */
23241 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023242get_return_cmd(rettv)
23243 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023245 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023246 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023247 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023249 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023250 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023251 if (s == NULL)
23252 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023253
23254 STRCPY(IObuff, ":return ");
23255 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23256 if (STRLEN(s) + 8 >= IOSIZE)
23257 STRCPY(IObuff + IOSIZE - 4, "...");
23258 vim_free(tofree);
23259 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260}
23261
23262/*
23263 * Get next function line.
23264 * Called by do_cmdline() to get the next line.
23265 * Returns allocated string, or NULL for end of function.
23266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267 char_u *
23268get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023269 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023271 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272{
Bram Moolenaar33570922005-01-25 22:26:29 +000023273 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023274 ufunc_T *fp = fcp->func;
23275 char_u *retval;
23276 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277
23278 /* If breakpoints have been added/deleted need to check for it. */
23279 if (fcp->dbg_tick != debug_tick)
23280 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023281 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 sourcing_lnum);
23283 fcp->dbg_tick = debug_tick;
23284 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023285#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023286 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023287 func_line_end(cookie);
23288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289
Bram Moolenaar05159a02005-02-26 23:04:13 +000023290 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023291 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23292 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 retval = NULL;
23294 else
23295 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023296 /* Skip NULL lines (continuation lines). */
23297 while (fcp->linenr < gap->ga_len
23298 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23299 ++fcp->linenr;
23300 if (fcp->linenr >= gap->ga_len)
23301 retval = NULL;
23302 else
23303 {
23304 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23305 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023306#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023307 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023308 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023309#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 }
23312
23313 /* Did we encounter a breakpoint? */
23314 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23315 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023316 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023318 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 sourcing_lnum);
23320 fcp->dbg_tick = debug_tick;
23321 }
23322
23323 return retval;
23324}
23325
Bram Moolenaar05159a02005-02-26 23:04:13 +000023326#if defined(FEAT_PROFILE) || defined(PROTO)
23327/*
23328 * Called when starting to read a function line.
23329 * "sourcing_lnum" must be correct!
23330 * When skipping lines it may not actually be executed, but we won't find out
23331 * until later and we need to store the time now.
23332 */
23333 void
23334func_line_start(cookie)
23335 void *cookie;
23336{
23337 funccall_T *fcp = (funccall_T *)cookie;
23338 ufunc_T *fp = fcp->func;
23339
23340 if (fp->uf_profiling && sourcing_lnum >= 1
23341 && sourcing_lnum <= fp->uf_lines.ga_len)
23342 {
23343 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023344 /* Skip continuation lines. */
23345 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23346 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023347 fp->uf_tml_execed = FALSE;
23348 profile_start(&fp->uf_tml_start);
23349 profile_zero(&fp->uf_tml_children);
23350 profile_get_wait(&fp->uf_tml_wait);
23351 }
23352}
23353
23354/*
23355 * Called when actually executing a function line.
23356 */
23357 void
23358func_line_exec(cookie)
23359 void *cookie;
23360{
23361 funccall_T *fcp = (funccall_T *)cookie;
23362 ufunc_T *fp = fcp->func;
23363
23364 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23365 fp->uf_tml_execed = TRUE;
23366}
23367
23368/*
23369 * Called when done with a function line.
23370 */
23371 void
23372func_line_end(cookie)
23373 void *cookie;
23374{
23375 funccall_T *fcp = (funccall_T *)cookie;
23376 ufunc_T *fp = fcp->func;
23377
23378 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23379 {
23380 if (fp->uf_tml_execed)
23381 {
23382 ++fp->uf_tml_count[fp->uf_tml_idx];
23383 profile_end(&fp->uf_tml_start);
23384 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023385 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023386 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23387 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023388 }
23389 fp->uf_tml_idx = -1;
23390 }
23391}
23392#endif
23393
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394/*
23395 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023396 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 */
23398 int
23399func_has_ended(cookie)
23400 void *cookie;
23401{
Bram Moolenaar33570922005-01-25 22:26:29 +000023402 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403
23404 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23405 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023406 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 || fcp->returned);
23408}
23409
23410/*
23411 * return TRUE if cookie indicates a function which "abort"s on errors.
23412 */
23413 int
23414func_has_abort(cookie)
23415 void *cookie;
23416{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023417 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418}
23419
23420#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23421typedef enum
23422{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023423 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23424 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23425 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426} var_flavour_T;
23427
23428static var_flavour_T var_flavour __ARGS((char_u *varname));
23429
23430 static var_flavour_T
23431var_flavour(varname)
23432 char_u *varname;
23433{
23434 char_u *p = varname;
23435
23436 if (ASCII_ISUPPER(*p))
23437 {
23438 while (*(++p))
23439 if (ASCII_ISLOWER(*p))
23440 return VAR_FLAVOUR_SESSION;
23441 return VAR_FLAVOUR_VIMINFO;
23442 }
23443 else
23444 return VAR_FLAVOUR_DEFAULT;
23445}
23446#endif
23447
23448#if defined(FEAT_VIMINFO) || defined(PROTO)
23449/*
23450 * Restore global vars that start with a capital from the viminfo file
23451 */
23452 int
23453read_viminfo_varlist(virp, writing)
23454 vir_T *virp;
23455 int writing;
23456{
23457 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023458 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023459 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460
23461 if (!writing && (find_viminfo_parameter('!') != NULL))
23462 {
23463 tab = vim_strchr(virp->vir_line + 1, '\t');
23464 if (tab != NULL)
23465 {
23466 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023467 switch (*tab)
23468 {
23469 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023470#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023471 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023472#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023473 case 'D': type = VAR_DICT; break;
23474 case 'L': type = VAR_LIST; break;
23475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476
23477 tab = vim_strchr(tab, '\t');
23478 if (tab != NULL)
23479 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023480 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023481 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023482 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023484#ifdef FEAT_FLOAT
23485 else if (type == VAR_FLOAT)
23486 (void)string2float(tab + 1, &tv.vval.v_float);
23487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023489 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023490 if (type == VAR_DICT || type == VAR_LIST)
23491 {
23492 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23493
23494 if (etv == NULL)
23495 /* Failed to parse back the dict or list, use it as a
23496 * string. */
23497 tv.v_type = VAR_STRING;
23498 else
23499 {
23500 vim_free(tv.vval.v_string);
23501 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023502 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023503 }
23504 }
23505
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023506 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023507
23508 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023509 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023510 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23511 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 }
23513 }
23514 }
23515
23516 return viminfo_readline(virp);
23517}
23518
23519/*
23520 * Write global vars that start with a capital to the viminfo file
23521 */
23522 void
23523write_viminfo_varlist(fp)
23524 FILE *fp;
23525{
Bram Moolenaar33570922005-01-25 22:26:29 +000023526 hashitem_T *hi;
23527 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023528 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023529 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023530 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023531 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023532 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533
23534 if (find_viminfo_parameter('!') == NULL)
23535 return;
23536
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023537 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023538
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023539 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023540 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023542 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023544 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023545 this_var = HI2DI(hi);
23546 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023547 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023548 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023549 {
23550 case VAR_STRING: s = "STR"; break;
23551 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023552#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023553 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023554#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023555 case VAR_DICT: s = "DIC"; break;
23556 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023557 default: continue;
23558 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023559 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023560 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023561 if (p != NULL)
23562 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023563 vim_free(tofree);
23564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 }
23566 }
23567}
23568#endif
23569
23570#if defined(FEAT_SESSION) || defined(PROTO)
23571 int
23572store_session_globals(fd)
23573 FILE *fd;
23574{
Bram Moolenaar33570922005-01-25 22:26:29 +000023575 hashitem_T *hi;
23576 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023577 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 char_u *p, *t;
23579
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023580 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023581 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023585 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023586 this_var = HI2DI(hi);
23587 if ((this_var->di_tv.v_type == VAR_NUMBER
23588 || this_var->di_tv.v_type == VAR_STRING)
23589 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023590 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023591 /* Escape special characters with a backslash. Turn a LF and
23592 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023593 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023594 (char_u *)"\\\"\n\r");
23595 if (p == NULL) /* out of memory */
23596 break;
23597 for (t = p; *t != NUL; ++t)
23598 if (*t == '\n')
23599 *t = 'n';
23600 else if (*t == '\r')
23601 *t = 'r';
23602 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023603 this_var->di_key,
23604 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23605 : ' ',
23606 p,
23607 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23608 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023609 || put_eol(fd) == FAIL)
23610 {
23611 vim_free(p);
23612 return FAIL;
23613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 vim_free(p);
23615 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023616#ifdef FEAT_FLOAT
23617 else if (this_var->di_tv.v_type == VAR_FLOAT
23618 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23619 {
23620 float_T f = this_var->di_tv.vval.v_float;
23621 int sign = ' ';
23622
23623 if (f < 0)
23624 {
23625 f = -f;
23626 sign = '-';
23627 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023628 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023629 this_var->di_key, sign, f) < 0)
23630 || put_eol(fd) == FAIL)
23631 return FAIL;
23632 }
23633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 }
23635 }
23636 return OK;
23637}
23638#endif
23639
Bram Moolenaar661b1822005-07-28 22:36:45 +000023640/*
23641 * Display script name where an item was last set.
23642 * Should only be invoked when 'verbose' is non-zero.
23643 */
23644 void
23645last_set_msg(scriptID)
23646 scid_T scriptID;
23647{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023648 char_u *p;
23649
Bram Moolenaar661b1822005-07-28 22:36:45 +000023650 if (scriptID != 0)
23651 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023652 p = home_replace_save(NULL, get_scriptname(scriptID));
23653 if (p != NULL)
23654 {
23655 verbose_enter();
23656 MSG_PUTS(_("\n\tLast set from "));
23657 MSG_PUTS(p);
23658 vim_free(p);
23659 verbose_leave();
23660 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023661 }
23662}
23663
Bram Moolenaard812df62008-11-09 12:46:09 +000023664/*
23665 * List v:oldfiles in a nice way.
23666 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023667 void
23668ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023669 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023670{
23671 list_T *l = vimvars[VV_OLDFILES].vv_list;
23672 listitem_T *li;
23673 int nr = 0;
23674
23675 if (l == NULL)
23676 msg((char_u *)_("No old files"));
23677 else
23678 {
23679 msg_start();
23680 msg_scroll = TRUE;
23681 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23682 {
23683 msg_outnum((long)++nr);
23684 MSG_PUTS(": ");
23685 msg_outtrans(get_tv_string(&li->li_tv));
23686 msg_putchar('\n');
23687 out_flush(); /* output one line at a time */
23688 ui_breakcheck();
23689 }
23690 /* Assume "got_int" was set to truncate the listing. */
23691 got_int = FALSE;
23692
23693#ifdef FEAT_BROWSE_CMD
23694 if (cmdmod.browse)
23695 {
23696 quit_more = FALSE;
23697 nr = prompt_for_number(FALSE);
23698 msg_starthere();
23699 if (nr > 0)
23700 {
23701 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23702 (long)nr);
23703
23704 if (p != NULL)
23705 {
23706 p = expand_env_save(p);
23707 eap->arg = p;
23708 eap->cmdidx = CMD_edit;
23709 cmdmod.browse = FALSE;
23710 do_exedit(eap, NULL);
23711 vim_free(p);
23712 }
23713 }
23714 }
23715#endif
23716 }
23717}
23718
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719#endif /* FEAT_EVAL */
23720
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023722#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723
23724#ifdef WIN3264
23725/*
23726 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23727 */
23728static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23729static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23730static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23731
23732/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023733 * Get the short path (8.3) for the filename in "fnamep".
23734 * Only works for a valid file name.
23735 * When the path gets longer "fnamep" is changed and the allocated buffer
23736 * is put in "bufp".
23737 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23738 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 */
23740 static int
23741get_short_pathname(fnamep, bufp, fnamelen)
23742 char_u **fnamep;
23743 char_u **bufp;
23744 int *fnamelen;
23745{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023746 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 char_u *newbuf;
23748
23749 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750 l = GetShortPathName(*fnamep, *fnamep, len);
23751 if (l > len - 1)
23752 {
23753 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023754 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 newbuf = vim_strnsave(*fnamep, l);
23756 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758
23759 vim_free(*bufp);
23760 *fnamep = *bufp = newbuf;
23761
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023762 /* Really should always succeed, as the buffer is big enough. */
23763 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 }
23765
23766 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023767 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768}
23769
23770/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023771 * Get the short path (8.3) for the filename in "fname". The converted
23772 * path is returned in "bufp".
23773 *
23774 * Some of the directories specified in "fname" may not exist. This function
23775 * will shorten the existing directories at the beginning of the path and then
23776 * append the remaining non-existing path.
23777 *
23778 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023779 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023780 * bufp - Pointer to an allocated buffer for the filename.
23781 * fnamelen - Length of the filename pointed to by fname
23782 *
23783 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 */
23785 static int
23786shortpath_for_invalid_fname(fname, bufp, fnamelen)
23787 char_u **fname;
23788 char_u **bufp;
23789 int *fnamelen;
23790{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023791 char_u *short_fname, *save_fname, *pbuf_unused;
23792 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023794 int old_len, len;
23795 int new_len, sfx_len;
23796 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797
23798 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023799 old_len = *fnamelen;
23800 save_fname = vim_strnsave(*fname, old_len);
23801 pbuf_unused = NULL;
23802 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023804 endp = save_fname + old_len - 1; /* Find the end of the copy */
23805 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023807 /*
23808 * Try shortening the supplied path till it succeeds by removing one
23809 * directory at a time from the tail of the path.
23810 */
23811 len = 0;
23812 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023814 /* go back one path-separator */
23815 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23816 --endp;
23817 if (endp <= save_fname)
23818 break; /* processed the complete path */
23819
23820 /*
23821 * Replace the path separator with a NUL and try to shorten the
23822 * resulting path.
23823 */
23824 ch = *endp;
23825 *endp = 0;
23826 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023827 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023828 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23829 {
23830 retval = FAIL;
23831 goto theend;
23832 }
23833 *endp = ch; /* preserve the string */
23834
23835 if (len > 0)
23836 break; /* successfully shortened the path */
23837
23838 /* failed to shorten the path. Skip the path separator */
23839 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 }
23841
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023842 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023844 /*
23845 * Succeeded in shortening the path. Now concatenate the shortened
23846 * path with the remaining path at the tail.
23847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023849 /* Compute the length of the new path. */
23850 sfx_len = (int)(save_endp - endp) + 1;
23851 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023853 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023855 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023857 /* There is not enough space in the currently allocated string,
23858 * copy it to a buffer big enough. */
23859 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023861 {
23862 retval = FAIL;
23863 goto theend;
23864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 }
23866 else
23867 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023868 /* Transfer short_fname to the main buffer (it's big enough),
23869 * unless get_short_pathname() did its work in-place. */
23870 *fname = *bufp = save_fname;
23871 if (short_fname != save_fname)
23872 vim_strncpy(save_fname, short_fname, len);
23873 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023875
23876 /* concat the not-shortened part of the path */
23877 vim_strncpy(*fname + len, endp, sfx_len);
23878 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023880
23881theend:
23882 vim_free(pbuf_unused);
23883 vim_free(save_fname);
23884
23885 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886}
23887
23888/*
23889 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023890 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 */
23892 static int
23893shortpath_for_partial(fnamep, bufp, fnamelen)
23894 char_u **fnamep;
23895 char_u **bufp;
23896 int *fnamelen;
23897{
23898 int sepcount, len, tflen;
23899 char_u *p;
23900 char_u *pbuf, *tfname;
23901 int hasTilde;
23902
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023903 /* Count up the path separators from the RHS.. so we know which part
23904 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023906 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 if (vim_ispathsep(*p))
23908 ++sepcount;
23909
23910 /* Need full path first (use expand_env() to remove a "~/") */
23911 hasTilde = (**fnamep == '~');
23912 if (hasTilde)
23913 pbuf = tfname = expand_env_save(*fnamep);
23914 else
23915 pbuf = tfname = FullName_save(*fnamep, FALSE);
23916
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023917 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023919 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23920 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921
23922 if (len == 0)
23923 {
23924 /* Don't have a valid filename, so shorten the rest of the
23925 * path if we can. This CAN give us invalid 8.3 filenames, but
23926 * there's not a lot of point in guessing what it might be.
23927 */
23928 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023929 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23930 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 }
23932
23933 /* Count the paths backward to find the beginning of the desired string. */
23934 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023935 {
23936#ifdef FEAT_MBYTE
23937 if (has_mbyte)
23938 p -= mb_head_off(tfname, p);
23939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 if (vim_ispathsep(*p))
23941 {
23942 if (sepcount == 0 || (hasTilde && sepcount == 1))
23943 break;
23944 else
23945 sepcount --;
23946 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 if (hasTilde)
23949 {
23950 --p;
23951 if (p >= tfname)
23952 *p = '~';
23953 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023954 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 }
23956 else
23957 ++p;
23958
23959 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23960 vim_free(*bufp);
23961 *fnamelen = (int)STRLEN(p);
23962 *bufp = pbuf;
23963 *fnamep = p;
23964
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023965 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966}
23967#endif /* WIN3264 */
23968
23969/*
23970 * Adjust a filename, according to a string of modifiers.
23971 * *fnamep must be NUL terminated when called. When returning, the length is
23972 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023973 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 * When there is an error, *fnamep is set to NULL.
23975 */
23976 int
23977modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23978 char_u *src; /* string with modifiers */
23979 int *usedlen; /* characters after src that are used */
23980 char_u **fnamep; /* file name so far */
23981 char_u **bufp; /* buffer for allocated file name or NULL */
23982 int *fnamelen; /* length of fnamep */
23983{
23984 int valid = 0;
23985 char_u *tail;
23986 char_u *s, *p, *pbuf;
23987 char_u dirname[MAXPATHL];
23988 int c;
23989 int has_fullname = 0;
23990#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023991 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 int has_shortname = 0;
23993#endif
23994
23995repeat:
23996 /* ":p" - full path/file_name */
23997 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23998 {
23999 has_fullname = 1;
24000
24001 valid |= VALID_PATH;
24002 *usedlen += 2;
24003
24004 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24005 if ((*fnamep)[0] == '~'
24006#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24007 && ((*fnamep)[1] == '/'
24008# ifdef BACKSLASH_IN_FILENAME
24009 || (*fnamep)[1] == '\\'
24010# endif
24011 || (*fnamep)[1] == NUL)
24012
24013#endif
24014 )
24015 {
24016 *fnamep = expand_env_save(*fnamep);
24017 vim_free(*bufp); /* free any allocated file name */
24018 *bufp = *fnamep;
24019 if (*fnamep == NULL)
24020 return -1;
24021 }
24022
24023 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024024 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 {
24026 if (vim_ispathsep(*p)
24027 && p[1] == '.'
24028 && (p[2] == NUL
24029 || vim_ispathsep(p[2])
24030 || (p[2] == '.'
24031 && (p[3] == NUL || vim_ispathsep(p[3])))))
24032 break;
24033 }
24034
24035 /* FullName_save() is slow, don't use it when not needed. */
24036 if (*p != NUL || !vim_isAbsName(*fnamep))
24037 {
24038 *fnamep = FullName_save(*fnamep, *p != NUL);
24039 vim_free(*bufp); /* free any allocated file name */
24040 *bufp = *fnamep;
24041 if (*fnamep == NULL)
24042 return -1;
24043 }
24044
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024045#ifdef WIN3264
24046# if _WIN32_WINNT >= 0x0500
24047 if (vim_strchr(*fnamep, '~') != NULL)
24048 {
24049 /* Expand 8.3 filename to full path. Needed to make sure the same
24050 * file does not have two different names.
24051 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24052 p = alloc(_MAX_PATH + 1);
24053 if (p != NULL)
24054 {
24055 if (GetLongPathName(*fnamep, p, MAXPATHL))
24056 {
24057 vim_free(*bufp);
24058 *bufp = *fnamep = p;
24059 }
24060 else
24061 vim_free(p);
24062 }
24063 }
24064# endif
24065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024066 /* Append a path separator to a directory. */
24067 if (mch_isdir(*fnamep))
24068 {
24069 /* Make room for one or two extra characters. */
24070 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24071 vim_free(*bufp); /* free any allocated file name */
24072 *bufp = *fnamep;
24073 if (*fnamep == NULL)
24074 return -1;
24075 add_pathsep(*fnamep);
24076 }
24077 }
24078
24079 /* ":." - path relative to the current directory */
24080 /* ":~" - path relative to the home directory */
24081 /* ":8" - shortname path - postponed till after */
24082 while (src[*usedlen] == ':'
24083 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24084 {
24085 *usedlen += 2;
24086 if (c == '8')
24087 {
24088#ifdef WIN3264
24089 has_shortname = 1; /* Postpone this. */
24090#endif
24091 continue;
24092 }
24093 pbuf = NULL;
24094 /* Need full path first (use expand_env() to remove a "~/") */
24095 if (!has_fullname)
24096 {
24097 if (c == '.' && **fnamep == '~')
24098 p = pbuf = expand_env_save(*fnamep);
24099 else
24100 p = pbuf = FullName_save(*fnamep, FALSE);
24101 }
24102 else
24103 p = *fnamep;
24104
24105 has_fullname = 0;
24106
24107 if (p != NULL)
24108 {
24109 if (c == '.')
24110 {
24111 mch_dirname(dirname, MAXPATHL);
24112 s = shorten_fname(p, dirname);
24113 if (s != NULL)
24114 {
24115 *fnamep = s;
24116 if (pbuf != NULL)
24117 {
24118 vim_free(*bufp); /* free any allocated file name */
24119 *bufp = pbuf;
24120 pbuf = NULL;
24121 }
24122 }
24123 }
24124 else
24125 {
24126 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24127 /* Only replace it when it starts with '~' */
24128 if (*dirname == '~')
24129 {
24130 s = vim_strsave(dirname);
24131 if (s != NULL)
24132 {
24133 *fnamep = s;
24134 vim_free(*bufp);
24135 *bufp = s;
24136 }
24137 }
24138 }
24139 vim_free(pbuf);
24140 }
24141 }
24142
24143 tail = gettail(*fnamep);
24144 *fnamelen = (int)STRLEN(*fnamep);
24145
24146 /* ":h" - head, remove "/file_name", can be repeated */
24147 /* Don't remove the first "/" or "c:\" */
24148 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24149 {
24150 valid |= VALID_HEAD;
24151 *usedlen += 2;
24152 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024153 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024154 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155 *fnamelen = (int)(tail - *fnamep);
24156#ifdef VMS
24157 if (*fnamelen > 0)
24158 *fnamelen += 1; /* the path separator is part of the path */
24159#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024160 if (*fnamelen == 0)
24161 {
24162 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24163 p = vim_strsave((char_u *)".");
24164 if (p == NULL)
24165 return -1;
24166 vim_free(*bufp);
24167 *bufp = *fnamep = tail = p;
24168 *fnamelen = 1;
24169 }
24170 else
24171 {
24172 while (tail > s && !after_pathsep(s, tail))
24173 mb_ptr_back(*fnamep, tail);
24174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 }
24176
24177 /* ":8" - shortname */
24178 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24179 {
24180 *usedlen += 2;
24181#ifdef WIN3264
24182 has_shortname = 1;
24183#endif
24184 }
24185
24186#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024187 /*
24188 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 */
24190 if (has_shortname)
24191 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024192 /* Copy the string if it is shortened by :h and when it wasn't copied
24193 * yet, because we are going to change it in place. Avoids changing
24194 * the buffer name for "%:8". */
24195 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 {
24197 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024198 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 return -1;
24200 vim_free(*bufp);
24201 *bufp = *fnamep = p;
24202 }
24203
24204 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024205 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 if (!has_fullname && !vim_isAbsName(*fnamep))
24207 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024208 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 return -1;
24210 }
24211 else
24212 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024213 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214
Bram Moolenaardc935552011-08-17 15:23:23 +020024215 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024217 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 return -1;
24219
24220 if (l == 0)
24221 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024222 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024224 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 return -1;
24226 }
24227 *fnamelen = l;
24228 }
24229 }
24230#endif /* WIN3264 */
24231
24232 /* ":t" - tail, just the basename */
24233 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24234 {
24235 *usedlen += 2;
24236 *fnamelen -= (int)(tail - *fnamep);
24237 *fnamep = tail;
24238 }
24239
24240 /* ":e" - extension, can be repeated */
24241 /* ":r" - root, without extension, can be repeated */
24242 while (src[*usedlen] == ':'
24243 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24244 {
24245 /* find a '.' in the tail:
24246 * - for second :e: before the current fname
24247 * - otherwise: The last '.'
24248 */
24249 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24250 s = *fnamep - 2;
24251 else
24252 s = *fnamep + *fnamelen - 1;
24253 for ( ; s > tail; --s)
24254 if (s[0] == '.')
24255 break;
24256 if (src[*usedlen + 1] == 'e') /* :e */
24257 {
24258 if (s > tail)
24259 {
24260 *fnamelen += (int)(*fnamep - (s + 1));
24261 *fnamep = s + 1;
24262#ifdef VMS
24263 /* cut version from the extension */
24264 s = *fnamep + *fnamelen - 1;
24265 for ( ; s > *fnamep; --s)
24266 if (s[0] == ';')
24267 break;
24268 if (s > *fnamep)
24269 *fnamelen = s - *fnamep;
24270#endif
24271 }
24272 else if (*fnamep <= tail)
24273 *fnamelen = 0;
24274 }
24275 else /* :r */
24276 {
24277 if (s > tail) /* remove one extension */
24278 *fnamelen = (int)(s - *fnamep);
24279 }
24280 *usedlen += 2;
24281 }
24282
24283 /* ":s?pat?foo?" - substitute */
24284 /* ":gs?pat?foo?" - global substitute */
24285 if (src[*usedlen] == ':'
24286 && (src[*usedlen + 1] == 's'
24287 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24288 {
24289 char_u *str;
24290 char_u *pat;
24291 char_u *sub;
24292 int sep;
24293 char_u *flags;
24294 int didit = FALSE;
24295
24296 flags = (char_u *)"";
24297 s = src + *usedlen + 2;
24298 if (src[*usedlen + 1] == 'g')
24299 {
24300 flags = (char_u *)"g";
24301 ++s;
24302 }
24303
24304 sep = *s++;
24305 if (sep)
24306 {
24307 /* find end of pattern */
24308 p = vim_strchr(s, sep);
24309 if (p != NULL)
24310 {
24311 pat = vim_strnsave(s, (int)(p - s));
24312 if (pat != NULL)
24313 {
24314 s = p + 1;
24315 /* find end of substitution */
24316 p = vim_strchr(s, sep);
24317 if (p != NULL)
24318 {
24319 sub = vim_strnsave(s, (int)(p - s));
24320 str = vim_strnsave(*fnamep, *fnamelen);
24321 if (sub != NULL && str != NULL)
24322 {
24323 *usedlen = (int)(p + 1 - src);
24324 s = do_string_sub(str, pat, sub, flags);
24325 if (s != NULL)
24326 {
24327 *fnamep = s;
24328 *fnamelen = (int)STRLEN(s);
24329 vim_free(*bufp);
24330 *bufp = s;
24331 didit = TRUE;
24332 }
24333 }
24334 vim_free(sub);
24335 vim_free(str);
24336 }
24337 vim_free(pat);
24338 }
24339 }
24340 /* after using ":s", repeat all the modifiers */
24341 if (didit)
24342 goto repeat;
24343 }
24344 }
24345
Bram Moolenaar26df0922014-02-23 23:39:13 +010024346 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24347 {
24348 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24349 if (p == NULL)
24350 return -1;
24351 vim_free(*bufp);
24352 *bufp = *fnamep = p;
24353 *fnamelen = (int)STRLEN(p);
24354 *usedlen += 2;
24355 }
24356
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 return valid;
24358}
24359
24360/*
24361 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24362 * "flags" can be "g" to do a global substitute.
24363 * Returns an allocated string, NULL for error.
24364 */
24365 char_u *
24366do_string_sub(str, pat, sub, flags)
24367 char_u *str;
24368 char_u *pat;
24369 char_u *sub;
24370 char_u *flags;
24371{
24372 int sublen;
24373 regmatch_T regmatch;
24374 int i;
24375 int do_all;
24376 char_u *tail;
24377 garray_T ga;
24378 char_u *ret;
24379 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024380 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381
24382 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24383 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024384 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024385
24386 ga_init2(&ga, 1, 200);
24387
24388 do_all = (flags[0] == 'g');
24389
24390 regmatch.rm_ic = p_ic;
24391 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24392 if (regmatch.regprog != NULL)
24393 {
24394 tail = str;
24395 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24396 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024397 /* Skip empty match except for first match. */
24398 if (regmatch.startp[0] == regmatch.endp[0])
24399 {
24400 if (zero_width == regmatch.startp[0])
24401 {
24402 /* avoid getting stuck on a match with an empty string */
24403 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24404 ++ga.ga_len;
24405 continue;
24406 }
24407 zero_width = regmatch.startp[0];
24408 }
24409
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 /*
24411 * Get some space for a temporary buffer to do the substitution
24412 * into. It will contain:
24413 * - The text up to where the match is.
24414 * - The substituted text.
24415 * - The text after the match.
24416 */
24417 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24418 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24419 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24420 {
24421 ga_clear(&ga);
24422 break;
24423 }
24424
24425 /* copy the text up to where the match is */
24426 i = (int)(regmatch.startp[0] - tail);
24427 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24428 /* add the substituted text */
24429 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24430 + ga.ga_len + i, TRUE, TRUE, FALSE);
24431 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024432 tail = regmatch.endp[0];
24433 if (*tail == NUL)
24434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435 if (!do_all)
24436 break;
24437 }
24438
24439 if (ga.ga_data != NULL)
24440 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24441
Bram Moolenaar473de612013-06-08 18:19:48 +020024442 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 }
24444
24445 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24446 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024447 if (p_cpo == empty_option)
24448 p_cpo = save_cpo;
24449 else
24450 /* Darn, evaluating {sub} expression changed the value. */
24451 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024452
24453 return ret;
24454}
24455
24456#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */