blob: 452014f75e365395d39ab7357527339915fe6f5d [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 Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000367};
368
369/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000370#define vv_type vv_di.di_tv.v_type
371#define vv_nr vv_di.di_tv.vval.v_number
372#define vv_float vv_di.di_tv.vval.v_float
373#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000374#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000376
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200377static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000378#define vimvarht vimvardict.dv_hashtab
379
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
381static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000382static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
383static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
384static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000385static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
386static void list_glob_vars __ARGS((int *first));
387static void list_buf_vars __ARGS((int *first));
388static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_vim_vars __ARGS((int *first));
393static void list_script_vars __ARGS((int *first));
394static void list_func_vars __ARGS((int *first));
395static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
397static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100398static 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 +0000399static void clear_lval __ARGS((lval_T *lp));
400static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
401static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
403static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
404static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
405static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
406static void item_lock __ARGS((typval_T *tv, int deep, int lock));
407static int tv_islocked __ARGS((typval_T *tv));
408
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
410static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000415static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
416static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000417
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000418static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000423static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100425static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
426static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
427static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000428static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000430static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000431static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
432static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000433static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100435static 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 +0000436static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000437static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200438static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
440static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
446static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000448#ifdef FEAT_FLOAT
449static int string2float __ARGS((char_u *text, float_T *value));
450#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
452static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100453static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static 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 +0200455static 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 +0000456static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000457static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#ifdef FEAT_FLOAT
460static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100464static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200468static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200471static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100484static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100486static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
489static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
490#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000491static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000494static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000496#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000497static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000498static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200505static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000506#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
510static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200520static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200522#ifdef FEAT_FLOAT
523static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
524#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000527static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#ifdef FEAT_FLOAT
534static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000538static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000547static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000549static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000555static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200556static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000564static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000565static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200566static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000567static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000568static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200571static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100578static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000581static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000595static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100600static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200615static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
617#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200618#ifdef FEAT_LUA
619static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200626static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100643static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000644static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000649static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000650static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200651#ifdef FEAT_PYTHON3
652static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
654#ifdef FEAT_PYTHON
655static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200674static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100676static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000679static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000681static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000688static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000689static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000690static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000691static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200693static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000694static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100696#ifdef FEAT_CRYPT
697static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
698#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000699static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200700static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200704static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000707static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000715static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717#ifdef HAVE_STRFTIME
718static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
720static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200726static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000733static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200734static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200736static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000738static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000740static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000741static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000743static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200744#ifdef FEAT_FLOAT
745static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
752static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200755static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200756static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100757static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100761static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000768static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100772static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773
Bram Moolenaar493c1782014-05-28 14:34:46 +0200774static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000775static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int get_env_len __ARGS((char_u **arg));
777static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
780#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
781#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
782 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000783static 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 +0000784static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000785static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200786static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000787static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static typval_T *alloc_tv __ARGS((void));
789static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void init_tv __ARGS((typval_T *varp));
791static long get_tv_number __ARGS((typval_T *varp));
792static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000793static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *get_tv_string __ARGS((typval_T *varp));
795static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100797static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
798static 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 +0000799static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
800static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
801static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000802static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
803static 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 +0000804static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200805static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
806static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200807static int var_check_func_name __ARGS((char_u *name, int new_var));
808static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200809static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000810static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
812static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
813static int eval_fname_script __ARGS((char_u *p));
814static int eval_fname_sid __ARGS((char_u *p));
815static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static ufunc_T *find_func __ARGS((char_u *name));
817static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200818static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
820static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000821static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
822static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
823static int
824# ifdef __BORLANDC__
825 _RTLENTRYF
826# endif
827 prof_total_cmp __ARGS((const void *s1, const void *s2));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200834static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838static 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 +0000839static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
840static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000841static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000842static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
843static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000844static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000846static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200847static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200848static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200850
851#ifdef EBCDIC
852static int compare_func_name __ARGS((const void *s1, const void *s2));
853static void sortFunctions __ARGS(());
854#endif
855
Bram Moolenaar33570922005-01-25 22:26:29 +0000856/*
857 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 */
859 void
860eval_init()
861{
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 int i;
863 struct vimvar *p;
864
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
866 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200867 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870
871 for (i = 0; i < VV_LEN; ++i)
872 {
873 p = &vimvars[i];
874 STRCPY(p->vv_di.di_key, p->vv_name);
875 if (p->vv_flags & VV_RO)
876 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
877 else if (p->vv_flags & VV_RO_SBX)
878 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
879 else
880 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000881
882 /* add to v: scope dict, unless the value is not always available */
883 if (p->vv_type != VAR_UNKNOWN)
884 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 /* add to compat scope dict */
887 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000889 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100890 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200891 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
894 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100895 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896 */
897 sortFunctions();
898#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000899}
900
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901#if defined(EXITFREE) || defined(PROTO)
902 void
903eval_clear()
904{
905 int i;
906 struct vimvar *p;
907
908 for (i = 0; i < VV_LEN; ++i)
909 {
910 p = &vimvars[i];
911 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000913 vim_free(p->vv_str);
914 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000915 }
916 else if (p->vv_di.di_tv.v_type == VAR_LIST)
917 {
918 list_unref(p->vv_list);
919 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921 }
922 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000923 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 hash_clear(&compat_hashtab);
925
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200928 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100929# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930
931 /* global variables */
932 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000934 /* autoloaded script names */
935 ga_clear_strings(&ga_loaded);
936
Bram Moolenaarcca74132013-09-25 21:00:28 +0200937 /* Script-local variables. First clear all the variables and in a second
938 * loop free the scriptvar_T, because a variable in one script might hold
939 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200942 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 ga_clear(&ga_scripts);
945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000946 /* unreferenced lists and dicts */
947 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000948
949 /* functions */
950 free_all_functions();
951 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952}
953#endif
954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Return the name of the executed function.
957 */
958 char_u *
959func_name(cookie)
960 void *cookie;
961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the next breakpoint line for a funccall cookie.
967 */
968 linenr_T *
969func_breakpoint(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the address holding the debug tick for a funccall cookie.
977 */
978 int *
979func_dbg_tick(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Return the nesting level for a funccall cookie.
987 */
988 int
989func_level(cookie)
990 void *cookie;
991{
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993}
994
995/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000996funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000998/* pointer to list of previously used funccal, still around because some
999 * item in it is still being used. */
1000funccall_T *previous_funccal = NULL;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Return TRUE when a function was ended by a ":return" command.
1004 */
1005 int
1006current_func_returned()
1007{
1008 return current_funccal->returned;
1009}
1010
1011
1012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * Set an internal variable to a string value. Creates the variable if it does
1014 * not already exist.
1015 */
1016 void
1017set_internal_string_var(name, value)
1018 char_u *name;
1019 char_u *value;
1020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 val = vim_strsave(value);
1025 if (val != NULL)
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 }
1034}
1035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001037static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038static char_u *redir_endp = NULL;
1039static char_u *redir_varname = NULL;
1040
1041/*
1042 * Start recording command output to a variable
1043 * Returns OK if successfully completed the setup. FAIL otherwise.
1044 */
1045 int
1046var_redir_start(name, append)
1047 char_u *name;
1048 int append; /* append to an existing variable */
1049{
1050 int save_emsg;
1051 int err;
1052 typval_T tv;
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 {
1057 EMSG(_(e_invarg));
1058 return FAIL;
1059 }
1060
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 redir_varname = vim_strsave(name);
1063 if (redir_varname == NULL)
1064 return FAIL;
1065
1066 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1067 if (redir_lval == NULL)
1068 {
1069 var_redir_stop();
1070 return FAIL;
1071 }
1072
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 /* The output is stored in growarray "redir_ga" until redirection ends. */
1074 ga_init2(&redir_ga, (int)sizeof(char), 500);
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001077 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001078 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1080 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp != NULL && *redir_endp != NUL)
1083 /* Trailing characters are present after the variable name */
1084 EMSG(_(e_trailing));
1085 else
1086 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
1091
1092 /* check if we can write to the variable: set it to or append an empty
1093 * string */
1094 save_emsg = did_emsg;
1095 did_emsg = FALSE;
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = (char_u *)"";
1098 if (append)
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1100 else
1101 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001104 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (err)
1106 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 var_redir_stop();
1109 return FAIL;
1110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 * Append "value[value_len]" to the variable set by var_redir_start().
1117 * The actual appending is postponed until redirection ends, because the value
1118 * appended may in fact be the string we write to, changing it may cause freed
1119 * memory to be used:
1120 * :redir => foo
1121 * :let foo
1122 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 */
1124 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130
1131 if (redir_lval == NULL)
1132 return;
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001139 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 {
1141 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 }
1144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146}
1147
1148/*
1149 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 */
1152 void
1153var_redir_stop()
1154{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 typval_T tv;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_lval != NULL)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* If there was no error: assign the text to the variable. */
1160 if (redir_endp != NULL)
1161 {
1162 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1163 tv.v_type = VAR_STRING;
1164 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 /* Call get_lval() again, if it's inside a Dict or List it may
1166 * have changed. */
1167 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001168 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001169 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1170 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1171 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* free the collected output */
1175 vim_free(redir_ga.ga_data);
1176 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 vim_free(redir_lval);
1179 redir_lval = NULL;
1180 }
1181 vim_free(redir_varname);
1182 redir_varname = NULL;
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# if defined(FEAT_MBYTE) || defined(PROTO)
1186 int
1187eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1188 char_u *enc_from;
1189 char_u *enc_to;
1190 char_u *fname_from;
1191 char_u *fname_to;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1196 set_vim_var_string(VV_CC_TO, enc_to, -1);
1197 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1198 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1199 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_CC_FROM, NULL, -1);
1202 set_vim_var_string(VV_CC_TO, NULL, -1);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205
1206 if (err)
1207 return FAIL;
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1213 int
1214eval_printexpr(fname, args)
1215 char_u *fname;
1216 char_u *args;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, fname, -1);
1221 set_vim_var_string(VV_CMDARG, args, -1);
1222 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1223 err = TRUE;
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_CMDARG, NULL, -1);
1226
1227 if (err)
1228 {
1229 mch_remove(fname);
1230 return FAIL;
1231 }
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_DIFF) || defined(PROTO)
1237 void
1238eval_diff(origfile, newfile, outfile)
1239 char_u *origfile;
1240 char_u *newfile;
1241 char_u *outfile;
1242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253
1254 void
1255eval_patch(origfile, difffile, outfile)
1256 char_u *origfile;
1257 char_u *difffile;
1258 char_u *outfile;
1259{
1260 int err;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270# endif
1271
1272/*
1273 * Top level evaluation function, returning a boolean.
1274 * Sets "error" to TRUE if there was an error.
1275 * Return TRUE or FALSE.
1276 */
1277 int
1278eval_to_bool(arg, error, nextcmd, skip)
1279 char_u *arg;
1280 int *error;
1281 char_u **nextcmd;
1282 int skip; /* only parse, don't execute */
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval = FALSE;
1286
1287 if (skip)
1288 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
1292 {
1293 *error = FALSE;
1294 if (!skip)
1295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 if (skip)
1301 --emsg_skip;
1302
1303 return retval;
1304}
1305
1306/*
1307 * Top level evaluation function, returning a string. If "skip" is TRUE,
1308 * only parsing to "nextcmd" is done, without reporting errors. Return
1309 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1310 */
1311 char_u *
1312eval_to_string_skip(arg, nextcmd, skip)
1313 char_u *arg;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *retval;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 retval = vim_strsave(get_tv_string(&tv));
1327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (skip)
1330 --emsg_skip;
1331
1332 return retval;
1333}
1334
1335/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336 * Skip over an expression at "*pp".
1337 * Return FAIL for an error, OK otherwise.
1338 */
1339 int
1340skip_expr(pp)
1341 char_u **pp;
1342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344
1345 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347}
1348
1349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 * When "convert" is TRUE convert a List into a sequence of lines and convert
1352 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Return pointer to allocated memory, or NULL for failure.
1354 */
1355 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *arg;
1358 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 retval = NULL;
1370 else
1371 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001373 {
1374 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001377 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001378 if (tv.vval.v_list->lv_len > 0)
1379 ga_append(&ga, NL);
1380 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 ga_append(&ga, NUL);
1382 retval = (char_u *)ga.ga_data;
1383 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384#ifdef FEAT_FLOAT
1385 else if (convert && tv.v_type == VAR_FLOAT)
1386 {
1387 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1388 retval = vim_strsave(numbuf);
1389 }
1390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 else
1392 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 return retval;
1397}
1398
1399/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 * Call eval_to_string() without using current local variables and using
1401 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *arg;
1406 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *retval;
1410 void *save_funccalp;
1411
1412 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 if (use_sandbox)
1414 ++sandbox;
1415 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 if (use_sandbox)
1418 --sandbox;
1419 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 restore_funccal(save_funccalp);
1421 return retval;
1422}
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Top level evaluation function, returning a number.
1426 * Evaluates "expr" silently.
1427 * Returns -1 for an error.
1428 */
1429 int
1430eval_to_number(expr)
1431 char_u *expr;
1432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001435 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 ++emsg_off;
1438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 retval = -1;
1441 else
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 --emsg_off;
1447
1448 return retval;
1449}
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451/*
1452 * Prepare v: variable "idx" to be used.
1453 * Save the current typeval in "save_tv".
1454 * When not used yet add the variable to the v: hashtable.
1455 */
1456 static void
1457prepare_vimvar(idx, save_tv)
1458 int idx;
1459 typval_T *save_tv;
1460{
1461 *save_tv = vimvars[idx].vv_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1464}
1465
1466/*
1467 * Restore v: variable "idx" to typeval "save_tv".
1468 * When no longer defined, remove the variable from the v: hashtable.
1469 */
1470 static void
1471restore_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 hashitem_T *hi;
1476
Bram Moolenaara40058a2005-07-11 22:42:07 +00001477 vimvars[idx].vv_tv = *save_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 {
1480 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1481 if (HASHITEM_EMPTY(hi))
1482 EMSG2(_(e_intern2), "restore_vimvar()");
1483 else
1484 hash_remove(&vimvarht, hi);
1485 }
1486}
1487
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001488#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001489/*
1490 * Evaluate an expression to a list with suggestions.
1491 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001492 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 */
1494 list_T *
1495eval_spell_expr(badword, expr)
1496 char_u *badword;
1497 char_u *expr;
1498{
1499 typval_T save_val;
1500 typval_T rettv;
1501 list_T *list = NULL;
1502 char_u *p = skipwhite(expr);
1503
1504 /* Set "v:val" to the bad word. */
1505 prepare_vimvar(VV_VAL, &save_val);
1506 vimvars[VV_VAL].vv_type = VAR_STRING;
1507 vimvars[VV_VAL].vv_str = badword;
1508 if (p_verbose == 0)
1509 ++emsg_off;
1510
1511 if (eval1(&p, &rettv, TRUE) == OK)
1512 {
1513 if (rettv.v_type != VAR_LIST)
1514 clear_tv(&rettv);
1515 else
1516 list = rettv.vval.v_list;
1517 }
1518
1519 if (p_verbose == 0)
1520 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521 restore_vimvar(VV_VAL, &save_val);
1522
1523 return list;
1524}
1525
1526/*
1527 * "list" is supposed to contain two items: a word and a number. Return the
1528 * word in "pp" and the number as the return value.
1529 * Return -1 if anything isn't right.
1530 * Used to get the good word and score from the eval_spell_expr() result.
1531 */
1532 int
1533get_spellword(list, pp)
1534 list_T *list;
1535 char_u **pp;
1536{
1537 listitem_T *li;
1538
1539 li = list->lv_first;
1540 if (li == NULL)
1541 return -1;
1542 *pp = get_tv_string(&li->li_tv);
1543
1544 li = li->li_next;
1545 if (li == NULL)
1546 return -1;
1547 return get_tv_number(&li->li_tv);
1548}
1549#endif
1550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 * Top level evaluation function.
1553 * Returns an allocated typval_T with the result.
1554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 */
1556 typval_T *
1557eval_expr(arg, nextcmd)
1558 char_u *arg;
1559 char_u **nextcmd;
1560{
1561 typval_T *tv;
1562
1563 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565 {
1566 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 }
1569
1570 return tv;
1571}
1572
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 * Uses argv[argc] for the function arguments. Only Number and String
1577 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001580 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001581call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 char_u *func;
1583 int argc;
1584 char_u **argv;
1585 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long n;
1591 int len;
1592 int i;
1593 int doesrange;
1594 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001597 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 for (i = 0; i < argc; i++)
1602 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001603 /* Pass a NULL or empty argument as an empty string */
1604 if (argv[i] == NULL || *argv[i] == NUL)
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001608 continue;
1609 }
1610
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001611 if (str_arg_only)
1612 len = 0;
1613 else
1614 /* Recognize a number argument, the others must be strings. */
1615 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (len != 0 && len == (int)STRLEN(argv[i]))
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_NUMBER;
1619 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627
1628 if (safe)
1629 {
1630 save_funccalp = save_funccal();
1631 ++sandbox;
1632 }
1633
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1635 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (safe)
1639 {
1640 --sandbox;
1641 restore_funccal(save_funccalp);
1642 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 vim_free(argvars);
1644
1645 if (ret == FAIL)
1646 clear_tv(rettv);
1647
1648 return ret;
1649}
1650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001651/*
1652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 /* All arguments are passed as strings, no conversion to number. */
1667 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1668 return -1;
1669
1670 retval = get_tv_number_chk(&rettv, NULL);
1671 clear_tv(&rettv);
1672 return retval;
1673}
1674
1675#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1676 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1677
Bram Moolenaar4f688582007-07-24 12:34:30 +00001678# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001680 * Call vimL function "func" and return the result as a string.
1681 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 * Uses argv[argc] for the function arguments.
1683 */
1684 void *
1685call_func_retstr(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 return NULL;
1697
1698 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001707 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 */
1709 void *
1710call_func_retlist(func, argc, argv, safe)
1711 char_u *func;
1712 int argc;
1713 char_u **argv;
1714 int safe; /* use the sandbox */
1715{
1716 typval_T rettv;
1717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 if (rettv.v_type != VAR_LIST)
1723 {
1724 clear_tv(&rettv);
1725 return NULL;
1726 }
1727
1728 return rettv.vval.v_list;
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
1733 * Save the current function call pointer, and set it to NULL.
1734 * Used when executing autocommands and for ":source".
1735 */
1736 void *
1737save_funccal()
1738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 current_funccal = NULL;
1742 return (void *)fc;
1743}
1744
1745 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746restore_funccal(vfc)
1747 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749 funccall_T *fc = (funccall_T *)vfc;
1750
1751 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754#if defined(FEAT_PROFILE) || defined(PROTO)
1755/*
1756 * Prepare profiling for entering a child or something else that is not
1757 * counted for the script/function itself.
1758 * Should always be called in pair with prof_child_exit().
1759 */
1760 void
1761prof_child_enter(tm)
1762 proftime_T *tm; /* place to store waittime */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 profile_start(&fc->prof_child);
1768 script_prof_save(tm);
1769}
1770
1771/*
1772 * Take care of time spent in a child.
1773 * Should always be called after prof_child_enter().
1774 */
1775 void
1776prof_child_exit(tm)
1777 proftime_T *tm; /* where waittime was stored */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 {
1783 profile_end(&fc->prof_child);
1784 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1785 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1786 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1787 }
1788 script_prof_restore(tm);
1789}
1790#endif
1791
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_FOLDING
1794/*
1795 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1796 * it in "*cp". Doesn't give error messages.
1797 */
1798 int
1799eval_foldexpr(arg, cp)
1800 char_u *arg;
1801 int *cp;
1802{
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int retval;
1805 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001806 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1807 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 ++sandbox;
1812 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 if (tv.v_type == VAR_NUMBER)
1820 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001821 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 retval = 0;
1823 else
1824 {
1825 /* If the result is a string, check if there is a non-digit before
1826 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!VIM_ISDIGIT(*s) && *s != '-')
1829 *cp = *s++;
1830 retval = atol((char *)s);
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 --sandbox;
1837 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let" list all variable values
1845 * ":let var1 var2" list variable values
1846 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 * ":let var += expr" assignment command.
1848 * ":let var -= expr" assignment command.
1849 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 void
1853ex_let(eap)
1854 exarg_T *eap;
1855{
1856 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 int var_count = 0;
1861 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 argend = skip_var_list(arg, &var_count, &semicolon);
1867 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001869 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1870 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001871 expr = skipwhite(argend);
1872 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1873 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001875 /*
1876 * ":let" without "=": list variables
1877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 if (*arg == '[')
1879 EMSG(_(e_invarg));
1880 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_glob_vars(&first);
1887 list_buf_vars(&first);
1888 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001891#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_script_vars(&first);
1893 list_func_vars(&first);
1894 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 eap->nextcmd = check_nextcmd(arg);
1897 }
1898 else
1899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op[0] = '=';
1901 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1905 op[0] = *expr; /* +=, -= or .= */
1906 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 else
1909 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (eap->skip)
1912 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 {
1916 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 --emsg_skip;
1919 }
1920 else if (i != FAIL)
1921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927}
1928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929/*
1930 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1931 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 * When "nextchars" is not NULL it points to a string with characters that
1933 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1934 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Returns OK or FAIL;
1936 */
1937 static int
1938ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1939 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int copy; /* copy values from "tv", don't move */
1942 int semicolon; /* from skip_var_list() */
1943 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
1946 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 listitem_T *item;
1950 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
1952 if (*arg != '[')
1953 {
1954 /*
1955 * ":let var = expr" or ":for var in list"
1956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 return OK;
1960 }
1961
1962 /*
1963 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1964 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001965 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
1967 EMSG(_(e_listreq));
1968 return FAIL;
1969 }
1970
1971 i = list_len(l);
1972 if (semicolon == 0 && var_count < i)
1973 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001974 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 }
1977 if (var_count - semicolon > i)
1978 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001979 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 return FAIL;
1981 }
1982
1983 item = l->lv_first;
1984 while (*arg != ']')
1985 {
1986 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 item = item->li_next;
1989 if (arg == NULL)
1990 return FAIL;
1991
1992 arg = skipwhite(arg);
1993 if (*arg == ';')
1994 {
1995 /* Put the rest of the list (may be empty) in the var after ';'.
1996 * Create a new list for this. */
1997 l = list_alloc();
1998 if (l == NULL)
1999 return FAIL;
2000 while (item != NULL)
2001 {
2002 list_append_tv(l, &item->li_tv);
2003 item = item->li_next;
2004 }
2005
2006 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002007 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 ltv.vval.v_list = l;
2009 l->lv_refcount = 1;
2010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2012 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 clear_tv(&ltv);
2014 if (arg == NULL)
2015 return FAIL;
2016 break;
2017 }
2018 else if (*arg != ',' && *arg != ']')
2019 {
2020 EMSG2(_(e_intern2), "ex_let_vars()");
2021 return FAIL;
2022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Skip over assignable variable "var" or list of variables "[var, var]".
2030 * Used for ":let varvar = expr" and ":for varvar in expr".
2031 * For "[var, var]" increment "*var_count" for each variable.
2032 * for "[var, var; var]" set "semicolon".
2033 * Return NULL for an error.
2034 */
2035 static char_u *
2036skip_var_list(arg, var_count, semicolon)
2037 char_u *arg;
2038 int *var_count;
2039 int *semicolon;
2040{
2041 char_u *p, *s;
2042
2043 if (*arg == '[')
2044 {
2045 /* "[var, var]": find the matching ']'. */
2046 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002047 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 {
2049 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2050 s = skip_var_one(p);
2051 if (s == p)
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 ++*var_count;
2057
2058 p = skipwhite(s);
2059 if (*p == ']')
2060 break;
2061 else if (*p == ';')
2062 {
2063 if (*semicolon == 1)
2064 {
2065 EMSG(_("Double ; in list of variables"));
2066 return NULL;
2067 }
2068 *semicolon = 1;
2069 }
2070 else if (*p != ',')
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 }
2076 return p + 1;
2077 }
2078 else
2079 return skip_var_one(arg);
2080}
2081
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 static char_u *
2087skip_var_one(arg)
2088 char_u *arg;
2089{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 if (*arg == '@' && arg[1] != NUL)
2091 return arg + 2;
2092 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2093 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094}
2095
Bram Moolenaara7043832005-01-21 11:56:39 +00002096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 * List variables for hashtab "ht" with prefix "prefix".
2098 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 hashitem_T *hi;
2108 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 int todo;
2110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
2116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 di = HI2DI(hi);
2118 if (empty || di->di_tv.v_type != VAR_STRING
2119 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 }
2123}
2124
2125/*
2126 * List global variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_glob_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List buffer variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_buf_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 char_u numbuf[NUMBUFLEN];
2143
Bram Moolenaar429fa852013-04-15 12:27:36 +02002144 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2149 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List window variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_win_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#ifdef FEAT_WINDOWS
2164/*
2165 * List tab page variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_tab_vars(first)
2169 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173}
2174#endif
2175
Bram Moolenaara7043832005-01-21 11:56:39 +00002176/*
2177 * List Vim variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_vim_vars(first)
2181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184}
2185
2186/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187 * List script-local variables, if there is a script.
2188 */
2189 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_script_vars(first)
2191 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192{
2193 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2195 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196}
2197
2198/*
2199 * List function variables, if there is a function.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_func_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_funccal != NULL)
2206 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * List variables in "arg".
2212 */
2213 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 exarg_T *eap;
2216 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002260 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
2519check_changedtick(arg)
2520 char_u *arg;
2521{
2522 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2523 {
2524 EMSG2(_(e_readonlyvar), arg);
2525 return TRUE;
2526 }
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Get an lval: variable, Dict item or List item that can be assigned a value
2532 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2533 * "name.key", "name.key[expr]" etc.
2534 * Indexing only works if "name" is an existing List or Dictionary.
2535 * "name" points to the start of the name.
2536 * If "rettv" is not NULL it points to the value to be assigned.
2537 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2538 * wrong; must end in space or cmd separator.
2539 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540 * flags:
2541 * GLV_QUIET: do not give error messages
2542 * GLV_NO_AUTOLOAD: do not use script autoloading
2543 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002545 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 */
2548 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
2552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int unlet;
2554 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 char_u *expr_start, *expr_end;
2560 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 dictitem_T *v;
2562 typval_T var1;
2563 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (skip)
2575 {
2576 /* When skipping just find the end of the name. */
2577 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002578 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
2580
2581 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (expr_start != NULL)
2584 {
2585 /* Don't expand the name when we already know there is an error. */
2586 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2587 && *p != '[' && *p != '.')
2588 {
2589 EMSG(_(e_trailing));
2590 return NULL;
2591 }
2592
2593 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2594 if (lp->ll_exp_name == NULL)
2595 {
2596 /* Report an invalid expression in braces, unless the
2597 * expression evaluation has been cancelled due to an
2598 * aborting error, an interrupt, or an exception. */
2599 if (!aborting() && !quiet)
2600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002601 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 EMSG2(_(e_invarg2), name);
2603 return NULL;
2604 }
2605 }
2606 lp->ll_name = lp->ll_exp_name;
2607 }
2608 else
2609 lp->ll_name = name;
2610
2611 /* Without [idx] or .key we are done. */
2612 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2613 return p;
2614
2615 cc = *p;
2616 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (v == NULL && !quiet)
2619 EMSG2(_(e_undefvar), lp->ll_name);
2620 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 if (v == NULL)
2622 return NULL;
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /*
2625 * Loop until no more [idx] or .key is following.
2626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2631 && !(lp->ll_tv->v_type == VAR_DICT
2632 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E689: Can only index a List or Dictionary"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E708: [:] must come last"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 len = -1;
2646 if (*p == '.')
2647 {
2648 key = p + 1;
2649 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2650 ;
2651 if (len == 0)
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_(e_emptykey));
2655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = key + len;
2658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (*p == ':')
2664 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 empty1 = FALSE;
2668 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var1) == NULL)
2671 {
2672 /* not a number or string */
2673 clear_tv(&var1);
2674 return NULL;
2675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677
2678 /* Optionally get the second index [ :expr]. */
2679 if (*p == ':')
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (rettv != NULL && (rettv->v_type != VAR_LIST
2690 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = skipwhite(p + 1);
2699 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (get_tv_string_chk(&var2) == NULL)
2711 {
2712 /* not a number or string */
2713 if (!empty1)
2714 clear_tv(&var1);
2715 clear_tv(&var2);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734
2735 /* Skip to past ']'. */
2736 ++p;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
2741 if (len == -1)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*key == NUL)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_list = NULL;
2754 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 /* When assigning to a scope dictionary check that a function and
2758 * variable name is valid (only variable name unless it is l: or
2759 * g: dictionary). Disallow overwriting a builtin function. */
2760 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 int prevval;
2763 int wrong;
2764
2765 if (len != -1)
2766 {
2767 prevval = key[len];
2768 key[len] = NUL;
2769 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002770 else
2771 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2773 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 || !valid_varname(key);
2776 if (len != -1)
2777 key[len] = prevval;
2778 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 return NULL;
2780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 /* Can't add "v:" variable. */
2785 if (lp->ll_dict == &vimvardict)
2786 {
2787 EMSG2(_(e_illvar), name);
2788 return NULL;
2789 }
2790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002791 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 p = NULL;
2808 break;
2809 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002811 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002812 return NULL;
2813
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 if (len == -1)
2815 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818 else
2819 {
2820 /*
2821 * Get the number and item for the only or first index of the List.
2822 */
2823 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
2826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var1);
2829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_list = lp->ll_tv->vval.v_list;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (lp->ll_n1 < 0)
2836 {
2837 lp->ll_n1 = 0;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 }
2840 }
2841 if (lp->ll_li == NULL)
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849
2850 /*
2851 * May need to find the item or absolute index for the second
2852 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 * When no index given: "lp->ll_empty2" is TRUE.
2854 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 {
2865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2873 if (lp->ll_n1 < 0)
2874 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2875 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return p;
2888}
2889
2890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 */
2893 static void
2894clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 vim_free(lp->ll_exp_name);
2898 vim_free(lp->ll_newkey);
2899}
2900
2901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 listitem_T *ri;
2916 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917
2918 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 cc = *endp;
2923 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002929 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002930 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002931 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002933 if ((di == NULL
2934 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2935 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2936 FALSE)))
2937 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 set_var(lp->ll_name, &tv, FALSE);
2939 clear_tv(&tv);
2940 }
2941 }
2942 else
2943 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002947 else if (tv_check_lock(lp->ll_newkey == NULL
2948 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002949 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002950 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 else if (lp->ll_range)
2952 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002953 listitem_T *ll_li = lp->ll_li;
2954 int ll_n1 = lp->ll_n1;
2955
2956 /*
2957 * Check whether any of the list items is locked
2958 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002959 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002960 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002961 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002962 return;
2963 ri = ri->li_next;
2964 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2965 break;
2966 ll_li = ll_li->li_next;
2967 ++ll_n1;
2968 }
2969
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 /*
2971 * Assign the List values to the list items.
2972 */
2973 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002974 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002975 if (op != NULL && *op != '=')
2976 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2977 else
2978 {
2979 clear_tv(&lp->ll_li->li_tv);
2980 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2981 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 ri = ri->li_next;
2983 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2984 break;
2985 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002988 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 ri = NULL;
2991 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002992 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 lp->ll_li = lp->ll_li->li_next;
2995 ++lp->ll_n1;
2996 }
2997 if (ri != NULL)
2998 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else if (lp->ll_empty2
3000 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 : lp->ll_n1 != lp->ll_n2)
3002 EMSG(_("E711: List value has not enough items"));
3003 }
3004 else
3005 {
3006 /*
3007 * Assign to a List or Dictionary item.
3008 */
3009 if (lp->ll_newkey != NULL)
3010 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 if (op != NULL && *op != '=')
3012 {
3013 EMSG2(_(e_letwrong), op);
3014 return;
3015 }
3016
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003018 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 if (di == NULL)
3020 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003021 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3022 {
3023 vim_free(di);
3024 return;
3025 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003027 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 else if (op != NULL && *op != '=')
3029 {
3030 tv_op(lp->ll_tv, rettv, op);
3031 return;
3032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003033 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 /*
3037 * Assign the value to the variable or list item.
3038 */
3039 if (copy)
3040 copy_tv(rettv, lp->ll_tv);
3041 else
3042 {
3043 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003044 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003046 }
3047 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048}
3049
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003051 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3052 * Returns OK or FAIL.
3053 */
3054 static int
3055tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003056 typval_T *tv1;
3057 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 char_u *op;
3059{
3060 long n;
3061 char_u numbuf[NUMBUFLEN];
3062 char_u *s;
3063
3064 /* Can't do anything with a Funcref or a Dict on the right. */
3065 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3066 {
3067 switch (tv1->v_type)
3068 {
3069 case VAR_DICT:
3070 case VAR_FUNC:
3071 break;
3072
3073 case VAR_LIST:
3074 if (*op != '+' || tv2->v_type != VAR_LIST)
3075 break;
3076 /* List += List */
3077 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3078 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3079 return OK;
3080
3081 case VAR_NUMBER:
3082 case VAR_STRING:
3083 if (tv2->v_type == VAR_LIST)
3084 break;
3085 if (*op == '+' || *op == '-')
3086 {
3087 /* nr += nr or nr -= nr*/
3088 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089#ifdef FEAT_FLOAT
3090 if (tv2->v_type == VAR_FLOAT)
3091 {
3092 float_T f = n;
3093
3094 if (*op == '+')
3095 f += tv2->vval.v_float;
3096 else
3097 f -= tv2->vval.v_float;
3098 clear_tv(tv1);
3099 tv1->v_type = VAR_FLOAT;
3100 tv1->vval.v_float = f;
3101 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103#endif
3104 {
3105 if (*op == '+')
3106 n += get_tv_number(tv2);
3107 else
3108 n -= get_tv_number(tv2);
3109 clear_tv(tv1);
3110 tv1->v_type = VAR_NUMBER;
3111 tv1->vval.v_number = n;
3112 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 }
3114 else
3115 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116 if (tv2->v_type == VAR_FLOAT)
3117 break;
3118
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 /* str .= str */
3120 s = get_tv_string(tv1);
3121 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_STRING;
3124 tv1->vval.v_string = s;
3125 }
3126 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127
3128#ifdef FEAT_FLOAT
3129 case VAR_FLOAT:
3130 {
3131 float_T f;
3132
3133 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3134 && tv2->v_type != VAR_NUMBER
3135 && tv2->v_type != VAR_STRING))
3136 break;
3137 if (tv2->v_type == VAR_FLOAT)
3138 f = tv2->vval.v_float;
3139 else
3140 f = get_tv_number(tv2);
3141 if (*op == '+')
3142 tv1->vval.v_float += f;
3143 else
3144 tv1->vval.v_float -= f;
3145 }
3146 return OK;
3147#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003148 }
3149 }
3150
3151 EMSG2(_(e_letwrong), op);
3152 return FAIL;
3153}
3154
3155/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003156 * Add a watcher to a list.
3157 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003158 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003159list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003160 list_T *l;
3161 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162{
3163 lw->lw_next = l->lv_watch;
3164 l->lv_watch = lw;
3165}
3166
3167/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003168 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 * No warning when it isn't found...
3170 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003171 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003173 list_T *l;
3174 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175{
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177
3178 lwp = &l->lv_watch;
3179 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3180 {
3181 if (lw == lwrem)
3182 {
3183 *lwp = lw->lw_next;
3184 break;
3185 }
3186 lwp = &lw->lw_next;
3187 }
3188}
3189
3190/*
3191 * Just before removing an item from a list: advance watchers to the next
3192 * item.
3193 */
3194 static void
3195list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 list_T *l;
3197 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198{
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200
3201 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3202 if (lw->lw_item == item)
3203 lw->lw_item = item->li_next;
3204}
3205
3206/*
3207 * Evaluate the expression used in a ":for var in expr" command.
3208 * "arg" points to "var".
3209 * Set "*errp" to TRUE for an error, FALSE otherwise;
3210 * Return a pointer that holds the info. Null when there is an error.
3211 */
3212 void *
3213eval_for_line(arg, errp, nextcmdp, skip)
3214 char_u *arg;
3215 int *errp;
3216 char_u **nextcmdp;
3217 int skip;
3218{
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 typval_T tv;
3222 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223
3224 *errp = TRUE; /* default: there is an error */
3225
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 if (fi == NULL)
3228 return NULL;
3229
3230 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3231 if (expr == NULL)
3232 return fi;
3233
3234 expr = skipwhite(expr);
3235 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3236 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003237 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238 return fi;
3239 }
3240
3241 if (skip)
3242 ++emsg_skip;
3243 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3244 {
3245 *errp = FALSE;
3246 if (!skip)
3247 {
3248 l = tv.vval.v_list;
3249 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003250 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003252 clear_tv(&tv);
3253 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 else
3255 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003256 /* No need to increment the refcount, it's already set for the
3257 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 fi->fi_list = l;
3259 list_add_watch(l, &fi->fi_lw);
3260 fi->fi_lw.lw_item = l->lv_first;
3261 }
3262 }
3263 }
3264 if (skip)
3265 --emsg_skip;
3266
3267 return fi;
3268}
3269
3270/*
3271 * Use the first item in a ":for" list. Advance to the next.
3272 * Assign the values to the variable (list). "arg" points to the first one.
3273 * Return TRUE when a valid item was found, FALSE when at end of list or
3274 * something wrong.
3275 */
3276 int
3277next_for_item(fi_void, arg)
3278 void *fi_void;
3279 char_u *arg;
3280{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003281 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284
3285 item = fi->fi_lw.lw_item;
3286 if (item == NULL)
3287 result = FALSE;
3288 else
3289 {
3290 fi->fi_lw.lw_item = item->li_next;
3291 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3292 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3293 }
3294 return result;
3295}
3296
3297/*
3298 * Free the structure used to store info used by ":for".
3299 */
3300 void
3301free_for_info(fi_void)
3302 void *fi_void;
3303{
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003306 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003309 list_unref(fi->fi_list);
3310 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 vim_free(fi);
3312}
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3315
3316 void
3317set_context_for_expression(xp, arg, cmdidx)
3318 expand_T *xp;
3319 char_u *arg;
3320 cmdidx_T cmdidx;
3321{
3322 int got_eq = FALSE;
3323 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 if (cmdidx == CMD_let)
3327 {
3328 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003329 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 {
3331 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003332 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 {
3334 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003335 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 if (vim_iswhite(*p))
3337 break;
3338 }
3339 return;
3340 }
3341 }
3342 else
3343 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3344 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 while ((xp->xp_pattern = vim_strpbrk(arg,
3346 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3347 {
3348 c = *xp->xp_pattern;
3349 if (c == '&')
3350 {
3351 c = xp->xp_pattern[1];
3352 if (c == '&')
3353 {
3354 ++xp->xp_pattern;
3355 xp->xp_context = cmdidx != CMD_let || got_eq
3356 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3357 }
3358 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003361 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3362 xp->xp_pattern += 2;
3363
3364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366 else if (c == '$')
3367 {
3368 /* environment variable */
3369 xp->xp_context = EXPAND_ENV_VARS;
3370 }
3371 else if (c == '=')
3372 {
3373 got_eq = TRUE;
3374 xp->xp_context = EXPAND_EXPRESSION;
3375 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003376 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 && xp->xp_context == EXPAND_FUNCTIONS
3378 && vim_strchr(xp->xp_pattern, '(') == NULL)
3379 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003380 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 break;
3382 }
3383 else if (cmdidx != CMD_let || got_eq)
3384 {
3385 if (c == '"') /* string */
3386 {
3387 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3388 if (c == '\\' && xp->xp_pattern[1] != NUL)
3389 ++xp->xp_pattern;
3390 xp->xp_context = EXPAND_NOTHING;
3391 }
3392 else if (c == '\'') /* literal string */
3393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003394 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3396 /* skip */ ;
3397 xp->xp_context = EXPAND_NOTHING;
3398 }
3399 else if (c == '|')
3400 {
3401 if (xp->xp_pattern[1] == '|')
3402 {
3403 ++xp->xp_pattern;
3404 xp->xp_context = EXPAND_EXPRESSION;
3405 }
3406 else
3407 xp->xp_context = EXPAND_COMMANDS;
3408 }
3409 else
3410 xp->xp_context = EXPAND_EXPRESSION;
3411 }
3412 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003413 /* Doesn't look like something valid, expand as an expression
3414 * anyway. */
3415 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 arg = xp->xp_pattern;
3417 if (*arg != NUL)
3418 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3419 /* skip */ ;
3420 }
3421 xp->xp_pattern = arg;
3422}
3423
3424#endif /* FEAT_CMDL_COMPL */
3425
3426/*
3427 * ":1,25call func(arg1, arg2)" function call.
3428 */
3429 void
3430ex_call(eap)
3431 exarg_T *eap;
3432{
3433 char_u *arg = eap->arg;
3434 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003436 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003438 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 linenr_T lnum;
3440 int doesrange;
3441 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003442 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003444 if (eap->skip)
3445 {
3446 /* trans_function_name() doesn't work well when skipping, use eval0()
3447 * instead to skip to any following command, e.g. for:
3448 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003449 ++emsg_skip;
3450 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3451 clear_tv(&rettv);
3452 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003453 return;
3454 }
3455
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003456 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003457 if (fudi.fd_newkey != NULL)
3458 {
3459 /* Still need to give an error message for missing key. */
3460 EMSG2(_(e_dictkey), fudi.fd_newkey);
3461 vim_free(fudi.fd_newkey);
3462 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003463 if (tofree == NULL)
3464 return;
3465
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003466 /* Increase refcount on dictionary, it could get deleted when evaluating
3467 * the arguments. */
3468 if (fudi.fd_dict != NULL)
3469 ++fudi.fd_dict->dv_refcount;
3470
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003471 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003473 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
Bram Moolenaar532c7802005-01-27 14:44:31 +00003475 /* Skip white space to allow ":call func ()". Not good, but required for
3476 * backward compatibility. */
3477 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
3480 if (*startarg != '(')
3481 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003482 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 goto end;
3484 }
3485
3486 /*
3487 * When skipping, evaluate the function once, to find the end of the
3488 * arguments.
3489 * When the function takes a range, this is discovered after the first
3490 * call, and the loop is broken.
3491 */
3492 if (eap->skip)
3493 {
3494 ++emsg_skip;
3495 lnum = eap->line2; /* do it once, also with an invalid range */
3496 }
3497 else
3498 lnum = eap->line1;
3499 for ( ; lnum <= eap->line2; ++lnum)
3500 {
3501 if (!eap->skip && eap->addr_count > 0)
3502 {
3503 curwin->w_cursor.lnum = lnum;
3504 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003505#ifdef FEAT_VIRTUALEDIT
3506 curwin->w_cursor.coladd = 0;
3507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003510 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003511 eap->line1, eap->line2, &doesrange,
3512 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
3514 failed = TRUE;
3515 break;
3516 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003517
3518 /* Handle a function returning a Funcref, Dictionary or List. */
3519 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3520 {
3521 failed = TRUE;
3522 break;
3523 }
3524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003525 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (doesrange || eap->skip)
3527 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003530 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003532 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (aborting())
3534 break;
3535 }
3536 if (eap->skip)
3537 --emsg_skip;
3538
3539 if (!failed)
3540 {
3541 /* Check for trailing illegal characters and a following command. */
3542 if (!ends_excmd(*arg))
3543 {
3544 emsg_severe = TRUE;
3545 EMSG(_(e_trailing));
3546 }
3547 else
3548 eap->nextcmd = check_nextcmd(arg);
3549 }
3550
3551end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003552 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003553 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554}
3555
3556/*
3557 * ":unlet[!] var1 ... " command.
3558 */
3559 void
3560ex_unlet(eap)
3561 exarg_T *eap;
3562{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003563 ex_unletlock(eap, eap->arg, 0);
3564}
3565
3566/*
3567 * ":lockvar" and ":unlockvar" commands
3568 */
3569 void
3570ex_lockvar(eap)
3571 exarg_T *eap;
3572{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003574 int deep = 2;
3575
3576 if (eap->forceit)
3577 deep = -1;
3578 else if (vim_isdigit(*arg))
3579 {
3580 deep = getdigits(&arg);
3581 arg = skipwhite(arg);
3582 }
3583
3584 ex_unletlock(eap, arg, deep);
3585}
3586
3587/*
3588 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3589 */
3590 static void
3591ex_unletlock(eap, argstart, deep)
3592 exarg_T *eap;
3593 char_u *argstart;
3594 int deep;
3595{
3596 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601 do
3602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003604 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003605 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (lv.ll_name == NULL)
3607 error = TRUE; /* error but continue parsing */
3608 if (name_end == NULL || (!vim_iswhite(*name_end)
3609 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 if (name_end != NULL)
3612 {
3613 emsg_severe = TRUE;
3614 EMSG(_(e_trailing));
3615 }
3616 if (!(eap->skip || error))
3617 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 break;
3619 }
3620
3621 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 {
3623 if (eap->cmdidx == CMD_unlet)
3624 {
3625 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3626 error = TRUE;
3627 }
3628 else
3629 {
3630 if (do_lock_var(&lv, name_end, deep,
3631 eap->cmdidx == CMD_lockvar) == FAIL)
3632 error = TRUE;
3633 }
3634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 if (!eap->skip)
3637 clear_lval(&lv);
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 arg = skipwhite(name_end);
3640 } while (!ends_excmd(*arg));
3641
3642 eap->nextcmd = check_nextcmd(arg);
3643}
3644
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 int forceit;
3650{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 int ret = OK;
3652 int cc;
3653
3654 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 cc = *name_end;
3657 *name_end = NUL;
3658
3659 /* Normal name or expanded name. */
3660 if (check_changedtick(lp->ll_name))
3661 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003666 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003667 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003668 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003669 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 else if (lp->ll_range)
3672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003674 listitem_T *ll_li = lp->ll_li;
3675 int ll_n1 = lp->ll_n1;
3676
3677 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3678 {
3679 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003680 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003681 return FAIL;
3682 ll_li = li;
3683 ++ll_n1;
3684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685
3686 /* Delete a range of List items. */
3687 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3688 {
3689 li = lp->ll_li->li_next;
3690 listitem_remove(lp->ll_list, lp->ll_li);
3691 lp->ll_li = li;
3692 ++lp->ll_n1;
3693 }
3694 }
3695 else
3696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 /* unlet a List item. */
3699 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003702 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 }
3704
3705 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003706}
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708/*
3709 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003710 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 */
3712 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003713do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003715 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 hashtab_T *ht;
3718 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003719 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003720 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003721 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 ht = find_var_ht(name, &varname);
3724 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003726 if (ht == &globvarht)
3727 d = &globvardict;
3728 else if (current_funccal != NULL
3729 && ht == &current_funccal->l_vars.dv_hashtab)
3730 d = &current_funccal->l_vars;
3731 else
3732 {
3733 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3734 d = di->di_tv.vval.v_dict;
3735 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 hi = hash_find(ht, varname);
3737 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003738 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003739 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003740 if (var_check_fixed(di->di_flags, name, FALSE)
3741 || var_check_ro(di->di_flags, name, FALSE)
3742 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003743 return FAIL;
3744 delete_var(ht, hi);
3745 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003748 if (forceit)
3749 return OK;
3750 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 return FAIL;
3752}
3753
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003754/*
3755 * Lock or unlock variable indicated by "lp".
3756 * "deep" is the levels to go (-1 for unlimited);
3757 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3758 */
3759 static int
3760do_lock_var(lp, name_end, deep, lock)
3761 lval_T *lp;
3762 char_u *name_end;
3763 int deep;
3764 int lock;
3765{
3766 int ret = OK;
3767 int cc;
3768 dictitem_T *di;
3769
3770 if (deep == 0) /* nothing to do */
3771 return OK;
3772
3773 if (lp->ll_tv == NULL)
3774 {
3775 cc = *name_end;
3776 *name_end = NUL;
3777
3778 /* Normal name or expanded name. */
3779 if (check_changedtick(lp->ll_name))
3780 ret = FAIL;
3781 else
3782 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003783 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003784 if (di == NULL)
3785 ret = FAIL;
3786 else
3787 {
3788 if (lock)
3789 di->di_flags |= DI_FLAGS_LOCK;
3790 else
3791 di->di_flags &= ~DI_FLAGS_LOCK;
3792 item_lock(&di->di_tv, deep, lock);
3793 }
3794 }
3795 *name_end = cc;
3796 }
3797 else if (lp->ll_range)
3798 {
3799 listitem_T *li = lp->ll_li;
3800
3801 /* (un)lock a range of List items. */
3802 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3803 {
3804 item_lock(&li->li_tv, deep, lock);
3805 li = li->li_next;
3806 ++lp->ll_n1;
3807 }
3808 }
3809 else if (lp->ll_list != NULL)
3810 /* (un)lock a List item. */
3811 item_lock(&lp->ll_li->li_tv, deep, lock);
3812 else
3813 /* un(lock) a Dictionary item. */
3814 item_lock(&lp->ll_di->di_tv, deep, lock);
3815
3816 return ret;
3817}
3818
3819/*
3820 * Lock or unlock an item. "deep" is nr of levels to go.
3821 */
3822 static void
3823item_lock(tv, deep, lock)
3824 typval_T *tv;
3825 int deep;
3826 int lock;
3827{
3828 static int recurse = 0;
3829 list_T *l;
3830 listitem_T *li;
3831 dict_T *d;
3832 hashitem_T *hi;
3833 int todo;
3834
3835 if (recurse >= DICT_MAXNEST)
3836 {
3837 EMSG(_("E743: variable nested too deep for (un)lock"));
3838 return;
3839 }
3840 if (deep == 0)
3841 return;
3842 ++recurse;
3843
3844 /* lock/unlock the item itself */
3845 if (lock)
3846 tv->v_lock |= VAR_LOCKED;
3847 else
3848 tv->v_lock &= ~VAR_LOCKED;
3849
3850 switch (tv->v_type)
3851 {
3852 case VAR_LIST:
3853 if ((l = tv->vval.v_list) != NULL)
3854 {
3855 if (lock)
3856 l->lv_lock |= VAR_LOCKED;
3857 else
3858 l->lv_lock &= ~VAR_LOCKED;
3859 if (deep < 0 || deep > 1)
3860 /* recursive: lock/unlock the items the List contains */
3861 for (li = l->lv_first; li != NULL; li = li->li_next)
3862 item_lock(&li->li_tv, deep - 1, lock);
3863 }
3864 break;
3865 case VAR_DICT:
3866 if ((d = tv->vval.v_dict) != NULL)
3867 {
3868 if (lock)
3869 d->dv_lock |= VAR_LOCKED;
3870 else
3871 d->dv_lock &= ~VAR_LOCKED;
3872 if (deep < 0 || deep > 1)
3873 {
3874 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003875 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003876 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3877 {
3878 if (!HASHITEM_EMPTY(hi))
3879 {
3880 --todo;
3881 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3882 }
3883 }
3884 }
3885 }
3886 }
3887 --recurse;
3888}
3889
Bram Moolenaara40058a2005-07-11 22:42:07 +00003890/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003891 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3892 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003893 */
3894 static int
3895tv_islocked(tv)
3896 typval_T *tv;
3897{
3898 return (tv->v_lock & VAR_LOCKED)
3899 || (tv->v_type == VAR_LIST
3900 && tv->vval.v_list != NULL
3901 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3902 || (tv->v_type == VAR_DICT
3903 && tv->vval.v_dict != NULL
3904 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3905}
3906
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3908/*
3909 * Delete all "menutrans_" variables.
3910 */
3911 void
3912del_menutrans_vars()
3913{
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003915 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003918 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003919 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003920 {
3921 if (!HASHITEM_EMPTY(hi))
3922 {
3923 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003924 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3925 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003926 }
3927 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003928 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929}
3930#endif
3931
3932#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3933
3934/*
3935 * Local string buffer for the next two functions to store a variable name
3936 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3937 * get_user_var_name().
3938 */
3939
3940static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3941
3942static char_u *varnamebuf = NULL;
3943static int varnamebuflen = 0;
3944
3945/*
3946 * Function to concatenate a prefix and a variable name.
3947 */
3948 static char_u *
3949cat_prefix_varname(prefix, name)
3950 int prefix;
3951 char_u *name;
3952{
3953 int len;
3954
3955 len = (int)STRLEN(name) + 3;
3956 if (len > varnamebuflen)
3957 {
3958 vim_free(varnamebuf);
3959 len += 10; /* some additional space */
3960 varnamebuf = alloc(len);
3961 if (varnamebuf == NULL)
3962 {
3963 varnamebuflen = 0;
3964 return NULL;
3965 }
3966 varnamebuflen = len;
3967 }
3968 *varnamebuf = prefix;
3969 varnamebuf[1] = ':';
3970 STRCPY(varnamebuf + 2, name);
3971 return varnamebuf;
3972}
3973
3974/*
3975 * Function given to ExpandGeneric() to obtain the list of user defined
3976 * (global/buffer/window/built-in) variable names.
3977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 char_u *
3979get_user_var_name(xp, idx)
3980 expand_T *xp;
3981 int idx;
3982{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003983 static long_u gdone;
3984 static long_u bdone;
3985 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003986#ifdef FEAT_WINDOWS
3987 static long_u tdone;
3988#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003989 static int vidx;
3990 static hashitem_T *hi;
3991 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003995 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003996#ifdef FEAT_WINDOWS
3997 tdone = 0;
3998#endif
3999 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004000
4001 /* Global variables */
4002 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004004 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 else
4007 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004008 while (HASHITEM_EMPTY(hi))
4009 ++hi;
4010 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4011 return cat_prefix_varname('g', hi->hi_key);
4012 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004014
4015 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004016 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004017 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004021 else
4022 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004023 while (HASHITEM_EMPTY(hi))
4024 ++hi;
4025 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004027 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 return (char_u *)"b:changedtick";
4031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
4033 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004034 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004037 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 else
4040 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004046#ifdef FEAT_WINDOWS
4047 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004048 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004049 if (tdone < ht->ht_used)
4050 {
4051 if (tdone++ == 0)
4052 hi = ht->ht_array;
4053 else
4054 ++hi;
4055 while (HASHITEM_EMPTY(hi))
4056 ++hi;
4057 return cat_prefix_varname('t', hi->hi_key);
4058 }
4059#endif
4060
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 /* v: variables */
4062 if (vidx < VV_LEN)
4063 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 vim_free(varnamebuf);
4066 varnamebuf = NULL;
4067 varnamebuflen = 0;
4068 return NULL;
4069}
4070
4071#endif /* FEAT_CMDL_COMPL */
4072
4073/*
4074 * types for expressions.
4075 */
4076typedef enum
4077{
4078 TYPE_UNKNOWN = 0
4079 , TYPE_EQUAL /* == */
4080 , TYPE_NEQUAL /* != */
4081 , TYPE_GREATER /* > */
4082 , TYPE_GEQUAL /* >= */
4083 , TYPE_SMALLER /* < */
4084 , TYPE_SEQUAL /* <= */
4085 , TYPE_MATCH /* =~ */
4086 , TYPE_NOMATCH /* !~ */
4087} exptype_T;
4088
4089/*
4090 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4093 */
4094
4095/*
4096 * Handle zero level expression.
4097 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004099 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 * Return OK or FAIL.
4101 */
4102 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 char_u **nextcmd;
4107 int evaluate;
4108{
4109 int ret;
4110 char_u *p;
4111
4112 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 if (ret == FAIL || !ends_excmd(*p))
4115 {
4116 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 /*
4119 * Report the invalid expression unless the expression evaluation has
4120 * been cancelled due to an aborting error, an interrupt, or an
4121 * exception.
4122 */
4123 if (!aborting())
4124 EMSG2(_(e_invexpr2), arg);
4125 ret = FAIL;
4126 }
4127 if (nextcmd != NULL)
4128 *nextcmd = check_nextcmd(p);
4129
4130 return ret;
4131}
4132
4133/*
4134 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004135 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 *
4137 * "arg" must point to the first non-white of the expression.
4138 * "arg" is advanced to the next non-white after the recognized expression.
4139 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004140 * Note: "rettv.v_lock" is not set.
4141 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 * Return OK or FAIL.
4143 */
4144 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int evaluate;
4149{
4150 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004151 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 if ((*arg)[0] == '?')
4160 {
4161 result = FALSE;
4162 if (evaluate)
4163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 int error = FALSE;
4165
4166 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 if (error)
4170 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172
4173 /*
4174 * Get the second variable.
4175 */
4176 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179
4180 /*
4181 * Check for the ":".
4182 */
4183 if ((*arg)[0] != ':')
4184 {
4185 EMSG(_("E109: Missing ':' after '?'"));
4186 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return FAIL;
4189 }
4190
4191 /*
4192 * Get the third variable.
4193 */
4194 *arg = skipwhite(*arg + 1);
4195 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4196 {
4197 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return FAIL;
4200 }
4201 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204
4205 return OK;
4206}
4207
4208/*
4209 * Handle first level expression:
4210 * expr2 || expr2 || expr2 logical OR
4211 *
4212 * "arg" must point to the first non-white of the expression.
4213 * "arg" is advanced to the next non-white after the recognized expression.
4214 *
4215 * Return OK or FAIL.
4216 */
4217 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int evaluate;
4222{
Bram Moolenaar33570922005-01-25 22:26:29 +00004223 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 long result;
4225 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 /*
4229 * Get the first variable.
4230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return FAIL;
4233
4234 /*
4235 * Repeat until there is no following "||".
4236 */
4237 first = TRUE;
4238 result = FALSE;
4239 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4240 {
4241 if (evaluate && first)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 first = FALSE;
4249 }
4250
4251 /*
4252 * Get the second variable.
4253 */
4254 *arg = skipwhite(*arg + 2);
4255 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4256 return FAIL;
4257
4258 /*
4259 * Compute the result.
4260 */
4261 if (evaluate && !result)
4262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (error)
4267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 if (evaluate)
4270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 rettv->v_type = VAR_NUMBER;
4272 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 }
4275
4276 return OK;
4277}
4278
4279/*
4280 * Handle second level expression:
4281 * expr3 && expr3 && expr3 logical AND
4282 *
4283 * "arg" must point to the first non-white of the expression.
4284 * "arg" is advanced to the next non-white after the recognized expression.
4285 *
4286 * Return OK or FAIL.
4287 */
4288 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 int evaluate;
4293{
Bram Moolenaar33570922005-01-25 22:26:29 +00004294 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 long result;
4296 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 /*
4300 * Get the first variable.
4301 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 return FAIL;
4304
4305 /*
4306 * Repeat until there is no following "&&".
4307 */
4308 first = TRUE;
4309 result = TRUE;
4310 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4311 {
4312 if (evaluate && first)
4313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (error)
4318 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 first = FALSE;
4320 }
4321
4322 /*
4323 * Get the second variable.
4324 */
4325 *arg = skipwhite(*arg + 2);
4326 if (eval4(arg, &var2, evaluate && result) == FAIL)
4327 return FAIL;
4328
4329 /*
4330 * Compute the result.
4331 */
4332 if (evaluate && result)
4333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (error)
4338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340 if (evaluate)
4341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 rettv->v_type = VAR_NUMBER;
4343 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 }
4346
4347 return OK;
4348}
4349
4350/*
4351 * Handle third level expression:
4352 * var1 == var2
4353 * var1 =~ var2
4354 * var1 != var2
4355 * var1 !~ var2
4356 * var1 > var2
4357 * var1 >= var2
4358 * var1 < var2
4359 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004360 * var1 is var2
4361 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 *
4363 * "arg" must point to the first non-white of the expression.
4364 * "arg" is advanced to the next non-white after the recognized expression.
4365 *
4366 * Return OK or FAIL.
4367 */
4368 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004369eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 int evaluate;
4373{
Bram Moolenaar33570922005-01-25 22:26:29 +00004374 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 char_u *p;
4376 int i;
4377 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004378 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 int len = 2;
4380 long n1, n2;
4381 char_u *s1, *s2;
4382 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4383 regmatch_T regmatch;
4384 int ic;
4385 char_u *save_cpo;
4386
4387 /*
4388 * Get the first variable.
4389 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 return FAIL;
4392
4393 p = *arg;
4394 switch (p[0])
4395 {
4396 case '=': if (p[1] == '=')
4397 type = TYPE_EQUAL;
4398 else if (p[1] == '~')
4399 type = TYPE_MATCH;
4400 break;
4401 case '!': if (p[1] == '=')
4402 type = TYPE_NEQUAL;
4403 else if (p[1] == '~')
4404 type = TYPE_NOMATCH;
4405 break;
4406 case '>': if (p[1] != '=')
4407 {
4408 type = TYPE_GREATER;
4409 len = 1;
4410 }
4411 else
4412 type = TYPE_GEQUAL;
4413 break;
4414 case '<': if (p[1] != '=')
4415 {
4416 type = TYPE_SMALLER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_SEQUAL;
4421 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004422 case 'i': if (p[1] == 's')
4423 {
4424 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4425 len = 5;
4426 if (!vim_isIDc(p[len]))
4427 {
4428 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4429 type_is = TRUE;
4430 }
4431 }
4432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434
4435 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004436 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
4438 if (type != TYPE_UNKNOWN)
4439 {
4440 /* extra question mark appended: ignore case */
4441 if (p[len] == '?')
4442 {
4443 ic = TRUE;
4444 ++len;
4445 }
4446 /* extra '#' appended: match case */
4447 else if (p[len] == '#')
4448 {
4449 ic = FALSE;
4450 ++len;
4451 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004452 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 else
4454 ic = p_ic;
4455
4456 /*
4457 * Get the second variable.
4458 */
4459 *arg = skipwhite(p + len);
4460 if (eval5(arg, &var2, evaluate) == FAIL)
4461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 return FAIL;
4464 }
4465
4466 if (evaluate)
4467 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 if (type_is && rettv->v_type != var2.v_type)
4469 {
4470 /* For "is" a different type always means FALSE, for "notis"
4471 * it means TRUE. */
4472 n1 = (type == TYPE_NEQUAL);
4473 }
4474 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4475 {
4476 if (type_is)
4477 {
4478 n1 = (rettv->v_type == var2.v_type
4479 && rettv->vval.v_list == var2.vval.v_list);
4480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 else if (rettv->v_type != var2.v_type
4484 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4485 {
4486 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004487 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004489 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004490 clear_tv(rettv);
4491 clear_tv(&var2);
4492 return FAIL;
4493 }
4494 else
4495 {
4496 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004497 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4498 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 if (type == TYPE_NEQUAL)
4500 n1 = !n1;
4501 }
4502 }
4503
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004504 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4505 {
4506 if (type_is)
4507 {
4508 n1 = (rettv->v_type == var2.v_type
4509 && rettv->vval.v_dict == var2.vval.v_dict);
4510 if (type == TYPE_NEQUAL)
4511 n1 = !n1;
4512 }
4513 else if (rettv->v_type != var2.v_type
4514 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4515 {
4516 if (rettv->v_type != var2.v_type)
4517 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4518 else
4519 EMSG(_("E736: Invalid operation for Dictionary"));
4520 clear_tv(rettv);
4521 clear_tv(&var2);
4522 return FAIL;
4523 }
4524 else
4525 {
4526 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004527 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4528 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 if (type == TYPE_NEQUAL)
4530 n1 = !n1;
4531 }
4532 }
4533
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4535 {
4536 if (rettv->v_type != var2.v_type
4537 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4538 {
4539 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004540 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004542 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 clear_tv(rettv);
4544 clear_tv(&var2);
4545 return FAIL;
4546 }
4547 else
4548 {
4549 /* Compare two Funcrefs for being equal or unequal. */
4550 if (rettv->vval.v_string == NULL
4551 || var2.vval.v_string == NULL)
4552 n1 = FALSE;
4553 else
4554 n1 = STRCMP(rettv->vval.v_string,
4555 var2.vval.v_string) == 0;
4556 if (type == TYPE_NEQUAL)
4557 n1 = !n1;
4558 }
4559 }
4560
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004561#ifdef FEAT_FLOAT
4562 /*
4563 * If one of the two variables is a float, compare as a float.
4564 * When using "=~" or "!~", always compare as string.
4565 */
4566 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4567 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4568 {
4569 float_T f1, f2;
4570
4571 if (rettv->v_type == VAR_FLOAT)
4572 f1 = rettv->vval.v_float;
4573 else
4574 f1 = get_tv_number(rettv);
4575 if (var2.v_type == VAR_FLOAT)
4576 f2 = var2.vval.v_float;
4577 else
4578 f2 = get_tv_number(&var2);
4579 n1 = FALSE;
4580 switch (type)
4581 {
4582 case TYPE_EQUAL: n1 = (f1 == f2); break;
4583 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4584 case TYPE_GREATER: n1 = (f1 > f2); break;
4585 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4586 case TYPE_SMALLER: n1 = (f1 < f2); break;
4587 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4588 case TYPE_UNKNOWN:
4589 case TYPE_MATCH:
4590 case TYPE_NOMATCH: break; /* avoid gcc warning */
4591 }
4592 }
4593#endif
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /*
4596 * If one of the two variables is a number, compare as a number.
4597 * When using "=~" or "!~", always compare as string.
4598 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004599 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 n1 = get_tv_number(rettv);
4603 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 switch (type)
4605 {
4606 case TYPE_EQUAL: n1 = (n1 == n2); break;
4607 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4608 case TYPE_GREATER: n1 = (n1 > n2); break;
4609 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4610 case TYPE_SMALLER: n1 = (n1 < n2); break;
4611 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4612 case TYPE_UNKNOWN:
4613 case TYPE_MATCH:
4614 case TYPE_NOMATCH: break; /* avoid gcc warning */
4615 }
4616 }
4617 else
4618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004619 s1 = get_tv_string_buf(rettv, buf1);
4620 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4622 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4623 else
4624 i = 0;
4625 n1 = FALSE;
4626 switch (type)
4627 {
4628 case TYPE_EQUAL: n1 = (i == 0); break;
4629 case TYPE_NEQUAL: n1 = (i != 0); break;
4630 case TYPE_GREATER: n1 = (i > 0); break;
4631 case TYPE_GEQUAL: n1 = (i >= 0); break;
4632 case TYPE_SMALLER: n1 = (i < 0); break;
4633 case TYPE_SEQUAL: n1 = (i <= 0); break;
4634
4635 case TYPE_MATCH:
4636 case TYPE_NOMATCH:
4637 /* avoid 'l' flag in 'cpoptions' */
4638 save_cpo = p_cpo;
4639 p_cpo = (char_u *)"";
4640 regmatch.regprog = vim_regcomp(s2,
4641 RE_MAGIC + RE_STRING);
4642 regmatch.rm_ic = ic;
4643 if (regmatch.regprog != NULL)
4644 {
4645 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004646 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (type == TYPE_NOMATCH)
4648 n1 = !n1;
4649 }
4650 p_cpo = save_cpo;
4651 break;
4652
4653 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4654 }
4655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004656 clear_tv(rettv);
4657 clear_tv(&var2);
4658 rettv->v_type = VAR_NUMBER;
4659 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
4661 }
4662
4663 return OK;
4664}
4665
4666/*
4667 * Handle fourth level expression:
4668 * + number addition
4669 * - number subtraction
4670 * . string concatenation
4671 *
4672 * "arg" must point to the first non-white of the expression.
4673 * "arg" is advanced to the next non-white after the recognized expression.
4674 *
4675 * Return OK or FAIL.
4676 */
4677 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 int evaluate;
4682{
Bram Moolenaar33570922005-01-25 22:26:29 +00004683 typval_T var2;
4684 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 int op;
4686 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004687#ifdef FEAT_FLOAT
4688 float_T f1 = 0, f2 = 0;
4689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 char_u *s1, *s2;
4691 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4692 char_u *p;
4693
4694 /*
4695 * Get the first variable.
4696 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004697 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 return FAIL;
4699
4700 /*
4701 * Repeat computing, until no '+', '-' or '.' is following.
4702 */
4703 for (;;)
4704 {
4705 op = **arg;
4706 if (op != '+' && op != '-' && op != '.')
4707 break;
4708
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004709 if ((op != '+' || rettv->v_type != VAR_LIST)
4710#ifdef FEAT_FLOAT
4711 && (op == '.' || rettv->v_type != VAR_FLOAT)
4712#endif
4713 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
4715 /* For "list + ...", an illegal use of the first operand as
4716 * a number cannot be determined before evaluating the 2nd
4717 * operand: if this is also a list, all is ok.
4718 * For "something . ...", "something - ..." or "non-list + ...",
4719 * we know that the first operand needs to be a string or number
4720 * without evaluating the 2nd operand. So check before to avoid
4721 * side effects after an error. */
4722 if (evaluate && get_tv_string_chk(rettv) == NULL)
4723 {
4724 clear_tv(rettv);
4725 return FAIL;
4726 }
4727 }
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 /*
4730 * Get the second variable.
4731 */
4732 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004733 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 return FAIL;
4737 }
4738
4739 if (evaluate)
4740 {
4741 /*
4742 * Compute the result.
4743 */
4744 if (op == '.')
4745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4747 s2 = get_tv_string_buf_chk(&var2, buf2);
4748 if (s2 == NULL) /* type error ? */
4749 {
4750 clear_tv(rettv);
4751 clear_tv(&var2);
4752 return FAIL;
4753 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004754 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004755 clear_tv(rettv);
4756 rettv->v_type = VAR_STRING;
4757 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004759 else if (op == '+' && rettv->v_type == VAR_LIST
4760 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004761 {
4762 /* concatenate Lists */
4763 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4764 &var3) == FAIL)
4765 {
4766 clear_tv(rettv);
4767 clear_tv(&var2);
4768 return FAIL;
4769 }
4770 clear_tv(rettv);
4771 *rettv = var3;
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 else
4774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 int error = FALSE;
4776
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777#ifdef FEAT_FLOAT
4778 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780 f1 = rettv->vval.v_float;
4781 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783 else
4784#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 n1 = get_tv_number_chk(rettv, &error);
4787 if (error)
4788 {
4789 /* This can only happen for "list + non-list". For
4790 * "non-list + ..." or "something - ...", we returned
4791 * before evaluating the 2nd operand. */
4792 clear_tv(rettv);
4793 return FAIL;
4794 }
4795#ifdef FEAT_FLOAT
4796 if (var2.v_type == VAR_FLOAT)
4797 f1 = n1;
4798#endif
4799 }
4800#ifdef FEAT_FLOAT
4801 if (var2.v_type == VAR_FLOAT)
4802 {
4803 f2 = var2.vval.v_float;
4804 n2 = 0;
4805 }
4806 else
4807#endif
4808 {
4809 n2 = get_tv_number_chk(&var2, &error);
4810 if (error)
4811 {
4812 clear_tv(rettv);
4813 clear_tv(&var2);
4814 return FAIL;
4815 }
4816#ifdef FEAT_FLOAT
4817 if (rettv->v_type == VAR_FLOAT)
4818 f2 = n2;
4819#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822
4823#ifdef FEAT_FLOAT
4824 /* If there is a float on either side the result is a float. */
4825 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4826 {
4827 if (op == '+')
4828 f1 = f1 + f2;
4829 else
4830 f1 = f1 - f2;
4831 rettv->v_type = VAR_FLOAT;
4832 rettv->vval.v_float = f1;
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004835#endif
4836 {
4837 if (op == '+')
4838 n1 = n1 + n2;
4839 else
4840 n1 = n1 - n2;
4841 rettv->v_type = VAR_NUMBER;
4842 rettv->vval.v_number = n1;
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847 }
4848 return OK;
4849}
4850
4851/*
4852 * Handle fifth level expression:
4853 * * number multiplication
4854 * / number division
4855 * % number modulo
4856 *
4857 * "arg" must point to the first non-white of the expression.
4858 * "arg" is advanced to the next non-white after the recognized expression.
4859 *
4860 * Return OK or FAIL.
4861 */
4862 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004863eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004867 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
Bram Moolenaar33570922005-01-25 22:26:29 +00004869 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 int op;
4871 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872#ifdef FEAT_FLOAT
4873 int use_float = FALSE;
4874 float_T f1 = 0, f2;
4875#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878 /*
4879 * Get the first variable.
4880 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004881 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 return FAIL;
4883
4884 /*
4885 * Repeat computing, until no '*', '/' or '%' is following.
4886 */
4887 for (;;)
4888 {
4889 op = **arg;
4890 if (op != '*' && op != '/' && op != '%')
4891 break;
4892
4893 if (evaluate)
4894 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895#ifdef FEAT_FLOAT
4896 if (rettv->v_type == VAR_FLOAT)
4897 {
4898 f1 = rettv->vval.v_float;
4899 use_float = TRUE;
4900 n1 = 0;
4901 }
4902 else
4903#endif
4904 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004906 if (error)
4907 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else
4910 n1 = 0;
4911
4912 /*
4913 * Get the second variable.
4914 */
4915 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004916 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return FAIL;
4918
4919 if (evaluate)
4920 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921#ifdef FEAT_FLOAT
4922 if (var2.v_type == VAR_FLOAT)
4923 {
4924 if (!use_float)
4925 {
4926 f1 = n1;
4927 use_float = TRUE;
4928 }
4929 f2 = var2.vval.v_float;
4930 n2 = 0;
4931 }
4932 else
4933#endif
4934 {
4935 n2 = get_tv_number_chk(&var2, &error);
4936 clear_tv(&var2);
4937 if (error)
4938 return FAIL;
4939#ifdef FEAT_FLOAT
4940 if (use_float)
4941 f2 = n2;
4942#endif
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 /*
4946 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004947 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949#ifdef FEAT_FLOAT
4950 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952 if (op == '*')
4953 f1 = f1 * f2;
4954 else if (op == '/')
4955 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004956# ifdef VMS
4957 /* VMS crashes on divide by zero, work around it */
4958 if (f2 == 0.0)
4959 {
4960 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004961 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004963 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 }
4967 else
4968 f1 = f1 / f2;
4969# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 /* We rely on the floating point library to handle divide
4971 * by zero to result in "inf" and not a crash. */
4972 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004977 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 return FAIL;
4979 }
4980 rettv->v_type = VAR_FLOAT;
4981 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 if (op == '*')
4987 n1 = n1 * n2;
4988 else if (op == '/')
4989 {
4990 if (n2 == 0) /* give an error message? */
4991 {
4992 if (n1 == 0)
4993 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4994 else if (n1 < 0)
4995 n1 = -0x7fffffffL;
4996 else
4997 n1 = 0x7fffffffL;
4998 }
4999 else
5000 n1 = n1 / n2;
5001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 {
5004 if (n2 == 0) /* give an error message? */
5005 n1 = 0;
5006 else
5007 n1 = n1 % n2;
5008 }
5009 rettv->v_type = VAR_NUMBER;
5010 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013 }
5014
5015 return OK;
5016}
5017
5018/*
5019 * Handle sixth level expression:
5020 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005021 * "string" string constant
5022 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 * &option-name option value
5024 * @r register contents
5025 * identifier variable value
5026 * function() function call
5027 * $VAR environment variable
5028 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005029 * [expr, expr] List
5030 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 *
5032 * Also handle:
5033 * ! in front logical NOT
5034 * - in front unary minus
5035 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 * trailing [] subscript in String or List
5037 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 *
5039 * "arg" must point to the first non-white of the expression.
5040 * "arg" is advanced to the next non-white after the recognized expression.
5041 *
5042 * Return OK or FAIL.
5043 */
5044 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005045eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005049 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 long n;
5052 int len;
5053 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u *start_leader, *end_leader;
5055 int ret = OK;
5056 char_u *alias;
5057
5058 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 /*
5065 * Skip '!' and '-' characters. They are handled later.
5066 */
5067 start_leader = *arg;
5068 while (**arg == '!' || **arg == '-' || **arg == '+')
5069 *arg = skipwhite(*arg + 1);
5070 end_leader = *arg;
5071
5072 switch (**arg)
5073 {
5074 /*
5075 * Number constant.
5076 */
5077 case '0':
5078 case '1':
5079 case '2':
5080 case '3':
5081 case '4':
5082 case '5':
5083 case '6':
5084 case '7':
5085 case '8':
5086 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087 {
5088#ifdef FEAT_FLOAT
5089 char_u *p = skipdigits(*arg + 1);
5090 int get_float = FALSE;
5091
5092 /* We accept a float when the format matches
5093 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005094 * strict to avoid backwards compatibility problems.
5095 * Don't look for a float after the "." operator, so that
5096 * ":let vers = 1.2.3" doesn't fail. */
5097 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005099 get_float = TRUE;
5100 p = skipdigits(p + 2);
5101 if (*p == 'e' || *p == 'E')
5102 {
5103 ++p;
5104 if (*p == '-' || *p == '+')
5105 ++p;
5106 if (!vim_isdigit(*p))
5107 get_float = FALSE;
5108 else
5109 p = skipdigits(p + 1);
5110 }
5111 if (ASCII_ISALPHA(*p) || *p == '.')
5112 get_float = FALSE;
5113 }
5114 if (get_float)
5115 {
5116 float_T f;
5117
5118 *arg += string2float(*arg, &f);
5119 if (evaluate)
5120 {
5121 rettv->v_type = VAR_FLOAT;
5122 rettv->vval.v_float = f;
5123 }
5124 }
5125 else
5126#endif
5127 {
5128 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5129 *arg += len;
5130 if (evaluate)
5131 {
5132 rettv->v_type = VAR_NUMBER;
5133 rettv->vval.v_number = n;
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 /*
5140 * String constant: "string".
5141 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 break;
5144
5145 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 break;
5150
5151 /*
5152 * List: [expr, expr]
5153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 break;
5156
5157 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 * Dictionary: {key: val, key: val}
5159 */
5160 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5161 break;
5162
5163 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005164 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168
5169 /*
5170 * Environment variable: $VAR.
5171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Register contents: @r.
5177 */
5178 case '@': ++*arg;
5179 if (evaluate)
5180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005182 rettv->vval.v_string = get_reg_contents(**arg,
5183 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 if (**arg != NUL)
5186 ++*arg;
5187 break;
5188
5189 /*
5190 * nested expression: (expression).
5191 */
5192 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 if (**arg == ')')
5195 ++*arg;
5196 else if (ret == OK)
5197 {
5198 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 ret = FAIL;
5201 }
5202 break;
5203
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207
5208 if (ret == NOTDONE)
5209 {
5210 /*
5211 * Must be a variable or function name.
5212 * Can also be a curly-braces kind of name: {expr}.
5213 */
5214 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005215 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 if (alias != NULL)
5217 s = alias;
5218
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 ret = FAIL;
5221 else
5222 {
5223 if (**arg == '(') /* recursive! */
5224 {
5225 /* If "s" is the name of a variable of type VAR_FUNC
5226 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005227 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228
5229 /* Invoke the function. */
5230 ret = get_func_tv(s, len, rettv, arg,
5231 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005232 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005233
5234 /* If evaluate is FALSE rettv->v_type was not set in
5235 * get_func_tv, but it's needed in handle_subscript() to parse
5236 * what follows. So set it here. */
5237 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5238 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005239 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005240 rettv->v_type = VAR_FUNC;
5241 }
5242
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 /* Stop the expression evaluation when immediately
5244 * aborting on error, or when an interrupt occurred or
5245 * an exception was thrown but not caught. */
5246 if (aborting())
5247 {
5248 if (ret == OK)
5249 clear_tv(rettv);
5250 ret = FAIL;
5251 }
5252 }
5253 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005254 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005255 else
5256 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005258 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 }
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 *arg = skipwhite(*arg);
5262
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5264 * expr(expr). */
5265 if (ret == OK)
5266 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268 /*
5269 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5270 */
5271 if (ret == OK && evaluate && end_leader > start_leader)
5272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005273 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005274 int val = 0;
5275#ifdef FEAT_FLOAT
5276 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005278 if (rettv->v_type == VAR_FLOAT)
5279 f = rettv->vval.v_float;
5280 else
5281#endif
5282 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285 clear_tv(rettv);
5286 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 else
5289 {
5290 while (end_leader > start_leader)
5291 {
5292 --end_leader;
5293 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005294 {
5295#ifdef FEAT_FLOAT
5296 if (rettv->v_type == VAR_FLOAT)
5297 f = !f;
5298 else
5299#endif
5300 val = !val;
5301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = -f;
5307 else
5308#endif
5309 val = -val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312#ifdef FEAT_FLOAT
5313 if (rettv->v_type == VAR_FLOAT)
5314 {
5315 clear_tv(rettv);
5316 rettv->vval.v_float = f;
5317 }
5318 else
5319#endif
5320 {
5321 clear_tv(rettv);
5322 rettv->v_type = VAR_NUMBER;
5323 rettv->vval.v_number = val;
5324 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327
5328 return ret;
5329}
5330
5331/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005332 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5333 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5335 */
5336 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005337eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005341 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342{
5343 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 long len = -1;
5347 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005351 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005353 if (verbose)
5354 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 return FAIL;
5356 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005357#ifdef FEAT_FLOAT
5358 else if (rettv->v_type == VAR_FLOAT)
5359 {
5360 if (verbose)
5361 EMSG(_(e_float_as_string));
5362 return FAIL;
5363 }
5364#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 /*
5369 * dict.name
5370 */
5371 key = *arg + 1;
5372 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5373 ;
5374 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 }
5378 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380 /*
5381 * something[idx]
5382 *
5383 * Get the (first) variable from inside the [].
5384 */
5385 *arg = skipwhite(*arg + 1);
5386 if (**arg == ':')
5387 empty1 = TRUE;
5388 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5389 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005390 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5391 {
5392 /* not a number or string */
5393 clear_tv(&var1);
5394 return FAIL;
5395 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396
5397 /*
5398 * Get the second variable from inside the [:].
5399 */
5400 if (**arg == ':')
5401 {
5402 range = TRUE;
5403 *arg = skipwhite(*arg + 1);
5404 if (**arg == ']')
5405 empty2 = TRUE;
5406 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005408 if (!empty1)
5409 clear_tv(&var1);
5410 return FAIL;
5411 }
5412 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5413 {
5414 /* not a number or string */
5415 if (!empty1)
5416 clear_tv(&var1);
5417 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005418 return FAIL;
5419 }
5420 }
5421
5422 /* Check for the ']'. */
5423 if (**arg != ']')
5424 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005425 if (verbose)
5426 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 clear_tv(&var1);
5428 if (range)
5429 clear_tv(&var2);
5430 return FAIL;
5431 }
5432 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434
5435 if (evaluate)
5436 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437 n1 = 0;
5438 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 n1 = get_tv_number(&var1);
5441 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
5443 if (range)
5444 {
5445 if (empty2)
5446 n2 = -1;
5447 else
5448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 n2 = get_tv_number(&var2);
5450 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 }
5452 }
5453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 {
5456 case VAR_NUMBER:
5457 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 len = (long)STRLEN(s);
5460 if (range)
5461 {
5462 /* The resulting variable is a substring. If the indexes
5463 * are out of range the result is empty. */
5464 if (n1 < 0)
5465 {
5466 n1 = len + n1;
5467 if (n1 < 0)
5468 n1 = 0;
5469 }
5470 if (n2 < 0)
5471 n2 = len + n2;
5472 else if (n2 >= len)
5473 n2 = len;
5474 if (n1 >= len || n2 < 0 || n1 > n2)
5475 s = NULL;
5476 else
5477 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5478 }
5479 else
5480 {
5481 /* The resulting variable is a string of a single
5482 * character. If the index is too big or negative the
5483 * result is empty. */
5484 if (n1 >= len || n1 < 0)
5485 s = NULL;
5486 else
5487 s = vim_strnsave(s + n1, 1);
5488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 clear_tv(rettv);
5490 rettv->v_type = VAR_STRING;
5491 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 break;
5493
5494 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 if (n1 < 0)
5497 n1 = len + n1;
5498 if (!empty1 && (n1 < 0 || n1 >= len))
5499 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005500 /* For a range we allow invalid values and return an empty
5501 * list. A list index out of range is an error. */
5502 if (!range)
5503 {
5504 if (verbose)
5505 EMSGN(_(e_listidx), n1);
5506 return FAIL;
5507 }
5508 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 }
5510 if (range)
5511 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005512 list_T *l;
5513 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514
5515 if (n2 < 0)
5516 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005517 else if (n2 >= len)
5518 n2 = len - 1;
5519 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005520 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 l = list_alloc();
5522 if (l == NULL)
5523 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 n1 <= n2; ++n1)
5526 {
5527 if (list_append_tv(l, &item->li_tv) == FAIL)
5528 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005529 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 return FAIL;
5531 }
5532 item = item->li_next;
5533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 clear_tv(rettv);
5535 rettv->v_type = VAR_LIST;
5536 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005537 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
5539 else
5540 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005541 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 clear_tv(rettv);
5543 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546
5547 case VAR_DICT:
5548 if (range)
5549 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005550 if (verbose)
5551 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552 if (len == -1)
5553 clear_tv(&var1);
5554 return FAIL;
5555 }
5556 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005557 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558
5559 if (len == -1)
5560 {
5561 key = get_tv_string(&var1);
5562 if (*key == NUL)
5563 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005564 if (verbose)
5565 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 clear_tv(&var1);
5567 return FAIL;
5568 }
5569 }
5570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005571 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005573 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005574 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575 if (len == -1)
5576 clear_tv(&var1);
5577 if (item == NULL)
5578 return FAIL;
5579
5580 copy_tv(&item->di_tv, &var1);
5581 clear_tv(rettv);
5582 *rettv = var1;
5583 }
5584 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 }
5586 }
5587
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 return OK;
5589}
5590
5591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 * Get an option value.
5593 * "arg" points to the '&' or '+' before the option name.
5594 * "arg" is advanced to character after the option name.
5595 * Return OK or FAIL.
5596 */
5597 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 int evaluate;
5602{
5603 char_u *option_end;
5604 long numval;
5605 char_u *stringval;
5606 int opt_type;
5607 int c;
5608 int working = (**arg == '+'); /* has("+option") */
5609 int ret = OK;
5610 int opt_flags;
5611
5612 /*
5613 * Isolate the option name and find its value.
5614 */
5615 option_end = find_option_end(arg, &opt_flags);
5616 if (option_end == NULL)
5617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 EMSG2(_("E112: Option name missing: %s"), *arg);
5620 return FAIL;
5621 }
5622
5623 if (!evaluate)
5624 {
5625 *arg = option_end;
5626 return OK;
5627 }
5628
5629 c = *option_end;
5630 *option_end = NUL;
5631 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005632 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
5634 if (opt_type == -3) /* invalid name */
5635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 EMSG2(_("E113: Unknown option: %s"), *arg);
5638 ret = FAIL;
5639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005640 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 if (opt_type == -2) /* hidden string option */
5643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 rettv->v_type = VAR_STRING;
5645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647 else if (opt_type == -1) /* hidden number option */
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 rettv->v_type = VAR_NUMBER;
5650 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652 else if (opt_type == 1) /* number option */
5653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 rettv->v_type = VAR_NUMBER;
5655 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657 else /* string option */
5658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv->v_type = VAR_STRING;
5660 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 }
5662 }
5663 else if (working && (opt_type == -2 || opt_type == -1))
5664 ret = FAIL;
5665
5666 *option_end = c; /* put back for error messages */
5667 *arg = option_end;
5668
5669 return ret;
5670}
5671
5672/*
5673 * Allocate a variable for a string constant.
5674 * Return OK or FAIL.
5675 */
5676 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 int evaluate;
5681{
5682 char_u *p;
5683 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 int extra = 0;
5685
5686 /*
5687 * Find the end of the string, skipping backslashed characters.
5688 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005689 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
5691 if (*p == '\\' && p[1] != NUL)
5692 {
5693 ++p;
5694 /* A "\<x>" form occupies at least 4 characters, and produces up
5695 * to 6 characters: reserve space for 2 extra */
5696 if (*p == '<')
5697 extra += 2;
5698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700
5701 if (*p != '"')
5702 {
5703 EMSG2(_("E114: Missing quote: %s"), *arg);
5704 return FAIL;
5705 }
5706
5707 /* If only parsing, set *arg and return here */
5708 if (!evaluate)
5709 {
5710 *arg = p + 1;
5711 return OK;
5712 }
5713
5714 /*
5715 * Copy the string into allocated memory, handling backslashed
5716 * characters.
5717 */
5718 name = alloc((unsigned)(p - *arg + extra));
5719 if (name == NULL)
5720 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 rettv->v_type = VAR_STRING;
5722 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 if (*p == '\\')
5727 {
5728 switch (*++p)
5729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 case 'b': *name++ = BS; ++p; break;
5731 case 'e': *name++ = ESC; ++p; break;
5732 case 'f': *name++ = FF; ++p; break;
5733 case 'n': *name++ = NL; ++p; break;
5734 case 'r': *name++ = CAR; ++p; break;
5735 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 case 'X': /* hex: "\x1", "\x12" */
5738 case 'x':
5739 case 'u': /* Unicode: "\u0023" */
5740 case 'U':
5741 if (vim_isxdigit(p[1]))
5742 {
5743 int n, nr;
5744 int c = toupper(*p);
5745
5746 if (c == 'X')
5747 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005748 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005750 else
5751 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 nr = 0;
5753 while (--n >= 0 && vim_isxdigit(p[1]))
5754 {
5755 ++p;
5756 nr = (nr << 4) + hex2nr(*p);
5757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759#ifdef FEAT_MBYTE
5760 /* For "\u" store the number according to
5761 * 'encoding'. */
5762 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 else
5765#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 break;
5769
5770 /* octal: "\1", "\12", "\123" */
5771 case '0':
5772 case '1':
5773 case '2':
5774 case '3':
5775 case '4':
5776 case '5':
5777 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 case '7': *name = *p++ - '0';
5779 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 *name = (*name << 3) + *p++ - '0';
5782 if (*p >= '0' && *p <= '7')
5783 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 break;
5787
5788 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 if (extra != 0)
5791 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 break;
5794 }
5795 /* FALLTHROUGH */
5796
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 break;
5799 }
5800 }
5801 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 *arg = p + 1;
5807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 return OK;
5809}
5810
5811/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 * Return OK or FAIL.
5814 */
5815 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005816get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 int evaluate;
5820{
5821 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005822 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005823 int reduce = 0;
5824
5825 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005827 */
5828 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5829 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005833 break;
5834 ++reduce;
5835 ++p;
5836 }
5837 }
5838
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 return FAIL;
5843 }
5844
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 if (!evaluate)
5847 {
5848 *arg = p + 1;
5849 return OK;
5850 }
5851
5852 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 */
5855 str = alloc((unsigned)((p - *arg) - reduce));
5856 if (str == NULL)
5857 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 rettv->v_type = VAR_STRING;
5859 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866 break;
5867 ++p;
5868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 *arg = p + 1;
5873
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 return OK;
5875}
5876
5877/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878 * Allocate a variable for a List and fill it from "*arg".
5879 * Return OK or FAIL.
5880 */
5881 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005882get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005884 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 int evaluate;
5886{
Bram Moolenaar33570922005-01-25 22:26:29 +00005887 list_T *l = NULL;
5888 typval_T tv;
5889 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890
5891 if (evaluate)
5892 {
5893 l = list_alloc();
5894 if (l == NULL)
5895 return FAIL;
5896 }
5897
5898 *arg = skipwhite(*arg + 1);
5899 while (**arg != ']' && **arg != NUL)
5900 {
5901 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5902 goto failret;
5903 if (evaluate)
5904 {
5905 item = listitem_alloc();
5906 if (item != NULL)
5907 {
5908 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005909 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 list_append(l, item);
5911 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005912 else
5913 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 }
5915
5916 if (**arg == ']')
5917 break;
5918 if (**arg != ',')
5919 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 goto failret;
5922 }
5923 *arg = skipwhite(*arg + 1);
5924 }
5925
5926 if (**arg != ']')
5927 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929failret:
5930 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005931 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 return FAIL;
5933 }
5934
5935 *arg = skipwhite(*arg + 1);
5936 if (evaluate)
5937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005938 rettv->v_type = VAR_LIST;
5939 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 ++l->lv_refcount;
5941 }
5942
5943 return OK;
5944}
5945
5946/*
5947 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005948 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005950 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951list_alloc()
5952{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 list_T *l;
5954
5955 l = (list_T *)alloc_clear(sizeof(list_T));
5956 if (l != NULL)
5957 {
5958 /* Prepend the list to the list of lists for garbage collection. */
5959 if (first_list != NULL)
5960 first_list->lv_used_prev = l;
5961 l->lv_used_prev = NULL;
5962 l->lv_used_next = first_list;
5963 first_list = l;
5964 }
5965 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966}
5967
5968/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005969 * Allocate an empty list for a return value.
5970 * Returns OK or FAIL.
5971 */
5972 static int
5973rettv_list_alloc(rettv)
5974 typval_T *rettv;
5975{
5976 list_T *l = list_alloc();
5977
5978 if (l == NULL)
5979 return FAIL;
5980
5981 rettv->vval.v_list = l;
5982 rettv->v_type = VAR_LIST;
5983 ++l->lv_refcount;
5984 return OK;
5985}
5986
5987/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 * Unreference a list: decrement the reference count and free it when it
5989 * becomes zero.
5990 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005991 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005995 if (l != NULL && --l->lv_refcount <= 0)
5996 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997}
5998
5999/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006000 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 * Ignores the reference count.
6002 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006003 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006004list_free(l, recurse)
6005 list_T *l;
6006 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007{
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006010 /* Remove the list from the list of lists for garbage collection. */
6011 if (l->lv_used_prev == NULL)
6012 first_list = l->lv_used_next;
6013 else
6014 l->lv_used_prev->lv_used_next = l->lv_used_next;
6015 if (l->lv_used_next != NULL)
6016 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6017
Bram Moolenaard9fba312005-06-26 22:34:35 +00006018 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006020 /* Remove the item before deleting it. */
6021 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006022 if (recurse || (item->li_tv.v_type != VAR_LIST
6023 && item->li_tv.v_type != VAR_DICT))
6024 clear_tv(&item->li_tv);
6025 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 }
6027 vim_free(l);
6028}
6029
6030/*
6031 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006032 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006034 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035listitem_alloc()
6036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038}
6039
6040/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006041 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006043 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006047 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 vim_free(item);
6049}
6050
6051/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006052 * Remove a list item from a List and free it. Also clears the value.
6053 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006054 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006055listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006056 list_T *l;
6057 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006058{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006059 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006060 listitem_free(item);
6061}
6062
6063/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 * Get the number of items in a list.
6065 */
6066 static long
6067list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 if (l == NULL)
6071 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006072 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073}
6074
6075/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076 * Return TRUE when two lists have exactly the same values.
6077 */
6078 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006079list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006080 list_T *l1;
6081 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006084{
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006086
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006087 if (l1 == NULL || l2 == NULL)
6088 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006089 if (l1 == l2)
6090 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006091 if (list_len(l1) != list_len(l2))
6092 return FALSE;
6093
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006094 for (item1 = l1->lv_first, item2 = l2->lv_first;
6095 item1 != NULL && item2 != NULL;
6096 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006097 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098 return FALSE;
6099 return item1 == NULL && item2 == NULL;
6100}
6101
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006102#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6103 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006104/*
6105 * Return the dictitem that an entry in a hashtable points to.
6106 */
6107 dictitem_T *
6108dict_lookup(hi)
6109 hashitem_T *hi;
6110{
6111 return HI2DI(hi);
6112}
6113#endif
6114
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006116 * Return TRUE when two dictionaries have exactly the same key/values.
6117 */
6118 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006119dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 dict_T *d1;
6121 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006122 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006124{
Bram Moolenaar33570922005-01-25 22:26:29 +00006125 hashitem_T *hi;
6126 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006127 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006129 if (d1 == NULL || d2 == NULL)
6130 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006131 if (d1 == d2)
6132 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006133 if (dict_len(d1) != dict_len(d2))
6134 return FALSE;
6135
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006136 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006138 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 if (!HASHITEM_EMPTY(hi))
6140 {
6141 item2 = dict_find(d2, hi->hi_key, -1);
6142 if (item2 == NULL)
6143 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006144 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006145 return FALSE;
6146 --todo;
6147 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 }
6149 return TRUE;
6150}
6151
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152static int tv_equal_recurse_limit;
6153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006155 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006156 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006157 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006158 */
6159 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 typval_T *tv1;
6162 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006163 int ic; /* ignore case */
6164 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006165{
6166 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006167 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006169 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006171 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006172 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006173
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006174 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006175 * recursiveness to a limit. We guess they are equal then.
6176 * A fixed limit has the problem of still taking an awful long time.
6177 * Reduce the limit every time running into it. That should work fine for
6178 * deeply linked structures that are not recursively linked and catch
6179 * recursiveness quickly. */
6180 if (!recursive)
6181 tv_equal_recurse_limit = 1000;
6182 if (recursive_cnt >= tv_equal_recurse_limit)
6183 {
6184 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006185 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187
6188 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006189 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006190 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 ++recursive_cnt;
6192 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6193 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006194 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195
6196 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006197 ++recursive_cnt;
6198 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6199 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006200 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201
6202 case VAR_FUNC:
6203 return (tv1->vval.v_string != NULL
6204 && tv2->vval.v_string != NULL
6205 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6206
6207 case VAR_NUMBER:
6208 return tv1->vval.v_number == tv2->vval.v_number;
6209
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006210#ifdef FEAT_FLOAT
6211 case VAR_FLOAT:
6212 return tv1->vval.v_float == tv2->vval.v_float;
6213#endif
6214
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006215 case VAR_STRING:
6216 s1 = get_tv_string_buf(tv1, buf1);
6217 s2 = get_tv_string_buf(tv2, buf2);
6218 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006220
6221 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006222 return TRUE;
6223}
6224
6225/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006226 * Locate item with index "n" in list "l" and return it.
6227 * A negative index is counted from the end; -1 is the last item.
6228 * Returns NULL when "n" is out of range.
6229 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006230 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006233 long n;
6234{
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 long idx;
6237
6238 if (l == NULL)
6239 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006240
6241 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006243 n = l->lv_len + n;
6244
6245 /* Check for index out of range. */
6246 if (n < 0 || n >= l->lv_len)
6247 return NULL;
6248
6249 /* When there is a cached index may start search from there. */
6250 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006251 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006252 if (n < l->lv_idx / 2)
6253 {
6254 /* closest to the start of the list */
6255 item = l->lv_first;
6256 idx = 0;
6257 }
6258 else if (n > (l->lv_idx + l->lv_len) / 2)
6259 {
6260 /* closest to the end of the list */
6261 item = l->lv_last;
6262 idx = l->lv_len - 1;
6263 }
6264 else
6265 {
6266 /* closest to the cached index */
6267 item = l->lv_idx_item;
6268 idx = l->lv_idx;
6269 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 }
6271 else
6272 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006273 if (n < l->lv_len / 2)
6274 {
6275 /* closest to the start of the list */
6276 item = l->lv_first;
6277 idx = 0;
6278 }
6279 else
6280 {
6281 /* closest to the end of the list */
6282 item = l->lv_last;
6283 idx = l->lv_len - 1;
6284 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006286
6287 while (n > idx)
6288 {
6289 /* search forward */
6290 item = item->li_next;
6291 ++idx;
6292 }
6293 while (n < idx)
6294 {
6295 /* search backward */
6296 item = item->li_prev;
6297 --idx;
6298 }
6299
6300 /* cache the used index */
6301 l->lv_idx = idx;
6302 l->lv_idx_item = item;
6303
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006304 return item;
6305}
6306
6307/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006308 * Get list item "l[idx]" as a number.
6309 */
6310 static long
6311list_find_nr(l, idx, errorp)
6312 list_T *l;
6313 long idx;
6314 int *errorp; /* set to TRUE when something wrong */
6315{
6316 listitem_T *li;
6317
6318 li = list_find(l, idx);
6319 if (li == NULL)
6320 {
6321 if (errorp != NULL)
6322 *errorp = TRUE;
6323 return -1L;
6324 }
6325 return get_tv_number_chk(&li->li_tv, errorp);
6326}
6327
6328/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006329 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6330 */
6331 char_u *
6332list_find_str(l, idx)
6333 list_T *l;
6334 long idx;
6335{
6336 listitem_T *li;
6337
6338 li = list_find(l, idx - 1);
6339 if (li == NULL)
6340 {
6341 EMSGN(_(e_listidx), idx);
6342 return NULL;
6343 }
6344 return get_tv_string(&li->li_tv);
6345}
6346
6347/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006348 * Locate "item" list "l" and return its index.
6349 * Returns -1 when "item" is not in the list.
6350 */
6351 static long
6352list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 list_T *l;
6354 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006355{
6356 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006358
6359 if (l == NULL)
6360 return -1;
6361 idx = 0;
6362 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6363 ++idx;
6364 if (li == NULL)
6365 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006366 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006367}
6368
6369/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 * Append item "item" to the end of list "l".
6371 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006372 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *l;
6375 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376{
6377 if (l->lv_last == NULL)
6378 {
6379 /* empty list */
6380 l->lv_first = item;
6381 l->lv_last = item;
6382 item->li_prev = NULL;
6383 }
6384 else
6385 {
6386 l->lv_last->li_next = item;
6387 item->li_prev = l->lv_last;
6388 l->lv_last = item;
6389 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006390 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 item->li_next = NULL;
6392}
6393
6394/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006396 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006398 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 list_T *l;
6401 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006403 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404
Bram Moolenaar05159a02005-02-26 23:04:13 +00006405 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006407 copy_tv(tv, &li->li_tv);
6408 list_append(l, li);
6409 return OK;
6410}
6411
6412/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006413 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006414 * Return FAIL when out of memory.
6415 */
6416 int
6417list_append_dict(list, dict)
6418 list_T *list;
6419 dict_T *dict;
6420{
6421 listitem_T *li = listitem_alloc();
6422
6423 if (li == NULL)
6424 return FAIL;
6425 li->li_tv.v_type = VAR_DICT;
6426 li->li_tv.v_lock = 0;
6427 li->li_tv.vval.v_dict = dict;
6428 list_append(list, li);
6429 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 return OK;
6431}
6432
6433/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006434 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006435 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006436 * Returns FAIL when out of memory.
6437 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006438 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006439list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006440 list_T *l;
6441 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006442 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006443{
6444 listitem_T *li = listitem_alloc();
6445
6446 if (li == NULL)
6447 return FAIL;
6448 list_append(l, li);
6449 li->li_tv.v_type = VAR_STRING;
6450 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006451 if (str == NULL)
6452 li->li_tv.vval.v_string = NULL;
6453 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006454 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006455 return FAIL;
6456 return OK;
6457}
6458
6459/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006460 * Append "n" to list "l".
6461 * Returns FAIL when out of memory.
6462 */
6463 static int
6464list_append_number(l, n)
6465 list_T *l;
6466 varnumber_T n;
6467{
6468 listitem_T *li;
6469
6470 li = listitem_alloc();
6471 if (li == NULL)
6472 return FAIL;
6473 li->li_tv.v_type = VAR_NUMBER;
6474 li->li_tv.v_lock = 0;
6475 li->li_tv.vval.v_number = n;
6476 list_append(l, li);
6477 return OK;
6478}
6479
6480/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006481 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482 * If "item" is NULL append at the end.
6483 * Return FAIL when out of memory.
6484 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006485 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 list_T *l;
6488 typval_T *tv;
6489 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006490{
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492
6493 if (ni == NULL)
6494 return FAIL;
6495 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006496 list_insert(l, ni, item);
6497 return OK;
6498}
6499
6500 void
6501list_insert(l, ni, item)
6502 list_T *l;
6503 listitem_T *ni;
6504 listitem_T *item;
6505{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506 if (item == NULL)
6507 /* Append new item at end of list. */
6508 list_append(l, ni);
6509 else
6510 {
6511 /* Insert new item before existing item. */
6512 ni->li_prev = item->li_prev;
6513 ni->li_next = item;
6514 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006515 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006517 ++l->lv_idx;
6518 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006520 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006521 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006522 l->lv_idx_item = NULL;
6523 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527}
6528
6529/*
6530 * Extend "l1" with "l2".
6531 * If "bef" is NULL append at the end, otherwise insert before this item.
6532 * Returns FAIL when out of memory.
6533 */
6534 static int
6535list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 list_T *l1;
6537 list_T *l2;
6538 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539{
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006541 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006543 /* We also quit the loop when we have inserted the original item count of
6544 * the list, avoid a hang when we extend a list with itself. */
6545 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6547 return FAIL;
6548 return OK;
6549}
6550
6551/*
6552 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6553 * Return FAIL when out of memory.
6554 */
6555 static int
6556list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 list_T *l1;
6558 list_T *l2;
6559 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006563 if (l1 == NULL || l2 == NULL)
6564 return FAIL;
6565
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 if (l == NULL)
6569 return FAIL;
6570 tv->v_type = VAR_LIST;
6571 tv->vval.v_list = l;
6572
6573 /* append all items from the second list */
6574 return list_extend(l, l2, NULL);
6575}
6576
6577/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 * Returns NULL when out of memory.
6582 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006584list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006587 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588{
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 list_T *copy;
6590 listitem_T *item;
6591 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592
6593 if (orig == NULL)
6594 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
6596 copy = list_alloc();
6597 if (copy != NULL)
6598 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006599 if (copyID != 0)
6600 {
6601 /* Do this before adding the items, because one of the items may
6602 * refer back to this list. */
6603 orig->lv_copyID = copyID;
6604 orig->lv_copylist = copy;
6605 }
6606 for (item = orig->lv_first; item != NULL && !got_int;
6607 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 {
6609 ni = listitem_alloc();
6610 if (ni == NULL)
6611 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006612 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006613 {
6614 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6615 {
6616 vim_free(ni);
6617 break;
6618 }
6619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006621 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 list_append(copy, ni);
6623 }
6624 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006625 if (item != NULL)
6626 {
6627 list_unref(copy);
6628 copy = NULL;
6629 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 }
6631
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 return copy;
6633}
6634
6635/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006636 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006637 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006638 * This used to be called list_remove, but that conflicts with a Sun header
6639 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006641 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006642vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006643 list_T *l;
6644 listitem_T *item;
6645 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646{
Bram Moolenaar33570922005-01-25 22:26:29 +00006647 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649 /* notify watchers */
6650 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006652 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006653 list_fix_watch(l, ip);
6654 if (ip == item2)
6655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657
6658 if (item2->li_next == NULL)
6659 l->lv_last = item->li_prev;
6660 else
6661 item2->li_next->li_prev = item->li_prev;
6662 if (item->li_prev == NULL)
6663 l->lv_first = item2->li_next;
6664 else
6665 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006666 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667}
6668
6669/*
6670 * Return an allocated string with the string representation of a list.
6671 * May return NULL.
6672 */
6673 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006674list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006675 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006676 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677{
6678 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679
6680 if (tv->vval.v_list == NULL)
6681 return NULL;
6682 ga_init2(&ga, (int)sizeof(char), 80);
6683 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006684 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 {
6686 vim_free(ga.ga_data);
6687 return NULL;
6688 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006689 ga_append(&ga, ']');
6690 ga_append(&ga, NUL);
6691 return (char_u *)ga.ga_data;
6692}
6693
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694typedef struct join_S {
6695 char_u *s;
6696 char_u *tofree;
6697} join_T;
6698
6699 static int
6700list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6701 garray_T *gap; /* to store the result in */
6702 list_T *l;
6703 char_u *sep;
6704 int echo_style;
6705 int copyID;
6706 garray_T *join_gap; /* to keep each list item string */
6707{
6708 int i;
6709 join_T *p;
6710 int len;
6711 int sumlen = 0;
6712 int first = TRUE;
6713 char_u *tofree;
6714 char_u numbuf[NUMBUFLEN];
6715 listitem_T *item;
6716 char_u *s;
6717
6718 /* Stringify each item in the list. */
6719 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6720 {
6721 if (echo_style)
6722 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6723 else
6724 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6725 if (s == NULL)
6726 return FAIL;
6727
6728 len = (int)STRLEN(s);
6729 sumlen += len;
6730
6731 ga_grow(join_gap, 1);
6732 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6733 if (tofree != NULL || s != numbuf)
6734 {
6735 p->s = s;
6736 p->tofree = tofree;
6737 }
6738 else
6739 {
6740 p->s = vim_strnsave(s, len);
6741 p->tofree = p->s;
6742 }
6743
6744 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006745 if (did_echo_string_emsg) /* recursion error, bail out */
6746 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006747 }
6748
6749 /* Allocate result buffer with its total size, avoid re-allocation and
6750 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6751 if (join_gap->ga_len >= 2)
6752 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6753 if (ga_grow(gap, sumlen + 2) == FAIL)
6754 return FAIL;
6755
6756 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6757 {
6758 if (first)
6759 first = FALSE;
6760 else
6761 ga_concat(gap, sep);
6762 p = ((join_T *)join_gap->ga_data) + i;
6763
6764 if (p->s != NULL)
6765 ga_concat(gap, p->s);
6766 line_breakcheck();
6767 }
6768
6769 return OK;
6770}
6771
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006772/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006774 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006775 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006777 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006778list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006782 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006783 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 garray_T join_ga;
6786 int retval;
6787 join_T *p;
6788 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789
Bram Moolenaard39a7512015-04-16 22:51:22 +02006790 if (l->lv_len < 1)
6791 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006792 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6793 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6794
6795 /* Dispose each item in join_ga. */
6796 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798 p = (join_T *)join_ga.ga_data;
6799 for (i = 0; i < join_ga.ga_len; ++i)
6800 {
6801 vim_free(p->tofree);
6802 ++p;
6803 }
6804 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006806
6807 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808}
6809
6810/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 * Garbage collection for lists and dictionaries.
6812 *
6813 * We use reference counts to be able to free most items right away when they
6814 * are no longer used. But for composite items it's possible that it becomes
6815 * unused while the reference count is > 0: When there is a recursive
6816 * reference. Example:
6817 * :let l = [1, 2, 3]
6818 * :let d = {9: l}
6819 * :let l[1] = d
6820 *
6821 * Since this is quite unusual we handle this with garbage collection: every
6822 * once in a while find out which lists and dicts are not referenced from any
6823 * variable.
6824 *
6825 * Here is a good reference text about garbage collection (refers to Python
6826 * but it applies to all reference-counting mechanisms):
6827 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829
6830/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 * Do garbage collection for lists and dicts.
6832 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 int
6835garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006837 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006838 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 buf_T *buf;
6840 win_T *wp;
6841 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006842 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006843 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006845#ifdef FEAT_WINDOWS
6846 tabpage_T *tp;
6847#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006849 /* Only do this once. */
6850 want_garbage_collect = FALSE;
6851 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006852 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006853
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 /* We advance by two because we add one for items referenced through
6855 * previous_funccal. */
6856 current_copyID += COPYID_INC;
6857 copyID = current_copyID;
6858
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 /*
6860 * 1. Go through all accessible variables and mark all lists and dicts
6861 * with copyID.
6862 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863
6864 /* Don't free variables in the previous_funccal list unless they are only
6865 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006866 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006867 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6868 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006869 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6870 NULL);
6871 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6872 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 }
6874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 /* script-local variables */
6876 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
6879 /* buffer-local variables */
6880 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6882 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883
6884 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006885 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006886 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6887 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006888#ifdef FEAT_AUTOCMD
6889 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6891 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006892#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006894#ifdef FEAT_WINDOWS
6895 /* tabpage-local variables */
6896 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006899#endif
6900
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903
6904 /* function-local variables */
6905 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6906 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6908 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 }
6910
Bram Moolenaard812df62008-11-09 12:46:09 +00006911 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006913
Bram Moolenaar1dced572012-04-05 16:54:08 +02006914#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006916#endif
6917
Bram Moolenaardb913952012-06-29 12:54:53 +02006918#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006920#endif
6921
6922#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006924#endif
6925
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006927 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 /*
6929 * 2. Free lists and dictionaries that are not referenced.
6930 */
6931 did_free = free_unref_items(copyID);
6932
6933 /*
6934 * 3. Check if any funccal can be freed now.
6935 */
6936 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006937 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 if (can_free_funccal(*pfc, copyID))
6939 {
6940 fc = *pfc;
6941 *pfc = fc->caller;
6942 free_funccal(fc, TRUE);
6943 did_free = TRUE;
6944 did_free_funccal = TRUE;
6945 }
6946 else
6947 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 if (did_free_funccal)
6950 /* When a funccal was freed some more items might be garbage
6951 * collected, so run again. */
6952 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006953 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 else if (p_verbose > 0)
6955 {
6956 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6957 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006958
6959 return did_free;
6960}
6961
6962/*
6963 * Free lists and dictionaries that are no longer referenced.
6964 */
6965 static int
6966free_unref_items(copyID)
6967 int copyID;
6968{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006969 dict_T *dd, *dd_next;
6970 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 int did_free = FALSE;
6972
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 */
6976 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006977 {
6978 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006981 /* Free the Dictionary and ordinary items it contains, but don't
6982 * recurse into Lists and Dictionaries, they will be in the list
6983 * of dicts or list of lists. */
6984 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006987 dd = dd_next;
6988 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989
6990 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 * Go through the list of lists and free items without the copyID.
6992 * But don't free a list that has a watcher (used in a for loop), these
6993 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 */
6995 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 {
6997 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6999 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007001 /* Free the List and ordinary items it contains, but don't recurse
7002 * into Lists and Dictionaries, they will be in the list of dicts
7003 * or list of lists. */
7004 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 ll = ll_next;
7008 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 return did_free;
7010}
7011
7012/*
7013 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007014 * "list_stack" is used to add lists to be marked. Can be NULL.
7015 *
7016 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 int
7019set_ref_in_ht(ht, copyID, list_stack)
7020 hashtab_T *ht;
7021 int copyID;
7022 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023{
7024 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 hashtab_T *cur_ht;
7028 ht_stack_T *ht_stack = NULL;
7029 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 cur_ht = ht;
7032 for (;;)
7033 {
7034 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 /* Mark each item in the hashtab. If the item contains a hashtab
7037 * it is added to ht_stack, if it contains a list it is added to
7038 * list_stack. */
7039 todo = (int)cur_ht->ht_used;
7040 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7041 if (!HASHITEM_EMPTY(hi))
7042 {
7043 --todo;
7044 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7045 &ht_stack, list_stack);
7046 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048
7049 if (ht_stack == NULL)
7050 break;
7051
7052 /* take an item from the stack */
7053 cur_ht = ht_stack->ht;
7054 tempitem = ht_stack;
7055 ht_stack = ht_stack->prev;
7056 free(tempitem);
7057 }
7058
7059 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060}
7061
7062/*
7063 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7065 *
7066 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 int
7069set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 list_T *l;
7071 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 listitem_T *li;
7075 int abort = FALSE;
7076 list_T *cur_l;
7077 list_stack_T *list_stack = NULL;
7078 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 cur_l = l;
7081 for (;;)
7082 {
7083 if (!abort)
7084 /* Mark each item in the list. If the item contains a hashtab
7085 * it is added to ht_stack, if it contains a list it is added to
7086 * list_stack. */
7087 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7088 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7089 ht_stack, &list_stack);
7090 if (list_stack == NULL)
7091 break;
7092
7093 /* take an item from the stack */
7094 cur_l = list_stack->list;
7095 tempitem = list_stack;
7096 list_stack = list_stack->prev;
7097 free(tempitem);
7098 }
7099
7100 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101}
7102
7103/*
7104 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 * "list_stack" is used to add lists to be marked. Can be NULL.
7106 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7107 *
7108 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 int
7111set_ref_in_item(tv, copyID, ht_stack, list_stack)
7112 typval_T *tv;
7113 int copyID;
7114 ht_stack_T **ht_stack;
7115 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116{
7117 dict_T *dd;
7118 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007120
7121 switch (tv->v_type)
7122 {
7123 case VAR_DICT:
7124 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007125 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007127 /* Didn't see this dict yet. */
7128 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007129 if (ht_stack == NULL)
7130 {
7131 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7132 }
7133 else
7134 {
7135 ht_stack_T *newitem = (ht_stack_T*)malloc(
7136 sizeof(ht_stack_T));
7137 if (newitem == NULL)
7138 abort = TRUE;
7139 else
7140 {
7141 newitem->ht = &dd->dv_hashtab;
7142 newitem->prev = *ht_stack;
7143 *ht_stack = newitem;
7144 }
7145 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007147 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007148
7149 case VAR_LIST:
7150 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007151 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007153 /* Didn't see this list yet. */
7154 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 if (list_stack == NULL)
7156 {
7157 abort = set_ref_in_list(ll, copyID, ht_stack);
7158 }
7159 else
7160 {
7161 list_stack_T *newitem = (list_stack_T*)malloc(
7162 sizeof(list_stack_T));
7163 if (newitem == NULL)
7164 abort = TRUE;
7165 else
7166 {
7167 newitem->list = ll;
7168 newitem->prev = *list_stack;
7169 *list_stack = newitem;
7170 }
7171 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007175 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007176}
7177
7178/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179 * Allocate an empty header for a dictionary.
7180 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007181 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182dict_alloc()
7183{
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185
Bram Moolenaar33570922005-01-25 22:26:29 +00007186 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007187 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007188 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007189 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007190 if (first_dict != NULL)
7191 first_dict->dv_used_prev = d;
7192 d->dv_used_next = first_dict;
7193 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007194 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007197 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007198 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007199 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007200 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007201 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203}
7204
7205/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007206 * Allocate an empty dict for a return value.
7207 * Returns OK or FAIL.
7208 */
7209 static int
7210rettv_dict_alloc(rettv)
7211 typval_T *rettv;
7212{
7213 dict_T *d = dict_alloc();
7214
7215 if (d == NULL)
7216 return FAIL;
7217
7218 rettv->vval.v_dict = d;
7219 rettv->v_type = VAR_DICT;
7220 ++d->dv_refcount;
7221 return OK;
7222}
7223
7224
7225/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226 * Unreference a Dictionary: decrement the reference count and free it when it
7227 * becomes zero.
7228 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007229 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007231 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007233 if (d != NULL && --d->dv_refcount <= 0)
7234 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235}
7236
7237/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007238 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 * Ignores the reference count.
7240 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007241 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007242dict_free(d, recurse)
7243 dict_T *d;
7244 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007248 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007250 /* Remove the dict from the list of dicts for garbage collection. */
7251 if (d->dv_used_prev == NULL)
7252 first_dict = d->dv_used_next;
7253 else
7254 d->dv_used_prev->dv_used_next = d->dv_used_next;
7255 if (d->dv_used_next != NULL)
7256 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7257
7258 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007259 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007260 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007261 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263 if (!HASHITEM_EMPTY(hi))
7264 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007265 /* Remove the item before deleting it, just in case there is
7266 * something recursive causing trouble. */
7267 di = HI2DI(hi);
7268 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007269 if (recurse || (di->di_tv.v_type != VAR_LIST
7270 && di->di_tv.v_type != VAR_DICT))
7271 clear_tv(&di->di_tv);
7272 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 --todo;
7274 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 vim_free(d);
7278}
7279
7280/*
7281 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 * The "key" is copied to the new item.
7283 * Note that the value of the item "di_tv" still needs to be initialized!
7284 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007286 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287dictitem_alloc(key)
7288 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289{
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007292 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007296 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007297 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299}
7300
7301/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 * Make a copy of a Dictionary item.
7303 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307{
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007310 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7311 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007312 if (di != NULL)
7313 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007315 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007316 copy_tv(&org->di_tv, &di->di_tv);
7317 }
7318 return di;
7319}
7320
7321/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 * Remove item "item" from Dictionary "dict" and free it.
7323 */
7324 static void
7325dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 dict_T *dict;
7327 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328{
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 if (HASHITEM_EMPTY(hi))
7333 EMSG2(_(e_intern2), "dictitem_remove()");
7334 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 dictitem_free(item);
7337}
7338
7339/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 * Free a dict item. Also clears the value.
7341 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007342 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007347 if (item->di_flags & DI_FLAGS_ALLOC)
7348 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349}
7350
7351/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7353 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007354 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355 * Returns NULL when out of memory.
7356 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007358dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362{
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 dict_T *copy;
7364 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367
7368 if (orig == NULL)
7369 return NULL;
7370
7371 copy = dict_alloc();
7372 if (copy != NULL)
7373 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 if (copyID != 0)
7375 {
7376 orig->dv_copyID = copyID;
7377 orig->dv_copydict = copy;
7378 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007379 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007380 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007384 --todo;
7385
7386 di = dictitem_alloc(hi->hi_key);
7387 if (di == NULL)
7388 break;
7389 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007390 {
7391 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7392 copyID) == FAIL)
7393 {
7394 vim_free(di);
7395 break;
7396 }
7397 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 else
7399 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7400 if (dict_add(copy, di) == FAIL)
7401 {
7402 dictitem_free(di);
7403 break;
7404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007407
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007409 if (todo > 0)
7410 {
7411 dict_unref(copy);
7412 copy = NULL;
7413 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007414 }
7415
7416 return copy;
7417}
7418
7419/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007421 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007423 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 dict_T *d;
7426 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427{
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429}
7430
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007432 * Add a number or string entry to dictionary "d".
7433 * When "str" is NULL use number "nr", otherwise use "str".
7434 * Returns FAIL when out of memory and when key already exists.
7435 */
7436 int
7437dict_add_nr_str(d, key, nr, str)
7438 dict_T *d;
7439 char *key;
7440 long nr;
7441 char_u *str;
7442{
7443 dictitem_T *item;
7444
7445 item = dictitem_alloc((char_u *)key);
7446 if (item == NULL)
7447 return FAIL;
7448 item->di_tv.v_lock = 0;
7449 if (str == NULL)
7450 {
7451 item->di_tv.v_type = VAR_NUMBER;
7452 item->di_tv.vval.v_number = nr;
7453 }
7454 else
7455 {
7456 item->di_tv.v_type = VAR_STRING;
7457 item->di_tv.vval.v_string = vim_strsave(str);
7458 }
7459 if (dict_add(d, item) == FAIL)
7460 {
7461 dictitem_free(item);
7462 return FAIL;
7463 }
7464 return OK;
7465}
7466
7467/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007468 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007469 * Returns FAIL when out of memory and when key already exists.
7470 */
7471 int
7472dict_add_list(d, key, list)
7473 dict_T *d;
7474 char *key;
7475 list_T *list;
7476{
7477 dictitem_T *item;
7478
7479 item = dictitem_alloc((char_u *)key);
7480 if (item == NULL)
7481 return FAIL;
7482 item->di_tv.v_lock = 0;
7483 item->di_tv.v_type = VAR_LIST;
7484 item->di_tv.vval.v_list = list;
7485 if (dict_add(d, item) == FAIL)
7486 {
7487 dictitem_free(item);
7488 return FAIL;
7489 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007490 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007491 return OK;
7492}
7493
7494/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495 * Get the number of items in a Dictionary.
7496 */
7497 static long
7498dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007500{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 if (d == NULL)
7502 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007503 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007504}
7505
7506/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 * Find item "key[len]" in Dictionary "d".
7508 * If "len" is negative use strlen(key).
7509 * Returns NULL when not found.
7510 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007511 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 char_u *key;
7515 int len;
7516{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517#define AKEYLEN 200
7518 char_u buf[AKEYLEN];
7519 char_u *akey;
7520 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 if (len < 0)
7524 akey = key;
7525 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007526 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 tofree = akey = vim_strnsave(key, len);
7528 if (akey == NULL)
7529 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007530 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 else
7532 {
7533 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007534 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 akey = buf;
7536 }
7537
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 vim_free(tofree);
7540 if (HASHITEM_EMPTY(hi))
7541 return NULL;
7542 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543}
7544
7545/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007546 * Get a string item from a dictionary.
7547 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007548 * Returns NULL if the entry doesn't exist or out of memory.
7549 */
7550 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007551get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007552 dict_T *d;
7553 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007554 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007555{
7556 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007557 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007558
7559 di = dict_find(d, key, -1);
7560 if (di == NULL)
7561 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562 s = get_tv_string(&di->di_tv);
7563 if (save && s != NULL)
7564 s = vim_strsave(s);
7565 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007566}
7567
7568/*
7569 * Get a number item from a dictionary.
7570 * Returns 0 if the entry doesn't exist or out of memory.
7571 */
7572 long
7573get_dict_number(d, key)
7574 dict_T *d;
7575 char_u *key;
7576{
7577 dictitem_T *di;
7578
7579 di = dict_find(d, key, -1);
7580 if (di == NULL)
7581 return 0;
7582 return get_tv_number(&di->di_tv);
7583}
7584
7585/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 * Return an allocated string with the string representation of a Dictionary.
7587 * May return NULL.
7588 */
7589 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007590dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593{
7594 garray_T ga;
7595 int first = TRUE;
7596 char_u *tofree;
7597 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007601 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007603 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 return NULL;
7605 ga_init2(&ga, (int)sizeof(char), 80);
7606 ga_append(&ga, '{');
7607
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007608 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007611 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007613 --todo;
7614
7615 if (first)
7616 first = FALSE;
7617 else
7618 ga_concat(&ga, (char_u *)", ");
7619
7620 tofree = string_quote(hi->hi_key, FALSE);
7621 if (tofree != NULL)
7622 {
7623 ga_concat(&ga, tofree);
7624 vim_free(tofree);
7625 }
7626 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007627 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 if (s != NULL)
7629 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007631 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007632 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007633 line_breakcheck();
7634
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007637 if (todo > 0)
7638 {
7639 vim_free(ga.ga_data);
7640 return NULL;
7641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642
7643 ga_append(&ga, '}');
7644 ga_append(&ga, NUL);
7645 return (char_u *)ga.ga_data;
7646}
7647
7648/*
7649 * Allocate a variable for a Dictionary and fill it from "*arg".
7650 * Return OK or FAIL. Returns NOTDONE for {expr}.
7651 */
7652 static int
7653get_dict_tv(arg, rettv, evaluate)
7654 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007655 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 int evaluate;
7657{
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 dict_T *d = NULL;
7659 typval_T tvkey;
7660 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007661 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665
7666 /*
7667 * First check if it's not a curly-braces thing: {expr}.
7668 * Must do this without evaluating, otherwise a function may be called
7669 * twice. Unfortunately this means we need to call eval1() twice for the
7670 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 if (*start != '}')
7674 {
7675 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7676 return FAIL;
7677 if (*start == '}')
7678 return NOTDONE;
7679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680
7681 if (evaluate)
7682 {
7683 d = dict_alloc();
7684 if (d == NULL)
7685 return FAIL;
7686 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687 tvkey.v_type = VAR_UNKNOWN;
7688 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689
7690 *arg = skipwhite(*arg + 1);
7691 while (**arg != '}' && **arg != NUL)
7692 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 goto failret;
7695 if (**arg != ':')
7696 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007697 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 goto failret;
7700 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007701 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007703 key = get_tv_string_buf_chk(&tvkey, buf);
7704 if (key == NULL || *key == NUL)
7705 {
7706 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7707 if (key != NULL)
7708 EMSG(_(e_emptykey));
7709 clear_tv(&tvkey);
7710 goto failret;
7711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713
7714 *arg = skipwhite(*arg + 1);
7715 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7716 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007717 if (evaluate)
7718 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 goto failret;
7720 }
7721 if (evaluate)
7722 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007723 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 if (item != NULL)
7725 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007726 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007727 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 clear_tv(&tv);
7729 goto failret;
7730 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007731 item = dictitem_alloc(key);
7732 clear_tv(&tvkey);
7733 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007736 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007737 if (dict_add(d, item) == FAIL)
7738 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 }
7740 }
7741
7742 if (**arg == '}')
7743 break;
7744 if (**arg != ',')
7745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007746 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 goto failret;
7748 }
7749 *arg = skipwhite(*arg + 1);
7750 }
7751
7752 if (**arg != '}')
7753 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007754 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755failret:
7756 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007757 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 return FAIL;
7759 }
7760
7761 *arg = skipwhite(*arg + 1);
7762 if (evaluate)
7763 {
7764 rettv->v_type = VAR_DICT;
7765 rettv->vval.v_dict = d;
7766 ++d->dv_refcount;
7767 }
7768
7769 return OK;
7770}
7771
Bram Moolenaar8c711452005-01-14 21:53:12 +00007772/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 * Return a string with the string representation of a variable.
7774 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007775 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007776 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007777 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007778 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007779 */
7780 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007781echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007783 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007784 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007785 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007786{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007787 static int recurse = 0;
7788 char_u *r = NULL;
7789
Bram Moolenaar33570922005-01-25 22:26:29 +00007790 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007791 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007792 if (!did_echo_string_emsg)
7793 {
7794 /* Only give this message once for a recursive call to avoid
7795 * flooding the user with errors. And stop iterating over lists
7796 * and dicts. */
7797 did_echo_string_emsg = TRUE;
7798 EMSG(_("E724: variable nested too deep for displaying"));
7799 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007801 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 }
7803 ++recurse;
7804
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007805 switch (tv->v_type)
7806 {
7807 case VAR_FUNC:
7808 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007809 r = tv->vval.v_string;
7810 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007811
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007813 if (tv->vval.v_list == NULL)
7814 {
7815 *tofree = NULL;
7816 r = NULL;
7817 }
7818 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7819 {
7820 *tofree = NULL;
7821 r = (char_u *)"[...]";
7822 }
7823 else
7824 {
7825 tv->vval.v_list->lv_copyID = copyID;
7826 *tofree = list2string(tv, copyID);
7827 r = *tofree;
7828 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007830
Bram Moolenaar8c711452005-01-14 21:53:12 +00007831 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007832 if (tv->vval.v_dict == NULL)
7833 {
7834 *tofree = NULL;
7835 r = NULL;
7836 }
7837 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7838 {
7839 *tofree = NULL;
7840 r = (char_u *)"{...}";
7841 }
7842 else
7843 {
7844 tv->vval.v_dict->dv_copyID = copyID;
7845 *tofree = dict2string(tv, copyID);
7846 r = *tofree;
7847 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007848 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 case VAR_STRING:
7851 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007852 *tofree = NULL;
7853 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007854 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007855
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007856#ifdef FEAT_FLOAT
7857 case VAR_FLOAT:
7858 *tofree = NULL;
7859 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7860 r = numbuf;
7861 break;
7862#endif
7863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007864 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007865 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007866 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868
Bram Moolenaar8502c702014-06-17 12:51:16 +02007869 if (--recurse == 0)
7870 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007871 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007872}
7873
7874/*
7875 * Return a string with the string representation of a variable.
7876 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7877 * "numbuf" is used for a number.
7878 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007879 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880 */
7881 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007883 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884 char_u **tofree;
7885 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007886 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887{
7888 switch (tv->v_type)
7889 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 case VAR_FUNC:
7891 *tofree = string_quote(tv->vval.v_string, TRUE);
7892 return *tofree;
7893 case VAR_STRING:
7894 *tofree = string_quote(tv->vval.v_string, FALSE);
7895 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896#ifdef FEAT_FLOAT
7897 case VAR_FLOAT:
7898 *tofree = NULL;
7899 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7900 return numbuf;
7901#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007903 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007904 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007907 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910}
7911
7912/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 * Return string "str" in ' quotes, doubling ' characters.
7914 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007915 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 */
7917 static char_u *
7918string_quote(str, function)
7919 char_u *str;
7920 int function;
7921{
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923 char_u *p, *r, *s;
7924
Bram Moolenaar33570922005-01-25 22:26:29 +00007925 len = (function ? 13 : 3);
7926 if (str != NULL)
7927 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007928 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007929 for (p = str; *p != NUL; mb_ptr_adv(p))
7930 if (*p == '\'')
7931 ++len;
7932 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 s = r = alloc(len);
7934 if (r != NULL)
7935 {
7936 if (function)
7937 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 r += 10;
7940 }
7941 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 if (str != NULL)
7944 for (p = str; *p != NUL; )
7945 {
7946 if (*p == '\'')
7947 *r++ = '\'';
7948 MB_COPY_CHAR(p, r);
7949 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007951 if (function)
7952 *r++ = ')';
7953 *r++ = NUL;
7954 }
7955 return s;
7956}
7957
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007958#ifdef FEAT_FLOAT
7959/*
7960 * Convert the string "text" to a floating point number.
7961 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7962 * this always uses a decimal point.
7963 * Returns the length of the text that was consumed.
7964 */
7965 static int
7966string2float(text, value)
7967 char_u *text;
7968 float_T *value; /* result stored here */
7969{
7970 char *s = (char *)text;
7971 float_T f;
7972
7973 f = strtod(s, &s);
7974 *value = f;
7975 return (int)((char_u *)s - text);
7976}
7977#endif
7978
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 * Get the value of an environment variable.
7981 * "arg" is pointing to the '$'. It is advanced to after the name.
7982 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007983 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 */
7985 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007986get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 int evaluate;
7990{
7991 char_u *string = NULL;
7992 int len;
7993 int cc;
7994 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007995 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
7997 ++*arg;
7998 name = *arg;
7999 len = get_env_len(arg);
8000 if (evaluate)
8001 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008002 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008003 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008004
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008005 cc = name[len];
8006 name[len] = NUL;
8007 /* first try vim_getenv(), fast for normal environment vars */
8008 string = vim_getenv(name, &mustfree);
8009 if (string != NULL && *string != NUL)
8010 {
8011 if (!mustfree)
8012 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008014 else
8015 {
8016 if (mustfree)
8017 vim_free(string);
8018
8019 /* next try expanding things like $VIM and ${HOME} */
8020 string = expand_env_save(name - 1);
8021 if (string != NULL && *string == '$')
8022 {
8023 vim_free(string);
8024 string = NULL;
8025 }
8026 }
8027 name[len] = cc;
8028
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029 rettv->v_type = VAR_STRING;
8030 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 }
8032
8033 return OK;
8034}
8035
8036/*
8037 * Array with names and number of arguments of all internal functions
8038 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8039 */
8040static struct fst
8041{
8042 char *f_name; /* function name */
8043 char f_min_argc; /* minimal number of arguments */
8044 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008046 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047} functions[] =
8048{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008049#ifdef FEAT_FLOAT
8050 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008051 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008052#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008053 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008054 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {"append", 2, 2, f_append},
8056 {"argc", 0, 0, f_argc},
8057 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008058 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008059 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008060#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008061 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008062 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008063 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008066 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"bufexists", 1, 1, f_bufexists},
8068 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8069 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8070 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8071 {"buflisted", 1, 1, f_buflisted},
8072 {"bufloaded", 1, 1, f_bufloaded},
8073 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008074 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"bufwinnr", 1, 1, f_bufwinnr},
8076 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008077 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008078 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008079 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080#ifdef FEAT_FLOAT
8081 {"ceil", 1, 1, f_ceil},
8082#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008083 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008084 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008086 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008088#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008089 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008090 {"complete_add", 1, 1, f_complete_add},
8091 {"complete_check", 0, 0, f_complete_check},
8092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008095#ifdef FEAT_FLOAT
8096 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008097 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008098#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008099 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008101 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008102 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"delete", 1, 1, f_delete},
8104 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008105 {"diff_filler", 1, 1, f_diff_filler},
8106 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008107 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008109 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"eventhandler", 0, 0, f_eventhandler},
8111 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008112 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008114#ifdef FEAT_FLOAT
8115 {"exp", 1, 1, f_exp},
8116#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008117 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008118 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008119 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8121 {"filereadable", 1, 1, f_filereadable},
8122 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008123 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008124 {"finddir", 1, 3, f_finddir},
8125 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008126#ifdef FEAT_FLOAT
8127 {"float2nr", 1, 1, f_float2nr},
8128 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008129 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008130#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008131 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"fnamemodify", 2, 2, f_fnamemodify},
8133 {"foldclosed", 1, 1, f_foldclosed},
8134 {"foldclosedend", 1, 1, f_foldclosedend},
8135 {"foldlevel", 1, 1, f_foldlevel},
8136 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008137 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008139 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008140 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008141 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008142 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008143 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"getchar", 0, 1, f_getchar},
8145 {"getcharmod", 0, 0, f_getcharmod},
8146 {"getcmdline", 0, 0, f_getcmdline},
8147 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008148 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008149 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008150 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008152 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008153 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"getfsize", 1, 1, f_getfsize},
8155 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008156 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008157 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008158 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008159 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008160 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008161 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008162 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008163 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008165 {"gettabvar", 2, 3, f_gettabvar},
8166 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"getwinposx", 0, 0, f_getwinposx},
8168 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008169 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008170 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008171 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008172 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008174 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008175 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008176 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8178 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8179 {"histadd", 2, 2, f_histadd},
8180 {"histdel", 1, 2, f_histdel},
8181 {"histget", 1, 2, f_histget},
8182 {"histnr", 1, 1, f_histnr},
8183 {"hlID", 1, 1, f_hlID},
8184 {"hlexists", 1, 1, f_hlexists},
8185 {"hostname", 0, 0, f_hostname},
8186 {"iconv", 3, 3, f_iconv},
8187 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008188 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008189 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008191 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"inputrestore", 0, 0, f_inputrestore},
8193 {"inputsave", 0, 0, f_inputsave},
8194 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008195 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008196 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008198 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008199 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008200 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008201 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008203 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"libcall", 3, 3, f_libcall},
8205 {"libcallnr", 3, 3, f_libcallnr},
8206 {"line", 1, 1, f_line},
8207 {"line2byte", 1, 1, f_line2byte},
8208 {"lispindent", 1, 1, f_lispindent},
8209 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008210#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008211 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008212 {"log10", 1, 1, f_log10},
8213#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008214#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008215 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008216#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008217 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008218 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008219 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008220 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008221 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008222 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008223 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008224 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008225 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008226 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008227 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008228 {"max", 1, 1, f_max},
8229 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008230#ifdef vim_mkdir
8231 {"mkdir", 1, 3, f_mkdir},
8232#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008233 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008234#ifdef FEAT_MZSCHEME
8235 {"mzeval", 1, 1, f_mzeval},
8236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008238 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008239 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008240 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008241#ifdef FEAT_FLOAT
8242 {"pow", 2, 2, f_pow},
8243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008245 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008246 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008247#ifdef FEAT_PYTHON3
8248 {"py3eval", 1, 1, f_py3eval},
8249#endif
8250#ifdef FEAT_PYTHON
8251 {"pyeval", 1, 1, f_pyeval},
8252#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008253 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008254 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008255 {"reltime", 0, 2, f_reltime},
8256 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {"remote_expr", 2, 3, f_remote_expr},
8258 {"remote_foreground", 1, 1, f_remote_foreground},
8259 {"remote_peek", 1, 2, f_remote_peek},
8260 {"remote_read", 1, 1, f_remote_read},
8261 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008262 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008264 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008266 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008267#ifdef FEAT_FLOAT
8268 {"round", 1, 1, f_round},
8269#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008270 {"screenattr", 2, 2, f_screenattr},
8271 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008272 {"screencol", 0, 0, f_screencol},
8273 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008274 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008275 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008276 {"searchpair", 3, 7, f_searchpair},
8277 {"searchpairpos", 3, 7, f_searchpairpos},
8278 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"server2client", 2, 2, f_server2client},
8280 {"serverlist", 0, 0, f_serverlist},
8281 {"setbufvar", 3, 3, f_setbufvar},
8282 {"setcmdpos", 1, 1, f_setcmdpos},
8283 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008284 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008285 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008286 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008287 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008289 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008290 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008292#ifdef FEAT_CRYPT
8293 {"sha256", 1, 1, f_sha256},
8294#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008295 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008296 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298#ifdef FEAT_FLOAT
8299 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008300 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008301#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008302 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008303 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008304 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008305 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008306 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008307#ifdef FEAT_FLOAT
8308 {"sqrt", 1, 1, f_sqrt},
8309 {"str2float", 1, 1, f_str2float},
8310#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008311 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008312 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008313 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314#ifdef HAVE_STRFTIME
8315 {"strftime", 1, 2, f_strftime},
8316#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"strlen", 1, 1, f_strlen},
8320 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008321 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008323 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008324 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"substitute", 4, 4, f_substitute},
8326 {"synID", 3, 3, f_synID},
8327 {"synIDattr", 2, 3, f_synIDattr},
8328 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008329 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008330 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008331 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008332 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008333 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008334 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008335 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008336 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008337 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008338#ifdef FEAT_FLOAT
8339 {"tan", 1, 1, f_tan},
8340 {"tanh", 1, 1, f_tanh},
8341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008343 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {"tolower", 1, 1, f_tolower},
8345 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008346 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347#ifdef FEAT_FLOAT
8348 {"trunc", 1, 1, f_trunc},
8349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008351 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008352 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008353 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008354 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"virtcol", 1, 1, f_virtcol},
8356 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008357 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"winbufnr", 1, 1, f_winbufnr},
8359 {"wincol", 0, 0, f_wincol},
8360 {"winheight", 1, 1, f_winheight},
8361 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008362 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008364 {"winrestview", 1, 1, f_winrestview},
8365 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008367 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008368 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369};
8370
8371#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8372
8373/*
8374 * Function given to ExpandGeneric() to obtain the list of internal
8375 * or user defined function names.
8376 */
8377 char_u *
8378get_function_name(xp, idx)
8379 expand_T *xp;
8380 int idx;
8381{
8382 static int intidx = -1;
8383 char_u *name;
8384
8385 if (idx == 0)
8386 intidx = -1;
8387 if (intidx < 0)
8388 {
8389 name = get_user_func_name(xp, idx);
8390 if (name != NULL)
8391 return name;
8392 }
8393 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8394 {
8395 STRCPY(IObuff, functions[intidx].f_name);
8396 STRCAT(IObuff, "(");
8397 if (functions[intidx].f_max_argc == 0)
8398 STRCAT(IObuff, ")");
8399 return IObuff;
8400 }
8401
8402 return NULL;
8403}
8404
8405/*
8406 * Function given to ExpandGeneric() to obtain the list of internal or
8407 * user defined variable or function names.
8408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 char_u *
8410get_expr_name(xp, idx)
8411 expand_T *xp;
8412 int idx;
8413{
8414 static int intidx = -1;
8415 char_u *name;
8416
8417 if (idx == 0)
8418 intidx = -1;
8419 if (intidx < 0)
8420 {
8421 name = get_function_name(xp, idx);
8422 if (name != NULL)
8423 return name;
8424 }
8425 return get_user_var_name(xp, ++intidx);
8426}
8427
8428#endif /* FEAT_CMDL_COMPL */
8429
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008430#if defined(EBCDIC) || defined(PROTO)
8431/*
8432 * Compare struct fst by function name.
8433 */
8434 static int
8435compare_func_name(s1, s2)
8436 const void *s1;
8437 const void *s2;
8438{
8439 struct fst *p1 = (struct fst *)s1;
8440 struct fst *p2 = (struct fst *)s2;
8441
8442 return STRCMP(p1->f_name, p2->f_name);
8443}
8444
8445/*
8446 * Sort the function table by function name.
8447 * The sorting of the table above is ASCII dependant.
8448 * On machines using EBCDIC we have to sort it.
8449 */
8450 static void
8451sortFunctions()
8452{
8453 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8454
8455 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8456}
8457#endif
8458
8459
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460/*
8461 * Find internal function in table above.
8462 * Return index, or -1 if not found
8463 */
8464 static int
8465find_internal_func(name)
8466 char_u *name; /* name of the function */
8467{
8468 int first = 0;
8469 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8470 int cmp;
8471 int x;
8472
8473 /*
8474 * Find the function name in the table. Binary search.
8475 */
8476 while (first <= last)
8477 {
8478 x = first + ((unsigned)(last - first) >> 1);
8479 cmp = STRCMP(name, functions[x].f_name);
8480 if (cmp < 0)
8481 last = x - 1;
8482 else if (cmp > 0)
8483 first = x + 1;
8484 else
8485 return x;
8486 }
8487 return -1;
8488}
8489
8490/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008491 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8492 * name it contains, otherwise return "name".
8493 */
8494 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008495deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008496 char_u *name;
8497 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008498 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499{
Bram Moolenaar33570922005-01-25 22:26:29 +00008500 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 int cc;
8502
8503 cc = name[*lenp];
8504 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008505 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008506 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008507 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008509 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008510 {
8511 *lenp = 0;
8512 return (char_u *)""; /* just in case */
8513 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008514 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 }
8517
8518 return name;
8519}
8520
8521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 * Allocate a variable for the result of a function.
8523 * Return OK or FAIL.
8524 */
8525 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008526get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8527 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 char_u *name; /* name of the function */
8529 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 char_u **arg; /* argument, pointing to the '(' */
8532 linenr_T firstline; /* first line of range */
8533 linenr_T lastline; /* last line of range */
8534 int *doesrange; /* return: function handled range */
8535 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 char_u *argp;
8539 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008540 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 int argcount = 0; /* number of arguments found */
8542
8543 /*
8544 * Get the arguments.
8545 */
8546 argp = *arg;
8547 while (argcount < MAX_FUNC_ARGS)
8548 {
8549 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8550 if (*argp == ')' || *argp == ',' || *argp == NUL)
8551 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8553 {
8554 ret = FAIL;
8555 break;
8556 }
8557 ++argcount;
8558 if (*argp != ',')
8559 break;
8560 }
8561 if (*argp == ')')
8562 ++argp;
8563 else
8564 ret = FAIL;
8565
8566 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008568 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008570 {
8571 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008572 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008574 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576
8577 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
8580 *arg = skipwhite(argp);
8581 return ret;
8582}
8583
8584
8585/*
8586 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008587 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008588 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 */
8590 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008591call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008592 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008593 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008597 typval_T *argvars; /* vars for arguments, must have "argcount"
8598 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 linenr_T firstline; /* first line of range */
8600 linenr_T lastline; /* last line of range */
8601 int *doesrange; /* return: function handled range */
8602 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606#define ERROR_UNKNOWN 0
8607#define ERROR_TOOMANY 1
8608#define ERROR_TOOFEW 2
8609#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008610#define ERROR_DICT 4
8611#define ERROR_NONE 5
8612#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 int error = ERROR_NONE;
8614 int i;
8615 int llen;
8616 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617#define FLEN_FIXED 40
8618 char_u fname_buf[FLEN_FIXED + 1];
8619 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008620 char_u *name;
8621
8622 /* Make a copy of the name, if it comes from a funcref variable it could
8623 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008624 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008625 if (name == NULL)
8626 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627
8628 /*
8629 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8630 * Change <SNR>123_name() to K_SNR 123_name().
8631 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 llen = eval_fname_script(name);
8634 if (llen > 0)
8635 {
8636 fname_buf[0] = K_SPECIAL;
8637 fname_buf[1] = KS_EXTRA;
8638 fname_buf[2] = (int)KE_SNR;
8639 i = 3;
8640 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8641 {
8642 if (current_SID <= 0)
8643 error = ERROR_SCRIPT;
8644 else
8645 {
8646 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8647 i = (int)STRLEN(fname_buf);
8648 }
8649 }
8650 if (i + STRLEN(name + llen) < FLEN_FIXED)
8651 {
8652 STRCPY(fname_buf + i, name + llen);
8653 fname = fname_buf;
8654 }
8655 else
8656 {
8657 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8658 if (fname == NULL)
8659 error = ERROR_OTHER;
8660 else
8661 {
8662 mch_memmove(fname, fname_buf, (size_t)i);
8663 STRCPY(fname + i, name + llen);
8664 }
8665 }
8666 }
8667 else
8668 fname = name;
8669
8670 *doesrange = FALSE;
8671
8672
8673 /* execute the function if no errors detected and executing */
8674 if (evaluate && error == ERROR_NONE)
8675 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008676 char_u *rfname = fname;
8677
8678 /* Ignore "g:" before a function name. */
8679 if (fname[0] == 'g' && fname[1] == ':')
8680 rfname = fname + 2;
8681
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008682 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8683 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 error = ERROR_UNKNOWN;
8685
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008686 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 {
8688 /*
8689 * User defined function.
8690 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008691 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008692
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008694 /* Trigger FuncUndefined event, may load the function. */
8695 if (fp == NULL
8696 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008697 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008698 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008700 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008701 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 }
8703#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008704 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008705 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008706 {
8707 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008708 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008709 }
8710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 if (fp != NULL)
8712 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008713 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008715 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008717 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008719 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008720 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 else
8722 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008723 int did_save_redo = FALSE;
8724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 /*
8726 * Call the user function.
8727 * Save and restore search patterns, script variables and
8728 * redo buffer.
8729 */
8730 save_search_patterns();
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008731 if (!ins_compl_active())
8732 {
8733 saveRedobuff();
8734 did_save_redo = TRUE;
8735 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008736 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008738 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008739 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8740 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8741 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008742 /* Function was unreferenced while being used, free it
8743 * now. */
8744 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008745 if (did_save_redo)
8746 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 restore_search_patterns();
8748 error = ERROR_NONE;
8749 }
8750 }
8751 }
8752 else
8753 {
8754 /*
8755 * Find the function name in the table, call its implementation.
8756 */
8757 i = find_internal_func(fname);
8758 if (i >= 0)
8759 {
8760 if (argcount < functions[i].f_min_argc)
8761 error = ERROR_TOOFEW;
8762 else if (argcount > functions[i].f_max_argc)
8763 error = ERROR_TOOMANY;
8764 else
8765 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008766 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 error = ERROR_NONE;
8769 }
8770 }
8771 }
8772 /*
8773 * The function call (or "FuncUndefined" autocommand sequence) might
8774 * have been aborted by an error, an interrupt, or an explicitly thrown
8775 * exception that has not been caught so far. This situation can be
8776 * tested for by calling aborting(). For an error in an internal
8777 * function or for the "E132" error in call_user_func(), however, the
8778 * throw point at which the "force_abort" flag (temporarily reset by
8779 * emsg()) is normally updated has not been reached yet. We need to
8780 * update that flag first to make aborting() reliable.
8781 */
8782 update_force_abort();
8783 }
8784 if (error == ERROR_NONE)
8785 ret = OK;
8786
8787 /*
8788 * Report an error unless the argument evaluation or function call has been
8789 * cancelled due to an aborting error, an interrupt, or an exception.
8790 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008791 if (!aborting())
8792 {
8793 switch (error)
8794 {
8795 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008796 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008797 break;
8798 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008799 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008800 break;
8801 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008802 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008803 name);
8804 break;
8805 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008806 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008807 name);
8808 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008809 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008810 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008811 name);
8812 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008813 }
8814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (fname != name && fname != fname_buf)
8817 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008818 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819
8820 return ret;
8821}
8822
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008823/*
8824 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008825 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008826 */
8827 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008828emsg_funcname(ermsg, name)
8829 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008830 char_u *name;
8831{
8832 char_u *p;
8833
8834 if (*name == K_SPECIAL)
8835 p = concat_str((char_u *)"<SNR>", name + 3);
8836 else
8837 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008838 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008839 if (p != name)
8840 vim_free(p);
8841}
8842
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008843/*
8844 * Return TRUE for a non-zero Number and a non-empty String.
8845 */
8846 static int
8847non_zero_arg(argvars)
8848 typval_T *argvars;
8849{
8850 return ((argvars[0].v_type == VAR_NUMBER
8851 && argvars[0].vval.v_number != 0)
8852 || (argvars[0].v_type == VAR_STRING
8853 && argvars[0].vval.v_string != NULL
8854 && *argvars[0].vval.v_string != NUL));
8855}
8856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857/*********************************************
8858 * Implementation of the built-in functions
8859 */
8860
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008861#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008862static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8863
8864/*
8865 * Get the float value of "argvars[0]" into "f".
8866 * Returns FAIL when the argument is not a Number or Float.
8867 */
8868 static int
8869get_float_arg(argvars, f)
8870 typval_T *argvars;
8871 float_T *f;
8872{
8873 if (argvars[0].v_type == VAR_FLOAT)
8874 {
8875 *f = argvars[0].vval.v_float;
8876 return OK;
8877 }
8878 if (argvars[0].v_type == VAR_NUMBER)
8879 {
8880 *f = (float_T)argvars[0].vval.v_number;
8881 return OK;
8882 }
8883 EMSG(_("E808: Number or Float required"));
8884 return FAIL;
8885}
8886
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008887/*
8888 * "abs(expr)" function
8889 */
8890 static void
8891f_abs(argvars, rettv)
8892 typval_T *argvars;
8893 typval_T *rettv;
8894{
8895 if (argvars[0].v_type == VAR_FLOAT)
8896 {
8897 rettv->v_type = VAR_FLOAT;
8898 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8899 }
8900 else
8901 {
8902 varnumber_T n;
8903 int error = FALSE;
8904
8905 n = get_tv_number_chk(&argvars[0], &error);
8906 if (error)
8907 rettv->vval.v_number = -1;
8908 else if (n > 0)
8909 rettv->vval.v_number = n;
8910 else
8911 rettv->vval.v_number = -n;
8912 }
8913}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008914
8915/*
8916 * "acos()" function
8917 */
8918 static void
8919f_acos(argvars, rettv)
8920 typval_T *argvars;
8921 typval_T *rettv;
8922{
8923 float_T f;
8924
8925 rettv->v_type = VAR_FLOAT;
8926 if (get_float_arg(argvars, &f) == OK)
8927 rettv->vval.v_float = acos(f);
8928 else
8929 rettv->vval.v_float = 0.0;
8930}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008931#endif
8932
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008934 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 */
8936 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008937f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008938 typval_T *argvars;
8939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008944 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008946 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008947 && !tv_check_lock(l->lv_lock,
8948 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008949 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008951 }
8952 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008953 EMSG(_(e_listreq));
8954}
8955
8956/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008957 * "and(expr, expr)" function
8958 */
8959 static void
8960f_and(argvars, rettv)
8961 typval_T *argvars;
8962 typval_T *rettv;
8963{
8964 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8965 & get_tv_number_chk(&argvars[1], NULL);
8966}
8967
8968/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008969 * "append(lnum, string/list)" function
8970 */
8971 static void
8972f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008973 typval_T *argvars;
8974 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008975{
8976 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008977 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 list_T *l = NULL;
8979 listitem_T *li = NULL;
8980 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008981 long added = 0;
8982
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008983 /* When coming here from Insert mode, sync undo, so that this can be
8984 * undone separately from what was previously inserted. */
8985 if (u_sync_once == 2)
8986 {
8987 u_sync_once = 1; /* notify that u_sync() was called */
8988 u_sync(TRUE);
8989 }
8990
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991 lnum = get_tv_lnum(argvars);
8992 if (lnum >= 0
8993 && lnum <= curbuf->b_ml.ml_line_count
8994 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008995 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008996 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008998 l = argvars[1].vval.v_list;
8999 if (l == NULL)
9000 return;
9001 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009002 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009003 for (;;)
9004 {
9005 if (l == NULL)
9006 tv = &argvars[1]; /* append a string */
9007 else if (li == NULL)
9008 break; /* end of list */
9009 else
9010 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 line = get_tv_string_chk(tv);
9012 if (line == NULL) /* type error */
9013 {
9014 rettv->vval.v_number = 1; /* Failed */
9015 break;
9016 }
9017 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009018 ++added;
9019 if (l == NULL)
9020 break;
9021 li = li->li_next;
9022 }
9023
9024 appended_lines_mark(lnum, added);
9025 if (curwin->w_cursor.lnum > lnum)
9026 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009028 else
9029 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030}
9031
9032/*
9033 * "argc()" function
9034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041}
9042
9043/*
9044 * "argidx()" function
9045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009048 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052}
9053
9054/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009055 * "arglistid()" function
9056 */
9057 static void
9058f_arglistid(argvars, rettv)
9059 typval_T *argvars UNUSED;
9060 typval_T *rettv;
9061{
9062 win_T *wp;
9063 tabpage_T *tp = NULL;
9064 long n;
9065
9066 rettv->vval.v_number = -1;
9067 if (argvars[0].v_type != VAR_UNKNOWN)
9068 {
9069 if (argvars[1].v_type != VAR_UNKNOWN)
9070 {
9071 n = get_tv_number(&argvars[1]);
9072 if (n >= 0)
9073 tp = find_tabpage(n);
9074 }
9075 else
9076 tp = curtab;
9077
9078 if (tp != NULL)
9079 {
9080 wp = find_win_by_nr(&argvars[0], tp);
9081 if (wp != NULL)
9082 rettv->vval.v_number = wp->w_alist->id;
9083 }
9084 }
9085 else
9086 rettv->vval.v_number = curwin->w_alist->id;
9087}
9088
9089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 * "argv(nr)" function
9091 */
9092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009094 typval_T *argvars;
9095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097 int idx;
9098
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009099 if (argvars[0].v_type != VAR_UNKNOWN)
9100 {
9101 idx = get_tv_number_chk(&argvars[0], NULL);
9102 if (idx >= 0 && idx < ARGCOUNT)
9103 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9104 else
9105 rettv->vval.v_string = NULL;
9106 rettv->v_type = VAR_STRING;
9107 }
9108 else if (rettv_list_alloc(rettv) == OK)
9109 for (idx = 0; idx < ARGCOUNT; ++idx)
9110 list_append_string(rettv->vval.v_list,
9111 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112}
9113
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009114#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009115/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009116 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009117 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009118 static void
9119f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009120 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009121 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009122{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009123 float_T f;
9124
9125 rettv->v_type = VAR_FLOAT;
9126 if (get_float_arg(argvars, &f) == OK)
9127 rettv->vval.v_float = asin(f);
9128 else
9129 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009130}
9131
9132/*
9133 * "atan()" function
9134 */
9135 static void
9136f_atan(argvars, rettv)
9137 typval_T *argvars;
9138 typval_T *rettv;
9139{
9140 float_T f;
9141
9142 rettv->v_type = VAR_FLOAT;
9143 if (get_float_arg(argvars, &f) == OK)
9144 rettv->vval.v_float = atan(f);
9145 else
9146 rettv->vval.v_float = 0.0;
9147}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009148
9149/*
9150 * "atan2()" function
9151 */
9152 static void
9153f_atan2(argvars, rettv)
9154 typval_T *argvars;
9155 typval_T *rettv;
9156{
9157 float_T fx, fy;
9158
9159 rettv->v_type = VAR_FLOAT;
9160 if (get_float_arg(argvars, &fx) == OK
9161 && get_float_arg(&argvars[1], &fy) == OK)
9162 rettv->vval.v_float = atan2(fx, fy);
9163 else
9164 rettv->vval.v_float = 0.0;
9165}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009166#endif
9167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168/*
9169 * "browse(save, title, initdir, default)" function
9170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009173 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176#ifdef FEAT_BROWSE
9177 int save;
9178 char_u *title;
9179 char_u *initdir;
9180 char_u *defname;
9181 char_u buf[NUMBUFLEN];
9182 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 save = get_tv_number_chk(&argvars[0], &error);
9186 title = get_tv_string_chk(&argvars[1]);
9187 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9188 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009190 if (error || title == NULL || initdir == NULL || defname == NULL)
9191 rettv->vval.v_string = NULL;
9192 else
9193 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009194 do_browse(save ? BROWSE_SAVE : 0,
9195 title, defname, NULL, initdir, NULL, curbuf);
9196#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009198#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009200}
9201
9202/*
9203 * "browsedir(title, initdir)" function
9204 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009207 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009209{
9210#ifdef FEAT_BROWSE
9211 char_u *title;
9212 char_u *initdir;
9213 char_u buf[NUMBUFLEN];
9214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009215 title = get_tv_string_chk(&argvars[0]);
9216 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009218 if (title == NULL || initdir == NULL)
9219 rettv->vval.v_string = NULL;
9220 else
9221 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009222 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228
Bram Moolenaar33570922005-01-25 22:26:29 +00009229static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009230
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231/*
9232 * Find a buffer by number or exact name.
9233 */
9234 static buf_T *
9235find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009236 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009240 if (avar->v_type == VAR_NUMBER)
9241 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009242 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009244 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009245 if (buf == NULL)
9246 {
9247 /* No full path name match, try a match with a URL or a "nofile"
9248 * buffer, these don't use the full path. */
9249 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9250 if (buf->b_fname != NULL
9251 && (path_with_url(buf->b_fname)
9252#ifdef FEAT_QUICKFIX
9253 || bt_nofile(buf)
9254#endif
9255 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009256 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009257 break;
9258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 }
9260 return buf;
9261}
9262
9263/*
9264 * "bufexists(expr)" function
9265 */
9266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009267f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009268 typval_T *argvars;
9269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009271 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272}
9273
9274/*
9275 * "buflisted(expr)" function
9276 */
9277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009279 typval_T *argvars;
9280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281{
9282 buf_T *buf;
9283
9284 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009285 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286}
9287
9288/*
9289 * "bufloaded(expr)" function
9290 */
9291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009293 typval_T *argvars;
9294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
9296 buf_T *buf;
9297
9298 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009299 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300}
9301
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009302static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009303
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304/*
9305 * Get buffer by number or pattern.
9306 */
9307 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009308get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009309 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009310 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 int save_magic;
9314 char_u *save_cpo;
9315 buf_T *buf;
9316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317 if (tv->v_type == VAR_NUMBER)
9318 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009319 if (tv->v_type != VAR_STRING)
9320 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 if (name == NULL || *name == NUL)
9322 return curbuf;
9323 if (name[0] == '$' && name[1] == NUL)
9324 return lastbuf;
9325
9326 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9327 save_magic = p_magic;
9328 p_magic = TRUE;
9329 save_cpo = p_cpo;
9330 p_cpo = (char_u *)"";
9331
9332 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009333 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
9335 p_magic = save_magic;
9336 p_cpo = save_cpo;
9337
9338 /* If not found, try expanding the name, like done for bufexists(). */
9339 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
9342 return buf;
9343}
9344
9345/*
9346 * "bufname(expr)" function
9347 */
9348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009350 typval_T *argvars;
9351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353 buf_T *buf;
9354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009355 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009357 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009360 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 --emsg_off;
9364}
9365
9366/*
9367 * "bufnr(expr)" function
9368 */
9369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009371 typval_T *argvars;
9372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373{
9374 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009375 int error = FALSE;
9376 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009380 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009381 --emsg_off;
9382
9383 /* If the buffer isn't found and the second argument is not zero create a
9384 * new buffer. */
9385 if (buf == NULL
9386 && argvars[1].v_type != VAR_UNKNOWN
9387 && get_tv_number_chk(&argvars[1], &error) != 0
9388 && !error
9389 && (name = get_tv_string_chk(&argvars[0])) != NULL
9390 && !error)
9391 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9392
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397}
9398
9399/*
9400 * "bufwinnr(nr)" function
9401 */
9402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009403f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009404 typval_T *argvars;
9405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406{
9407#ifdef FEAT_WINDOWS
9408 win_T *wp;
9409 int winnr = 0;
9410#endif
9411 buf_T *buf;
9412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009413 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009415 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416#ifdef FEAT_WINDOWS
9417 for (wp = firstwin; wp; wp = wp->w_next)
9418 {
9419 ++winnr;
9420 if (wp->w_buffer == buf)
9421 break;
9422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#endif
9427 --emsg_off;
9428}
9429
9430/*
9431 * "byte2line(byte)" function
9432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009435 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
9438#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009439 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440#else
9441 long boff = 0;
9442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009443 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009447 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 (linenr_T)0, &boff);
9449#endif
9450}
9451
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009452 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009453byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009454 typval_T *argvars;
9455 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009456 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009457{
9458#ifdef FEAT_MBYTE
9459 char_u *t;
9460#endif
9461 char_u *str;
9462 long idx;
9463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009464 str = get_tv_string_chk(&argvars[0]);
9465 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009468 return;
9469
9470#ifdef FEAT_MBYTE
9471 t = str;
9472 for ( ; idx > 0; idx--)
9473 {
9474 if (*t == NUL) /* EOL reached */
9475 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009476 if (enc_utf8 && comp)
9477 t += utf_ptr2len(t);
9478 else
9479 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009480 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009481 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009482#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009483 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009485#endif
9486}
9487
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009488/*
9489 * "byteidx()" function
9490 */
9491 static void
9492f_byteidx(argvars, rettv)
9493 typval_T *argvars;
9494 typval_T *rettv;
9495{
9496 byteidx(argvars, rettv, FALSE);
9497}
9498
9499/*
9500 * "byteidxcomp()" function
9501 */
9502 static void
9503f_byteidxcomp(argvars, rettv)
9504 typval_T *argvars;
9505 typval_T *rettv;
9506{
9507 byteidx(argvars, rettv, TRUE);
9508}
9509
Bram Moolenaardb913952012-06-29 12:54:53 +02009510 int
9511func_call(name, args, selfdict, rettv)
9512 char_u *name;
9513 typval_T *args;
9514 dict_T *selfdict;
9515 typval_T *rettv;
9516{
9517 listitem_T *item;
9518 typval_T argv[MAX_FUNC_ARGS + 1];
9519 int argc = 0;
9520 int dummy;
9521 int r = 0;
9522
9523 for (item = args->vval.v_list->lv_first; item != NULL;
9524 item = item->li_next)
9525 {
9526 if (argc == MAX_FUNC_ARGS)
9527 {
9528 EMSG(_("E699: Too many arguments"));
9529 break;
9530 }
9531 /* Make a copy of each argument. This is needed to be able to set
9532 * v_lock to VAR_FIXED in the copy without changing the original list.
9533 */
9534 copy_tv(&item->li_tv, &argv[argc++]);
9535 }
9536
9537 if (item == NULL)
9538 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9539 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9540 &dummy, TRUE, selfdict);
9541
9542 /* Free the arguments. */
9543 while (argc > 0)
9544 clear_tv(&argv[--argc]);
9545
9546 return r;
9547}
9548
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009549/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009550 * "call(func, arglist)" function
9551 */
9552 static void
9553f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009554 typval_T *argvars;
9555 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009556{
9557 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009558 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009559
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009560 if (argvars[1].v_type != VAR_LIST)
9561 {
9562 EMSG(_(e_listreq));
9563 return;
9564 }
9565 if (argvars[1].vval.v_list == NULL)
9566 return;
9567
9568 if (argvars[0].v_type == VAR_FUNC)
9569 func = argvars[0].vval.v_string;
9570 else
9571 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 if (*func == NUL)
9573 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009574
Bram Moolenaare9a41262005-01-15 22:18:47 +00009575 if (argvars[2].v_type != VAR_UNKNOWN)
9576 {
9577 if (argvars[2].v_type != VAR_DICT)
9578 {
9579 EMSG(_(e_dictreq));
9580 return;
9581 }
9582 selfdict = argvars[2].vval.v_dict;
9583 }
9584
Bram Moolenaardb913952012-06-29 12:54:53 +02009585 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009586}
9587
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009588#ifdef FEAT_FLOAT
9589/*
9590 * "ceil({float})" function
9591 */
9592 static void
9593f_ceil(argvars, rettv)
9594 typval_T *argvars;
9595 typval_T *rettv;
9596{
9597 float_T f;
9598
9599 rettv->v_type = VAR_FLOAT;
9600 if (get_float_arg(argvars, &f) == OK)
9601 rettv->vval.v_float = ceil(f);
9602 else
9603 rettv->vval.v_float = 0.0;
9604}
9605#endif
9606
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009607/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009608 * "changenr()" function
9609 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009610 static void
9611f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009612 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009613 typval_T *rettv;
9614{
9615 rettv->vval.v_number = curbuf->b_u_seq_cur;
9616}
9617
9618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 * "char2nr(string)" function
9620 */
9621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009622f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009623 typval_T *argvars;
9624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
9626#ifdef FEAT_MBYTE
9627 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009628 {
9629 int utf8 = 0;
9630
9631 if (argvars[1].v_type != VAR_UNKNOWN)
9632 utf8 = get_tv_number_chk(&argvars[1], NULL);
9633
9634 if (utf8)
9635 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9636 else
9637 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 else
9640#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642}
9643
9644/*
9645 * "cindent(lnum)" function
9646 */
9647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009649 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652#ifdef FEAT_CINDENT
9653 pos_T pos;
9654 linenr_T lnum;
9655
9656 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9659 {
9660 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 curwin->w_cursor = pos;
9663 }
9664 else
9665#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667}
9668
9669/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009670 * "clearmatches()" function
9671 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009672 static void
9673f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009674 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009675 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009676{
9677#ifdef FEAT_SEARCH_EXTRA
9678 clear_matches(curwin);
9679#endif
9680}
9681
9682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 * "col(string)" function
9684 */
9685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009687 typval_T *argvars;
9688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
9690 colnr_T col = 0;
9691 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009692 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009694 fp = var2fpos(&argvars[0], FALSE, &fnum);
9695 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 {
9697 if (fp->col == MAXCOL)
9698 {
9699 /* '> can be MAXCOL, get the length of the line then */
9700 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009701 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 else
9703 col = MAXCOL;
9704 }
9705 else
9706 {
9707 col = fp->col + 1;
9708#ifdef FEAT_VIRTUALEDIT
9709 /* col(".") when the cursor is on the NUL at the end of the line
9710 * because of "coladd" can be seen as an extra column. */
9711 if (virtual_active() && fp == &curwin->w_cursor)
9712 {
9713 char_u *p = ml_get_cursor();
9714
9715 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9716 curwin->w_virtcol - curwin->w_cursor.coladd))
9717 {
9718# ifdef FEAT_MBYTE
9719 int l;
9720
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009721 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 col += l;
9723# else
9724 if (*p != NUL && p[1] == NUL)
9725 ++col;
9726# endif
9727 }
9728 }
9729#endif
9730 }
9731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733}
9734
Bram Moolenaar572cb562005-08-05 21:35:02 +00009735#if defined(FEAT_INS_EXPAND)
9736/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009737 * "complete()" function
9738 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009739 static void
9740f_complete(argvars, rettv)
9741 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009742 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009743{
9744 int startcol;
9745
9746 if ((State & INSERT) == 0)
9747 {
9748 EMSG(_("E785: complete() can only be used in Insert mode"));
9749 return;
9750 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009751
9752 /* Check for undo allowed here, because if something was already inserted
9753 * the line was already saved for undo and this check isn't done. */
9754 if (!undo_allowed())
9755 return;
9756
Bram Moolenaarade00832006-03-10 21:46:58 +00009757 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9758 {
9759 EMSG(_(e_invarg));
9760 return;
9761 }
9762
9763 startcol = get_tv_number_chk(&argvars[0], NULL);
9764 if (startcol <= 0)
9765 return;
9766
9767 set_completion(startcol - 1, argvars[1].vval.v_list);
9768}
9769
9770/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009771 * "complete_add()" function
9772 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009773 static void
9774f_complete_add(argvars, rettv)
9775 typval_T *argvars;
9776 typval_T *rettv;
9777{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009778 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009779}
9780
9781/*
9782 * "complete_check()" function
9783 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009784 static void
9785f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009786 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009787 typval_T *rettv;
9788{
9789 int saved = RedrawingDisabled;
9790
9791 RedrawingDisabled = 0;
9792 ins_compl_check_keys(0);
9793 rettv->vval.v_number = compl_interrupted;
9794 RedrawingDisabled = saved;
9795}
9796#endif
9797
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798/*
9799 * "confirm(message, buttons[, default [, type]])" function
9800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009803 typval_T *argvars UNUSED;
9804 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9807 char_u *message;
9808 char_u *buttons = NULL;
9809 char_u buf[NUMBUFLEN];
9810 char_u buf2[NUMBUFLEN];
9811 int def = 1;
9812 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 char_u *typestr;
9814 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 message = get_tv_string_chk(&argvars[0]);
9817 if (message == NULL)
9818 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009819 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009821 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9822 if (buttons == NULL)
9823 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009824 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009827 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009829 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9830 if (typestr == NULL)
9831 error = TRUE;
9832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 switch (TOUPPER_ASC(*typestr))
9835 {
9836 case 'E': type = VIM_ERROR; break;
9837 case 'Q': type = VIM_QUESTION; break;
9838 case 'I': type = VIM_INFO; break;
9839 case 'W': type = VIM_WARNING; break;
9840 case 'G': type = VIM_GENERIC; break;
9841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843 }
9844 }
9845 }
9846
9847 if (buttons == NULL || *buttons == NUL)
9848 buttons = (char_u *)_("&Ok");
9849
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009850 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009852 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853#endif
9854}
9855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009856/*
9857 * "copy()" function
9858 */
9859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009861 typval_T *argvars;
9862 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009863{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009864 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009865}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009867#ifdef FEAT_FLOAT
9868/*
9869 * "cos()" function
9870 */
9871 static void
9872f_cos(argvars, rettv)
9873 typval_T *argvars;
9874 typval_T *rettv;
9875{
9876 float_T f;
9877
9878 rettv->v_type = VAR_FLOAT;
9879 if (get_float_arg(argvars, &f) == OK)
9880 rettv->vval.v_float = cos(f);
9881 else
9882 rettv->vval.v_float = 0.0;
9883}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009884
9885/*
9886 * "cosh()" function
9887 */
9888 static void
9889f_cosh(argvars, rettv)
9890 typval_T *argvars;
9891 typval_T *rettv;
9892{
9893 float_T f;
9894
9895 rettv->v_type = VAR_FLOAT;
9896 if (get_float_arg(argvars, &f) == OK)
9897 rettv->vval.v_float = cosh(f);
9898 else
9899 rettv->vval.v_float = 0.0;
9900}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009901#endif
9902
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009904 * "count()" function
9905 */
9906 static void
9907f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 typval_T *argvars;
9909 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009910{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009911 long n = 0;
9912 int ic = FALSE;
9913
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009915 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009916 listitem_T *li;
9917 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009919
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 if ((l = argvars[0].vval.v_list) != NULL)
9921 {
9922 li = l->lv_first;
9923 if (argvars[2].v_type != VAR_UNKNOWN)
9924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 int error = FALSE;
9926
9927 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 if (argvars[3].v_type != VAR_UNKNOWN)
9929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009930 idx = get_tv_number_chk(&argvars[3], &error);
9931 if (!error)
9932 {
9933 li = list_find(l, idx);
9934 if (li == NULL)
9935 EMSGN(_(e_listidx), idx);
9936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009937 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 if (error)
9939 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009940 }
9941
9942 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009943 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009944 ++n;
9945 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009946 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009947 else if (argvars[0].v_type == VAR_DICT)
9948 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009949 int todo;
9950 dict_T *d;
9951 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009952
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009953 if ((d = argvars[0].vval.v_dict) != NULL)
9954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 int error = FALSE;
9956
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 if (argvars[2].v_type != VAR_UNKNOWN)
9958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009959 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 if (argvars[3].v_type != VAR_UNKNOWN)
9961 EMSG(_(e_invarg));
9962 }
9963
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009964 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009966 {
9967 if (!HASHITEM_EMPTY(hi))
9968 {
9969 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009970 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009971 ++n;
9972 }
9973 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 }
9975 }
9976 else
9977 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009978 rettv->vval.v_number = n;
9979}
9980
9981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9983 *
9984 * Checks the existence of a cscope connection.
9985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009988 typval_T *argvars UNUSED;
9989 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990{
9991#ifdef FEAT_CSCOPE
9992 int num = 0;
9993 char_u *dbpath = NULL;
9994 char_u *prepend = NULL;
9995 char_u buf[NUMBUFLEN];
9996
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009997 if (argvars[0].v_type != VAR_UNKNOWN
9998 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 num = (int)get_tv_number(&argvars[0]);
10001 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010002 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010003 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 }
10005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007#endif
10008}
10009
10010/*
10011 * "cursor(lnum, col)" function
10012 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010013 * Moves the cursor to the specified line and column.
10014 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010018 typval_T *argvars;
10019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020{
10021 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010022#ifdef FEAT_VIRTUALEDIT
10023 long coladd = 0;
10024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010026 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010027 if (argvars[1].v_type == VAR_UNKNOWN)
10028 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010029 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010030 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010031
Bram Moolenaar493c1782014-05-28 14:34:46 +020010032 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010033 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010034 line = pos.lnum;
10035 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010036#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010037 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010038#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010039 if (curswant >= 0)
10040 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010041 }
10042 else
10043 {
10044 line = get_tv_lnum(argvars);
10045 col = get_tv_number_chk(&argvars[1], NULL);
10046#ifdef FEAT_VIRTUALEDIT
10047 if (argvars[2].v_type != VAR_UNKNOWN)
10048 coladd = get_tv_number_chk(&argvars[2], NULL);
10049#endif
10050 }
10051 if (line < 0 || col < 0
10052#ifdef FEAT_VIRTUALEDIT
10053 || coladd < 0
10054#endif
10055 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010056 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 if (line > 0)
10058 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 if (col > 0)
10060 curwin->w_cursor.col = col - 1;
10061#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010062 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063#endif
10064
10065 /* Make sure the cursor is in a valid position. */
10066 check_cursor();
10067#ifdef FEAT_MBYTE
10068 /* Correct cursor for multi-byte character. */
10069 if (has_mbyte)
10070 mb_adjust_cursor();
10071#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010072
10073 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010074 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
10077/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010078 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 */
10080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010082 typval_T *argvars;
10083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010085 int noref = 0;
10086
10087 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010088 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010089 if (noref < 0 || noref > 1)
10090 EMSG(_(e_invarg));
10091 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010092 {
10093 current_copyID += COPYID_INC;
10094 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096}
10097
10098/*
10099 * "delete()" function
10100 */
10101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010103 typval_T *argvars;
10104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
10106 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110}
10111
10112/*
10113 * "did_filetype()" function
10114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010117 typval_T *argvars UNUSED;
10118 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
10120#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122#endif
10123}
10124
10125/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010126 * "diff_filler()" function
10127 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010130 typval_T *argvars UNUSED;
10131 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010132{
10133#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010135#endif
10136}
10137
10138/*
10139 * "diff_hlID()" function
10140 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010143 typval_T *argvars UNUSED;
10144 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010145{
10146#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010148 static linenr_T prev_lnum = 0;
10149 static int changedtick = 0;
10150 static int fnum = 0;
10151 static int change_start = 0;
10152 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010153 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010154 int filler_lines;
10155 int col;
10156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 if (lnum < 0) /* ignore type error in {lnum} arg */
10158 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010159 if (lnum != prev_lnum
10160 || changedtick != curbuf->b_changedtick
10161 || fnum != curbuf->b_fnum)
10162 {
10163 /* New line, buffer, change: need to get the values. */
10164 filler_lines = diff_check(curwin, lnum);
10165 if (filler_lines < 0)
10166 {
10167 if (filler_lines == -1)
10168 {
10169 change_start = MAXCOL;
10170 change_end = -1;
10171 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10172 hlID = HLF_ADD; /* added line */
10173 else
10174 hlID = HLF_CHD; /* changed line */
10175 }
10176 else
10177 hlID = HLF_ADD; /* added line */
10178 }
10179 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010180 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010181 prev_lnum = lnum;
10182 changedtick = curbuf->b_changedtick;
10183 fnum = curbuf->b_fnum;
10184 }
10185
10186 if (hlID == HLF_CHD || hlID == HLF_TXD)
10187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010188 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010189 if (col >= change_start && col <= change_end)
10190 hlID = HLF_TXD; /* changed text */
10191 else
10192 hlID = HLF_CHD; /* changed line */
10193 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010194 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010195#endif
10196}
10197
10198/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010199 * "empty({expr})" function
10200 */
10201 static void
10202f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010203 typval_T *argvars;
10204 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010205{
10206 int n;
10207
10208 switch (argvars[0].v_type)
10209 {
10210 case VAR_STRING:
10211 case VAR_FUNC:
10212 n = argvars[0].vval.v_string == NULL
10213 || *argvars[0].vval.v_string == NUL;
10214 break;
10215 case VAR_NUMBER:
10216 n = argvars[0].vval.v_number == 0;
10217 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010218#ifdef FEAT_FLOAT
10219 case VAR_FLOAT:
10220 n = argvars[0].vval.v_float == 0.0;
10221 break;
10222#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010223 case VAR_LIST:
10224 n = argvars[0].vval.v_list == NULL
10225 || argvars[0].vval.v_list->lv_first == NULL;
10226 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 case VAR_DICT:
10228 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010229 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010231 default:
10232 EMSG2(_(e_intern2), "f_empty()");
10233 n = 0;
10234 }
10235
10236 rettv->vval.v_number = n;
10237}
10238
10239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 * "escape({string}, {chars})" function
10241 */
10242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010243f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010244 typval_T *argvars;
10245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246{
10247 char_u buf[NUMBUFLEN];
10248
Bram Moolenaar758711c2005-02-02 23:11:38 +000010249 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10250 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252}
10253
10254/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010255 * "eval()" function
10256 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010257 static void
10258f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010259 typval_T *argvars;
10260 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010261{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010262 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010264 s = get_tv_string_chk(&argvars[0]);
10265 if (s != NULL)
10266 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010267
Bram Moolenaar615b9972015-01-14 17:15:05 +010010268 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010269 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10270 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010271 if (p != NULL && !aborting())
10272 EMSG2(_(e_invexpr2), p);
10273 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010275 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010277 else if (*s != NUL)
10278 EMSG(_(e_trailing));
10279}
10280
10281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 * "eventhandler()" function
10283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010286 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010289 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290}
10291
10292/*
10293 * "executable()" function
10294 */
10295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010297 typval_T *argvars;
10298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010300 char_u *name = get_tv_string(&argvars[0]);
10301
10302 /* Check in $PATH and also check directly if there is a directory name. */
10303 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10304 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010305}
10306
10307/*
10308 * "exepath()" function
10309 */
10310 static void
10311f_exepath(argvars, rettv)
10312 typval_T *argvars;
10313 typval_T *rettv;
10314{
10315 char_u *p = NULL;
10316
Bram Moolenaarb5971142015-03-21 17:32:19 +010010317 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010318 rettv->v_type = VAR_STRING;
10319 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320}
10321
10322/*
10323 * "exists()" function
10324 */
10325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010327 typval_T *argvars;
10328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329{
10330 char_u *p;
10331 char_u *name;
10332 int n = FALSE;
10333 int len = 0;
10334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 if (*p == '$') /* environment variable */
10337 {
10338 /* first try "normal" environment variables (fast) */
10339 if (mch_getenv(p + 1) != NULL)
10340 n = TRUE;
10341 else
10342 {
10343 /* try expanding things like $VIM and ${HOME} */
10344 p = expand_env_save(p);
10345 if (p != NULL && *p != '$')
10346 n = TRUE;
10347 vim_free(p);
10348 }
10349 }
10350 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010352 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010353 if (*skipwhite(p) != NUL)
10354 n = FALSE; /* trailing garbage */
10355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 else if (*p == '*') /* internal or user defined function */
10357 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010358 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 }
10360 else if (*p == ':')
10361 {
10362 n = cmd_exists(p + 1);
10363 }
10364 else if (*p == '#')
10365 {
10366#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010367 if (p[1] == '#')
10368 n = autocmd_supported(p + 2);
10369 else
10370 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371#endif
10372 }
10373 else /* internal variable */
10374 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010375 char_u *tofree;
10376 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010378 /* get_name_len() takes care of expanding curly braces */
10379 name = p;
10380 len = get_name_len(&p, &tofree, TRUE, FALSE);
10381 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010383 if (tofree != NULL)
10384 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010385 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010386 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010388 /* handle d.key, l[idx], f(expr) */
10389 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10390 if (n)
10391 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 }
10393 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010394 if (*p != NUL)
10395 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010397 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 }
10399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010400 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401}
10402
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010403#ifdef FEAT_FLOAT
10404/*
10405 * "exp()" function
10406 */
10407 static void
10408f_exp(argvars, rettv)
10409 typval_T *argvars;
10410 typval_T *rettv;
10411{
10412 float_T f;
10413
10414 rettv->v_type = VAR_FLOAT;
10415 if (get_float_arg(argvars, &f) == OK)
10416 rettv->vval.v_float = exp(f);
10417 else
10418 rettv->vval.v_float = 0.0;
10419}
10420#endif
10421
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422/*
10423 * "expand()" function
10424 */
10425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010426f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010427 typval_T *argvars;
10428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429{
10430 char_u *s;
10431 int len;
10432 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010433 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010436 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010439 if (argvars[1].v_type != VAR_UNKNOWN
10440 && argvars[2].v_type != VAR_UNKNOWN
10441 && get_tv_number_chk(&argvars[2], &error)
10442 && !error)
10443 {
10444 rettv->v_type = VAR_LIST;
10445 rettv->vval.v_list = NULL;
10446 }
10447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 if (*s == '%' || *s == '#' || *s == '<')
10450 {
10451 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010452 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010454 if (rettv->v_type == VAR_LIST)
10455 {
10456 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10457 list_append_string(rettv->vval.v_list, result, -1);
10458 else
10459 vim_free(result);
10460 }
10461 else
10462 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 }
10464 else
10465 {
10466 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010467 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 if (argvars[1].v_type != VAR_UNKNOWN
10469 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010470 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 if (!error)
10472 {
10473 ExpandInit(&xpc);
10474 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010475 if (p_wic)
10476 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010477 if (rettv->v_type == VAR_STRING)
10478 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10479 options, WILD_ALL);
10480 else if (rettv_list_alloc(rettv) != FAIL)
10481 {
10482 int i;
10483
10484 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10485 for (i = 0; i < xpc.xp_numfiles; i++)
10486 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10487 ExpandCleanup(&xpc);
10488 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 }
10490 else
10491 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 }
10493}
10494
10495/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010496 * Go over all entries in "d2" and add them to "d1".
10497 * When "action" is "error" then a duplicate key is an error.
10498 * When "action" is "force" then a duplicate key is overwritten.
10499 * Otherwise duplicate keys are ignored ("action" is "keep").
10500 */
10501 void
10502dict_extend(d1, d2, action)
10503 dict_T *d1;
10504 dict_T *d2;
10505 char_u *action;
10506{
10507 dictitem_T *di1;
10508 hashitem_T *hi2;
10509 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010510 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010511
10512 todo = (int)d2->dv_hashtab.ht_used;
10513 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10514 {
10515 if (!HASHITEM_EMPTY(hi2))
10516 {
10517 --todo;
10518 di1 = dict_find(d1, hi2->hi_key, -1);
10519 if (d1->dv_scope != 0)
10520 {
10521 /* Disallow replacing a builtin function in l: and g:.
10522 * Check the key to be valid when adding to any
10523 * scope. */
10524 if (d1->dv_scope == VAR_DEF_SCOPE
10525 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10526 && var_check_func_name(hi2->hi_key,
10527 di1 == NULL))
10528 break;
10529 if (!valid_varname(hi2->hi_key))
10530 break;
10531 }
10532 if (di1 == NULL)
10533 {
10534 di1 = dictitem_copy(HI2DI(hi2));
10535 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10536 dictitem_free(di1);
10537 }
10538 else if (*action == 'e')
10539 {
10540 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10541 break;
10542 }
10543 else if (*action == 'f' && HI2DI(hi2) != di1)
10544 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010545 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10546 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010547 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010548 clear_tv(&di1->di_tv);
10549 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10550 }
10551 }
10552 }
10553}
10554
10555/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010556 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010558 */
10559 static void
10560f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010561 typval_T *argvars;
10562 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010563{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010564 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010565
Bram Moolenaare9a41262005-01-15 22:18:47 +000010566 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010567 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010568 list_T *l1, *l2;
10569 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010572
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 l1 = argvars[0].vval.v_list;
10574 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010575 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010576 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577 {
10578 if (argvars[2].v_type != VAR_UNKNOWN)
10579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 before = get_tv_number_chk(&argvars[2], &error);
10581 if (error)
10582 return; /* type error; errmsg already given */
10583
Bram Moolenaar758711c2005-02-02 23:11:38 +000010584 if (before == l1->lv_len)
10585 item = NULL;
10586 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010588 item = list_find(l1, before);
10589 if (item == NULL)
10590 {
10591 EMSGN(_(e_listidx), before);
10592 return;
10593 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 }
10595 }
10596 else
10597 item = NULL;
10598 list_extend(l1, l2, item);
10599
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 copy_tv(&argvars[0], rettv);
10601 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010602 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10604 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010605 dict_T *d1, *d2;
10606 char_u *action;
10607 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608
10609 d1 = argvars[0].vval.v_dict;
10610 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010611 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010612 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 {
10614 /* Check the third argument. */
10615 if (argvars[2].v_type != VAR_UNKNOWN)
10616 {
10617 static char *(av[]) = {"keep", "force", "error"};
10618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 action = get_tv_string_chk(&argvars[2]);
10620 if (action == NULL)
10621 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622 for (i = 0; i < 3; ++i)
10623 if (STRCMP(action, av[i]) == 0)
10624 break;
10625 if (i == 3)
10626 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010627 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010628 return;
10629 }
10630 }
10631 else
10632 action = (char_u *)"force";
10633
Bram Moolenaara9922d62013-05-30 13:01:18 +020010634 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010635
Bram Moolenaare9a41262005-01-15 22:18:47 +000010636 copy_tv(&argvars[0], rettv);
10637 }
10638 }
10639 else
10640 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010641}
10642
10643/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010644 * "feedkeys()" function
10645 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010646 static void
10647f_feedkeys(argvars, rettv)
10648 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010649 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010650{
10651 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010652 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010653 char_u *keys, *flags;
10654 char_u nbuf[NUMBUFLEN];
10655 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010656 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010657
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010658 /* This is not allowed in the sandbox. If the commands would still be
10659 * executed in the sandbox it would be OK, but it probably happens later,
10660 * when "sandbox" is no longer set. */
10661 if (check_secure())
10662 return;
10663
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010664 keys = get_tv_string(&argvars[0]);
10665 if (*keys != NUL)
10666 {
10667 if (argvars[1].v_type != VAR_UNKNOWN)
10668 {
10669 flags = get_tv_string_buf(&argvars[1], nbuf);
10670 for ( ; *flags != NUL; ++flags)
10671 {
10672 switch (*flags)
10673 {
10674 case 'n': remap = FALSE; break;
10675 case 'm': remap = TRUE; break;
10676 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010677 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010678 }
10679 }
10680 }
10681
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010682 /* Need to escape K_SPECIAL and CSI before putting the string in the
10683 * typeahead buffer. */
10684 keys_esc = vim_strsave_escape_csi(keys);
10685 if (keys_esc != NULL)
10686 {
10687 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010688 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010689 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010690 if (vgetc_busy)
10691 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010692 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010693 }
10694}
10695
10696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 * "filereadable()" function
10698 */
10699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010700f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010701 typval_T *argvars;
10702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010704 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 char_u *p;
10706 int n;
10707
Bram Moolenaarc236c162008-07-13 17:41:49 +000010708#ifndef O_NONBLOCK
10709# define O_NONBLOCK 0
10710#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010712 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10713 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 {
10715 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010716 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 }
10718 else
10719 n = FALSE;
10720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722}
10723
10724/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010725 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 * rights to write into.
10727 */
10728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010730 typval_T *argvars;
10731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010733 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734}
10735
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010736static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010737
10738 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010739findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010740 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010741 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010742 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010743{
10744#ifdef FEAT_SEARCHPATH
10745 char_u *fname;
10746 char_u *fresult = NULL;
10747 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10748 char_u *p;
10749 char_u pathbuf[NUMBUFLEN];
10750 int count = 1;
10751 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010752 int error = FALSE;
10753#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010754
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010755 rettv->vval.v_string = NULL;
10756 rettv->v_type = VAR_STRING;
10757
10758#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010760
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010761 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10764 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010765 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 else
10767 {
10768 if (*p != NUL)
10769 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010772 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010774 }
10775
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010776 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10777 error = TRUE;
10778
10779 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010781 do
10782 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010783 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010784 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 fresult = find_file_in_path_option(first ? fname : NULL,
10786 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010787 0, first, path,
10788 find_what,
10789 curbuf->b_ffname,
10790 find_what == FINDFILE_DIR
10791 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010792 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010793
10794 if (fresult != NULL && rettv->v_type == VAR_LIST)
10795 list_append_string(rettv->vval.v_list, fresult, -1);
10796
10797 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010799
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010800 if (rettv->v_type == VAR_STRING)
10801 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010802#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010803}
10804
Bram Moolenaar33570922005-01-25 22:26:29 +000010805static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10806static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010807
10808/*
10809 * Implementation of map() and filter().
10810 */
10811 static void
10812filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010813 typval_T *argvars;
10814 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010815 int map;
10816{
10817 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010818 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010819 listitem_T *li, *nli;
10820 list_T *l = NULL;
10821 dictitem_T *di;
10822 hashtab_T *ht;
10823 hashitem_T *hi;
10824 dict_T *d = NULL;
10825 typval_T save_val;
10826 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010828 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010829 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020010830 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010831 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010832 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010833 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010834
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010836 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010837 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010838 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010839 return;
10840 }
10841 else if (argvars[0].v_type == VAR_DICT)
10842 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010843 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020010844 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 return;
10846 }
10847 else
10848 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010849 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850 return;
10851 }
10852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 expr = get_tv_string_buf_chk(&argvars[1], buf);
10854 /* On type errors, the preceding call has already displayed an error
10855 * message. Avoid a misleading error message for an empty string that
10856 * was not passed as argument. */
10857 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010859 prepare_vimvar(VV_VAL, &save_val);
10860 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010861
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010862 /* We reset "did_emsg" to be able to detect whether an error
10863 * occurred during evaluation of the expression. */
10864 save_did_emsg = did_emsg;
10865 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010866
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010867 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 vimvars[VV_KEY].vv_type = VAR_STRING;
10871
10872 ht = &d->dv_hashtab;
10873 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010874 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010877 if (!HASHITEM_EMPTY(hi))
10878 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010879 int r;
10880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 --todo;
10882 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010883 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020010884 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
10885 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010886 break;
10887 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010888 r = filter_map_one(&di->di_tv, expr, map, &rem);
10889 clear_tv(&vimvars[VV_KEY].vv_tv);
10890 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 break;
10892 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010893 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010894 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10895 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010896 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010898 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 }
10900 }
10901 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 }
10903 else
10904 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010905 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 for (li = l->lv_first; li != NULL; li = nli)
10908 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010909 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010910 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010912 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010913 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010914 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010915 break;
10916 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010918 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010919 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010920 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010921
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010922 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010924
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010925 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927
10928 copy_tv(&argvars[0], rettv);
10929}
10930
10931 static int
10932filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010933 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010934 char_u *expr;
10935 int map;
10936 int *remp;
10937{
Bram Moolenaar33570922005-01-25 22:26:29 +000010938 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010940 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941
Bram Moolenaar33570922005-01-25 22:26:29 +000010942 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 s = expr;
10944 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010945 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 if (*s != NUL) /* check for trailing chars after expr */
10947 {
10948 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010949 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010950 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 }
10952 if (map)
10953 {
10954 /* map(): replace the list item value */
10955 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010956 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010957 *tv = rettv;
10958 }
10959 else
10960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 int error = FALSE;
10962
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010964 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010965 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010966 /* On type error, nothing has been removed; return FAIL to stop the
10967 * loop. The error message was given by get_tv_number_chk(). */
10968 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010969 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010971 retval = OK;
10972theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010973 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010974 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010975}
10976
10977/*
10978 * "filter()" function
10979 */
10980 static void
10981f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010982 typval_T *argvars;
10983 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010984{
10985 filter_map(argvars, rettv, FALSE);
10986}
10987
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010989 * "finddir({fname}[, {path}[, {count}]])" function
10990 */
10991 static void
10992f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010993 typval_T *argvars;
10994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010995{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010996 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010997}
10998
10999/*
11000 * "findfile({fname}[, {path}[, {count}]])" function
11001 */
11002 static void
11003f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011004 typval_T *argvars;
11005 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011006{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011007 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011008}
11009
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011010#ifdef FEAT_FLOAT
11011/*
11012 * "float2nr({float})" function
11013 */
11014 static void
11015f_float2nr(argvars, rettv)
11016 typval_T *argvars;
11017 typval_T *rettv;
11018{
11019 float_T f;
11020
11021 if (get_float_arg(argvars, &f) == OK)
11022 {
11023 if (f < -0x7fffffff)
11024 rettv->vval.v_number = -0x7fffffff;
11025 else if (f > 0x7fffffff)
11026 rettv->vval.v_number = 0x7fffffff;
11027 else
11028 rettv->vval.v_number = (varnumber_T)f;
11029 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011030}
11031
11032/*
11033 * "floor({float})" function
11034 */
11035 static void
11036f_floor(argvars, rettv)
11037 typval_T *argvars;
11038 typval_T *rettv;
11039{
11040 float_T f;
11041
11042 rettv->v_type = VAR_FLOAT;
11043 if (get_float_arg(argvars, &f) == OK)
11044 rettv->vval.v_float = floor(f);
11045 else
11046 rettv->vval.v_float = 0.0;
11047}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011048
11049/*
11050 * "fmod()" function
11051 */
11052 static void
11053f_fmod(argvars, rettv)
11054 typval_T *argvars;
11055 typval_T *rettv;
11056{
11057 float_T fx, fy;
11058
11059 rettv->v_type = VAR_FLOAT;
11060 if (get_float_arg(argvars, &fx) == OK
11061 && get_float_arg(&argvars[1], &fy) == OK)
11062 rettv->vval.v_float = fmod(fx, fy);
11063 else
11064 rettv->vval.v_float = 0.0;
11065}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011066#endif
11067
Bram Moolenaar0d660222005-01-07 21:51:51 +000011068/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011069 * "fnameescape({string})" function
11070 */
11071 static void
11072f_fnameescape(argvars, rettv)
11073 typval_T *argvars;
11074 typval_T *rettv;
11075{
11076 rettv->vval.v_string = vim_strsave_fnameescape(
11077 get_tv_string(&argvars[0]), FALSE);
11078 rettv->v_type = VAR_STRING;
11079}
11080
11081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 * "fnamemodify({fname}, {mods})" function
11083 */
11084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011086 typval_T *argvars;
11087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089 char_u *fname;
11090 char_u *mods;
11091 int usedlen = 0;
11092 int len;
11093 char_u *fbuf = NULL;
11094 char_u buf[NUMBUFLEN];
11095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 fname = get_tv_string_chk(&argvars[0]);
11097 mods = get_tv_string_buf_chk(&argvars[1], buf);
11098 if (fname == NULL || mods == NULL)
11099 fname = NULL;
11100 else
11101 {
11102 len = (int)STRLEN(fname);
11103 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 vim_free(fbuf);
11112}
11113
Bram Moolenaar33570922005-01-25 22:26:29 +000011114static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115
11116/*
11117 * "foldclosed()" function
11118 */
11119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011121 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011122 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011123 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124{
11125#ifdef FEAT_FOLDING
11126 linenr_T lnum;
11127 linenr_T first, last;
11128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11131 {
11132 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11133 {
11134 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 return;
11139 }
11140 }
11141#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143}
11144
11145/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011146 * "foldclosed()" function
11147 */
11148 static void
11149f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011150 typval_T *argvars;
11151 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011152{
11153 foldclosed_both(argvars, rettv, FALSE);
11154}
11155
11156/*
11157 * "foldclosedend()" function
11158 */
11159 static void
11160f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011161 typval_T *argvars;
11162 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011163{
11164 foldclosed_both(argvars, rettv, TRUE);
11165}
11166
11167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 * "foldlevel()" function
11169 */
11170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011172 typval_T *argvars UNUSED;
11173 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174{
11175#ifdef FEAT_FOLDING
11176 linenr_T lnum;
11177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182}
11183
11184/*
11185 * "foldtext()" function
11186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011189 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191{
11192#ifdef FEAT_FOLDING
11193 linenr_T lnum;
11194 char_u *s;
11195 char_u *r;
11196 int len;
11197 char *txt;
11198#endif
11199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 rettv->v_type = VAR_STRING;
11201 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011203 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11204 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11205 <= curbuf->b_ml.ml_line_count
11206 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 {
11208 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11210 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 {
11212 if (!linewhite(lnum))
11213 break;
11214 ++lnum;
11215 }
11216
11217 /* Find interesting text in this line. */
11218 s = skipwhite(ml_get(lnum));
11219 /* skip C comment-start */
11220 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011223 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011224 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011225 {
11226 s = skipwhite(ml_get(lnum + 1));
11227 if (*s == '*')
11228 s = skipwhite(s + 1);
11229 }
11230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 txt = _("+-%s%3ld lines: ");
11232 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011233 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 + 20 /* for %3ld */
11235 + STRLEN(s))); /* concatenated */
11236 if (r != NULL)
11237 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011238 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11239 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11240 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 len = (int)STRLEN(r);
11242 STRCAT(r, s);
11243 /* remove 'foldmarker' and 'commentstring' */
11244 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 }
11247 }
11248#endif
11249}
11250
11251/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011252 * "foldtextresult(lnum)" function
11253 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011256 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011257 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011258{
11259#ifdef FEAT_FOLDING
11260 linenr_T lnum;
11261 char_u *text;
11262 char_u buf[51];
11263 foldinfo_T foldinfo;
11264 int fold_count;
11265#endif
11266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267 rettv->v_type = VAR_STRING;
11268 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011269#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 /* treat illegal types and illegal string values for {lnum} the same */
11272 if (lnum < 0)
11273 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011274 fold_count = foldedCount(curwin, lnum, &foldinfo);
11275 if (fold_count > 0)
11276 {
11277 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11278 &foldinfo, buf);
11279 if (text == buf)
11280 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011282 }
11283#endif
11284}
11285
11286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 * "foreground()" function
11288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011291 typval_T *argvars UNUSED;
11292 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294#ifdef FEAT_GUI
11295 if (gui.in_use)
11296 gui_mch_set_foreground();
11297#else
11298# ifdef WIN32
11299 win32_set_foreground();
11300# endif
11301#endif
11302}
11303
11304/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011305 * "function()" function
11306 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011311{
11312 char_u *s;
11313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011315 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011316 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011317 /* Don't check an autoload name for existence here. */
11318 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011319 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011320 else
11321 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011322 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011323 {
11324 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011325 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011326
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011327 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11328 * also be called from another script. Using trans_function_name()
11329 * would also work, but some plugins depend on the name being
11330 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011331 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011332 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011333 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011334 if (rettv->vval.v_string != NULL)
11335 {
11336 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011337 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011338 }
11339 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011340 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011341 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011343 }
11344}
11345
11346/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011347 * "garbagecollect()" function
11348 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011349 static void
11350f_garbagecollect(argvars, rettv)
11351 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011352 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011353{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011354 /* This is postponed until we are back at the toplevel, because we may be
11355 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11356 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011357
11358 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11359 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011360}
11361
11362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011363 * "get()" function
11364 */
11365 static void
11366f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011367 typval_T *argvars;
11368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011369{
Bram Moolenaar33570922005-01-25 22:26:29 +000011370 listitem_T *li;
11371 list_T *l;
11372 dictitem_T *di;
11373 dict_T *d;
11374 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011375
Bram Moolenaare9a41262005-01-15 22:18:47 +000011376 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011377 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011378 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 int error = FALSE;
11381
11382 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11383 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011384 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011385 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011387 else if (argvars[0].v_type == VAR_DICT)
11388 {
11389 if ((d = argvars[0].vval.v_dict) != NULL)
11390 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011391 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011392 if (di != NULL)
11393 tv = &di->di_tv;
11394 }
11395 }
11396 else
11397 EMSG2(_(e_listdictarg), "get()");
11398
11399 if (tv == NULL)
11400 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011402 copy_tv(&argvars[2], rettv);
11403 }
11404 else
11405 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011406}
11407
Bram Moolenaar342337a2005-07-21 21:11:17 +000011408static 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 +000011409
11410/*
11411 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011412 * Return a range (from start to end) of lines in rettv from the specified
11413 * buffer.
11414 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011415 */
11416 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011417get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011418 buf_T *buf;
11419 linenr_T start;
11420 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011421 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011422 typval_T *rettv;
11423{
11424 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011425
Bram Moolenaar959a1432013-12-14 12:17:38 +010011426 rettv->v_type = VAR_STRING;
11427 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011428 if (retlist && rettv_list_alloc(rettv) == FAIL)
11429 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011430
11431 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11432 return;
11433
11434 if (!retlist)
11435 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011436 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11437 p = ml_get_buf(buf, start, FALSE);
11438 else
11439 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011440 rettv->vval.v_string = vim_strsave(p);
11441 }
11442 else
11443 {
11444 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011445 return;
11446
11447 if (start < 1)
11448 start = 1;
11449 if (end > buf->b_ml.ml_line_count)
11450 end = buf->b_ml.ml_line_count;
11451 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011452 if (list_append_string(rettv->vval.v_list,
11453 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011454 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011455 }
11456}
11457
11458/*
11459 * "getbufline()" function
11460 */
11461 static void
11462f_getbufline(argvars, rettv)
11463 typval_T *argvars;
11464 typval_T *rettv;
11465{
11466 linenr_T lnum;
11467 linenr_T end;
11468 buf_T *buf;
11469
11470 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11471 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011472 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011473 --emsg_off;
11474
Bram Moolenaar661b1822005-07-28 22:36:45 +000011475 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011476 if (argvars[2].v_type == VAR_UNKNOWN)
11477 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011478 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011479 end = get_tv_lnum_buf(&argvars[2], buf);
11480
Bram Moolenaar342337a2005-07-21 21:11:17 +000011481 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011482}
11483
Bram Moolenaar0d660222005-01-07 21:51:51 +000011484/*
11485 * "getbufvar()" function
11486 */
11487 static void
11488f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011489 typval_T *argvars;
11490 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011491{
11492 buf_T *buf;
11493 buf_T *save_curbuf;
11494 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011496 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11499 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011501 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011502
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011503 rettv->v_type = VAR_STRING;
11504 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011505
11506 if (buf != NULL && varname != NULL)
11507 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011508 /* set curbuf to be our buf, temporarily */
11509 save_curbuf = curbuf;
11510 curbuf = buf;
11511
Bram Moolenaar0d660222005-01-07 21:51:51 +000011512 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011513 {
11514 if (get_option_tv(&varname, rettv, TRUE) == OK)
11515 done = TRUE;
11516 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011517 else if (STRCMP(varname, "changedtick") == 0)
11518 {
11519 rettv->v_type = VAR_NUMBER;
11520 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011521 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011522 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011523 else
11524 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011525 /* Look up the variable. */
11526 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11527 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11528 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011529 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011530 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011531 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011532 done = TRUE;
11533 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011534 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011535
11536 /* restore previous notion of curbuf */
11537 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011538 }
11539
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011540 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11541 /* use the default value */
11542 copy_tv(&argvars[2], rettv);
11543
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544 --emsg_off;
11545}
11546
11547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 * "getchar()" function
11549 */
11550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 typval_T *argvars;
11553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554{
11555 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011556 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011558 /* Position the cursor. Needed after a message that ends in a space. */
11559 windgoto(msg_row, msg_col);
11560
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 ++no_mapping;
11562 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011563 for (;;)
11564 {
11565 if (argvars[0].v_type == VAR_UNKNOWN)
11566 /* getchar(): blocking wait. */
11567 n = safe_vgetc();
11568 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11569 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011570 n = vpeekc_any();
11571 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011572 /* illegal argument or getchar(0) and no char avail: return zero */
11573 n = 0;
11574 else
11575 /* getchar(0) and char avail: return char */
11576 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011577
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011578 if (n == K_IGNORE)
11579 continue;
11580 break;
11581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 --no_mapping;
11583 --allow_keys;
11584
Bram Moolenaar219b8702006-11-01 14:32:36 +000011585 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11586 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11587 vimvars[VV_MOUSE_COL].vv_nr = 0;
11588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 if (IS_SPECIAL(n) || mod_mask != 0)
11591 {
11592 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11593 int i = 0;
11594
11595 /* Turn a special key into three bytes, plus modifier. */
11596 if (mod_mask != 0)
11597 {
11598 temp[i++] = K_SPECIAL;
11599 temp[i++] = KS_MODIFIER;
11600 temp[i++] = mod_mask;
11601 }
11602 if (IS_SPECIAL(n))
11603 {
11604 temp[i++] = K_SPECIAL;
11605 temp[i++] = K_SECOND(n);
11606 temp[i++] = K_THIRD(n);
11607 }
11608#ifdef FEAT_MBYTE
11609 else if (has_mbyte)
11610 i += (*mb_char2bytes)(n, temp + i);
11611#endif
11612 else
11613 temp[i++] = n;
11614 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615 rettv->v_type = VAR_STRING;
11616 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011617
11618#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011619 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011620 {
11621 int row = mouse_row;
11622 int col = mouse_col;
11623 win_T *win;
11624 linenr_T lnum;
11625# ifdef FEAT_WINDOWS
11626 win_T *wp;
11627# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011628 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011629
11630 if (row >= 0 && col >= 0)
11631 {
11632 /* Find the window at the mouse coordinates and compute the
11633 * text position. */
11634 win = mouse_find_win(&row, &col);
11635 (void)mouse_comp_pos(win, &row, &col, &lnum);
11636# ifdef FEAT_WINDOWS
11637 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011638 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011639# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011640 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011641 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11642 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11643 }
11644 }
11645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 }
11647}
11648
11649/*
11650 * "getcharmod()" function
11651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011654 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658}
11659
11660/*
11661 * "getcmdline()" function
11662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011664f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 rettv->v_type = VAR_STRING;
11669 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670}
11671
11672/*
11673 * "getcmdpos()" function
11674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011677 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681}
11682
11683/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011684 * "getcmdtype()" function
11685 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011686 static void
11687f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011688 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011689 typval_T *rettv;
11690{
11691 rettv->v_type = VAR_STRING;
11692 rettv->vval.v_string = alloc(2);
11693 if (rettv->vval.v_string != NULL)
11694 {
11695 rettv->vval.v_string[0] = get_cmdline_type();
11696 rettv->vval.v_string[1] = NUL;
11697 }
11698}
11699
11700/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011701 * "getcmdwintype()" function
11702 */
11703 static void
11704f_getcmdwintype(argvars, rettv)
11705 typval_T *argvars UNUSED;
11706 typval_T *rettv;
11707{
11708 rettv->v_type = VAR_STRING;
11709 rettv->vval.v_string = NULL;
11710#ifdef FEAT_CMDWIN
11711 rettv->vval.v_string = alloc(2);
11712 if (rettv->vval.v_string != NULL)
11713 {
11714 rettv->vval.v_string[0] = cmdwin_type;
11715 rettv->vval.v_string[1] = NUL;
11716 }
11717#endif
11718}
11719
11720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011721 * "getcwd()" function
11722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011724f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011725 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011728 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011731 rettv->vval.v_string = NULL;
11732 cwd = alloc(MAXPATHL);
11733 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011735 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11736 {
11737 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011739 if (rettv->vval.v_string != NULL)
11740 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011742 }
11743 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 }
11745}
11746
11747/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011748 * "getfontname()" function
11749 */
11750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011752 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011753 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011754{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755 rettv->v_type = VAR_STRING;
11756 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011757#ifdef FEAT_GUI
11758 if (gui.in_use)
11759 {
11760 GuiFont font;
11761 char_u *name = NULL;
11762
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011763 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011764 {
11765 /* Get the "Normal" font. Either the name saved by
11766 * hl_set_font_name() or from the font ID. */
11767 font = gui.norm_font;
11768 name = hl_get_font_name();
11769 }
11770 else
11771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011773 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11774 return;
11775 font = gui_mch_get_font(name, FALSE);
11776 if (font == NOFONT)
11777 return; /* Invalid font name, return empty string. */
11778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011780 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011781 gui_mch_free_font(font);
11782 }
11783#endif
11784}
11785
11786/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011787 * "getfperm({fname})" function
11788 */
11789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011791 typval_T *argvars;
11792 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011793{
11794 char_u *fname;
11795 struct stat st;
11796 char_u *perm = NULL;
11797 char_u flags[] = "rwx";
11798 int i;
11799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011803 if (mch_stat((char *)fname, &st) >= 0)
11804 {
11805 perm = vim_strsave((char_u *)"---------");
11806 if (perm != NULL)
11807 {
11808 for (i = 0; i < 9; i++)
11809 {
11810 if (st.st_mode & (1 << (8 - i)))
11811 perm[i] = flags[i % 3];
11812 }
11813 }
11814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011816}
11817
11818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 * "getfsize({fname})" function
11820 */
11821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011823 typval_T *argvars;
11824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825{
11826 char_u *fname;
11827 struct stat st;
11828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832
11833 if (mch_stat((char *)fname, &st) >= 0)
11834 {
11835 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011840
11841 /* non-perfect check for overflow */
11842 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11843 rettv->vval.v_number = -2;
11844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 }
11846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848}
11849
11850/*
11851 * "getftime({fname})" function
11852 */
11853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 typval_T *argvars;
11856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857{
11858 char_u *fname;
11859 struct stat st;
11860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862
11863 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867}
11868
11869/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011870 * "getftype({fname})" function
11871 */
11872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011874 typval_T *argvars;
11875 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011876{
11877 char_u *fname;
11878 struct stat st;
11879 char_u *type = NULL;
11880 char *t;
11881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011885 if (mch_lstat((char *)fname, &st) >= 0)
11886 {
11887#ifdef S_ISREG
11888 if (S_ISREG(st.st_mode))
11889 t = "file";
11890 else if (S_ISDIR(st.st_mode))
11891 t = "dir";
11892# ifdef S_ISLNK
11893 else if (S_ISLNK(st.st_mode))
11894 t = "link";
11895# endif
11896# ifdef S_ISBLK
11897 else if (S_ISBLK(st.st_mode))
11898 t = "bdev";
11899# endif
11900# ifdef S_ISCHR
11901 else if (S_ISCHR(st.st_mode))
11902 t = "cdev";
11903# endif
11904# ifdef S_ISFIFO
11905 else if (S_ISFIFO(st.st_mode))
11906 t = "fifo";
11907# endif
11908# ifdef S_ISSOCK
11909 else if (S_ISSOCK(st.st_mode))
11910 t = "fifo";
11911# endif
11912 else
11913 t = "other";
11914#else
11915# ifdef S_IFMT
11916 switch (st.st_mode & S_IFMT)
11917 {
11918 case S_IFREG: t = "file"; break;
11919 case S_IFDIR: t = "dir"; break;
11920# ifdef S_IFLNK
11921 case S_IFLNK: t = "link"; break;
11922# endif
11923# ifdef S_IFBLK
11924 case S_IFBLK: t = "bdev"; break;
11925# endif
11926# ifdef S_IFCHR
11927 case S_IFCHR: t = "cdev"; break;
11928# endif
11929# ifdef S_IFIFO
11930 case S_IFIFO: t = "fifo"; break;
11931# endif
11932# ifdef S_IFSOCK
11933 case S_IFSOCK: t = "socket"; break;
11934# endif
11935 default: t = "other";
11936 }
11937# else
11938 if (mch_isdir(fname))
11939 t = "dir";
11940 else
11941 t = "file";
11942# endif
11943#endif
11944 type = vim_strsave((char_u *)t);
11945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011947}
11948
11949/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011950 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011951 */
11952 static void
11953f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011954 typval_T *argvars;
11955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011956{
11957 linenr_T lnum;
11958 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011959 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011960
11961 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011962 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011963 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011964 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011965 retlist = FALSE;
11966 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011967 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011968 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011969 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011970 retlist = TRUE;
11971 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011972
Bram Moolenaar342337a2005-07-21 21:11:17 +000011973 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011974}
11975
11976/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011977 * "getmatches()" function
11978 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011979 static void
11980f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011981 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011982 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011983{
11984#ifdef FEAT_SEARCH_EXTRA
11985 dict_T *dict;
11986 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011987 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011988
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011989 if (rettv_list_alloc(rettv) == OK)
11990 {
11991 while (cur != NULL)
11992 {
11993 dict = dict_alloc();
11994 if (dict == NULL)
11995 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011996 if (cur->match.regprog == NULL)
11997 {
11998 /* match added with matchaddpos() */
11999 for (i = 0; i < MAXPOSMATCH; ++i)
12000 {
12001 llpos_T *llpos;
12002 char buf[6];
12003 list_T *l;
12004
12005 llpos = &cur->pos.pos[i];
12006 if (llpos->lnum == 0)
12007 break;
12008 l = list_alloc();
12009 if (l == NULL)
12010 break;
12011 list_append_number(l, (varnumber_T)llpos->lnum);
12012 if (llpos->col > 0)
12013 {
12014 list_append_number(l, (varnumber_T)llpos->col);
12015 list_append_number(l, (varnumber_T)llpos->len);
12016 }
12017 sprintf(buf, "pos%d", i + 1);
12018 dict_add_list(dict, buf, l);
12019 }
12020 }
12021 else
12022 {
12023 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12024 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012025 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012026 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12027 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
12028 list_append_dict(rettv->vval.v_list, dict);
12029 cur = cur->next;
12030 }
12031 }
12032#endif
12033}
12034
12035/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012036 * "getpid()" function
12037 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012038 static void
12039f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012040 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012041 typval_T *rettv;
12042{
12043 rettv->vval.v_number = mch_get_pid();
12044}
12045
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012046static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12047
12048/*
12049 * "getcurpos()" function
12050 */
12051 static void
12052f_getcurpos(argvars, rettv)
12053 typval_T *argvars;
12054 typval_T *rettv;
12055{
12056 getpos_both(argvars, rettv, TRUE);
12057}
12058
Bram Moolenaar18081e32008-02-20 19:11:07 +000012059/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012060 * "getpos(string)" function
12061 */
12062 static void
12063f_getpos(argvars, rettv)
12064 typval_T *argvars;
12065 typval_T *rettv;
12066{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012067 getpos_both(argvars, rettv, FALSE);
12068}
12069
12070 static void
12071getpos_both(argvars, rettv, getcurpos)
12072 typval_T *argvars;
12073 typval_T *rettv;
12074 int getcurpos;
12075{
Bram Moolenaara5525202006-03-02 22:52:09 +000012076 pos_T *fp;
12077 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012078 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012079
12080 if (rettv_list_alloc(rettv) == OK)
12081 {
12082 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012083 if (getcurpos)
12084 fp = &curwin->w_cursor;
12085 else
12086 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012087 if (fnum != -1)
12088 list_append_number(l, (varnumber_T)fnum);
12089 else
12090 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012091 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12092 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012093 list_append_number(l, (fp != NULL)
12094 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012095 : (varnumber_T)0);
12096 list_append_number(l,
12097#ifdef FEAT_VIRTUALEDIT
12098 (fp != NULL) ? (varnumber_T)fp->coladd :
12099#endif
12100 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012101 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012102 list_append_number(l, curwin->w_curswant == MAXCOL ?
12103 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012104 }
12105 else
12106 rettv->vval.v_number = FALSE;
12107}
12108
12109/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012110 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012111 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012112 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012113f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012114 typval_T *argvars UNUSED;
12115 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012116{
12117#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012118 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012119#endif
12120
Bram Moolenaar2641f772005-03-25 21:58:17 +000012121#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012122 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012123 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012124 wp = NULL;
12125 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12126 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012127 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012128 if (wp == NULL)
12129 return;
12130 }
12131
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012132 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012133 }
12134#endif
12135}
12136
12137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138 * "getreg()" function
12139 */
12140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012142 typval_T *argvars;
12143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144{
12145 char_u *strregname;
12146 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012147 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012148 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012149 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012151 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 strregname = get_tv_string_chk(&argvars[0]);
12154 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012155 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012157 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012158 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12159 return_list = get_tv_number_chk(&argvars[2], &error);
12160 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012163 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012164
12165 if (error)
12166 return;
12167
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 regname = (strregname == NULL ? '"' : *strregname);
12169 if (regname == 0)
12170 regname = '"';
12171
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012172 if (return_list)
12173 {
12174 rettv->v_type = VAR_LIST;
12175 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12176 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012177 if (rettv->vval.v_list != NULL)
12178 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012179 }
12180 else
12181 {
12182 rettv->v_type = VAR_STRING;
12183 rettv->vval.v_string = get_reg_contents(regname,
12184 arg2 ? GREG_EXPR_SRC : 0);
12185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186}
12187
12188/*
12189 * "getregtype()" function
12190 */
12191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012193 typval_T *argvars;
12194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195{
12196 char_u *strregname;
12197 int regname;
12198 char_u buf[NUMBUFLEN + 2];
12199 long reglen = 0;
12200
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012201 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012202 {
12203 strregname = get_tv_string_chk(&argvars[0]);
12204 if (strregname == NULL) /* type error; errmsg already given */
12205 {
12206 rettv->v_type = VAR_STRING;
12207 rettv->vval.v_string = NULL;
12208 return;
12209 }
12210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211 else
12212 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012213 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214
12215 regname = (strregname == NULL ? '"' : *strregname);
12216 if (regname == 0)
12217 regname = '"';
12218
12219 buf[0] = NUL;
12220 buf[1] = NUL;
12221 switch (get_reg_type(regname, &reglen))
12222 {
12223 case MLINE: buf[0] = 'V'; break;
12224 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225 case MBLOCK:
12226 buf[0] = Ctrl_V;
12227 sprintf((char *)buf + 1, "%ld", reglen + 1);
12228 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 rettv->v_type = VAR_STRING;
12231 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232}
12233
12234/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012235 * "gettabvar()" function
12236 */
12237 static void
12238f_gettabvar(argvars, rettv)
12239 typval_T *argvars;
12240 typval_T *rettv;
12241{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012242 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012243 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012244 dictitem_T *v;
12245 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012246 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012247
12248 rettv->v_type = VAR_STRING;
12249 rettv->vval.v_string = NULL;
12250
12251 varname = get_tv_string_chk(&argvars[1]);
12252 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12253 if (tp != NULL && varname != NULL)
12254 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012255 /* Set tp to be our tabpage, temporarily. Also set the window to the
12256 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012257 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12258 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012259 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012260 /* look up the variable */
12261 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12262 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12263 if (v != NULL)
12264 {
12265 copy_tv(&v->di_tv, rettv);
12266 done = TRUE;
12267 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012268 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012269
12270 /* restore previous notion of curwin */
12271 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012272 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012273
12274 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012275 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012276}
12277
12278/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012279 * "gettabwinvar()" function
12280 */
12281 static void
12282f_gettabwinvar(argvars, rettv)
12283 typval_T *argvars;
12284 typval_T *rettv;
12285{
12286 getwinvar(argvars, rettv, 1);
12287}
12288
12289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 * "getwinposx()" function
12291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012293f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012294 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298#ifdef FEAT_GUI
12299 if (gui.in_use)
12300 {
12301 int x, y;
12302
12303 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 }
12306#endif
12307}
12308
12309/*
12310 * "getwinposy()" function
12311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012314 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318#ifdef FEAT_GUI
12319 if (gui.in_use)
12320 {
12321 int x, y;
12322
12323 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 }
12326#endif
12327}
12328
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012329/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012330 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012331 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012332 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012333find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012334 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012335 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012336{
12337#ifdef FEAT_WINDOWS
12338 win_T *wp;
12339#endif
12340 int nr;
12341
12342 nr = get_tv_number_chk(vp, NULL);
12343
12344#ifdef FEAT_WINDOWS
12345 if (nr < 0)
12346 return NULL;
12347 if (nr == 0)
12348 return curwin;
12349
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012350 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12351 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012352 if (--nr <= 0)
12353 break;
12354 return wp;
12355#else
12356 if (nr == 0 || nr == 1)
12357 return curwin;
12358 return NULL;
12359#endif
12360}
12361
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362/*
12363 * "getwinvar()" function
12364 */
12365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012367 typval_T *argvars;
12368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012370 getwinvar(argvars, rettv, 0);
12371}
12372
12373/*
12374 * getwinvar() and gettabwinvar()
12375 */
12376 static void
12377getwinvar(argvars, rettv, off)
12378 typval_T *argvars;
12379 typval_T *rettv;
12380 int off; /* 1 for gettabwinvar() */
12381{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 win_T *win, *oldcurwin;
12383 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012384 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012385 tabpage_T *tp = NULL;
12386 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012387 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012389#ifdef FEAT_WINDOWS
12390 if (off == 1)
12391 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12392 else
12393 tp = curtab;
12394#endif
12395 win = find_win_by_nr(&argvars[off], tp);
12396 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012397 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012399 rettv->v_type = VAR_STRING;
12400 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401
12402 if (win != NULL && varname != NULL)
12403 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012404 /* Set curwin to be our win, temporarily. Also set the tabpage,
12405 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012406 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012407 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012408 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012409 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012410 if (get_option_tv(&varname, rettv, 1) == OK)
12411 done = TRUE;
12412 }
12413 else
12414 {
12415 /* Look up the variable. */
12416 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12417 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12418 varname, FALSE);
12419 if (v != NULL)
12420 {
12421 copy_tv(&v->di_tv, rettv);
12422 done = TRUE;
12423 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012426
12427 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012428 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 }
12430
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012431 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12432 /* use the default return value */
12433 copy_tv(&argvars[off + 2], rettv);
12434
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 --emsg_off;
12436}
12437
12438/*
12439 * "glob()" function
12440 */
12441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012442f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012443 typval_T *argvars;
12444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012446 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012448 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012450 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012451 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012453 if (argvars[1].v_type != VAR_UNKNOWN)
12454 {
12455 if (get_tv_number_chk(&argvars[1], &error))
12456 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012457 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012458 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012459 if (get_tv_number_chk(&argvars[2], &error))
12460 {
12461 rettv->v_type = VAR_LIST;
12462 rettv->vval.v_list = NULL;
12463 }
12464 if (argvars[3].v_type != VAR_UNKNOWN
12465 && get_tv_number_chk(&argvars[3], &error))
12466 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012467 }
12468 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012469 if (!error)
12470 {
12471 ExpandInit(&xpc);
12472 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012473 if (p_wic)
12474 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012475 if (rettv->v_type == VAR_STRING)
12476 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012477 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012478 else if (rettv_list_alloc(rettv) != FAIL)
12479 {
12480 int i;
12481
12482 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12483 NULL, options, WILD_ALL_KEEP);
12484 for (i = 0; i < xpc.xp_numfiles; i++)
12485 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12486
12487 ExpandCleanup(&xpc);
12488 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012489 }
12490 else
12491 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492}
12493
12494/*
12495 * "globpath()" function
12496 */
12497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012498f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 typval_T *argvars;
12500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012502 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012505 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012506 garray_T ga;
12507 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012509 /* When the optional second argument is non-zero, don't remove matches
12510 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012512 if (argvars[2].v_type != VAR_UNKNOWN)
12513 {
12514 if (get_tv_number_chk(&argvars[2], &error))
12515 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012516 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012517 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012518 if (get_tv_number_chk(&argvars[3], &error))
12519 {
12520 rettv->v_type = VAR_LIST;
12521 rettv->vval.v_list = NULL;
12522 }
12523 if (argvars[4].v_type != VAR_UNKNOWN
12524 && get_tv_number_chk(&argvars[4], &error))
12525 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012526 }
12527 }
12528 if (file != NULL && !error)
12529 {
12530 ga_init2(&ga, (int)sizeof(char_u *), 10);
12531 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12532 if (rettv->v_type == VAR_STRING)
12533 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12534 else if (rettv_list_alloc(rettv) != FAIL)
12535 for (i = 0; i < ga.ga_len; ++i)
12536 list_append_string(rettv->vval.v_list,
12537 ((char_u **)(ga.ga_data))[i], -1);
12538 ga_clear_strings(&ga);
12539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012540 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542}
12543
12544/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012545 * "glob2regpat()" function
12546 */
12547 static void
12548f_glob2regpat(argvars, rettv)
12549 typval_T *argvars;
12550 typval_T *rettv;
12551{
12552 char_u *pat = get_tv_string_chk(&argvars[0]);
12553
12554 rettv->v_type = VAR_STRING;
12555 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12556}
12557
12558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559 * "has()" function
12560 */
12561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012563 typval_T *argvars;
12564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565{
12566 int i;
12567 char_u *name;
12568 int n = FALSE;
12569 static char *(has_list[]) =
12570 {
12571#ifdef AMIGA
12572 "amiga",
12573# ifdef FEAT_ARP
12574 "arp",
12575# endif
12576#endif
12577#ifdef __BEOS__
12578 "beos",
12579#endif
12580#ifdef MSDOS
12581# ifdef DJGPP
12582 "dos32",
12583# else
12584 "dos16",
12585# endif
12586#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012587#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588 "mac",
12589#endif
12590#if defined(MACOS_X_UNIX)
12591 "macunix",
12592#endif
12593#ifdef OS2
12594 "os2",
12595#endif
12596#ifdef __QNX__
12597 "qnx",
12598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599#ifdef UNIX
12600 "unix",
12601#endif
12602#ifdef VMS
12603 "vms",
12604#endif
12605#ifdef WIN16
12606 "win16",
12607#endif
12608#ifdef WIN32
12609 "win32",
12610#endif
12611#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12612 "win32unix",
12613#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012614#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 "win64",
12616#endif
12617#ifdef EBCDIC
12618 "ebcdic",
12619#endif
12620#ifndef CASE_INSENSITIVE_FILENAME
12621 "fname_case",
12622#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012623#ifdef HAVE_ACL
12624 "acl",
12625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626#ifdef FEAT_ARABIC
12627 "arabic",
12628#endif
12629#ifdef FEAT_AUTOCMD
12630 "autocmd",
12631#endif
12632#ifdef FEAT_BEVAL
12633 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012634# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12635 "balloon_multiline",
12636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637#endif
12638#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12639 "builtin_terms",
12640# ifdef ALL_BUILTIN_TCAPS
12641 "all_builtin_terms",
12642# endif
12643#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012644#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12645 || defined(FEAT_GUI_W32) \
12646 || defined(FEAT_GUI_MOTIF))
12647 "browsefilter",
12648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649#ifdef FEAT_BYTEOFF
12650 "byte_offset",
12651#endif
12652#ifdef FEAT_CINDENT
12653 "cindent",
12654#endif
12655#ifdef FEAT_CLIENTSERVER
12656 "clientserver",
12657#endif
12658#ifdef FEAT_CLIPBOARD
12659 "clipboard",
12660#endif
12661#ifdef FEAT_CMDL_COMPL
12662 "cmdline_compl",
12663#endif
12664#ifdef FEAT_CMDHIST
12665 "cmdline_hist",
12666#endif
12667#ifdef FEAT_COMMENTS
12668 "comments",
12669#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012670#ifdef FEAT_CONCEAL
12671 "conceal",
12672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673#ifdef FEAT_CRYPT
12674 "cryptv",
12675#endif
12676#ifdef FEAT_CSCOPE
12677 "cscope",
12678#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012679#ifdef FEAT_CURSORBIND
12680 "cursorbind",
12681#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012682#ifdef CURSOR_SHAPE
12683 "cursorshape",
12684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685#ifdef DEBUG
12686 "debug",
12687#endif
12688#ifdef FEAT_CON_DIALOG
12689 "dialog_con",
12690#endif
12691#ifdef FEAT_GUI_DIALOG
12692 "dialog_gui",
12693#endif
12694#ifdef FEAT_DIFF
12695 "diff",
12696#endif
12697#ifdef FEAT_DIGRAPHS
12698 "digraphs",
12699#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012700#ifdef FEAT_DIRECTX
12701 "directx",
12702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703#ifdef FEAT_DND
12704 "dnd",
12705#endif
12706#ifdef FEAT_EMACS_TAGS
12707 "emacs_tags",
12708#endif
12709 "eval", /* always present, of course! */
12710#ifdef FEAT_EX_EXTRA
12711 "ex_extra",
12712#endif
12713#ifdef FEAT_SEARCH_EXTRA
12714 "extra_search",
12715#endif
12716#ifdef FEAT_FKMAP
12717 "farsi",
12718#endif
12719#ifdef FEAT_SEARCHPATH
12720 "file_in_path",
12721#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012722#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012723 "filterpipe",
12724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725#ifdef FEAT_FIND_ID
12726 "find_in_path",
12727#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012728#ifdef FEAT_FLOAT
12729 "float",
12730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731#ifdef FEAT_FOLDING
12732 "folding",
12733#endif
12734#ifdef FEAT_FOOTER
12735 "footer",
12736#endif
12737#if !defined(USE_SYSTEM) && defined(UNIX)
12738 "fork",
12739#endif
12740#ifdef FEAT_GETTEXT
12741 "gettext",
12742#endif
12743#ifdef FEAT_GUI
12744 "gui",
12745#endif
12746#ifdef FEAT_GUI_ATHENA
12747# ifdef FEAT_GUI_NEXTAW
12748 "gui_neXtaw",
12749# else
12750 "gui_athena",
12751# endif
12752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753#ifdef FEAT_GUI_GTK
12754 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012757#ifdef FEAT_GUI_GNOME
12758 "gui_gnome",
12759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760#ifdef FEAT_GUI_MAC
12761 "gui_mac",
12762#endif
12763#ifdef FEAT_GUI_MOTIF
12764 "gui_motif",
12765#endif
12766#ifdef FEAT_GUI_PHOTON
12767 "gui_photon",
12768#endif
12769#ifdef FEAT_GUI_W16
12770 "gui_win16",
12771#endif
12772#ifdef FEAT_GUI_W32
12773 "gui_win32",
12774#endif
12775#ifdef FEAT_HANGULIN
12776 "hangul_input",
12777#endif
12778#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12779 "iconv",
12780#endif
12781#ifdef FEAT_INS_EXPAND
12782 "insert_expand",
12783#endif
12784#ifdef FEAT_JUMPLIST
12785 "jumplist",
12786#endif
12787#ifdef FEAT_KEYMAP
12788 "keymap",
12789#endif
12790#ifdef FEAT_LANGMAP
12791 "langmap",
12792#endif
12793#ifdef FEAT_LIBCALL
12794 "libcall",
12795#endif
12796#ifdef FEAT_LINEBREAK
12797 "linebreak",
12798#endif
12799#ifdef FEAT_LISP
12800 "lispindent",
12801#endif
12802#ifdef FEAT_LISTCMDS
12803 "listcmds",
12804#endif
12805#ifdef FEAT_LOCALMAP
12806 "localmap",
12807#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012808#ifdef FEAT_LUA
12809# ifndef DYNAMIC_LUA
12810 "lua",
12811# endif
12812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813#ifdef FEAT_MENU
12814 "menu",
12815#endif
12816#ifdef FEAT_SESSION
12817 "mksession",
12818#endif
12819#ifdef FEAT_MODIFY_FNAME
12820 "modify_fname",
12821#endif
12822#ifdef FEAT_MOUSE
12823 "mouse",
12824#endif
12825#ifdef FEAT_MOUSESHAPE
12826 "mouseshape",
12827#endif
12828#if defined(UNIX) || defined(VMS)
12829# ifdef FEAT_MOUSE_DEC
12830 "mouse_dec",
12831# endif
12832# ifdef FEAT_MOUSE_GPM
12833 "mouse_gpm",
12834# endif
12835# ifdef FEAT_MOUSE_JSB
12836 "mouse_jsbterm",
12837# endif
12838# ifdef FEAT_MOUSE_NET
12839 "mouse_netterm",
12840# endif
12841# ifdef FEAT_MOUSE_PTERM
12842 "mouse_pterm",
12843# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012844# ifdef FEAT_MOUSE_SGR
12845 "mouse_sgr",
12846# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012847# ifdef FEAT_SYSMOUSE
12848 "mouse_sysmouse",
12849# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012850# ifdef FEAT_MOUSE_URXVT
12851 "mouse_urxvt",
12852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853# ifdef FEAT_MOUSE_XTERM
12854 "mouse_xterm",
12855# endif
12856#endif
12857#ifdef FEAT_MBYTE
12858 "multi_byte",
12859#endif
12860#ifdef FEAT_MBYTE_IME
12861 "multi_byte_ime",
12862#endif
12863#ifdef FEAT_MULTI_LANG
12864 "multi_lang",
12865#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012866#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012867#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012868 "mzscheme",
12869#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871#ifdef FEAT_OLE
12872 "ole",
12873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874#ifdef FEAT_PATH_EXTRA
12875 "path_extra",
12876#endif
12877#ifdef FEAT_PERL
12878#ifndef DYNAMIC_PERL
12879 "perl",
12880#endif
12881#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012882#ifdef FEAT_PERSISTENT_UNDO
12883 "persistent_undo",
12884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885#ifdef FEAT_PYTHON
12886#ifndef DYNAMIC_PYTHON
12887 "python",
12888#endif
12889#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012890#ifdef FEAT_PYTHON3
12891#ifndef DYNAMIC_PYTHON3
12892 "python3",
12893#endif
12894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895#ifdef FEAT_POSTSCRIPT
12896 "postscript",
12897#endif
12898#ifdef FEAT_PRINTER
12899 "printer",
12900#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012901#ifdef FEAT_PROFILE
12902 "profile",
12903#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012904#ifdef FEAT_RELTIME
12905 "reltime",
12906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907#ifdef FEAT_QUICKFIX
12908 "quickfix",
12909#endif
12910#ifdef FEAT_RIGHTLEFT
12911 "rightleft",
12912#endif
12913#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12914 "ruby",
12915#endif
12916#ifdef FEAT_SCROLLBIND
12917 "scrollbind",
12918#endif
12919#ifdef FEAT_CMDL_INFO
12920 "showcmd",
12921 "cmdline_info",
12922#endif
12923#ifdef FEAT_SIGNS
12924 "signs",
12925#endif
12926#ifdef FEAT_SMARTINDENT
12927 "smartindent",
12928#endif
12929#ifdef FEAT_SNIFF
12930 "sniff",
12931#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012932#ifdef STARTUPTIME
12933 "startuptime",
12934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935#ifdef FEAT_STL_OPT
12936 "statusline",
12937#endif
12938#ifdef FEAT_SUN_WORKSHOP
12939 "sun_workshop",
12940#endif
12941#ifdef FEAT_NETBEANS_INTG
12942 "netbeans_intg",
12943#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012944#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012945 "spell",
12946#endif
12947#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 "syntax",
12949#endif
12950#if defined(USE_SYSTEM) || !defined(UNIX)
12951 "system",
12952#endif
12953#ifdef FEAT_TAG_BINS
12954 "tag_binary",
12955#endif
12956#ifdef FEAT_TAG_OLDSTATIC
12957 "tag_old_static",
12958#endif
12959#ifdef FEAT_TAG_ANYWHITE
12960 "tag_any_white",
12961#endif
12962#ifdef FEAT_TCL
12963# ifndef DYNAMIC_TCL
12964 "tcl",
12965# endif
12966#endif
12967#ifdef TERMINFO
12968 "terminfo",
12969#endif
12970#ifdef FEAT_TERMRESPONSE
12971 "termresponse",
12972#endif
12973#ifdef FEAT_TEXTOBJ
12974 "textobjects",
12975#endif
12976#ifdef HAVE_TGETENT
12977 "tgetent",
12978#endif
12979#ifdef FEAT_TITLE
12980 "title",
12981#endif
12982#ifdef FEAT_TOOLBAR
12983 "toolbar",
12984#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012985#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12986 "unnamedplus",
12987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988#ifdef FEAT_USR_CMDS
12989 "user-commands", /* was accidentally included in 5.4 */
12990 "user_commands",
12991#endif
12992#ifdef FEAT_VIMINFO
12993 "viminfo",
12994#endif
12995#ifdef FEAT_VERTSPLIT
12996 "vertsplit",
12997#endif
12998#ifdef FEAT_VIRTUALEDIT
12999 "virtualedit",
13000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002#ifdef FEAT_VISUALEXTRA
13003 "visualextra",
13004#endif
13005#ifdef FEAT_VREPLACE
13006 "vreplace",
13007#endif
13008#ifdef FEAT_WILDIGN
13009 "wildignore",
13010#endif
13011#ifdef FEAT_WILDMENU
13012 "wildmenu",
13013#endif
13014#ifdef FEAT_WINDOWS
13015 "windows",
13016#endif
13017#ifdef FEAT_WAK
13018 "winaltkeys",
13019#endif
13020#ifdef FEAT_WRITEBACKUP
13021 "writebackup",
13022#endif
13023#ifdef FEAT_XIM
13024 "xim",
13025#endif
13026#ifdef FEAT_XFONTSET
13027 "xfontset",
13028#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013029#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013030 "xpm",
13031 "xpm_w32", /* for backward compatibility */
13032#else
13033# if defined(HAVE_XPM)
13034 "xpm",
13035# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037#ifdef USE_XSMP
13038 "xsmp",
13039#endif
13040#ifdef USE_XSMP_INTERACT
13041 "xsmp_interact",
13042#endif
13043#ifdef FEAT_XCLIPBOARD
13044 "xterm_clipboard",
13045#endif
13046#ifdef FEAT_XTERM_SAVE
13047 "xterm_save",
13048#endif
13049#if defined(UNIX) && defined(FEAT_X11)
13050 "X11",
13051#endif
13052 NULL
13053 };
13054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056 for (i = 0; has_list[i] != NULL; ++i)
13057 if (STRICMP(name, has_list[i]) == 0)
13058 {
13059 n = TRUE;
13060 break;
13061 }
13062
13063 if (n == FALSE)
13064 {
13065 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013066 {
13067 if (name[5] == '-'
13068 && STRLEN(name) > 11
13069 && vim_isdigit(name[6])
13070 && vim_isdigit(name[8])
13071 && vim_isdigit(name[10]))
13072 {
13073 int major = atoi((char *)name + 6);
13074 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013075
13076 /* Expect "patch-9.9.01234". */
13077 n = (major < VIM_VERSION_MAJOR
13078 || (major == VIM_VERSION_MAJOR
13079 && (minor < VIM_VERSION_MINOR
13080 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013081 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013082 }
13083 else
13084 n = has_patch(atoi((char *)name + 5));
13085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 else if (STRICMP(name, "vim_starting") == 0)
13087 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013088#ifdef FEAT_MBYTE
13089 else if (STRICMP(name, "multi_byte_encoding") == 0)
13090 n = has_mbyte;
13091#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013092#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13093 else if (STRICMP(name, "balloon_multiline") == 0)
13094 n = multiline_balloon_available();
13095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096#ifdef DYNAMIC_TCL
13097 else if (STRICMP(name, "tcl") == 0)
13098 n = tcl_enabled(FALSE);
13099#endif
13100#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13101 else if (STRICMP(name, "iconv") == 0)
13102 n = iconv_enabled(FALSE);
13103#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013104#ifdef DYNAMIC_LUA
13105 else if (STRICMP(name, "lua") == 0)
13106 n = lua_enabled(FALSE);
13107#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013108#ifdef DYNAMIC_MZSCHEME
13109 else if (STRICMP(name, "mzscheme") == 0)
13110 n = mzscheme_enabled(FALSE);
13111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112#ifdef DYNAMIC_RUBY
13113 else if (STRICMP(name, "ruby") == 0)
13114 n = ruby_enabled(FALSE);
13115#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013116#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117#ifdef DYNAMIC_PYTHON
13118 else if (STRICMP(name, "python") == 0)
13119 n = python_enabled(FALSE);
13120#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013121#endif
13122#ifdef FEAT_PYTHON3
13123#ifdef DYNAMIC_PYTHON3
13124 else if (STRICMP(name, "python3") == 0)
13125 n = python3_enabled(FALSE);
13126#endif
13127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128#ifdef DYNAMIC_PERL
13129 else if (STRICMP(name, "perl") == 0)
13130 n = perl_enabled(FALSE);
13131#endif
13132#ifdef FEAT_GUI
13133 else if (STRICMP(name, "gui_running") == 0)
13134 n = (gui.in_use || gui.starting);
13135# ifdef FEAT_GUI_W32
13136 else if (STRICMP(name, "gui_win32s") == 0)
13137 n = gui_is_win32s();
13138# endif
13139# ifdef FEAT_BROWSE
13140 else if (STRICMP(name, "browse") == 0)
13141 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13142# endif
13143#endif
13144#ifdef FEAT_SYN_HL
13145 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013146 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147#endif
13148#if defined(WIN3264)
13149 else if (STRICMP(name, "win95") == 0)
13150 n = mch_windows95();
13151#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013152#ifdef FEAT_NETBEANS_INTG
13153 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013154 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 }
13157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159}
13160
13161/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013162 * "has_key()" function
13163 */
13164 static void
13165f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 typval_T *argvars;
13167 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013168{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013169 if (argvars[0].v_type != VAR_DICT)
13170 {
13171 EMSG(_(e_dictreq));
13172 return;
13173 }
13174 if (argvars[0].vval.v_dict == NULL)
13175 return;
13176
13177 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013178 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013179}
13180
13181/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013182 * "haslocaldir()" function
13183 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013184 static void
13185f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013186 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013187 typval_T *rettv;
13188{
13189 rettv->vval.v_number = (curwin->w_localdir != NULL);
13190}
13191
13192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 * "hasmapto()" function
13194 */
13195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013197 typval_T *argvars;
13198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199{
13200 char_u *name;
13201 char_u *mode;
13202 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013203 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013206 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 mode = (char_u *)"nvo";
13208 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013211 if (argvars[2].v_type != VAR_UNKNOWN)
13212 abbr = get_tv_number(&argvars[2]);
13213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214
Bram Moolenaar2c932302006-03-18 21:42:09 +000013215 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013218 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219}
13220
13221/*
13222 * "histadd()" function
13223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013226 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228{
13229#ifdef FEAT_CMDHIST
13230 int histype;
13231 char_u *str;
13232 char_u buf[NUMBUFLEN];
13233#endif
13234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 if (check_restricted() || check_secure())
13237 return;
13238#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013239 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13240 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 if (histype >= 0)
13242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 if (*str != NUL)
13245 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013246 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249 return;
13250 }
13251 }
13252#endif
13253}
13254
13255/*
13256 * "histdel()" function
13257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013259f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013260 typval_T *argvars UNUSED;
13261 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262{
13263#ifdef FEAT_CMDHIST
13264 int n;
13265 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13269 if (str == NULL)
13270 n = 0;
13271 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013273 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013274 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013276 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 else
13279 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013281 get_tv_string_buf(&argvars[1], buf));
13282 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283#endif
13284}
13285
13286/*
13287 * "histget()" function
13288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013291 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293{
13294#ifdef FEAT_CMDHIST
13295 int type;
13296 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013297 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013299 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13300 if (str == NULL)
13301 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013303 {
13304 type = get_histtype(str);
13305 if (argvars[1].v_type == VAR_UNKNOWN)
13306 idx = get_history_idx(type);
13307 else
13308 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13309 /* -1 on type error */
13310 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316}
13317
13318/*
13319 * "histnr()" function
13320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013322f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013323 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325{
13326 int i;
13327
13328#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013329 char_u *history = get_tv_string_chk(&argvars[0]);
13330
13331 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 if (i >= HIST_CMD && i < HIST_COUNT)
13333 i = get_history_idx(i);
13334 else
13335#endif
13336 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338}
13339
13340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341 * "highlightID(name)" function
13342 */
13343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349}
13350
13351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013352 * "highlight_exists()" function
13353 */
13354 static void
13355f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013356 typval_T *argvars;
13357 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013358{
13359 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13360}
13361
13362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 * "hostname()" function
13364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013366f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013367 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
13370 char_u hostname[256];
13371
13372 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013373 rettv->v_type = VAR_STRING;
13374 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375}
13376
13377/*
13378 * iconv() function
13379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013382 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384{
13385#ifdef FEAT_MBYTE
13386 char_u buf1[NUMBUFLEN];
13387 char_u buf2[NUMBUFLEN];
13388 char_u *from, *to, *str;
13389 vimconv_T vimconv;
13390#endif
13391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013392 rettv->v_type = VAR_STRING;
13393 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394
13395#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013396 str = get_tv_string(&argvars[0]);
13397 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13398 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 vimconv.vc_type = CONV_NONE;
13400 convert_setup(&vimconv, from, to);
13401
13402 /* If the encodings are equal, no conversion needed. */
13403 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013406 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407
13408 convert_setup(&vimconv, NULL, NULL);
13409 vim_free(from);
13410 vim_free(to);
13411#endif
13412}
13413
13414/*
13415 * "indent()" function
13416 */
13417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013418f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013419 typval_T *argvars;
13420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421{
13422 linenr_T lnum;
13423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013424 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013426 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013428 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429}
13430
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013431/*
13432 * "index()" function
13433 */
13434 static void
13435f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 typval_T *argvars;
13437 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013438{
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 list_T *l;
13440 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013441 long idx = 0;
13442 int ic = FALSE;
13443
13444 rettv->vval.v_number = -1;
13445 if (argvars[0].v_type != VAR_LIST)
13446 {
13447 EMSG(_(e_listreq));
13448 return;
13449 }
13450 l = argvars[0].vval.v_list;
13451 if (l != NULL)
13452 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013453 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013454 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013456 int error = FALSE;
13457
Bram Moolenaar758711c2005-02-02 23:11:38 +000013458 /* Start at specified item. Use the cached index that list_find()
13459 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013460 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013461 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013462 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 ic = get_tv_number_chk(&argvars[3], &error);
13464 if (error)
13465 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013466 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013467
Bram Moolenaar758711c2005-02-02 23:11:38 +000013468 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013469 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013470 {
13471 rettv->vval.v_number = idx;
13472 break;
13473 }
13474 }
13475}
13476
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477static int inputsecret_flag = 0;
13478
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013479static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13480
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013482 * This function is used by f_input() and f_inputdialog() functions. The third
13483 * argument to f_input() specifies the type of completion to use at the
13484 * prompt. The third argument to f_inputdialog() specifies the value to return
13485 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 */
13487 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013488get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013489 typval_T *argvars;
13490 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013491 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 char_u *p = NULL;
13495 int c;
13496 char_u buf[NUMBUFLEN];
13497 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013498 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013499 int xp_type = EXPAND_NOTHING;
13500 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013503 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504
13505#ifdef NO_CONSOLE_INPUT
13506 /* While starting up, there is no place to enter text. */
13507 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509#endif
13510
13511 cmd_silent = FALSE; /* Want to see the prompt. */
13512 if (prompt != NULL)
13513 {
13514 /* Only the part of the message after the last NL is considered as
13515 * prompt for the command line */
13516 p = vim_strrchr(prompt, '\n');
13517 if (p == NULL)
13518 p = prompt;
13519 else
13520 {
13521 ++p;
13522 c = *p;
13523 *p = NUL;
13524 msg_start();
13525 msg_clr_eos();
13526 msg_puts_attr(prompt, echo_attr);
13527 msg_didout = FALSE;
13528 msg_starthere();
13529 *p = c;
13530 }
13531 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013533 if (argvars[1].v_type != VAR_UNKNOWN)
13534 {
13535 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13536 if (defstr != NULL)
13537 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013539 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013540 {
13541 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013542 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013543 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013544
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013545 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013546 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013547
Bram Moolenaar4463f292005-09-25 22:20:24 +000013548 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13549 if (xp_name == NULL)
13550 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013551
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013552 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013553
Bram Moolenaar4463f292005-09-25 22:20:24 +000013554 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13555 &xp_arg) == FAIL)
13556 return;
13557 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013558 }
13559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013560 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013561 {
13562# ifdef FEAT_EX_EXTRA
13563 int save_ex_normal_busy = ex_normal_busy;
13564 ex_normal_busy = 0;
13565# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013566 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013567 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13568 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013569# ifdef FEAT_EX_EXTRA
13570 ex_normal_busy = save_ex_normal_busy;
13571# endif
13572 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013573 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013574 && argvars[1].v_type != VAR_UNKNOWN
13575 && argvars[2].v_type != VAR_UNKNOWN)
13576 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13577 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013578
13579 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 /* since the user typed this, no need to wait for return */
13582 need_wait_return = FALSE;
13583 msg_didout = FALSE;
13584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 cmd_silent = cmd_silent_save;
13586}
13587
13588/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013589 * "input()" function
13590 * Also handles inputsecret() when inputsecret is set.
13591 */
13592 static void
13593f_input(argvars, rettv)
13594 typval_T *argvars;
13595 typval_T *rettv;
13596{
13597 get_user_input(argvars, rettv, FALSE);
13598}
13599
13600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 * "inputdialog()" function
13602 */
13603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013605 typval_T *argvars;
13606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607{
13608#if defined(FEAT_GUI_TEXTDIALOG)
13609 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13610 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13611 {
13612 char_u *message;
13613 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013614 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013616 message = get_tv_string_chk(&argvars[0]);
13617 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013618 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013619 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 else
13621 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 if (message != NULL && defstr != NULL
13623 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013624 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 else
13627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 if (message != NULL && defstr != NULL
13629 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013630 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_string = vim_strsave(
13632 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 }
13638 else
13639#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013640 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641}
13642
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013643/*
13644 * "inputlist()" function
13645 */
13646 static void
13647f_inputlist(argvars, rettv)
13648 typval_T *argvars;
13649 typval_T *rettv;
13650{
13651 listitem_T *li;
13652 int selected;
13653 int mouse_used;
13654
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013655#ifdef NO_CONSOLE_INPUT
13656 /* While starting up, there is no place to enter text. */
13657 if (no_console_input())
13658 return;
13659#endif
13660 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13661 {
13662 EMSG2(_(e_listarg), "inputlist()");
13663 return;
13664 }
13665
13666 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013667 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013668 lines_left = Rows; /* avoid more prompt */
13669 msg_scroll = TRUE;
13670 msg_clr_eos();
13671
13672 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13673 {
13674 msg_puts(get_tv_string(&li->li_tv));
13675 msg_putchar('\n');
13676 }
13677
13678 /* Ask for choice. */
13679 selected = prompt_for_number(&mouse_used);
13680 if (mouse_used)
13681 selected -= lines_left;
13682
13683 rettv->vval.v_number = selected;
13684}
13685
13686
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13688
13689/*
13690 * "inputrestore()" function
13691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013694 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696{
13697 if (ga_userinput.ga_len > 0)
13698 {
13699 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13701 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013702 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 }
13704 else if (p_verbose > 1)
13705 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013706 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013707 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708 }
13709}
13710
13711/*
13712 * "inputsave()" function
13713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013715f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013716 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013719 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 if (ga_grow(&ga_userinput, 1) == OK)
13721 {
13722 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13723 + ga_userinput.ga_len);
13724 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013725 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 }
13727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729}
13730
13731/*
13732 * "inputsecret()" function
13733 */
13734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013735f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013736 typval_T *argvars;
13737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738{
13739 ++cmdline_star;
13740 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 --cmdline_star;
13743 --inputsecret_flag;
13744}
13745
13746/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013747 * "insert()" function
13748 */
13749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013751 typval_T *argvars;
13752 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013753{
13754 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013755 listitem_T *item;
13756 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013757 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013758
13759 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013760 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013761 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020013762 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013763 {
13764 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 before = get_tv_number_chk(&argvars[2], &error);
13766 if (error)
13767 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013768
Bram Moolenaar758711c2005-02-02 23:11:38 +000013769 if (before == l->lv_len)
13770 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013771 else
13772 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013773 item = list_find(l, before);
13774 if (item == NULL)
13775 {
13776 EMSGN(_(e_listidx), before);
13777 l = NULL;
13778 }
13779 }
13780 if (l != NULL)
13781 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013782 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013783 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013784 }
13785 }
13786}
13787
13788/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013789 * "invert(expr)" function
13790 */
13791 static void
13792f_invert(argvars, rettv)
13793 typval_T *argvars;
13794 typval_T *rettv;
13795{
13796 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13797}
13798
13799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 * "isdirectory()" function
13801 */
13802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013803f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013804 typval_T *argvars;
13805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013807 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808}
13809
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013810/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013811 * "islocked()" function
13812 */
13813 static void
13814f_islocked(argvars, rettv)
13815 typval_T *argvars;
13816 typval_T *rettv;
13817{
13818 lval_T lv;
13819 char_u *end;
13820 dictitem_T *di;
13821
13822 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013823 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13824 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013825 if (end != NULL && lv.ll_name != NULL)
13826 {
13827 if (*end != NUL)
13828 EMSG(_(e_trailing));
13829 else
13830 {
13831 if (lv.ll_tv == NULL)
13832 {
13833 if (check_changedtick(lv.ll_name))
13834 rettv->vval.v_number = 1; /* always locked */
13835 else
13836 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013837 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013838 if (di != NULL)
13839 {
13840 /* Consider a variable locked when:
13841 * 1. the variable itself is locked
13842 * 2. the value of the variable is locked.
13843 * 3. the List or Dict value is locked.
13844 */
13845 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13846 || tv_islocked(&di->di_tv));
13847 }
13848 }
13849 }
13850 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013851 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013852 else if (lv.ll_newkey != NULL)
13853 EMSG2(_(e_dictkey), lv.ll_newkey);
13854 else if (lv.ll_list != NULL)
13855 /* List item. */
13856 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13857 else
13858 /* Dictionary item. */
13859 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13860 }
13861 }
13862
13863 clear_lval(&lv);
13864}
13865
Bram Moolenaar33570922005-01-25 22:26:29 +000013866static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013867
13868/*
13869 * Turn a dict into a list:
13870 * "what" == 0: list of keys
13871 * "what" == 1: list of values
13872 * "what" == 2: list of items
13873 */
13874 static void
13875dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013876 typval_T *argvars;
13877 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013878 int what;
13879{
Bram Moolenaar33570922005-01-25 22:26:29 +000013880 list_T *l2;
13881 dictitem_T *di;
13882 hashitem_T *hi;
13883 listitem_T *li;
13884 listitem_T *li2;
13885 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013886 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013887
Bram Moolenaar8c711452005-01-14 21:53:12 +000013888 if (argvars[0].v_type != VAR_DICT)
13889 {
13890 EMSG(_(e_dictreq));
13891 return;
13892 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013893 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013894 return;
13895
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013896 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013897 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013898
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013899 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013900 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013901 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013902 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013903 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013904 --todo;
13905 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013906
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013907 li = listitem_alloc();
13908 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013909 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013910 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013911
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013912 if (what == 0)
13913 {
13914 /* keys() */
13915 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013916 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013917 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13918 }
13919 else if (what == 1)
13920 {
13921 /* values() */
13922 copy_tv(&di->di_tv, &li->li_tv);
13923 }
13924 else
13925 {
13926 /* items() */
13927 l2 = list_alloc();
13928 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013929 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013930 li->li_tv.vval.v_list = l2;
13931 if (l2 == NULL)
13932 break;
13933 ++l2->lv_refcount;
13934
13935 li2 = listitem_alloc();
13936 if (li2 == NULL)
13937 break;
13938 list_append(l2, li2);
13939 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013940 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013941 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13942
13943 li2 = listitem_alloc();
13944 if (li2 == NULL)
13945 break;
13946 list_append(l2, li2);
13947 copy_tv(&di->di_tv, &li2->li_tv);
13948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013949 }
13950 }
13951}
13952
13953/*
13954 * "items(dict)" function
13955 */
13956 static void
13957f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013958 typval_T *argvars;
13959 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013960{
13961 dict_list(argvars, rettv, 2);
13962}
13963
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013965 * "join()" function
13966 */
13967 static void
13968f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013971{
13972 garray_T ga;
13973 char_u *sep;
13974
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013975 if (argvars[0].v_type != VAR_LIST)
13976 {
13977 EMSG(_(e_listreq));
13978 return;
13979 }
13980 if (argvars[0].vval.v_list == NULL)
13981 return;
13982 if (argvars[1].v_type == VAR_UNKNOWN)
13983 sep = (char_u *)" ";
13984 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013986
13987 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988
13989 if (sep != NULL)
13990 {
13991 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013992 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013993 ga_append(&ga, NUL);
13994 rettv->vval.v_string = (char_u *)ga.ga_data;
13995 }
13996 else
13997 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013998}
13999
14000/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014001 * "keys()" function
14002 */
14003 static void
14004f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014005 typval_T *argvars;
14006 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014007{
14008 dict_list(argvars, rettv, 0);
14009}
14010
14011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 * "last_buffer_nr()" function.
14013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014016 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018{
14019 int n = 0;
14020 buf_T *buf;
14021
14022 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14023 if (n < buf->b_fnum)
14024 n = buf->b_fnum;
14025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014027}
14028
14029/*
14030 * "len()" function
14031 */
14032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014033f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014034 typval_T *argvars;
14035 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014036{
14037 switch (argvars[0].v_type)
14038 {
14039 case VAR_STRING:
14040 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 rettv->vval.v_number = (varnumber_T)STRLEN(
14042 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014043 break;
14044 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014045 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014046 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014047 case VAR_DICT:
14048 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14049 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014050 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014051 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014052 break;
14053 }
14054}
14055
Bram Moolenaar33570922005-01-25 22:26:29 +000014056static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014057
14058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014059libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014060 typval_T *argvars;
14061 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014062 int type;
14063{
14064#ifdef FEAT_LIBCALL
14065 char_u *string_in;
14066 char_u **string_result;
14067 int nr_result;
14068#endif
14069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014071 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014072 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014073
14074 if (check_restricted() || check_secure())
14075 return;
14076
14077#ifdef FEAT_LIBCALL
14078 /* The first two args must be strings, otherwise its meaningless */
14079 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14080 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014081 string_in = NULL;
14082 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014083 string_in = argvars[2].vval.v_string;
14084 if (type == VAR_NUMBER)
14085 string_result = NULL;
14086 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014088 if (mch_libcall(argvars[0].vval.v_string,
14089 argvars[1].vval.v_string,
14090 string_in,
14091 argvars[2].vval.v_number,
14092 string_result,
14093 &nr_result) == OK
14094 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014095 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014096 }
14097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098}
14099
14100/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014101 * "libcall()" function
14102 */
14103 static void
14104f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014105 typval_T *argvars;
14106 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107{
14108 libcall_common(argvars, rettv, VAR_STRING);
14109}
14110
14111/*
14112 * "libcallnr()" function
14113 */
14114 static void
14115f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 typval_T *argvars;
14117 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014118{
14119 libcall_common(argvars, rettv, VAR_NUMBER);
14120}
14121
14122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 * "line(string)" function
14124 */
14125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014127 typval_T *argvars;
14128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129{
14130 linenr_T lnum = 0;
14131 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014132 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014134 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 if (fp != NULL)
14136 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138}
14139
14140/*
14141 * "line2byte(lnum)" function
14142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014144f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014145 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147{
14148#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150#else
14151 linenr_T lnum;
14152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14158 if (rettv->vval.v_number >= 0)
14159 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160#endif
14161}
14162
14163/*
14164 * "lispindent(lnum)" function
14165 */
14166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014167f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170{
14171#ifdef FEAT_LISP
14172 pos_T pos;
14173 linenr_T lnum;
14174
14175 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14178 {
14179 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014180 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 curwin->w_cursor = pos;
14182 }
14183 else
14184#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186}
14187
14188/*
14189 * "localtime()" function
14190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014192f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014193 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014196 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197}
14198
Bram Moolenaar33570922005-01-25 22:26:29 +000014199static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200
14201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014202get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014203 typval_T *argvars;
14204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 int exact;
14206{
14207 char_u *keys;
14208 char_u *which;
14209 char_u buf[NUMBUFLEN];
14210 char_u *keys_buf = NULL;
14211 char_u *rhs;
14212 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014213 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014214 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014215 mapblock_T *mp;
14216 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217
14218 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 rettv->v_type = VAR_STRING;
14220 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014222 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 if (*keys == NUL)
14224 return;
14225
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014226 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014229 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014230 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014231 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014232 if (argvars[3].v_type != VAR_UNKNOWN)
14233 get_dict = get_tv_number(&argvars[3]);
14234 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 else
14237 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 if (which == NULL)
14239 return;
14240
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 mode = get_map_mode(&which, 0);
14242
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014243 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014244 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014246
14247 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014249 /* Return a string. */
14250 if (rhs != NULL)
14251 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252
Bram Moolenaarbd743252010-10-20 21:23:33 +020014253 }
14254 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14255 {
14256 /* Return a dictionary. */
14257 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14258 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14259 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260
Bram Moolenaarbd743252010-10-20 21:23:33 +020014261 dict_add_nr_str(dict, "lhs", 0L, lhs);
14262 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14263 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14264 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14265 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14266 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14267 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014268 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014269 dict_add_nr_str(dict, "mode", 0L, mapmode);
14270
14271 vim_free(lhs);
14272 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 }
14274}
14275
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014276#ifdef FEAT_FLOAT
14277/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014278 * "log()" function
14279 */
14280 static void
14281f_log(argvars, rettv)
14282 typval_T *argvars;
14283 typval_T *rettv;
14284{
14285 float_T f;
14286
14287 rettv->v_type = VAR_FLOAT;
14288 if (get_float_arg(argvars, &f) == OK)
14289 rettv->vval.v_float = log(f);
14290 else
14291 rettv->vval.v_float = 0.0;
14292}
14293
14294/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014295 * "log10()" function
14296 */
14297 static void
14298f_log10(argvars, rettv)
14299 typval_T *argvars;
14300 typval_T *rettv;
14301{
14302 float_T f;
14303
14304 rettv->v_type = VAR_FLOAT;
14305 if (get_float_arg(argvars, &f) == OK)
14306 rettv->vval.v_float = log10(f);
14307 else
14308 rettv->vval.v_float = 0.0;
14309}
14310#endif
14311
Bram Moolenaar1dced572012-04-05 16:54:08 +020014312#ifdef FEAT_LUA
14313/*
14314 * "luaeval()" function
14315 */
14316 static void
14317f_luaeval(argvars, rettv)
14318 typval_T *argvars;
14319 typval_T *rettv;
14320{
14321 char_u *str;
14322 char_u buf[NUMBUFLEN];
14323
14324 str = get_tv_string_buf(&argvars[0], buf);
14325 do_luaeval(str, argvars + 1, rettv);
14326}
14327#endif
14328
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014330 * "map()" function
14331 */
14332 static void
14333f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014334 typval_T *argvars;
14335 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014336{
14337 filter_map(argvars, rettv, TRUE);
14338}
14339
14340/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014341 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 */
14343 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014344f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014345 typval_T *argvars;
14346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014348 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349}
14350
14351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014352 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 */
14354 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014355f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014356 typval_T *argvars;
14357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014359 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360}
14361
Bram Moolenaar33570922005-01-25 22:26:29 +000014362static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363
14364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014366 typval_T *argvars;
14367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 int type;
14369{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014370 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014371 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014372 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 char_u *pat;
14374 regmatch_T regmatch;
14375 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014376 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 char_u *save_cpo;
14378 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014379 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014380 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014381 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014382 list_T *l = NULL;
14383 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014384 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014385 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386
14387 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14388 save_cpo = p_cpo;
14389 p_cpo = (char_u *)"";
14390
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014391 rettv->vval.v_number = -1;
14392 if (type == 3)
14393 {
14394 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014395 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014396 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014397 }
14398 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 rettv->v_type = VAR_STRING;
14401 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014404 if (argvars[0].v_type == VAR_LIST)
14405 {
14406 if ((l = argvars[0].vval.v_list) == NULL)
14407 goto theend;
14408 li = l->lv_first;
14409 }
14410 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014411 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014412 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014413 len = (long)STRLEN(str);
14414 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14417 if (pat == NULL)
14418 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014419
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014420 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 int error = FALSE;
14423
14424 start = get_tv_number_chk(&argvars[2], &error);
14425 if (error)
14426 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014427 if (l != NULL)
14428 {
14429 li = list_find(l, start);
14430 if (li == NULL)
14431 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014432 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014433 }
14434 else
14435 {
14436 if (start < 0)
14437 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014438 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014439 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014440 /* When "count" argument is there ignore matches before "start",
14441 * otherwise skip part of the string. Differs when pattern is "^"
14442 * or "\<". */
14443 if (argvars[3].v_type != VAR_UNKNOWN)
14444 startcol = start;
14445 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014446 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014447 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014448 len -= start;
14449 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014450 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014451
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014452 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 nth = get_tv_number_chk(&argvars[3], &error);
14454 if (error)
14455 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 }
14457
14458 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14459 if (regmatch.regprog != NULL)
14460 {
14461 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014462
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014463 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014464 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014465 if (l != NULL)
14466 {
14467 if (li == NULL)
14468 {
14469 match = FALSE;
14470 break;
14471 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014472 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014473 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014474 if (str == NULL)
14475 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014476 }
14477
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014478 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014479
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014480 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014481 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014482 if (l == NULL && !match)
14483 break;
14484
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014485 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014486 if (l != NULL)
14487 {
14488 li = li->li_next;
14489 ++idx;
14490 }
14491 else
14492 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014493#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014494 startcol = (colnr_T)(regmatch.startp[0]
14495 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014496#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014497 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014498#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014499 if (startcol > (colnr_T)len
14500 || str + startcol <= regmatch.startp[0])
14501 {
14502 match = FALSE;
14503 break;
14504 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014505 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014506 }
14507
14508 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014510 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014511 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014512 int i;
14513
14514 /* return list with matched string and submatches */
14515 for (i = 0; i < NSUBEXP; ++i)
14516 {
14517 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014518 {
14519 if (list_append_string(rettv->vval.v_list,
14520 (char_u *)"", 0) == FAIL)
14521 break;
14522 }
14523 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014524 regmatch.startp[i],
14525 (int)(regmatch.endp[i] - regmatch.startp[i]))
14526 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014527 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014528 }
14529 }
14530 else if (type == 2)
14531 {
14532 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014533 if (l != NULL)
14534 copy_tv(&li->li_tv, rettv);
14535 else
14536 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014538 }
14539 else if (l != NULL)
14540 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 else
14542 {
14543 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014544 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 (varnumber_T)(regmatch.startp[0] - str);
14546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014549 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 }
14551 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014552 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 }
14554
14555theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014556 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557 p_cpo = save_cpo;
14558}
14559
14560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561 * "match()" function
14562 */
14563 static void
14564f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014565 typval_T *argvars;
14566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567{
14568 find_some_match(argvars, rettv, 1);
14569}
14570
14571/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014572 * "matchadd()" function
14573 */
14574 static void
14575f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014576 typval_T *argvars UNUSED;
14577 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014578{
14579#ifdef FEAT_SEARCH_EXTRA
14580 char_u buf[NUMBUFLEN];
14581 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14582 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14583 int prio = 10; /* default priority */
14584 int id = -1;
14585 int error = FALSE;
14586
14587 rettv->vval.v_number = -1;
14588
14589 if (grp == NULL || pat == NULL)
14590 return;
14591 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014592 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014593 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014594 if (argvars[3].v_type != VAR_UNKNOWN)
14595 id = get_tv_number_chk(&argvars[3], &error);
14596 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014597 if (error == TRUE)
14598 return;
14599 if (id >= 1 && id <= 3)
14600 {
14601 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14602 return;
14603 }
14604
Bram Moolenaarb3414592014-06-17 17:48:32 +020014605 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14606#endif
14607}
14608
14609/*
14610 * "matchaddpos()" function
14611 */
14612 static void
14613f_matchaddpos(argvars, rettv)
14614 typval_T *argvars UNUSED;
14615 typval_T *rettv UNUSED;
14616{
14617#ifdef FEAT_SEARCH_EXTRA
14618 char_u buf[NUMBUFLEN];
14619 char_u *group;
14620 int prio = 10;
14621 int id = -1;
14622 int error = FALSE;
14623 list_T *l;
14624
14625 rettv->vval.v_number = -1;
14626
14627 group = get_tv_string_buf_chk(&argvars[0], buf);
14628 if (group == NULL)
14629 return;
14630
14631 if (argvars[1].v_type != VAR_LIST)
14632 {
14633 EMSG2(_(e_listarg), "matchaddpos()");
14634 return;
14635 }
14636 l = argvars[1].vval.v_list;
14637 if (l == NULL)
14638 return;
14639
14640 if (argvars[2].v_type != VAR_UNKNOWN)
14641 {
14642 prio = get_tv_number_chk(&argvars[2], &error);
14643 if (argvars[3].v_type != VAR_UNKNOWN)
14644 id = get_tv_number_chk(&argvars[3], &error);
14645 }
14646 if (error == TRUE)
14647 return;
14648
14649 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14650 if (id == 1 || id == 2)
14651 {
14652 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14653 return;
14654 }
14655
14656 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014657#endif
14658}
14659
14660/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014661 * "matcharg()" function
14662 */
14663 static void
14664f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014665 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014666 typval_T *rettv;
14667{
14668 if (rettv_list_alloc(rettv) == OK)
14669 {
14670#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014671 int id = get_tv_number(&argvars[0]);
14672 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014673
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014674 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014675 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014676 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14677 {
14678 list_append_string(rettv->vval.v_list,
14679 syn_id2name(m->hlg_id), -1);
14680 list_append_string(rettv->vval.v_list, m->pattern, -1);
14681 }
14682 else
14683 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014684 list_append_string(rettv->vval.v_list, NULL, -1);
14685 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014686 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014687 }
14688#endif
14689 }
14690}
14691
14692/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014693 * "matchdelete()" function
14694 */
14695 static void
14696f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014697 typval_T *argvars UNUSED;
14698 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014699{
14700#ifdef FEAT_SEARCH_EXTRA
14701 rettv->vval.v_number = match_delete(curwin,
14702 (int)get_tv_number(&argvars[0]), TRUE);
14703#endif
14704}
14705
14706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014707 * "matchend()" function
14708 */
14709 static void
14710f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014711 typval_T *argvars;
14712 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713{
14714 find_some_match(argvars, rettv, 0);
14715}
14716
14717/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014718 * "matchlist()" function
14719 */
14720 static void
14721f_matchlist(argvars, rettv)
14722 typval_T *argvars;
14723 typval_T *rettv;
14724{
14725 find_some_match(argvars, rettv, 3);
14726}
14727
14728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729 * "matchstr()" function
14730 */
14731 static void
14732f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014733 typval_T *argvars;
14734 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014735{
14736 find_some_match(argvars, rettv, 2);
14737}
14738
Bram Moolenaar33570922005-01-25 22:26:29 +000014739static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014740
14741 static void
14742max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 typval_T *argvars;
14744 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014745 int domax;
14746{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014747 long n = 0;
14748 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014749 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014750
14751 if (argvars[0].v_type == VAR_LIST)
14752 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014753 list_T *l;
14754 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014755
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014756 l = argvars[0].vval.v_list;
14757 if (l != NULL)
14758 {
14759 li = l->lv_first;
14760 if (li != NULL)
14761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014762 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014763 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014764 {
14765 li = li->li_next;
14766 if (li == NULL)
14767 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014768 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014769 if (domax ? i > n : i < n)
14770 n = i;
14771 }
14772 }
14773 }
14774 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014775 else if (argvars[0].v_type == VAR_DICT)
14776 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014778 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014779 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014780 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014781
14782 d = argvars[0].vval.v_dict;
14783 if (d != NULL)
14784 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014785 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014786 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014787 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014788 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014789 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014790 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014791 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014792 if (first)
14793 {
14794 n = i;
14795 first = FALSE;
14796 }
14797 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014798 n = i;
14799 }
14800 }
14801 }
14802 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014803 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014804 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014805 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014806}
14807
14808/*
14809 * "max()" function
14810 */
14811 static void
14812f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014813 typval_T *argvars;
14814 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014815{
14816 max_min(argvars, rettv, TRUE);
14817}
14818
14819/*
14820 * "min()" function
14821 */
14822 static void
14823f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014824 typval_T *argvars;
14825 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014826{
14827 max_min(argvars, rettv, FALSE);
14828}
14829
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014830static int mkdir_recurse __ARGS((char_u *dir, int prot));
14831
14832/*
14833 * Create the directory in which "dir" is located, and higher levels when
14834 * needed.
14835 */
14836 static int
14837mkdir_recurse(dir, prot)
14838 char_u *dir;
14839 int prot;
14840{
14841 char_u *p;
14842 char_u *updir;
14843 int r = FAIL;
14844
14845 /* Get end of directory name in "dir".
14846 * We're done when it's "/" or "c:/". */
14847 p = gettail_sep(dir);
14848 if (p <= get_past_head(dir))
14849 return OK;
14850
14851 /* If the directory exists we're done. Otherwise: create it.*/
14852 updir = vim_strnsave(dir, (int)(p - dir));
14853 if (updir == NULL)
14854 return FAIL;
14855 if (mch_isdir(updir))
14856 r = OK;
14857 else if (mkdir_recurse(updir, prot) == OK)
14858 r = vim_mkdir_emsg(updir, prot);
14859 vim_free(updir);
14860 return r;
14861}
14862
14863#ifdef vim_mkdir
14864/*
14865 * "mkdir()" function
14866 */
14867 static void
14868f_mkdir(argvars, rettv)
14869 typval_T *argvars;
14870 typval_T *rettv;
14871{
14872 char_u *dir;
14873 char_u buf[NUMBUFLEN];
14874 int prot = 0755;
14875
14876 rettv->vval.v_number = FAIL;
14877 if (check_restricted() || check_secure())
14878 return;
14879
14880 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014881 if (*dir == NUL)
14882 rettv->vval.v_number = FAIL;
14883 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014884 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014885 if (*gettail(dir) == NUL)
14886 /* remove trailing slashes */
14887 *gettail_sep(dir) = NUL;
14888
14889 if (argvars[1].v_type != VAR_UNKNOWN)
14890 {
14891 if (argvars[2].v_type != VAR_UNKNOWN)
14892 prot = get_tv_number_chk(&argvars[2], NULL);
14893 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14894 mkdir_recurse(dir, prot);
14895 }
14896 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014897 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014898}
14899#endif
14900
Bram Moolenaar0d660222005-01-07 21:51:51 +000014901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 * "mode()" function
14903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014905f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014906 typval_T *argvars;
14907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014909 char_u buf[3];
14910
14911 buf[1] = NUL;
14912 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 if (VIsual_active)
14915 {
14916 if (VIsual_select)
14917 buf[0] = VIsual_mode + 's' - 'v';
14918 else
14919 buf[0] = VIsual_mode;
14920 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014921 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014922 || State == CONFIRM)
14923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014925 if (State == ASKMORE)
14926 buf[1] = 'm';
14927 else if (State == CONFIRM)
14928 buf[1] = '?';
14929 }
14930 else if (State == EXTERNCMD)
14931 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 else if (State & INSERT)
14933 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014934#ifdef FEAT_VREPLACE
14935 if (State & VREPLACE_FLAG)
14936 {
14937 buf[0] = 'R';
14938 buf[1] = 'v';
14939 }
14940 else
14941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 if (State & REPLACE_FLAG)
14943 buf[0] = 'R';
14944 else
14945 buf[0] = 'i';
14946 }
14947 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014950 if (exmode_active)
14951 buf[1] = 'v';
14952 }
14953 else if (exmode_active)
14954 {
14955 buf[0] = 'c';
14956 buf[1] = 'e';
14957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014961 if (finish_op)
14962 buf[1] = 'o';
14963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014965 /* Clear out the minor mode when the argument is not a non-zero number or
14966 * non-empty string. */
14967 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014968 buf[1] = NUL;
14969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014970 rettv->vval.v_string = vim_strsave(buf);
14971 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972}
14973
Bram Moolenaar429fa852013-04-15 12:27:36 +020014974#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014975/*
14976 * "mzeval()" function
14977 */
14978 static void
14979f_mzeval(argvars, rettv)
14980 typval_T *argvars;
14981 typval_T *rettv;
14982{
14983 char_u *str;
14984 char_u buf[NUMBUFLEN];
14985
14986 str = get_tv_string_buf(&argvars[0], buf);
14987 do_mzeval(str, rettv);
14988}
Bram Moolenaar75676462013-01-30 14:55:42 +010014989
14990 void
14991mzscheme_call_vim(name, args, rettv)
14992 char_u *name;
14993 typval_T *args;
14994 typval_T *rettv;
14995{
14996 typval_T argvars[3];
14997
14998 argvars[0].v_type = VAR_STRING;
14999 argvars[0].vval.v_string = name;
15000 copy_tv(args, &argvars[1]);
15001 argvars[2].v_type = VAR_UNKNOWN;
15002 f_call(argvars, rettv);
15003 clear_tv(&argvars[1]);
15004}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015005#endif
15006
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015008 * "nextnonblank()" function
15009 */
15010 static void
15011f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015012 typval_T *argvars;
15013 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015014{
15015 linenr_T lnum;
15016
15017 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015019 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015020 {
15021 lnum = 0;
15022 break;
15023 }
15024 if (*skipwhite(ml_get(lnum)) != NUL)
15025 break;
15026 }
15027 rettv->vval.v_number = lnum;
15028}
15029
15030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 * "nr2char()" function
15032 */
15033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015035 typval_T *argvars;
15036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037{
15038 char_u buf[NUMBUFLEN];
15039
15040#ifdef FEAT_MBYTE
15041 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015042 {
15043 int utf8 = 0;
15044
15045 if (argvars[1].v_type != VAR_UNKNOWN)
15046 utf8 = get_tv_number_chk(&argvars[1], NULL);
15047 if (utf8)
15048 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15049 else
15050 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 else
15053#endif
15054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 buf[1] = NUL;
15057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015058 rettv->v_type = VAR_STRING;
15059 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015060}
15061
15062/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015063 * "or(expr, expr)" function
15064 */
15065 static void
15066f_or(argvars, rettv)
15067 typval_T *argvars;
15068 typval_T *rettv;
15069{
15070 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15071 | get_tv_number_chk(&argvars[1], NULL);
15072}
15073
15074/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015075 * "pathshorten()" function
15076 */
15077 static void
15078f_pathshorten(argvars, rettv)
15079 typval_T *argvars;
15080 typval_T *rettv;
15081{
15082 char_u *p;
15083
15084 rettv->v_type = VAR_STRING;
15085 p = get_tv_string_chk(&argvars[0]);
15086 if (p == NULL)
15087 rettv->vval.v_string = NULL;
15088 else
15089 {
15090 p = vim_strsave(p);
15091 rettv->vval.v_string = p;
15092 if (p != NULL)
15093 shorten_dir(p);
15094 }
15095}
15096
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015097#ifdef FEAT_FLOAT
15098/*
15099 * "pow()" function
15100 */
15101 static void
15102f_pow(argvars, rettv)
15103 typval_T *argvars;
15104 typval_T *rettv;
15105{
15106 float_T fx, fy;
15107
15108 rettv->v_type = VAR_FLOAT;
15109 if (get_float_arg(argvars, &fx) == OK
15110 && get_float_arg(&argvars[1], &fy) == OK)
15111 rettv->vval.v_float = pow(fx, fy);
15112 else
15113 rettv->vval.v_float = 0.0;
15114}
15115#endif
15116
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015117/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015118 * "prevnonblank()" function
15119 */
15120 static void
15121f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 typval_T *argvars;
15123 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124{
15125 linenr_T lnum;
15126
15127 lnum = get_tv_lnum(argvars);
15128 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15129 lnum = 0;
15130 else
15131 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15132 --lnum;
15133 rettv->vval.v_number = lnum;
15134}
15135
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015136#ifdef HAVE_STDARG_H
15137/* This dummy va_list is here because:
15138 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15139 * - locally in the function results in a "used before set" warning
15140 * - using va_start() to initialize it gives "function with fixed args" error */
15141static va_list ap;
15142#endif
15143
Bram Moolenaar8c711452005-01-14 21:53:12 +000015144/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015145 * "printf()" function
15146 */
15147 static void
15148f_printf(argvars, rettv)
15149 typval_T *argvars;
15150 typval_T *rettv;
15151{
15152 rettv->v_type = VAR_STRING;
15153 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015154#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015155 {
15156 char_u buf[NUMBUFLEN];
15157 int len;
15158 char_u *s;
15159 int saved_did_emsg = did_emsg;
15160 char *fmt;
15161
15162 /* Get the required length, allocate the buffer and do it for real. */
15163 did_emsg = FALSE;
15164 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015165 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015166 if (!did_emsg)
15167 {
15168 s = alloc(len + 1);
15169 if (s != NULL)
15170 {
15171 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015172 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015173 }
15174 }
15175 did_emsg |= saved_did_emsg;
15176 }
15177#endif
15178}
15179
15180/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015181 * "pumvisible()" function
15182 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015183 static void
15184f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015185 typval_T *argvars UNUSED;
15186 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015187{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015188#ifdef FEAT_INS_EXPAND
15189 if (pum_visible())
15190 rettv->vval.v_number = 1;
15191#endif
15192}
15193
Bram Moolenaardb913952012-06-29 12:54:53 +020015194#ifdef FEAT_PYTHON3
15195/*
15196 * "py3eval()" function
15197 */
15198 static void
15199f_py3eval(argvars, rettv)
15200 typval_T *argvars;
15201 typval_T *rettv;
15202{
15203 char_u *str;
15204 char_u buf[NUMBUFLEN];
15205
15206 str = get_tv_string_buf(&argvars[0], buf);
15207 do_py3eval(str, rettv);
15208}
15209#endif
15210
15211#ifdef FEAT_PYTHON
15212/*
15213 * "pyeval()" function
15214 */
15215 static void
15216f_pyeval(argvars, rettv)
15217 typval_T *argvars;
15218 typval_T *rettv;
15219{
15220 char_u *str;
15221 char_u buf[NUMBUFLEN];
15222
15223 str = get_tv_string_buf(&argvars[0], buf);
15224 do_pyeval(str, rettv);
15225}
15226#endif
15227
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015228/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015229 * "range()" function
15230 */
15231 static void
15232f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015233 typval_T *argvars;
15234 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015235{
15236 long start;
15237 long end;
15238 long stride = 1;
15239 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015240 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015242 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015243 if (argvars[1].v_type == VAR_UNKNOWN)
15244 {
15245 end = start - 1;
15246 start = 0;
15247 }
15248 else
15249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015251 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015253 }
15254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015255 if (error)
15256 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015257 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015258 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015259 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015260 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015261 else
15262 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015263 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015264 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015265 if (list_append_number(rettv->vval.v_list,
15266 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015267 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015268 }
15269}
15270
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015271/*
15272 * "readfile()" function
15273 */
15274 static void
15275f_readfile(argvars, rettv)
15276 typval_T *argvars;
15277 typval_T *rettv;
15278{
15279 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015280 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015281 char_u *fname;
15282 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015283 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15284 int io_size = sizeof(buf);
15285 int readlen; /* size of last fread() */
15286 char_u *prev = NULL; /* previously read bytes, if any */
15287 long prevlen = 0; /* length of data in prev */
15288 long prevsize = 0; /* size of prev buffer */
15289 long maxline = MAXLNUM;
15290 long cnt = 0;
15291 char_u *p; /* position in buf */
15292 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015293
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015294 if (argvars[1].v_type != VAR_UNKNOWN)
15295 {
15296 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15297 binary = TRUE;
15298 if (argvars[2].v_type != VAR_UNKNOWN)
15299 maxline = get_tv_number(&argvars[2]);
15300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015301
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015302 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015303 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015304
15305 /* Always open the file in binary mode, library functions have a mind of
15306 * their own about CR-LF conversion. */
15307 fname = get_tv_string(&argvars[0]);
15308 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15309 {
15310 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15311 return;
15312 }
15313
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015314 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015315 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015316 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015317
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015318 /* This for loop processes what was read, but is also entered at end
15319 * of file so that either:
15320 * - an incomplete line gets written
15321 * - a "binary" file gets an empty line at the end if it ends in a
15322 * newline. */
15323 for (p = buf, start = buf;
15324 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15325 ++p)
15326 {
15327 if (*p == '\n' || readlen <= 0)
15328 {
15329 listitem_T *li;
15330 char_u *s = NULL;
15331 long_u len = p - start;
15332
15333 /* Finished a line. Remove CRs before NL. */
15334 if (readlen > 0 && !binary)
15335 {
15336 while (len > 0 && start[len - 1] == '\r')
15337 --len;
15338 /* removal may cross back to the "prev" string */
15339 if (len == 0)
15340 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15341 --prevlen;
15342 }
15343 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015344 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015345 else
15346 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015347 /* Change "prev" buffer to be the right size. This way
15348 * the bytes are only copied once, and very long lines are
15349 * allocated only once. */
15350 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015352 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015353 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015354 prev = NULL; /* the list will own the string */
15355 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015356 }
15357 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015358 if (s == NULL)
15359 {
15360 do_outofmem_msg((long_u) prevlen + len + 1);
15361 failed = TRUE;
15362 break;
15363 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015364
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015365 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015366 {
15367 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015368 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015369 break;
15370 }
15371 li->li_tv.v_type = VAR_STRING;
15372 li->li_tv.v_lock = 0;
15373 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015374 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015375
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015376 start = p + 1; /* step over newline */
15377 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378 break;
15379 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015380 else if (*p == NUL)
15381 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015382#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015383 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15384 * when finding the BF and check the previous two bytes. */
15385 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015386 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015387 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15388 * + 1, these may be in the "prev" string. */
15389 char_u back1 = p >= buf + 1 ? p[-1]
15390 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15391 char_u back2 = p >= buf + 2 ? p[-2]
15392 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15393 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015394
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015395 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015396 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015397 char_u *dest = p - 2;
15398
15399 /* Usually a BOM is at the beginning of a file, and so at
15400 * the beginning of a line; then we can just step over it.
15401 */
15402 if (start == dest)
15403 start = p + 1;
15404 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015405 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015406 /* have to shuffle buf to close gap */
15407 int adjust_prevlen = 0;
15408
15409 if (dest < buf)
15410 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015411 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015412 dest = buf;
15413 }
15414 if (readlen > p - buf + 1)
15415 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15416 readlen -= 3 - adjust_prevlen;
15417 prevlen -= adjust_prevlen;
15418 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015419 }
15420 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015421 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015422#endif
15423 } /* for */
15424
15425 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15426 break;
15427 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015428 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015429 /* There's part of a line in buf, store it in "prev". */
15430 if (p - start + prevlen >= prevsize)
15431 {
15432 /* need bigger "prev" buffer */
15433 char_u *newprev;
15434
15435 /* A common use case is ordinary text files and "prev" gets a
15436 * fragment of a line, so the first allocation is made
15437 * small, to avoid repeatedly 'allocing' large and
15438 * 'reallocing' small. */
15439 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015440 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015441 else
15442 {
15443 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015444 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015445 prevsize = grow50pc > growmin ? grow50pc : growmin;
15446 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015447 newprev = prev == NULL ? alloc(prevsize)
15448 : vim_realloc(prev, prevsize);
15449 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015450 {
15451 do_outofmem_msg((long_u)prevsize);
15452 failed = TRUE;
15453 break;
15454 }
15455 prev = newprev;
15456 }
15457 /* Add the line part to end of "prev". */
15458 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015459 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015460 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015461 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015462
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015463 /*
15464 * For a negative line count use only the lines at the end of the file,
15465 * free the rest.
15466 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015467 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015468 while (cnt > -maxline)
15469 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015470 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015471 --cnt;
15472 }
15473
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015474 if (failed)
15475 {
15476 list_free(rettv->vval.v_list, TRUE);
15477 /* readfile doc says an empty list is returned on error */
15478 rettv->vval.v_list = list_alloc();
15479 }
15480
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015481 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015482 fclose(fd);
15483}
15484
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015485#if defined(FEAT_RELTIME)
15486static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15487
15488/*
15489 * Convert a List to proftime_T.
15490 * Return FAIL when there is something wrong.
15491 */
15492 static int
15493list2proftime(arg, tm)
15494 typval_T *arg;
15495 proftime_T *tm;
15496{
15497 long n1, n2;
15498 int error = FALSE;
15499
15500 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15501 || arg->vval.v_list->lv_len != 2)
15502 return FAIL;
15503 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15504 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15505# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015506 tm->HighPart = n1;
15507 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015508# else
15509 tm->tv_sec = n1;
15510 tm->tv_usec = n2;
15511# endif
15512 return error ? FAIL : OK;
15513}
15514#endif /* FEAT_RELTIME */
15515
15516/*
15517 * "reltime()" function
15518 */
15519 static void
15520f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015521 typval_T *argvars UNUSED;
15522 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015523{
15524#ifdef FEAT_RELTIME
15525 proftime_T res;
15526 proftime_T start;
15527
15528 if (argvars[0].v_type == VAR_UNKNOWN)
15529 {
15530 /* No arguments: get current time. */
15531 profile_start(&res);
15532 }
15533 else if (argvars[1].v_type == VAR_UNKNOWN)
15534 {
15535 if (list2proftime(&argvars[0], &res) == FAIL)
15536 return;
15537 profile_end(&res);
15538 }
15539 else
15540 {
15541 /* Two arguments: compute the difference. */
15542 if (list2proftime(&argvars[0], &start) == FAIL
15543 || list2proftime(&argvars[1], &res) == FAIL)
15544 return;
15545 profile_sub(&res, &start);
15546 }
15547
15548 if (rettv_list_alloc(rettv) == OK)
15549 {
15550 long n1, n2;
15551
15552# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015553 n1 = res.HighPart;
15554 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015555# else
15556 n1 = res.tv_sec;
15557 n2 = res.tv_usec;
15558# endif
15559 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15560 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15561 }
15562#endif
15563}
15564
15565/*
15566 * "reltimestr()" function
15567 */
15568 static void
15569f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015570 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015571 typval_T *rettv;
15572{
15573#ifdef FEAT_RELTIME
15574 proftime_T tm;
15575#endif
15576
15577 rettv->v_type = VAR_STRING;
15578 rettv->vval.v_string = NULL;
15579#ifdef FEAT_RELTIME
15580 if (list2proftime(&argvars[0], &tm) == OK)
15581 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15582#endif
15583}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015584
Bram Moolenaar0d660222005-01-07 21:51:51 +000015585#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15586static void make_connection __ARGS((void));
15587static int check_connection __ARGS((void));
15588
15589 static void
15590make_connection()
15591{
15592 if (X_DISPLAY == NULL
15593# ifdef FEAT_GUI
15594 && !gui.in_use
15595# endif
15596 )
15597 {
15598 x_force_connect = TRUE;
15599 setup_term_clip();
15600 x_force_connect = FALSE;
15601 }
15602}
15603
15604 static int
15605check_connection()
15606{
15607 make_connection();
15608 if (X_DISPLAY == NULL)
15609 {
15610 EMSG(_("E240: No connection to Vim server"));
15611 return FAIL;
15612 }
15613 return OK;
15614}
15615#endif
15616
15617#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015618static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015619
15620 static void
15621remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015622 typval_T *argvars;
15623 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015624 int expr;
15625{
15626 char_u *server_name;
15627 char_u *keys;
15628 char_u *r = NULL;
15629 char_u buf[NUMBUFLEN];
15630# ifdef WIN32
15631 HWND w;
15632# else
15633 Window w;
15634# endif
15635
15636 if (check_restricted() || check_secure())
15637 return;
15638
15639# ifdef FEAT_X11
15640 if (check_connection() == FAIL)
15641 return;
15642# endif
15643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015644 server_name = get_tv_string_chk(&argvars[0]);
15645 if (server_name == NULL)
15646 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015647 keys = get_tv_string_buf(&argvars[1], buf);
15648# ifdef WIN32
15649 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15650# else
15651 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15652 < 0)
15653# endif
15654 {
15655 if (r != NULL)
15656 EMSG(r); /* sending worked but evaluation failed */
15657 else
15658 EMSG2(_("E241: Unable to send to %s"), server_name);
15659 return;
15660 }
15661
15662 rettv->vval.v_string = r;
15663
15664 if (argvars[2].v_type != VAR_UNKNOWN)
15665 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015666 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015667 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015668 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015669
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015670 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015671 v.di_tv.v_type = VAR_STRING;
15672 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015673 idvar = get_tv_string_chk(&argvars[2]);
15674 if (idvar != NULL)
15675 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015676 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677 }
15678}
15679#endif
15680
15681/*
15682 * "remote_expr()" function
15683 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015684 static void
15685f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015686 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015687 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688{
15689 rettv->v_type = VAR_STRING;
15690 rettv->vval.v_string = NULL;
15691#ifdef FEAT_CLIENTSERVER
15692 remote_common(argvars, rettv, TRUE);
15693#endif
15694}
15695
15696/*
15697 * "remote_foreground()" function
15698 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 static void
15700f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015701 typval_T *argvars UNUSED;
15702 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015703{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015704#ifdef FEAT_CLIENTSERVER
15705# ifdef WIN32
15706 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 {
15708 char_u *server_name = get_tv_string_chk(&argvars[0]);
15709
15710 if (server_name != NULL)
15711 serverForeground(server_name);
15712 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015713# else
15714 /* Send a foreground() expression to the server. */
15715 argvars[1].v_type = VAR_STRING;
15716 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15717 argvars[2].v_type = VAR_UNKNOWN;
15718 remote_common(argvars, rettv, TRUE);
15719 vim_free(argvars[1].vval.v_string);
15720# endif
15721#endif
15722}
15723
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724 static void
15725f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015726 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015727 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015728{
15729#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015730 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731 char_u *s = NULL;
15732# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015733 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015736
15737 if (check_restricted() || check_secure())
15738 {
15739 rettv->vval.v_number = -1;
15740 return;
15741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015742 serverid = get_tv_string_chk(&argvars[0]);
15743 if (serverid == NULL)
15744 {
15745 rettv->vval.v_number = -1;
15746 return; /* type error; errmsg already given */
15747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015748# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015749 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015750 if (n == 0)
15751 rettv->vval.v_number = -1;
15752 else
15753 {
15754 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15755 rettv->vval.v_number = (s != NULL);
15756 }
15757# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758 if (check_connection() == FAIL)
15759 return;
15760
15761 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015762 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763# endif
15764
15765 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015767 char_u *retvar;
15768
Bram Moolenaar33570922005-01-25 22:26:29 +000015769 v.di_tv.v_type = VAR_STRING;
15770 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015771 retvar = get_tv_string_chk(&argvars[1]);
15772 if (retvar != NULL)
15773 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015775 }
15776#else
15777 rettv->vval.v_number = -1;
15778#endif
15779}
15780
Bram Moolenaar0d660222005-01-07 21:51:51 +000015781 static void
15782f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015783 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785{
15786 char_u *r = NULL;
15787
15788#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015789 char_u *serverid = get_tv_string_chk(&argvars[0]);
15790
15791 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015792 {
15793# ifdef WIN32
15794 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015795 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015796
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015797 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015798 if (n != 0)
15799 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15800 if (r == NULL)
15801# else
15802 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015803 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804# endif
15805 EMSG(_("E277: Unable to read a server reply"));
15806 }
15807#endif
15808 rettv->v_type = VAR_STRING;
15809 rettv->vval.v_string = r;
15810}
15811
15812/*
15813 * "remote_send()" function
15814 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815 static void
15816f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015817 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819{
15820 rettv->v_type = VAR_STRING;
15821 rettv->vval.v_string = NULL;
15822#ifdef FEAT_CLIENTSERVER
15823 remote_common(argvars, rettv, FALSE);
15824#endif
15825}
15826
15827/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015828 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015829 */
15830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015831f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015832 typval_T *argvars;
15833 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015834{
Bram Moolenaar33570922005-01-25 22:26:29 +000015835 list_T *l;
15836 listitem_T *item, *item2;
15837 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015838 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015839 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015840 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015841 dict_T *d;
15842 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020015843 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015844
Bram Moolenaar8c711452005-01-14 21:53:12 +000015845 if (argvars[0].v_type == VAR_DICT)
15846 {
15847 if (argvars[2].v_type != VAR_UNKNOWN)
15848 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015849 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015850 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015852 key = get_tv_string_chk(&argvars[1]);
15853 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015855 di = dict_find(d, key, -1);
15856 if (di == NULL)
15857 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020015858 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
15859 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015860 {
15861 *rettv = di->di_tv;
15862 init_tv(&di->di_tv);
15863 dictitem_remove(d, di);
15864 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015865 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015866 }
15867 }
15868 else if (argvars[0].v_type != VAR_LIST)
15869 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015870 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015871 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015873 int error = FALSE;
15874
15875 idx = get_tv_number_chk(&argvars[1], &error);
15876 if (error)
15877 ; /* type error: do nothing, errmsg already given */
15878 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015879 EMSGN(_(e_listidx), idx);
15880 else
15881 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015882 if (argvars[2].v_type == VAR_UNKNOWN)
15883 {
15884 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015885 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015886 *rettv = item->li_tv;
15887 vim_free(item);
15888 }
15889 else
15890 {
15891 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015892 end = get_tv_number_chk(&argvars[2], &error);
15893 if (error)
15894 ; /* type error: do nothing */
15895 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015896 EMSGN(_(e_listidx), end);
15897 else
15898 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015899 int cnt = 0;
15900
15901 for (li = item; li != NULL; li = li->li_next)
15902 {
15903 ++cnt;
15904 if (li == item2)
15905 break;
15906 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015907 if (li == NULL) /* didn't find "item2" after "item" */
15908 EMSG(_(e_invrange));
15909 else
15910 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015911 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015912 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015913 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015914 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015915 l->lv_first = item;
15916 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015917 item->li_prev = NULL;
15918 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015919 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015920 }
15921 }
15922 }
15923 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015924 }
15925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926}
15927
15928/*
15929 * "rename({from}, {to})" function
15930 */
15931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015932f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015933 typval_T *argvars;
15934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935{
15936 char_u buf[NUMBUFLEN];
15937
15938 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015941 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15942 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943}
15944
15945/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015946 * "repeat()" function
15947 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015948 static void
15949f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015950 typval_T *argvars;
15951 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015952{
15953 char_u *p;
15954 int n;
15955 int slen;
15956 int len;
15957 char_u *r;
15958 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015959
15960 n = get_tv_number(&argvars[1]);
15961 if (argvars[0].v_type == VAR_LIST)
15962 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015963 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015964 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015965 if (list_extend(rettv->vval.v_list,
15966 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015967 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015968 }
15969 else
15970 {
15971 p = get_tv_string(&argvars[0]);
15972 rettv->v_type = VAR_STRING;
15973 rettv->vval.v_string = NULL;
15974
15975 slen = (int)STRLEN(p);
15976 len = slen * n;
15977 if (len <= 0)
15978 return;
15979
15980 r = alloc(len + 1);
15981 if (r != NULL)
15982 {
15983 for (i = 0; i < n; i++)
15984 mch_memmove(r + i * slen, p, (size_t)slen);
15985 r[len] = NUL;
15986 }
15987
15988 rettv->vval.v_string = r;
15989 }
15990}
15991
15992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 * "resolve()" function
15994 */
15995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015996f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015997 typval_T *argvars;
15998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999{
16000 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016001#ifdef HAVE_READLINK
16002 char_u *buf = NULL;
16003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006#ifdef FEAT_SHORTCUT
16007 {
16008 char_u *v = NULL;
16009
16010 v = mch_resolve_shortcut(p);
16011 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016014 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 }
16016#else
16017# ifdef HAVE_READLINK
16018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 char_u *cpy;
16020 int len;
16021 char_u *remain = NULL;
16022 char_u *q;
16023 int is_relative_to_current = FALSE;
16024 int has_trailing_pathsep = FALSE;
16025 int limit = 100;
16026
16027 p = vim_strsave(p);
16028
16029 if (p[0] == '.' && (vim_ispathsep(p[1])
16030 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16031 is_relative_to_current = TRUE;
16032
16033 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016034 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016037 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039
16040 q = getnextcomp(p);
16041 if (*q != NUL)
16042 {
16043 /* Separate the first path component in "p", and keep the
16044 * remainder (beginning with the path separator). */
16045 remain = vim_strsave(q - 1);
16046 q[-1] = NUL;
16047 }
16048
Bram Moolenaard9462e32011-04-11 21:35:11 +020016049 buf = alloc(MAXPATHL + 1);
16050 if (buf == NULL)
16051 goto fail;
16052
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 for (;;)
16054 {
16055 for (;;)
16056 {
16057 len = readlink((char *)p, (char *)buf, MAXPATHL);
16058 if (len <= 0)
16059 break;
16060 buf[len] = NUL;
16061
16062 if (limit-- == 0)
16063 {
16064 vim_free(p);
16065 vim_free(remain);
16066 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016067 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068 goto fail;
16069 }
16070
16071 /* Ensure that the result will have a trailing path separator
16072 * if the argument has one. */
16073 if (remain == NULL && has_trailing_pathsep)
16074 add_pathsep(buf);
16075
16076 /* Separate the first path component in the link value and
16077 * concatenate the remainders. */
16078 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16079 if (*q != NUL)
16080 {
16081 if (remain == NULL)
16082 remain = vim_strsave(q - 1);
16083 else
16084 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016085 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 if (cpy != NULL)
16087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 vim_free(remain);
16089 remain = cpy;
16090 }
16091 }
16092 q[-1] = NUL;
16093 }
16094
16095 q = gettail(p);
16096 if (q > p && *q == NUL)
16097 {
16098 /* Ignore trailing path separator. */
16099 q[-1] = NUL;
16100 q = gettail(p);
16101 }
16102 if (q > p && !mch_isFullName(buf))
16103 {
16104 /* symlink is relative to directory of argument */
16105 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16106 if (cpy != NULL)
16107 {
16108 STRCPY(cpy, p);
16109 STRCPY(gettail(cpy), buf);
16110 vim_free(p);
16111 p = cpy;
16112 }
16113 }
16114 else
16115 {
16116 vim_free(p);
16117 p = vim_strsave(buf);
16118 }
16119 }
16120
16121 if (remain == NULL)
16122 break;
16123
16124 /* Append the first path component of "remain" to "p". */
16125 q = getnextcomp(remain + 1);
16126 len = q - remain - (*q != NUL);
16127 cpy = vim_strnsave(p, STRLEN(p) + len);
16128 if (cpy != NULL)
16129 {
16130 STRNCAT(cpy, remain, len);
16131 vim_free(p);
16132 p = cpy;
16133 }
16134 /* Shorten "remain". */
16135 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016136 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 else
16138 {
16139 vim_free(remain);
16140 remain = NULL;
16141 }
16142 }
16143
16144 /* If the result is a relative path name, make it explicitly relative to
16145 * the current directory if and only if the argument had this form. */
16146 if (!vim_ispathsep(*p))
16147 {
16148 if (is_relative_to_current
16149 && *p != NUL
16150 && !(p[0] == '.'
16151 && (p[1] == NUL
16152 || vim_ispathsep(p[1])
16153 || (p[1] == '.'
16154 && (p[2] == NUL
16155 || vim_ispathsep(p[2]))))))
16156 {
16157 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016158 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 if (cpy != NULL)
16160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 vim_free(p);
16162 p = cpy;
16163 }
16164 }
16165 else if (!is_relative_to_current)
16166 {
16167 /* Strip leading "./". */
16168 q = p;
16169 while (q[0] == '.' && vim_ispathsep(q[1]))
16170 q += 2;
16171 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016172 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 }
16174 }
16175
16176 /* Ensure that the result will have no trailing path separator
16177 * if the argument had none. But keep "/" or "//". */
16178 if (!has_trailing_pathsep)
16179 {
16180 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016181 if (after_pathsep(p, q))
16182 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 }
16184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 }
16187# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016188 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189# endif
16190#endif
16191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016192 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193
16194#ifdef HAVE_READLINK
16195fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016196 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016198 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199}
16200
16201/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016202 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 */
16204 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016206 typval_T *argvars;
16207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208{
Bram Moolenaar33570922005-01-25 22:26:29 +000016209 list_T *l;
16210 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 if (argvars[0].v_type != VAR_LIST)
16213 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016214 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016215 && !tv_check_lock(l->lv_lock,
16216 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016217 {
16218 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016219 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016220 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016221 while (li != NULL)
16222 {
16223 ni = li->li_prev;
16224 list_append(l, li);
16225 li = ni;
16226 }
16227 rettv->vval.v_list = l;
16228 rettv->v_type = VAR_LIST;
16229 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016230 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232}
16233
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016234#define SP_NOMOVE 0x01 /* don't move cursor */
16235#define SP_REPEAT 0x02 /* repeat to find outer pair */
16236#define SP_RETCOUNT 0x04 /* return matchcount */
16237#define SP_SETPCMARK 0x08 /* set previous context mark */
16238#define SP_START 0x10 /* accept match at start position */
16239#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16240#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016241
Bram Moolenaar33570922005-01-25 22:26:29 +000016242static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016243
16244/*
16245 * Get flags for a search function.
16246 * Possibly sets "p_ws".
16247 * Returns BACKWARD, FORWARD or zero (for an error).
16248 */
16249 static int
16250get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016251 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252 int *flagsp;
16253{
16254 int dir = FORWARD;
16255 char_u *flags;
16256 char_u nbuf[NUMBUFLEN];
16257 int mask;
16258
16259 if (varp->v_type != VAR_UNKNOWN)
16260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 flags = get_tv_string_buf_chk(varp, nbuf);
16262 if (flags == NULL)
16263 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264 while (*flags != NUL)
16265 {
16266 switch (*flags)
16267 {
16268 case 'b': dir = BACKWARD; break;
16269 case 'w': p_ws = TRUE; break;
16270 case 'W': p_ws = FALSE; break;
16271 default: mask = 0;
16272 if (flagsp != NULL)
16273 switch (*flags)
16274 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016275 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016276 case 'e': mask = SP_END; break;
16277 case 'm': mask = SP_RETCOUNT; break;
16278 case 'n': mask = SP_NOMOVE; break;
16279 case 'p': mask = SP_SUBPAT; break;
16280 case 'r': mask = SP_REPEAT; break;
16281 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282 }
16283 if (mask == 0)
16284 {
16285 EMSG2(_(e_invarg2), flags);
16286 dir = 0;
16287 }
16288 else
16289 *flagsp |= mask;
16290 }
16291 if (dir == 0)
16292 break;
16293 ++flags;
16294 }
16295 }
16296 return dir;
16297}
16298
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016300 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016302 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016303search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016305 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016306 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016308 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309 char_u *pat;
16310 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016311 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 int save_p_ws = p_ws;
16313 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016314 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016315 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016316 proftime_T tm;
16317#ifdef FEAT_RELTIME
16318 long time_limit = 0;
16319#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016320 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016321 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016323 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016324 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016325 if (dir == 0)
16326 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016327 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016328 if (flags & SP_START)
16329 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016330 if (flags & SP_END)
16331 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016332
Bram Moolenaar76929292008-01-06 19:07:36 +000016333 /* Optional arguments: line number to stop searching and timeout. */
16334 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016335 {
16336 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16337 if (lnum_stop < 0)
16338 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016339#ifdef FEAT_RELTIME
16340 if (argvars[3].v_type != VAR_UNKNOWN)
16341 {
16342 time_limit = get_tv_number_chk(&argvars[3], NULL);
16343 if (time_limit < 0)
16344 goto theend;
16345 }
16346#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016347 }
16348
Bram Moolenaar76929292008-01-06 19:07:36 +000016349#ifdef FEAT_RELTIME
16350 /* Set the time limit, if there is one. */
16351 profile_setlimit(time_limit, &tm);
16352#endif
16353
Bram Moolenaar231334e2005-07-25 20:46:57 +000016354 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016355 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016356 * Check to make sure only those flags are set.
16357 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16358 * flags cannot be set. Check for that condition also.
16359 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016360 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016361 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016363 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016364 goto theend;
16365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016367 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016368 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016369 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016370 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016372 if (flags & SP_SUBPAT)
16373 retval = subpatnum;
16374 else
16375 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016376 if (flags & SP_SETPCMARK)
16377 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016379 if (match_pos != NULL)
16380 {
16381 /* Store the match cursor position */
16382 match_pos->lnum = pos.lnum;
16383 match_pos->col = pos.col + 1;
16384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 /* "/$" will put the cursor after the end of the line, may need to
16386 * correct that here */
16387 check_cursor();
16388 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016389
16390 /* If 'n' flag is used: restore cursor position. */
16391 if (flags & SP_NOMOVE)
16392 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016393 else
16394 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016395theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016397
16398 return retval;
16399}
16400
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016401#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016402
16403/*
16404 * round() is not in C90, use ceil() or floor() instead.
16405 */
16406 float_T
16407vim_round(f)
16408 float_T f;
16409{
16410 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16411}
16412
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016413/*
16414 * "round({float})" function
16415 */
16416 static void
16417f_round(argvars, rettv)
16418 typval_T *argvars;
16419 typval_T *rettv;
16420{
16421 float_T f;
16422
16423 rettv->v_type = VAR_FLOAT;
16424 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016425 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016426 else
16427 rettv->vval.v_float = 0.0;
16428}
16429#endif
16430
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016431/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016432 * "screenattr()" function
16433 */
16434 static void
16435f_screenattr(argvars, rettv)
16436 typval_T *argvars UNUSED;
16437 typval_T *rettv;
16438{
16439 int row;
16440 int col;
16441 int c;
16442
16443 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16444 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16445 if (row < 0 || row >= screen_Rows
16446 || col < 0 || col >= screen_Columns)
16447 c = -1;
16448 else
16449 c = ScreenAttrs[LineOffset[row] + col];
16450 rettv->vval.v_number = c;
16451}
16452
16453/*
16454 * "screenchar()" function
16455 */
16456 static void
16457f_screenchar(argvars, rettv)
16458 typval_T *argvars UNUSED;
16459 typval_T *rettv;
16460{
16461 int row;
16462 int col;
16463 int off;
16464 int c;
16465
16466 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16467 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16468 if (row < 0 || row >= screen_Rows
16469 || col < 0 || col >= screen_Columns)
16470 c = -1;
16471 else
16472 {
16473 off = LineOffset[row] + col;
16474#ifdef FEAT_MBYTE
16475 if (enc_utf8 && ScreenLinesUC[off] != 0)
16476 c = ScreenLinesUC[off];
16477 else
16478#endif
16479 c = ScreenLines[off];
16480 }
16481 rettv->vval.v_number = c;
16482}
16483
16484/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016485 * "screencol()" function
16486 *
16487 * First column is 1 to be consistent with virtcol().
16488 */
16489 static void
16490f_screencol(argvars, rettv)
16491 typval_T *argvars UNUSED;
16492 typval_T *rettv;
16493{
16494 rettv->vval.v_number = screen_screencol() + 1;
16495}
16496
16497/*
16498 * "screenrow()" function
16499 */
16500 static void
16501f_screenrow(argvars, rettv)
16502 typval_T *argvars UNUSED;
16503 typval_T *rettv;
16504{
16505 rettv->vval.v_number = screen_screenrow() + 1;
16506}
16507
16508/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016509 * "search()" function
16510 */
16511 static void
16512f_search(argvars, rettv)
16513 typval_T *argvars;
16514 typval_T *rettv;
16515{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016516 int flags = 0;
16517
16518 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519}
16520
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016522 * "searchdecl()" function
16523 */
16524 static void
16525f_searchdecl(argvars, rettv)
16526 typval_T *argvars;
16527 typval_T *rettv;
16528{
16529 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016530 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016531 int error = FALSE;
16532 char_u *name;
16533
16534 rettv->vval.v_number = 1; /* default: FAIL */
16535
16536 name = get_tv_string_chk(&argvars[0]);
16537 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016538 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016539 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016540 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16541 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16542 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016543 if (!error && name != NULL)
16544 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016545 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016546}
16547
16548/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016549 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016551 static int
16552searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016553 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016554 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555{
16556 char_u *spat, *mpat, *epat;
16557 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 int dir;
16560 int flags = 0;
16561 char_u nbuf1[NUMBUFLEN];
16562 char_u nbuf2[NUMBUFLEN];
16563 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016564 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016565 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016566 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016569 spat = get_tv_string_chk(&argvars[0]);
16570 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16571 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16572 if (spat == NULL || mpat == NULL || epat == NULL)
16573 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 /* Handle the optional fourth argument: flags */
16576 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016577 if (dir == 0)
16578 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016579
16580 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016581 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16582 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016583 if ((flags & (SP_END | SP_SUBPAT)) != 0
16584 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016585 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016586 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016587 goto theend;
16588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016590 /* Using 'r' implies 'W', otherwise it doesn't work. */
16591 if (flags & SP_REPEAT)
16592 p_ws = FALSE;
16593
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016594 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016595 if (argvars[3].v_type == VAR_UNKNOWN
16596 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597 skip = (char_u *)"";
16598 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016600 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016601 if (argvars[5].v_type != VAR_UNKNOWN)
16602 {
16603 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16604 if (lnum_stop < 0)
16605 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016606#ifdef FEAT_RELTIME
16607 if (argvars[6].v_type != VAR_UNKNOWN)
16608 {
16609 time_limit = get_tv_number_chk(&argvars[6], NULL);
16610 if (time_limit < 0)
16611 goto theend;
16612 }
16613#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016614 }
16615 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016616 if (skip == NULL)
16617 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016619 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016620 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016621
16622theend:
16623 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016624
16625 return retval;
16626}
16627
16628/*
16629 * "searchpair()" function
16630 */
16631 static void
16632f_searchpair(argvars, rettv)
16633 typval_T *argvars;
16634 typval_T *rettv;
16635{
16636 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16637}
16638
16639/*
16640 * "searchpairpos()" function
16641 */
16642 static void
16643f_searchpairpos(argvars, rettv)
16644 typval_T *argvars;
16645 typval_T *rettv;
16646{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016647 pos_T match_pos;
16648 int lnum = 0;
16649 int col = 0;
16650
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016651 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016652 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016653
16654 if (searchpair_cmn(argvars, &match_pos) > 0)
16655 {
16656 lnum = match_pos.lnum;
16657 col = match_pos.col;
16658 }
16659
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016660 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16661 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016662}
16663
16664/*
16665 * Search for a start/middle/end thing.
16666 * Used by searchpair(), see its documentation for the details.
16667 * Returns 0 or -1 for no match,
16668 */
16669 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016670do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16671 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016672 char_u *spat; /* start pattern */
16673 char_u *mpat; /* middle pattern */
16674 char_u *epat; /* end pattern */
16675 int dir; /* BACKWARD or FORWARD */
16676 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016677 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016678 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016679 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016680 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016681{
16682 char_u *save_cpo;
16683 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16684 long retval = 0;
16685 pos_T pos;
16686 pos_T firstpos;
16687 pos_T foundpos;
16688 pos_T save_cursor;
16689 pos_T save_pos;
16690 int n;
16691 int r;
16692 int nest = 1;
16693 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016694 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016695 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016696
16697 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16698 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016699 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016700
Bram Moolenaar76929292008-01-06 19:07:36 +000016701#ifdef FEAT_RELTIME
16702 /* Set the time limit, if there is one. */
16703 profile_setlimit(time_limit, &tm);
16704#endif
16705
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016706 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16707 * start/middle/end (pat3, for the top pair). */
16708 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16709 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16710 if (pat2 == NULL || pat3 == NULL)
16711 goto theend;
16712 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16713 if (*mpat == NUL)
16714 STRCPY(pat3, pat2);
16715 else
16716 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16717 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016718 if (flags & SP_START)
16719 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016720
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 save_cursor = curwin->w_cursor;
16722 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016723 clearpos(&firstpos);
16724 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 pat = pat3;
16726 for (;;)
16727 {
16728 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016729 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16731 /* didn't find it or found the first match again: FAIL */
16732 break;
16733
16734 if (firstpos.lnum == 0)
16735 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016736 if (equalpos(pos, foundpos))
16737 {
16738 /* Found the same position again. Can happen with a pattern that
16739 * has "\zs" at the end and searching backwards. Advance one
16740 * character and try again. */
16741 if (dir == BACKWARD)
16742 decl(&pos);
16743 else
16744 incl(&pos);
16745 }
16746 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016748 /* clear the start flag to avoid getting stuck here */
16749 options &= ~SEARCH_START;
16750
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 /* If the skip pattern matches, ignore this match. */
16752 if (*skip != NUL)
16753 {
16754 save_pos = curwin->w_cursor;
16755 curwin->w_cursor = pos;
16756 r = eval_to_bool(skip, &err, NULL, FALSE);
16757 curwin->w_cursor = save_pos;
16758 if (err)
16759 {
16760 /* Evaluating {skip} caused an error, break here. */
16761 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016762 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 break;
16764 }
16765 if (r)
16766 continue;
16767 }
16768
16769 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16770 {
16771 /* Found end when searching backwards or start when searching
16772 * forward: nested pair. */
16773 ++nest;
16774 pat = pat2; /* nested, don't search for middle */
16775 }
16776 else
16777 {
16778 /* Found end when searching forward or start when searching
16779 * backward: end of (nested) pair; or found middle in outer pair. */
16780 if (--nest == 1)
16781 pat = pat3; /* outer level, search for middle */
16782 }
16783
16784 if (nest == 0)
16785 {
16786 /* Found the match: return matchcount or line number. */
16787 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016788 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016790 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016791 if (flags & SP_SETPCMARK)
16792 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793 curwin->w_cursor = pos;
16794 if (!(flags & SP_REPEAT))
16795 break;
16796 nest = 1; /* search for next unmatched */
16797 }
16798 }
16799
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016800 if (match_pos != NULL)
16801 {
16802 /* Store the match cursor position */
16803 match_pos->lnum = curwin->w_cursor.lnum;
16804 match_pos->col = curwin->w_cursor.col + 1;
16805 }
16806
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016808 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 curwin->w_cursor = save_cursor;
16810
16811theend:
16812 vim_free(pat2);
16813 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016814 if (p_cpo == empty_option)
16815 p_cpo = save_cpo;
16816 else
16817 /* Darn, evaluating the {skip} expression changed the value. */
16818 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016819
16820 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821}
16822
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016823/*
16824 * "searchpos()" function
16825 */
16826 static void
16827f_searchpos(argvars, rettv)
16828 typval_T *argvars;
16829 typval_T *rettv;
16830{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016831 pos_T match_pos;
16832 int lnum = 0;
16833 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016834 int n;
16835 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016836
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016837 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016838 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016839
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016840 n = search_cmn(argvars, &match_pos, &flags);
16841 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016842 {
16843 lnum = match_pos.lnum;
16844 col = match_pos.col;
16845 }
16846
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016847 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16848 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016849 if (flags & SP_SUBPAT)
16850 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016851}
16852
16853
Bram Moolenaar0d660222005-01-07 21:51:51 +000016854 static void
16855f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016856 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859#ifdef FEAT_CLIENTSERVER
16860 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016861 char_u *server = get_tv_string_chk(&argvars[0]);
16862 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 if (server == NULL || reply == NULL)
16866 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867 if (check_restricted() || check_secure())
16868 return;
16869# ifdef FEAT_X11
16870 if (check_connection() == FAIL)
16871 return;
16872# endif
16873
16874 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016876 EMSG(_("E258: Unable to send to client"));
16877 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016879 rettv->vval.v_number = 0;
16880#else
16881 rettv->vval.v_number = -1;
16882#endif
16883}
16884
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885 static void
16886f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016887 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016888 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889{
16890 char_u *r = NULL;
16891
16892#ifdef FEAT_CLIENTSERVER
16893# ifdef WIN32
16894 r = serverGetVimNames();
16895# else
16896 make_connection();
16897 if (X_DISPLAY != NULL)
16898 r = serverGetVimNames(X_DISPLAY);
16899# endif
16900#endif
16901 rettv->v_type = VAR_STRING;
16902 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903}
16904
16905/*
16906 * "setbufvar()" function
16907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016909f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016911 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912{
16913 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016915 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 char_u nbuf[NUMBUFLEN];
16918
16919 if (check_restricted() || check_secure())
16920 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016921 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16922 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016923 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 varp = &argvars[2];
16925
16926 if (buf != NULL && varname != NULL && varp != NULL)
16927 {
16928 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930
16931 if (*varname == '&')
16932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016933 long numval;
16934 char_u *strval;
16935 int error = FALSE;
16936
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016938 numval = get_tv_number_chk(varp, &error);
16939 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016940 if (!error && strval != NULL)
16941 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 }
16943 else
16944 {
16945 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16946 if (bufvarname != NULL)
16947 {
16948 STRCPY(bufvarname, "b:");
16949 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016950 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 vim_free(bufvarname);
16952 }
16953 }
16954
16955 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958}
16959
16960/*
16961 * "setcmdpos()" function
16962 */
16963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016964f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016965 typval_T *argvars;
16966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016968 int pos = (int)get_tv_number(&argvars[0]) - 1;
16969
16970 if (pos >= 0)
16971 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972}
16973
16974/*
16975 * "setline()" function
16976 */
16977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016978f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 typval_T *argvars;
16980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981{
16982 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016983 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016984 list_T *l = NULL;
16985 listitem_T *li = NULL;
16986 long added = 0;
16987 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016989 lnum = get_tv_lnum(&argvars[0]);
16990 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016992 l = argvars[1].vval.v_list;
16993 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016995 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016996 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016997
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016998 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016999 for (;;)
17000 {
17001 if (l != NULL)
17002 {
17003 /* list argument, get next string */
17004 if (li == NULL)
17005 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017007 li = li->li_next;
17008 }
17009
17010 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017011 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017012 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017013
17014 /* When coming here from Insert mode, sync undo, so that this can be
17015 * undone separately from what was previously inserted. */
17016 if (u_sync_once == 2)
17017 {
17018 u_sync_once = 1; /* notify that u_sync() was called */
17019 u_sync(TRUE);
17020 }
17021
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017022 if (lnum <= curbuf->b_ml.ml_line_count)
17023 {
17024 /* existing line, replace it */
17025 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17026 {
17027 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017028 if (lnum == curwin->w_cursor.lnum)
17029 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017030 rettv->vval.v_number = 0; /* OK */
17031 }
17032 }
17033 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17034 {
17035 /* lnum is one past the last line, append the line */
17036 ++added;
17037 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17038 rettv->vval.v_number = 0; /* OK */
17039 }
17040
17041 if (l == NULL) /* only one string argument */
17042 break;
17043 ++lnum;
17044 }
17045
17046 if (added > 0)
17047 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048}
17049
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017050static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17051
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017053 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017054 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017055 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017056set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017057 win_T *wp UNUSED;
17058 typval_T *list_arg UNUSED;
17059 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017060 typval_T *rettv;
17061{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017062#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017063 char_u *act;
17064 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017065#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017066
Bram Moolenaar2641f772005-03-25 21:58:17 +000017067 rettv->vval.v_number = -1;
17068
17069#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017070 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017071 EMSG(_(e_listreq));
17072 else
17073 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017074 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017075
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017076 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017077 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017078 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017079 if (act == NULL)
17080 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017081 if (*act == 'a' || *act == 'r')
17082 action = *act;
17083 }
17084
Bram Moolenaar81484f42012-12-05 15:16:47 +010017085 if (l != NULL && set_errorlist(wp, l, action,
17086 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017087 rettv->vval.v_number = 0;
17088 }
17089#endif
17090}
17091
17092/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017093 * "setloclist()" function
17094 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017095 static void
17096f_setloclist(argvars, rettv)
17097 typval_T *argvars;
17098 typval_T *rettv;
17099{
17100 win_T *win;
17101
17102 rettv->vval.v_number = -1;
17103
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017104 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017105 if (win != NULL)
17106 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17107}
17108
17109/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017110 * "setmatches()" function
17111 */
17112 static void
17113f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017114 typval_T *argvars UNUSED;
17115 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017116{
17117#ifdef FEAT_SEARCH_EXTRA
17118 list_T *l;
17119 listitem_T *li;
17120 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017121 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017122
17123 rettv->vval.v_number = -1;
17124 if (argvars[0].v_type != VAR_LIST)
17125 {
17126 EMSG(_(e_listreq));
17127 return;
17128 }
17129 if ((l = argvars[0].vval.v_list) != NULL)
17130 {
17131
17132 /* To some extent make sure that we are dealing with a list from
17133 * "getmatches()". */
17134 li = l->lv_first;
17135 while (li != NULL)
17136 {
17137 if (li->li_tv.v_type != VAR_DICT
17138 || (d = li->li_tv.vval.v_dict) == NULL)
17139 {
17140 EMSG(_(e_invarg));
17141 return;
17142 }
17143 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017144 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17145 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017146 && dict_find(d, (char_u *)"priority", -1) != NULL
17147 && dict_find(d, (char_u *)"id", -1) != NULL))
17148 {
17149 EMSG(_(e_invarg));
17150 return;
17151 }
17152 li = li->li_next;
17153 }
17154
17155 clear_matches(curwin);
17156 li = l->lv_first;
17157 while (li != NULL)
17158 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017159 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017160 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017161 dictitem_T *di;
17162
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017163 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017164
17165 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17166 {
17167 if (s == NULL)
17168 {
17169 s = list_alloc();
17170 if (s == NULL)
17171 return;
17172 }
17173
17174 /* match from matchaddpos() */
17175 for (i = 1; i < 9; i++)
17176 {
17177 sprintf((char *)buf, (char *)"pos%d", i);
17178 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17179 {
17180 if (di->di_tv.v_type != VAR_LIST)
17181 return;
17182
17183 list_append_tv(s, &di->di_tv);
17184 s->lv_refcount++;
17185 }
17186 else
17187 break;
17188 }
17189 }
17190 if (i == 0)
17191 {
17192 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017193 get_dict_string(d, (char_u *)"pattern", FALSE),
17194 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020017195 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017196 }
17197 else
17198 {
17199 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
17200 NULL, (int)get_dict_number(d, (char_u *)"priority"),
17201 (int)get_dict_number(d, (char_u *)"id"), s);
17202 list_unref(s);
17203 s = NULL;
17204 }
17205
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017206 li = li->li_next;
17207 }
17208 rettv->vval.v_number = 0;
17209 }
17210#endif
17211}
17212
17213/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017214 * "setpos()" function
17215 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017216 static void
17217f_setpos(argvars, rettv)
17218 typval_T *argvars;
17219 typval_T *rettv;
17220{
17221 pos_T pos;
17222 int fnum;
17223 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017224 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017225
Bram Moolenaar08250432008-02-13 11:42:46 +000017226 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017227 name = get_tv_string_chk(argvars);
17228 if (name != NULL)
17229 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017230 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017231 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017232 if (--pos.col < 0)
17233 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017234 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017235 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017236 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017237 if (fnum == curbuf->b_fnum)
17238 {
17239 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017240 if (curswant >= 0)
17241 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017242 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017243 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017244 }
17245 else
17246 EMSG(_(e_invarg));
17247 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017248 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17249 {
17250 /* set mark */
17251 if (setmark_pos(name[1], &pos, fnum) == OK)
17252 rettv->vval.v_number = 0;
17253 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017254 else
17255 EMSG(_(e_invarg));
17256 }
17257 }
17258}
17259
17260/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017261 * "setqflist()" function
17262 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017263 static void
17264f_setqflist(argvars, rettv)
17265 typval_T *argvars;
17266 typval_T *rettv;
17267{
17268 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17269}
17270
17271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 * "setreg()" function
17273 */
17274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 typval_T *argvars;
17277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278{
17279 int regname;
17280 char_u *strregname;
17281 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017282 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 int append;
17284 char_u yank_type;
17285 long block_len;
17286
17287 block_len = -1;
17288 yank_type = MAUTO;
17289 append = FALSE;
17290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017291 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017292 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017294 if (strregname == NULL)
17295 return; /* type error; errmsg already given */
17296 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 if (regname == 0 || regname == '@')
17298 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017300 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017302 stropt = get_tv_string_chk(&argvars[2]);
17303 if (stropt == NULL)
17304 return; /* type error */
17305 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 switch (*stropt)
17307 {
17308 case 'a': case 'A': /* append */
17309 append = TRUE;
17310 break;
17311 case 'v': case 'c': /* character-wise selection */
17312 yank_type = MCHAR;
17313 break;
17314 case 'V': case 'l': /* line-wise selection */
17315 yank_type = MLINE;
17316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 case 'b': case Ctrl_V: /* block-wise selection */
17318 yank_type = MBLOCK;
17319 if (VIM_ISDIGIT(stropt[1]))
17320 {
17321 ++stropt;
17322 block_len = getdigits(&stropt) - 1;
17323 --stropt;
17324 }
17325 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 }
17327 }
17328
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017329 if (argvars[1].v_type == VAR_LIST)
17330 {
17331 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017332 char_u **allocval;
17333 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017334 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017335 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017336 int len = argvars[1].vval.v_list->lv_len;
17337 listitem_T *li;
17338
Bram Moolenaar7d647822014-04-05 21:28:56 +020017339 /* First half: use for pointers to result lines; second half: use for
17340 * pointers to allocated copies. */
17341 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017342 if (lstval == NULL)
17343 return;
17344 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017345 allocval = lstval + len + 2;
17346 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017347
17348 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17349 li = li->li_next)
17350 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017351 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017352 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017353 goto free_lstval;
17354 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017355 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017356 /* Need to make a copy, next get_tv_string_buf_chk() will
17357 * overwrite the string. */
17358 strval = vim_strsave(buf);
17359 if (strval == NULL)
17360 goto free_lstval;
17361 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017362 }
17363 *curval++ = strval;
17364 }
17365 *curval++ = NULL;
17366
17367 write_reg_contents_lst(regname, lstval, -1,
17368 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017369free_lstval:
17370 while (curallocval > allocval)
17371 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017372 vim_free(lstval);
17373 }
17374 else
17375 {
17376 strval = get_tv_string_chk(&argvars[1]);
17377 if (strval == NULL)
17378 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017379 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017382 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383}
17384
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017385/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017386 * "settabvar()" function
17387 */
17388 static void
17389f_settabvar(argvars, rettv)
17390 typval_T *argvars;
17391 typval_T *rettv;
17392{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017393#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017394 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017395 tabpage_T *tp;
17396#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017397 char_u *varname, *tabvarname;
17398 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017399
17400 rettv->vval.v_number = 0;
17401
17402 if (check_restricted() || check_secure())
17403 return;
17404
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017405#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017406 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017407#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017408 varname = get_tv_string_chk(&argvars[1]);
17409 varp = &argvars[2];
17410
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017411 if (varname != NULL && varp != NULL
17412#ifdef FEAT_WINDOWS
17413 && tp != NULL
17414#endif
17415 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017416 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017417#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017418 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017419 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017420#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017421
17422 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17423 if (tabvarname != NULL)
17424 {
17425 STRCPY(tabvarname, "t:");
17426 STRCPY(tabvarname + 2, varname);
17427 set_var(tabvarname, varp, TRUE);
17428 vim_free(tabvarname);
17429 }
17430
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017431#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017432 /* Restore current tabpage */
17433 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017434 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017435#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017436 }
17437}
17438
17439/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017440 * "settabwinvar()" function
17441 */
17442 static void
17443f_settabwinvar(argvars, rettv)
17444 typval_T *argvars;
17445 typval_T *rettv;
17446{
17447 setwinvar(argvars, rettv, 1);
17448}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017449
17450/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017451 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017454f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017455 typval_T *argvars;
17456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017458 setwinvar(argvars, rettv, 0);
17459}
17460
17461/*
17462 * "setwinvar()" and "settabwinvar()" functions
17463 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017464
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017465 static void
17466setwinvar(argvars, rettv, off)
17467 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017468 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017469 int off;
17470{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 win_T *win;
17472#ifdef FEAT_WINDOWS
17473 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017474 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475#endif
17476 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017477 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017479 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480
17481 if (check_restricted() || check_secure())
17482 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017483
17484#ifdef FEAT_WINDOWS
17485 if (off == 1)
17486 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17487 else
17488 tp = curtab;
17489#endif
17490 win = find_win_by_nr(&argvars[off], tp);
17491 varname = get_tv_string_chk(&argvars[off + 1]);
17492 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493
17494 if (win != NULL && varname != NULL && varp != NULL)
17495 {
17496#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017497 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017500 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017502 long numval;
17503 char_u *strval;
17504 int error = FALSE;
17505
17506 ++varname;
17507 numval = get_tv_number_chk(varp, &error);
17508 strval = get_tv_string_buf_chk(varp, nbuf);
17509 if (!error && strval != NULL)
17510 set_option_value(varname, numval, strval, OPT_LOCAL);
17511 }
17512 else
17513 {
17514 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17515 if (winvarname != NULL)
17516 {
17517 STRCPY(winvarname, "w:");
17518 STRCPY(winvarname + 2, varname);
17519 set_var(winvarname, varp, TRUE);
17520 vim_free(winvarname);
17521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 }
17523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017525 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526#endif
17527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528}
17529
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017530#ifdef FEAT_CRYPT
17531/*
17532 * "sha256({string})" function
17533 */
17534 static void
17535f_sha256(argvars, rettv)
17536 typval_T *argvars;
17537 typval_T *rettv;
17538{
17539 char_u *p;
17540
17541 p = get_tv_string(&argvars[0]);
17542 rettv->vval.v_string = vim_strsave(
17543 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17544 rettv->v_type = VAR_STRING;
17545}
17546#endif /* FEAT_CRYPT */
17547
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017549 * "shellescape({string})" function
17550 */
17551 static void
17552f_shellescape(argvars, rettv)
17553 typval_T *argvars;
17554 typval_T *rettv;
17555{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017556 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017557 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017558 rettv->v_type = VAR_STRING;
17559}
17560
17561/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017562 * shiftwidth() function
17563 */
17564 static void
17565f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017566 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017567 typval_T *rettv;
17568{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017569 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017570}
17571
17572/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 */
17575 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017577 typval_T *argvars;
17578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017580 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581
Bram Moolenaar0d660222005-01-07 21:51:51 +000017582 p = get_tv_string(&argvars[0]);
17583 rettv->vval.v_string = vim_strsave(p);
17584 simplify_filename(rettv->vval.v_string); /* simplify in place */
17585 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586}
17587
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017588#ifdef FEAT_FLOAT
17589/*
17590 * "sin()" function
17591 */
17592 static void
17593f_sin(argvars, rettv)
17594 typval_T *argvars;
17595 typval_T *rettv;
17596{
17597 float_T f;
17598
17599 rettv->v_type = VAR_FLOAT;
17600 if (get_float_arg(argvars, &f) == OK)
17601 rettv->vval.v_float = sin(f);
17602 else
17603 rettv->vval.v_float = 0.0;
17604}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017605
17606/*
17607 * "sinh()" function
17608 */
17609 static void
17610f_sinh(argvars, rettv)
17611 typval_T *argvars;
17612 typval_T *rettv;
17613{
17614 float_T f;
17615
17616 rettv->v_type = VAR_FLOAT;
17617 if (get_float_arg(argvars, &f) == OK)
17618 rettv->vval.v_float = sinh(f);
17619 else
17620 rettv->vval.v_float = 0.0;
17621}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017622#endif
17623
Bram Moolenaar0d660222005-01-07 21:51:51 +000017624static int
17625#ifdef __BORLANDC__
17626 _RTLENTRYF
17627#endif
17628 item_compare __ARGS((const void *s1, const void *s2));
17629static int
17630#ifdef __BORLANDC__
17631 _RTLENTRYF
17632#endif
17633 item_compare2 __ARGS((const void *s1, const void *s2));
17634
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017635/* struct used in the array that's given to qsort() */
17636typedef struct
17637{
17638 listitem_T *item;
17639 int idx;
17640} sortItem_T;
17641
Bram Moolenaar0d660222005-01-07 21:51:51 +000017642static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017643static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017644static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017645static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017646static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017647static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017648static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017649#define ITEM_COMPARE_FAIL 999
17650
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017652 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017654 static int
17655#ifdef __BORLANDC__
17656_RTLENTRYF
17657#endif
17658item_compare(s1, s2)
17659 const void *s1;
17660 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017662 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017663 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017664 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017665 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017666 int res;
17667 char_u numbuf1[NUMBUFLEN];
17668 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017670 si1 = (sortItem_T *)s1;
17671 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017672 tv1 = &si1->item->li_tv;
17673 tv2 = &si2->item->li_tv;
17674 /* tv2string() puts quotes around a string and allocates memory. Don't do
17675 * that for string variables. Use a single quote when comparing with a
17676 * non-string to do what the docs promise. */
17677 if (tv1->v_type == VAR_STRING)
17678 {
17679 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17680 p1 = (char_u *)"'";
17681 else
17682 p1 = tv1->vval.v_string;
17683 }
17684 else
17685 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17686 if (tv2->v_type == VAR_STRING)
17687 {
17688 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17689 p2 = (char_u *)"'";
17690 else
17691 p2 = tv2->vval.v_string;
17692 }
17693 else
17694 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017695 if (p1 == NULL)
17696 p1 = (char_u *)"";
17697 if (p2 == NULL)
17698 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017699 if (!item_compare_numeric)
17700 {
17701 if (item_compare_ic)
17702 res = STRICMP(p1, p2);
17703 else
17704 res = STRCMP(p1, p2);
17705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017707 {
17708 double n1, n2;
17709 n1 = strtod((char *)p1, (char **)&p1);
17710 n2 = strtod((char *)p2, (char **)&p2);
17711 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17712 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017713
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017714 /* When the result would be zero, compare the item indexes. Makes the
17715 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017716 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017717 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017718
Bram Moolenaar0d660222005-01-07 21:51:51 +000017719 vim_free(tofree1);
17720 vim_free(tofree2);
17721 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722}
17723
17724 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017725#ifdef __BORLANDC__
17726_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017728item_compare2(s1, s2)
17729 const void *s1;
17730 const void *s2;
17731{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017732 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017733 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017734 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017735 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017736 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017738 /* shortcut after failure in previous call; compare all items equal */
17739 if (item_compare_func_err)
17740 return 0;
17741
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017742 si1 = (sortItem_T *)s1;
17743 si2 = (sortItem_T *)s2;
17744
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017745 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017746 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017747 copy_tv(&si1->item->li_tv, &argv[0]);
17748 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017749
17750 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017751 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017752 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17753 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017754 clear_tv(&argv[0]);
17755 clear_tv(&argv[1]);
17756
17757 if (res == FAIL)
17758 res = ITEM_COMPARE_FAIL;
17759 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17761 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017762 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017764
17765 /* When the result would be zero, compare the pointers themselves. Makes
17766 * the sort stable. */
17767 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017768 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017769
Bram Moolenaar0d660222005-01-07 21:51:51 +000017770 return res;
17771}
17772
17773/*
17774 * "sort({list})" function
17775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017777do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 typval_T *argvars;
17779 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017780 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781{
Bram Moolenaar33570922005-01-25 22:26:29 +000017782 list_T *l;
17783 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017784 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017785 long len;
17786 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787
Bram Moolenaar0d660222005-01-07 21:51:51 +000017788 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017789 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 else
17791 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017793 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020017794 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
17795 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017796 return;
17797 rettv->vval.v_list = l;
17798 rettv->v_type = VAR_LIST;
17799 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800
Bram Moolenaar0d660222005-01-07 21:51:51 +000017801 len = list_len(l);
17802 if (len <= 1)
17803 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804
Bram Moolenaar0d660222005-01-07 21:51:51 +000017805 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017806 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017807 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017808 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017809 if (argvars[1].v_type != VAR_UNKNOWN)
17810 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017811 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017812 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017813 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017814 else
17815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017816 int error = FALSE;
17817
17818 i = get_tv_number_chk(&argvars[1], &error);
17819 if (error)
17820 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017821 if (i == 1)
17822 item_compare_ic = TRUE;
17823 else
17824 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017825 if (item_compare_func != NULL)
17826 {
17827 if (STRCMP(item_compare_func, "n") == 0)
17828 {
17829 item_compare_func = NULL;
17830 item_compare_numeric = TRUE;
17831 }
17832 else if (STRCMP(item_compare_func, "i") == 0)
17833 {
17834 item_compare_func = NULL;
17835 item_compare_ic = TRUE;
17836 }
17837 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017838 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017839
17840 if (argvars[2].v_type != VAR_UNKNOWN)
17841 {
17842 /* optional third argument: {dict} */
17843 if (argvars[2].v_type != VAR_DICT)
17844 {
17845 EMSG(_(e_dictreq));
17846 return;
17847 }
17848 item_compare_selfdict = argvars[2].vval.v_dict;
17849 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851
Bram Moolenaar0d660222005-01-07 21:51:51 +000017852 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017853 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017854 if (ptrs == NULL)
17855 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856
Bram Moolenaar327aa022014-03-25 18:24:23 +010017857 i = 0;
17858 if (sort)
17859 {
17860 /* sort(): ptrs will be the list to sort */
17861 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017862 {
17863 ptrs[i].item = li;
17864 ptrs[i].idx = i;
17865 ++i;
17866 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017867
17868 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017869 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017870 /* test the compare function */
17871 if (item_compare_func != NULL
17872 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017873 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017874 EMSG(_("E702: Sort compare function failed"));
17875 else
17876 {
17877 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017878 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017879 item_compare_func == NULL ? item_compare : item_compare2);
17880
17881 if (!item_compare_func_err)
17882 {
17883 /* Clear the List and append the items in sorted order. */
17884 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17885 l->lv_len = 0;
17886 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017887 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017888 }
17889 }
17890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017892 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017893 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17894
17895 /* f_uniq(): ptrs will be a stack of items to remove */
17896 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017897 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017898 item_compare_func_ptr = item_compare_func
17899 ? item_compare2 : item_compare;
17900
17901 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17902 li = li->li_next)
17903 {
17904 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17905 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017906 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017907 if (item_compare_func_err)
17908 {
17909 EMSG(_("E882: Uniq compare function failed"));
17910 break;
17911 }
17912 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017914 if (!item_compare_func_err)
17915 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017916 while (--i >= 0)
17917 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017918 li = ptrs[i].item->li_next;
17919 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017920 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017921 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017922 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017923 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017924 list_fix_watch(l, li);
17925 listitem_free(li);
17926 l->lv_len--;
17927 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017928 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017929 }
17930
17931 vim_free(ptrs);
17932 }
17933}
17934
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017935/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017936 * "sort({list})" function
17937 */
17938 static void
17939f_sort(argvars, rettv)
17940 typval_T *argvars;
17941 typval_T *rettv;
17942{
17943 do_sort_uniq(argvars, rettv, TRUE);
17944}
17945
17946/*
17947 * "uniq({list})" function
17948 */
17949 static void
17950f_uniq(argvars, rettv)
17951 typval_T *argvars;
17952 typval_T *rettv;
17953{
17954 do_sort_uniq(argvars, rettv, FALSE);
17955}
17956
17957/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017958 * "soundfold({word})" function
17959 */
17960 static void
17961f_soundfold(argvars, rettv)
17962 typval_T *argvars;
17963 typval_T *rettv;
17964{
17965 char_u *s;
17966
17967 rettv->v_type = VAR_STRING;
17968 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017969#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017970 rettv->vval.v_string = eval_soundfold(s);
17971#else
17972 rettv->vval.v_string = vim_strsave(s);
17973#endif
17974}
17975
17976/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017977 * "spellbadword()" function
17978 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017979 static void
17980f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017981 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017982 typval_T *rettv;
17983{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017984 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017985 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017986 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017987
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017988 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017989 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017990
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017991#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017992 if (argvars[0].v_type == VAR_UNKNOWN)
17993 {
17994 /* Find the start and length of the badly spelled word. */
17995 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17996 if (len != 0)
17997 word = ml_get_cursor();
17998 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017999 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018000 {
18001 char_u *str = get_tv_string_chk(&argvars[0]);
18002 int capcol = -1;
18003
18004 if (str != NULL)
18005 {
18006 /* Check the argument for spelling. */
18007 while (*str != NUL)
18008 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018009 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018010 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018011 {
18012 word = str;
18013 break;
18014 }
18015 str += len;
18016 }
18017 }
18018 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018019#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018020
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018021 list_append_string(rettv->vval.v_list, word, len);
18022 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018023 attr == HLF_SPB ? "bad" :
18024 attr == HLF_SPR ? "rare" :
18025 attr == HLF_SPL ? "local" :
18026 attr == HLF_SPC ? "caps" :
18027 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018028}
18029
18030/*
18031 * "spellsuggest()" function
18032 */
18033 static void
18034f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018035 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018036 typval_T *rettv;
18037{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018038#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018039 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018040 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018041 int maxcount;
18042 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018043 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018044 listitem_T *li;
18045 int need_capital = FALSE;
18046#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018047
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018048 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018049 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018050
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018051#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018052 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018053 {
18054 str = get_tv_string(&argvars[0]);
18055 if (argvars[1].v_type != VAR_UNKNOWN)
18056 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018057 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018058 if (maxcount <= 0)
18059 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018060 if (argvars[2].v_type != VAR_UNKNOWN)
18061 {
18062 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18063 if (typeerr)
18064 return;
18065 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018066 }
18067 else
18068 maxcount = 25;
18069
Bram Moolenaar4770d092006-01-12 23:22:24 +000018070 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018071
18072 for (i = 0; i < ga.ga_len; ++i)
18073 {
18074 str = ((char_u **)ga.ga_data)[i];
18075
18076 li = listitem_alloc();
18077 if (li == NULL)
18078 vim_free(str);
18079 else
18080 {
18081 li->li_tv.v_type = VAR_STRING;
18082 li->li_tv.v_lock = 0;
18083 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018084 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018085 }
18086 }
18087 ga_clear(&ga);
18088 }
18089#endif
18090}
18091
Bram Moolenaar0d660222005-01-07 21:51:51 +000018092 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018093f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018094 typval_T *argvars;
18095 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018096{
18097 char_u *str;
18098 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018099 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018100 regmatch_T regmatch;
18101 char_u patbuf[NUMBUFLEN];
18102 char_u *save_cpo;
18103 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018104 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018105 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018106 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018107
18108 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18109 save_cpo = p_cpo;
18110 p_cpo = (char_u *)"";
18111
18112 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018113 if (argvars[1].v_type != VAR_UNKNOWN)
18114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018115 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18116 if (pat == NULL)
18117 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018118 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018119 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018120 }
18121 if (pat == NULL || *pat == NUL)
18122 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018124 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018126 if (typeerr)
18127 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128
Bram Moolenaar0d660222005-01-07 21:51:51 +000018129 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18130 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018132 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018133 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018134 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018135 if (*str == NUL)
18136 match = FALSE; /* empty item at the end */
18137 else
18138 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018139 if (match)
18140 end = regmatch.startp[0];
18141 else
18142 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018143 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18144 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018145 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018146 if (list_append_string(rettv->vval.v_list, str,
18147 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018148 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018149 }
18150 if (!match)
18151 break;
18152 /* Advance to just after the match. */
18153 if (regmatch.endp[0] > str)
18154 col = 0;
18155 else
18156 {
18157 /* Don't get stuck at the same match. */
18158#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018159 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160#else
18161 col = 1;
18162#endif
18163 }
18164 str = regmatch.endp[0];
18165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166
Bram Moolenaar473de612013-06-08 18:19:48 +020018167 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaar0d660222005-01-07 21:51:51 +000018170 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171}
18172
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018173#ifdef FEAT_FLOAT
18174/*
18175 * "sqrt()" function
18176 */
18177 static void
18178f_sqrt(argvars, rettv)
18179 typval_T *argvars;
18180 typval_T *rettv;
18181{
18182 float_T f;
18183
18184 rettv->v_type = VAR_FLOAT;
18185 if (get_float_arg(argvars, &f) == OK)
18186 rettv->vval.v_float = sqrt(f);
18187 else
18188 rettv->vval.v_float = 0.0;
18189}
18190
18191/*
18192 * "str2float()" function
18193 */
18194 static void
18195f_str2float(argvars, rettv)
18196 typval_T *argvars;
18197 typval_T *rettv;
18198{
18199 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18200
18201 if (*p == '+')
18202 p = skipwhite(p + 1);
18203 (void)string2float(p, &rettv->vval.v_float);
18204 rettv->v_type = VAR_FLOAT;
18205}
18206#endif
18207
Bram Moolenaar2c932302006-03-18 21:42:09 +000018208/*
18209 * "str2nr()" function
18210 */
18211 static void
18212f_str2nr(argvars, rettv)
18213 typval_T *argvars;
18214 typval_T *rettv;
18215{
18216 int base = 10;
18217 char_u *p;
18218 long n;
18219
18220 if (argvars[1].v_type != VAR_UNKNOWN)
18221 {
18222 base = get_tv_number(&argvars[1]);
18223 if (base != 8 && base != 10 && base != 16)
18224 {
18225 EMSG(_(e_invarg));
18226 return;
18227 }
18228 }
18229
18230 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018231 if (*p == '+')
18232 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018233 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18234 rettv->vval.v_number = n;
18235}
18236
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237#ifdef HAVE_STRFTIME
18238/*
18239 * "strftime({format}[, {time}])" function
18240 */
18241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018242f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018243 typval_T *argvars;
18244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245{
18246 char_u result_buf[256];
18247 struct tm *curtime;
18248 time_t seconds;
18249 char_u *p;
18250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018251 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018253 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018254 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 seconds = time(NULL);
18256 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 curtime = localtime(&seconds);
18259 /* MSVC returns NULL for an invalid value of seconds. */
18260 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018261 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 else
18263 {
18264# ifdef FEAT_MBYTE
18265 vimconv_T conv;
18266 char_u *enc;
18267
18268 conv.vc_type = CONV_NONE;
18269 enc = enc_locale();
18270 convert_setup(&conv, p_enc, enc);
18271 if (conv.vc_type != CONV_NONE)
18272 p = string_convert(&conv, p, NULL);
18273# endif
18274 if (p != NULL)
18275 (void)strftime((char *)result_buf, sizeof(result_buf),
18276 (char *)p, curtime);
18277 else
18278 result_buf[0] = NUL;
18279
18280# ifdef FEAT_MBYTE
18281 if (conv.vc_type != CONV_NONE)
18282 vim_free(p);
18283 convert_setup(&conv, enc, p_enc);
18284 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018285 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 else
18287# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018288 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289
18290# ifdef FEAT_MBYTE
18291 /* Release conversion descriptors */
18292 convert_setup(&conv, NULL, NULL);
18293 vim_free(enc);
18294# endif
18295 }
18296}
18297#endif
18298
18299/*
18300 * "stridx()" function
18301 */
18302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018303f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018304 typval_T *argvars;
18305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306{
18307 char_u buf[NUMBUFLEN];
18308 char_u *needle;
18309 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018310 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018312 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018314 needle = get_tv_string_chk(&argvars[1]);
18315 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018316 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018317 if (needle == NULL || haystack == NULL)
18318 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319
Bram Moolenaar33570922005-01-25 22:26:29 +000018320 if (argvars[2].v_type != VAR_UNKNOWN)
18321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018322 int error = FALSE;
18323
18324 start_idx = get_tv_number_chk(&argvars[2], &error);
18325 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018326 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018327 if (start_idx >= 0)
18328 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018329 }
18330
18331 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18332 if (pos != NULL)
18333 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334}
18335
18336/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018337 * "string()" function
18338 */
18339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018341 typval_T *argvars;
18342 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018343{
18344 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018347 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018348 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018349 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018350 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018351 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352}
18353
18354/*
18355 * "strlen()" function
18356 */
18357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018358f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018359 typval_T *argvars;
18360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362 rettv->vval.v_number = (varnumber_T)(STRLEN(
18363 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364}
18365
18366/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018367 * "strchars()" function
18368 */
18369 static void
18370f_strchars(argvars, rettv)
18371 typval_T *argvars;
18372 typval_T *rettv;
18373{
18374 char_u *s = get_tv_string(&argvars[0]);
18375#ifdef FEAT_MBYTE
18376 varnumber_T len = 0;
18377
18378 while (*s != NUL)
18379 {
18380 mb_cptr2char_adv(&s);
18381 ++len;
18382 }
18383 rettv->vval.v_number = len;
18384#else
18385 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18386#endif
18387}
18388
18389/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018390 * "strdisplaywidth()" function
18391 */
18392 static void
18393f_strdisplaywidth(argvars, rettv)
18394 typval_T *argvars;
18395 typval_T *rettv;
18396{
18397 char_u *s = get_tv_string(&argvars[0]);
18398 int col = 0;
18399
18400 if (argvars[1].v_type != VAR_UNKNOWN)
18401 col = get_tv_number(&argvars[1]);
18402
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018403 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018404}
18405
18406/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018407 * "strwidth()" function
18408 */
18409 static void
18410f_strwidth(argvars, rettv)
18411 typval_T *argvars;
18412 typval_T *rettv;
18413{
18414 char_u *s = get_tv_string(&argvars[0]);
18415
18416 rettv->vval.v_number = (varnumber_T)(
18417#ifdef FEAT_MBYTE
18418 mb_string2cells(s, -1)
18419#else
18420 STRLEN(s)
18421#endif
18422 );
18423}
18424
18425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426 * "strpart()" function
18427 */
18428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 typval_T *argvars;
18431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432{
18433 char_u *p;
18434 int n;
18435 int len;
18436 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018437 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018439 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 slen = (int)STRLEN(p);
18441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018442 n = get_tv_number_chk(&argvars[1], &error);
18443 if (error)
18444 len = 0;
18445 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018446 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 else
18448 len = slen - n; /* default len: all bytes that are available. */
18449
18450 /*
18451 * Only return the overlap between the specified part and the actual
18452 * string.
18453 */
18454 if (n < 0)
18455 {
18456 len += n;
18457 n = 0;
18458 }
18459 else if (n > slen)
18460 n = slen;
18461 if (len < 0)
18462 len = 0;
18463 else if (n + len > slen)
18464 len = slen - n;
18465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018466 rettv->v_type = VAR_STRING;
18467 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468}
18469
18470/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018471 * "strridx()" function
18472 */
18473 static void
18474f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018475 typval_T *argvars;
18476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018477{
18478 char_u buf[NUMBUFLEN];
18479 char_u *needle;
18480 char_u *haystack;
18481 char_u *rest;
18482 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018483 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018485 needle = get_tv_string_chk(&argvars[1]);
18486 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018487
18488 rettv->vval.v_number = -1;
18489 if (needle == NULL || haystack == NULL)
18490 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018491
18492 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018493 if (argvars[2].v_type != VAR_UNKNOWN)
18494 {
18495 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018496 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018497 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018498 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018499 }
18500 else
18501 end_idx = haystack_len;
18502
Bram Moolenaar0d660222005-01-07 21:51:51 +000018503 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018504 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018505 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018506 lastmatch = haystack + end_idx;
18507 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018508 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018509 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018510 for (rest = haystack; *rest != '\0'; ++rest)
18511 {
18512 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018513 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018514 break;
18515 lastmatch = rest;
18516 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018517 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018518
18519 if (lastmatch == NULL)
18520 rettv->vval.v_number = -1;
18521 else
18522 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18523}
18524
18525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 * "strtrans()" function
18527 */
18528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018529f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018530 typval_T *argvars;
18531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018533 rettv->v_type = VAR_STRING;
18534 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535}
18536
18537/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018538 * "submatch()" function
18539 */
18540 static void
18541f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018542 typval_T *argvars;
18543 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018544{
Bram Moolenaar41571762014-04-02 19:00:58 +020018545 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018546 int no;
18547 int retList = 0;
18548
18549 no = (int)get_tv_number_chk(&argvars[0], &error);
18550 if (error)
18551 return;
18552 error = FALSE;
18553 if (argvars[1].v_type != VAR_UNKNOWN)
18554 retList = get_tv_number_chk(&argvars[1], &error);
18555 if (error)
18556 return;
18557
18558 if (retList == 0)
18559 {
18560 rettv->v_type = VAR_STRING;
18561 rettv->vval.v_string = reg_submatch(no);
18562 }
18563 else
18564 {
18565 rettv->v_type = VAR_LIST;
18566 rettv->vval.v_list = reg_submatch_list(no);
18567 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018568}
18569
18570/*
18571 * "substitute()" function
18572 */
18573 static void
18574f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018575 typval_T *argvars;
18576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018577{
18578 char_u patbuf[NUMBUFLEN];
18579 char_u subbuf[NUMBUFLEN];
18580 char_u flagsbuf[NUMBUFLEN];
18581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018582 char_u *str = get_tv_string_chk(&argvars[0]);
18583 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18584 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18585 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18586
Bram Moolenaar0d660222005-01-07 21:51:51 +000018587 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018588 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18589 rettv->vval.v_string = NULL;
18590 else
18591 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018592}
18593
18594/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018595 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018598f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018599 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601{
18602 int id = 0;
18603#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018604 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 long col;
18606 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018607 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018609 lnum = get_tv_lnum(argvars); /* -1 on type error */
18610 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18611 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018613 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018614 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018615 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616#endif
18617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018618 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619}
18620
18621/*
18622 * "synIDattr(id, what [, mode])" function
18623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018626 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628{
18629 char_u *p = NULL;
18630#ifdef FEAT_SYN_HL
18631 int id;
18632 char_u *what;
18633 char_u *mode;
18634 char_u modebuf[NUMBUFLEN];
18635 int modec;
18636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018637 id = get_tv_number(&argvars[0]);
18638 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018639 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018641 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018643 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 modec = 0; /* replace invalid with current */
18645 }
18646 else
18647 {
18648#ifdef FEAT_GUI
18649 if (gui.in_use)
18650 modec = 'g';
18651 else
18652#endif
18653 if (t_colors > 1)
18654 modec = 'c';
18655 else
18656 modec = 't';
18657 }
18658
18659
18660 switch (TOLOWER_ASC(what[0]))
18661 {
18662 case 'b':
18663 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18664 p = highlight_color(id, what, modec);
18665 else /* bold */
18666 p = highlight_has_attr(id, HL_BOLD, modec);
18667 break;
18668
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018669 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 p = highlight_color(id, what, modec);
18671 break;
18672
18673 case 'i':
18674 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18675 p = highlight_has_attr(id, HL_INVERSE, modec);
18676 else /* italic */
18677 p = highlight_has_attr(id, HL_ITALIC, modec);
18678 break;
18679
18680 case 'n': /* name */
18681 p = get_highlight_name(NULL, id - 1);
18682 break;
18683
18684 case 'r': /* reverse */
18685 p = highlight_has_attr(id, HL_INVERSE, modec);
18686 break;
18687
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018688 case 's':
18689 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18690 p = highlight_color(id, what, modec);
18691 else /* standout */
18692 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 break;
18694
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018695 case 'u':
18696 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18697 /* underline */
18698 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18699 else
18700 /* undercurl */
18701 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 break;
18703 }
18704
18705 if (p != NULL)
18706 p = vim_strsave(p);
18707#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018708 rettv->v_type = VAR_STRING;
18709 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710}
18711
18712/*
18713 * "synIDtrans(id)" function
18714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018716f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018717 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719{
18720 int id;
18721
18722#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724
18725 if (id > 0)
18726 id = syn_get_final_id(id);
18727 else
18728#endif
18729 id = 0;
18730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732}
18733
18734/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018735 * "synconcealed(lnum, col)" function
18736 */
18737 static void
18738f_synconcealed(argvars, rettv)
18739 typval_T *argvars UNUSED;
18740 typval_T *rettv;
18741{
18742#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18743 long lnum;
18744 long col;
18745 int syntax_flags = 0;
18746 int cchar;
18747 int matchid = 0;
18748 char_u str[NUMBUFLEN];
18749#endif
18750
18751 rettv->v_type = VAR_LIST;
18752 rettv->vval.v_list = NULL;
18753
18754#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18755 lnum = get_tv_lnum(argvars); /* -1 on type error */
18756 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18757
18758 vim_memset(str, NUL, sizeof(str));
18759
18760 if (rettv_list_alloc(rettv) != FAIL)
18761 {
18762 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18763 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18764 && curwin->w_p_cole > 0)
18765 {
18766 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18767 syntax_flags = get_syntax_info(&matchid);
18768
18769 /* get the conceal character */
18770 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18771 {
18772 cchar = syn_get_sub_char();
18773 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18774 cchar = lcs_conceal;
18775 if (cchar != NUL)
18776 {
18777# ifdef FEAT_MBYTE
18778 if (has_mbyte)
18779 (*mb_char2bytes)(cchar, str);
18780 else
18781# endif
18782 str[0] = cchar;
18783 }
18784 }
18785 }
18786
18787 list_append_number(rettv->vval.v_list,
18788 (syntax_flags & HL_CONCEAL) != 0);
18789 /* -1 to auto-determine strlen */
18790 list_append_string(rettv->vval.v_list, str, -1);
18791 list_append_number(rettv->vval.v_list, matchid);
18792 }
18793#endif
18794}
18795
18796/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018797 * "synstack(lnum, col)" function
18798 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018799 static void
18800f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018801 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018802 typval_T *rettv;
18803{
18804#ifdef FEAT_SYN_HL
18805 long lnum;
18806 long col;
18807 int i;
18808 int id;
18809#endif
18810
18811 rettv->v_type = VAR_LIST;
18812 rettv->vval.v_list = NULL;
18813
18814#ifdef FEAT_SYN_HL
18815 lnum = get_tv_lnum(argvars); /* -1 on type error */
18816 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18817
18818 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018819 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018820 && rettv_list_alloc(rettv) != FAIL)
18821 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018822 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018823 for (i = 0; ; ++i)
18824 {
18825 id = syn_get_stack_item(i);
18826 if (id < 0)
18827 break;
18828 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18829 break;
18830 }
18831 }
18832#endif
18833}
18834
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018836get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018837 typval_T *argvars;
18838 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018839 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018841 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018843 char_u *infile = NULL;
18844 char_u buf[NUMBUFLEN];
18845 int err = FALSE;
18846 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018847 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018848 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018850 rettv->v_type = VAR_STRING;
18851 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018852 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018853 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018854
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018855 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018856 {
18857 /*
18858 * Write the string to a temp file, to be used for input of the shell
18859 * command.
18860 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020018861 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018862 {
18863 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018864 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018865 }
18866
18867 fd = mch_fopen((char *)infile, WRITEBIN);
18868 if (fd == NULL)
18869 {
18870 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018871 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018872 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018873 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018874 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018875 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18876 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018877 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018878 else
18879 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018880 size_t len;
18881
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018882 p = get_tv_string_buf_chk(&argvars[1], buf);
18883 if (p == NULL)
18884 {
18885 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018886 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018887 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018888 len = STRLEN(p);
18889 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018890 err = TRUE;
18891 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018892 if (fclose(fd) != 0)
18893 err = TRUE;
18894 if (err)
18895 {
18896 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018897 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018898 }
18899 }
18900
Bram Moolenaar52a72462014-08-29 15:53:52 +020018901 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18902 * echoes typeahead, that messes up the display. */
18903 if (!msg_silent)
18904 flags += SHELL_COOKED;
18905
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018906 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018908 int len;
18909 listitem_T *li;
18910 char_u *s = NULL;
18911 char_u *start;
18912 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018913 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914
Bram Moolenaar52a72462014-08-29 15:53:52 +020018915 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018916 if (res == NULL)
18917 goto errret;
18918
18919 list = list_alloc();
18920 if (list == NULL)
18921 goto errret;
18922
18923 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018925 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018926 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018927 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018928 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018929
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018930 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018931 if (s == NULL)
18932 goto errret;
18933
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018934 for (p = s; start < end; ++p, ++start)
18935 *p = *start == NUL ? NL : *start;
18936 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018937
18938 li = listitem_alloc();
18939 if (li == NULL)
18940 {
18941 vim_free(s);
18942 goto errret;
18943 }
18944 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018945 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018946 li->li_tv.vval.v_string = s;
18947 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018949
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018950 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018951 rettv->v_type = VAR_LIST;
18952 rettv->vval.v_list = list;
18953 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018955 else
18956 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018957 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018958#ifdef USE_CR
18959 /* translate <CR> into <NL> */
18960 if (res != NULL)
18961 {
18962 char_u *s;
18963
18964 for (s = res; *s; ++s)
18965 {
18966 if (*s == CAR)
18967 *s = NL;
18968 }
18969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970#else
18971# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018972 /* translate <CR><NL> into <NL> */
18973 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018975 char_u *s, *d;
18976
18977 d = res;
18978 for (s = res; *s; ++s)
18979 {
18980 if (s[0] == CAR && s[1] == NL)
18981 ++s;
18982 *d++ = *s;
18983 }
18984 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986# endif
18987#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018988 rettv->vval.v_string = res;
18989 res = NULL;
18990 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018991
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018992errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018993 if (infile != NULL)
18994 {
18995 mch_remove(infile);
18996 vim_free(infile);
18997 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018998 if (res != NULL)
18999 vim_free(res);
19000 if (list != NULL)
19001 list_free(list, TRUE);
19002}
19003
19004/*
19005 * "system()" function
19006 */
19007 static void
19008f_system(argvars, rettv)
19009 typval_T *argvars;
19010 typval_T *rettv;
19011{
19012 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19013}
19014
19015/*
19016 * "systemlist()" function
19017 */
19018 static void
19019f_systemlist(argvars, rettv)
19020 typval_T *argvars;
19021 typval_T *rettv;
19022{
19023 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024}
19025
19026/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019027 * "tabpagebuflist()" function
19028 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019029 static void
19030f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019031 typval_T *argvars UNUSED;
19032 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019033{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019034#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019035 tabpage_T *tp;
19036 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019037
19038 if (argvars[0].v_type == VAR_UNKNOWN)
19039 wp = firstwin;
19040 else
19041 {
19042 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19043 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019044 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019045 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019046 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019047 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019048 for (; wp != NULL; wp = wp->w_next)
19049 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019050 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019051 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019052 }
19053#endif
19054}
19055
19056
19057/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019058 * "tabpagenr()" function
19059 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019060 static void
19061f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019062 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019063 typval_T *rettv;
19064{
19065 int nr = 1;
19066#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019067 char_u *arg;
19068
19069 if (argvars[0].v_type != VAR_UNKNOWN)
19070 {
19071 arg = get_tv_string_chk(&argvars[0]);
19072 nr = 0;
19073 if (arg != NULL)
19074 {
19075 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019076 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019077 else
19078 EMSG2(_(e_invexpr2), arg);
19079 }
19080 }
19081 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019082 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019083#endif
19084 rettv->vval.v_number = nr;
19085}
19086
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019087
19088#ifdef FEAT_WINDOWS
19089static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19090
19091/*
19092 * Common code for tabpagewinnr() and winnr().
19093 */
19094 static int
19095get_winnr(tp, argvar)
19096 tabpage_T *tp;
19097 typval_T *argvar;
19098{
19099 win_T *twin;
19100 int nr = 1;
19101 win_T *wp;
19102 char_u *arg;
19103
19104 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19105 if (argvar->v_type != VAR_UNKNOWN)
19106 {
19107 arg = get_tv_string_chk(argvar);
19108 if (arg == NULL)
19109 nr = 0; /* type error; errmsg already given */
19110 else if (STRCMP(arg, "$") == 0)
19111 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19112 else if (STRCMP(arg, "#") == 0)
19113 {
19114 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19115 if (twin == NULL)
19116 nr = 0;
19117 }
19118 else
19119 {
19120 EMSG2(_(e_invexpr2), arg);
19121 nr = 0;
19122 }
19123 }
19124
19125 if (nr > 0)
19126 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19127 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019128 {
19129 if (wp == NULL)
19130 {
19131 /* didn't find it in this tabpage */
19132 nr = 0;
19133 break;
19134 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019135 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019136 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019137 return nr;
19138}
19139#endif
19140
19141/*
19142 * "tabpagewinnr()" function
19143 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019144 static void
19145f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019146 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019147 typval_T *rettv;
19148{
19149 int nr = 1;
19150#ifdef FEAT_WINDOWS
19151 tabpage_T *tp;
19152
19153 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19154 if (tp == NULL)
19155 nr = 0;
19156 else
19157 nr = get_winnr(tp, &argvars[1]);
19158#endif
19159 rettv->vval.v_number = nr;
19160}
19161
19162
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019163/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019164 * "tagfiles()" function
19165 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019166 static void
19167f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019168 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019169 typval_T *rettv;
19170{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019171 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019172 tagname_T tn;
19173 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019174
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019175 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019176 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019177 fname = alloc(MAXPATHL);
19178 if (fname == NULL)
19179 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019180
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019181 for (first = TRUE; ; first = FALSE)
19182 if (get_tagfname(&tn, first, fname) == FAIL
19183 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019184 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019185 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019186 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019187}
19188
19189/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019190 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019191 */
19192 static void
19193f_taglist(argvars, rettv)
19194 typval_T *argvars;
19195 typval_T *rettv;
19196{
19197 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019198
19199 tag_pattern = get_tv_string(&argvars[0]);
19200
19201 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019202 if (*tag_pattern == NUL)
19203 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019204
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019205 if (rettv_list_alloc(rettv) == OK)
19206 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019207}
19208
19209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210 * "tempname()" function
19211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019213f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019214 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216{
19217 static int x = 'A';
19218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019220 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221
19222 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19223 * names. Skip 'I' and 'O', they are used for shell redirection. */
19224 do
19225 {
19226 if (x == 'Z')
19227 x = '0';
19228 else if (x == '9')
19229 x = 'A';
19230 else
19231 {
19232#ifdef EBCDIC
19233 if (x == 'I')
19234 x = 'J';
19235 else if (x == 'R')
19236 x = 'S';
19237 else
19238#endif
19239 ++x;
19240 }
19241 } while (x == 'I' || x == 'O');
19242}
19243
19244/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019245 * "test(list)" function: Just checking the walls...
19246 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019247 static void
19248f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019249 typval_T *argvars UNUSED;
19250 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019251{
19252 /* Used for unit testing. Change the code below to your liking. */
19253#if 0
19254 listitem_T *li;
19255 list_T *l;
19256 char_u *bad, *good;
19257
19258 if (argvars[0].v_type != VAR_LIST)
19259 return;
19260 l = argvars[0].vval.v_list;
19261 if (l == NULL)
19262 return;
19263 li = l->lv_first;
19264 if (li == NULL)
19265 return;
19266 bad = get_tv_string(&li->li_tv);
19267 li = li->li_next;
19268 if (li == NULL)
19269 return;
19270 good = get_tv_string(&li->li_tv);
19271 rettv->vval.v_number = test_edit_score(bad, good);
19272#endif
19273}
19274
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019275#ifdef FEAT_FLOAT
19276/*
19277 * "tan()" function
19278 */
19279 static void
19280f_tan(argvars, rettv)
19281 typval_T *argvars;
19282 typval_T *rettv;
19283{
19284 float_T f;
19285
19286 rettv->v_type = VAR_FLOAT;
19287 if (get_float_arg(argvars, &f) == OK)
19288 rettv->vval.v_float = tan(f);
19289 else
19290 rettv->vval.v_float = 0.0;
19291}
19292
19293/*
19294 * "tanh()" function
19295 */
19296 static void
19297f_tanh(argvars, rettv)
19298 typval_T *argvars;
19299 typval_T *rettv;
19300{
19301 float_T f;
19302
19303 rettv->v_type = VAR_FLOAT;
19304 if (get_float_arg(argvars, &f) == OK)
19305 rettv->vval.v_float = tanh(f);
19306 else
19307 rettv->vval.v_float = 0.0;
19308}
19309#endif
19310
Bram Moolenaard52d9742005-08-21 22:20:28 +000019311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 * "tolower(string)" function
19313 */
19314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019315f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019316 typval_T *argvars;
19317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318{
19319 char_u *p;
19320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019321 p = vim_strsave(get_tv_string(&argvars[0]));
19322 rettv->v_type = VAR_STRING;
19323 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324
19325 if (p != NULL)
19326 while (*p != NUL)
19327 {
19328#ifdef FEAT_MBYTE
19329 int l;
19330
19331 if (enc_utf8)
19332 {
19333 int c, lc;
19334
19335 c = utf_ptr2char(p);
19336 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019337 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 /* TODO: reallocate string when byte count changes. */
19339 if (utf_char2len(lc) == l)
19340 utf_char2bytes(lc, p);
19341 p += l;
19342 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019343 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 p += l; /* skip multi-byte character */
19345 else
19346#endif
19347 {
19348 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19349 ++p;
19350 }
19351 }
19352}
19353
19354/*
19355 * "toupper(string)" function
19356 */
19357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019358f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019359 typval_T *argvars;
19360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019362 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019363 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364}
19365
19366/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019367 * "tr(string, fromstr, tostr)" function
19368 */
19369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019370f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019371 typval_T *argvars;
19372 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019373{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019374 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019375 char_u *fromstr;
19376 char_u *tostr;
19377 char_u *p;
19378#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019379 int inlen;
19380 int fromlen;
19381 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019382 int idx;
19383 char_u *cpstr;
19384 int cplen;
19385 int first = TRUE;
19386#endif
19387 char_u buf[NUMBUFLEN];
19388 char_u buf2[NUMBUFLEN];
19389 garray_T ga;
19390
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019391 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019392 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19393 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019394
19395 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019396 rettv->v_type = VAR_STRING;
19397 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019398 if (fromstr == NULL || tostr == NULL)
19399 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019400 ga_init2(&ga, (int)sizeof(char), 80);
19401
19402#ifdef FEAT_MBYTE
19403 if (!has_mbyte)
19404#endif
19405 /* not multi-byte: fromstr and tostr must be the same length */
19406 if (STRLEN(fromstr) != STRLEN(tostr))
19407 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019408#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019409error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019410#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019411 EMSG2(_(e_invarg2), fromstr);
19412 ga_clear(&ga);
19413 return;
19414 }
19415
19416 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019417 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019418 {
19419#ifdef FEAT_MBYTE
19420 if (has_mbyte)
19421 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019422 inlen = (*mb_ptr2len)(in_str);
19423 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019424 cplen = inlen;
19425 idx = 0;
19426 for (p = fromstr; *p != NUL; p += fromlen)
19427 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019428 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019429 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019430 {
19431 for (p = tostr; *p != NUL; p += tolen)
19432 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019433 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019434 if (idx-- == 0)
19435 {
19436 cplen = tolen;
19437 cpstr = p;
19438 break;
19439 }
19440 }
19441 if (*p == NUL) /* tostr is shorter than fromstr */
19442 goto error;
19443 break;
19444 }
19445 ++idx;
19446 }
19447
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019448 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019449 {
19450 /* Check that fromstr and tostr have the same number of
19451 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019452 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019453 first = FALSE;
19454 for (p = tostr; *p != NUL; p += tolen)
19455 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019456 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019457 --idx;
19458 }
19459 if (idx != 0)
19460 goto error;
19461 }
19462
19463 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019464 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019465 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019466
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019467 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019468 }
19469 else
19470#endif
19471 {
19472 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019473 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019474 if (p != NULL)
19475 ga_append(&ga, tostr[p - fromstr]);
19476 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019477 ga_append(&ga, *in_str);
19478 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019479 }
19480 }
19481
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019482 /* add a terminating NUL */
19483 ga_grow(&ga, 1);
19484 ga_append(&ga, NUL);
19485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019486 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019487}
19488
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019489#ifdef FEAT_FLOAT
19490/*
19491 * "trunc({float})" function
19492 */
19493 static void
19494f_trunc(argvars, rettv)
19495 typval_T *argvars;
19496 typval_T *rettv;
19497{
19498 float_T f;
19499
19500 rettv->v_type = VAR_FLOAT;
19501 if (get_float_arg(argvars, &f) == OK)
19502 /* trunc() is not in C90, use floor() or ceil() instead. */
19503 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19504 else
19505 rettv->vval.v_float = 0.0;
19506}
19507#endif
19508
Bram Moolenaar8299df92004-07-10 09:47:34 +000019509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 * "type(expr)" function
19511 */
19512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019513f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 typval_T *argvars;
19515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019517 int n;
19518
19519 switch (argvars[0].v_type)
19520 {
19521 case VAR_NUMBER: n = 0; break;
19522 case VAR_STRING: n = 1; break;
19523 case VAR_FUNC: n = 2; break;
19524 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019525 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019526#ifdef FEAT_FLOAT
19527 case VAR_FLOAT: n = 5; break;
19528#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019529 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19530 }
19531 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532}
19533
19534/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019535 * "undofile(name)" function
19536 */
19537 static void
19538f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019539 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019540 typval_T *rettv;
19541{
19542 rettv->v_type = VAR_STRING;
19543#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019544 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019545 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019546
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019547 if (*fname == NUL)
19548 {
19549 /* If there is no file name there will be no undo file. */
19550 rettv->vval.v_string = NULL;
19551 }
19552 else
19553 {
19554 char_u *ffname = FullName_save(fname, FALSE);
19555
19556 if (ffname != NULL)
19557 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19558 vim_free(ffname);
19559 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019560 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019561#else
19562 rettv->vval.v_string = NULL;
19563#endif
19564}
19565
19566/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019567 * "undotree()" function
19568 */
19569 static void
19570f_undotree(argvars, rettv)
19571 typval_T *argvars UNUSED;
19572 typval_T *rettv;
19573{
19574 if (rettv_dict_alloc(rettv) == OK)
19575 {
19576 dict_T *dict = rettv->vval.v_dict;
19577 list_T *list;
19578
Bram Moolenaar730cde92010-06-27 05:18:54 +020019579 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019580 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019581 dict_add_nr_str(dict, "save_last",
19582 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019583 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19584 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019585 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019586
19587 list = list_alloc();
19588 if (list != NULL)
19589 {
19590 u_eval_tree(curbuf->b_u_oldhead, list);
19591 dict_add_list(dict, "entries", list);
19592 }
19593 }
19594}
19595
19596/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019597 * "values(dict)" function
19598 */
19599 static void
19600f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019601 typval_T *argvars;
19602 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019603{
19604 dict_list(argvars, rettv, 1);
19605}
19606
19607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 * "virtcol(string)" function
19609 */
19610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 typval_T *argvars;
19613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614{
19615 colnr_T vcol = 0;
19616 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019617 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019619 fp = var2fpos(&argvars[0], FALSE, &fnum);
19620 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19621 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 {
19623 getvvcol(curwin, fp, NULL, NULL, &vcol);
19624 ++vcol;
19625 }
19626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019627 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628}
19629
19630/*
19631 * "visualmode()" function
19632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019634f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019635 typval_T *argvars UNUSED;
19636 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 char_u str[2];
19639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019640 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 str[0] = curbuf->b_visual_mode_eval;
19642 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644
19645 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019646 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648}
19649
19650/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019651 * "wildmenumode()" function
19652 */
19653 static void
19654f_wildmenumode(argvars, rettv)
19655 typval_T *argvars UNUSED;
19656 typval_T *rettv UNUSED;
19657{
19658#ifdef FEAT_WILDMENU
19659 if (wild_menu_showing)
19660 rettv->vval.v_number = 1;
19661#endif
19662}
19663
19664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 * "winbufnr(nr)" function
19666 */
19667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 typval_T *argvars;
19670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671{
19672 win_T *wp;
19673
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019674 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019678 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679}
19680
19681/*
19682 * "wincol()" function
19683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019685f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019686 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688{
19689 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019690 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691}
19692
19693/*
19694 * "winheight(nr)" function
19695 */
19696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019697f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019698 typval_T *argvars;
19699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700{
19701 win_T *wp;
19702
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019703 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019705 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019707 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708}
19709
19710/*
19711 * "winline()" function
19712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019715 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717{
19718 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720}
19721
19722/*
19723 * "winnr()" function
19724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019727 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729{
19730 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019731
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019733 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736}
19737
19738/*
19739 * "winrestcmd()" function
19740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019743 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745{
19746#ifdef FEAT_WINDOWS
19747 win_T *wp;
19748 int winnr = 1;
19749 garray_T ga;
19750 char_u buf[50];
19751
19752 ga_init2(&ga, (int)sizeof(char), 70);
19753 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19754 {
19755 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19756 ga_concat(&ga, buf);
19757# ifdef FEAT_VERTSPLIT
19758 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19759 ga_concat(&ga, buf);
19760# endif
19761 ++winnr;
19762 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019763 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019767 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770}
19771
19772/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019773 * "winrestview()" function
19774 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019775 static void
19776f_winrestview(argvars, rettv)
19777 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019778 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019779{
19780 dict_T *dict;
19781
19782 if (argvars[0].v_type != VAR_DICT
19783 || (dict = argvars[0].vval.v_dict) == NULL)
19784 EMSG(_(e_invarg));
19785 else
19786 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019787 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19788 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19789 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19790 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019791#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019792 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19793 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019794#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019795 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19796 {
19797 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19798 curwin->w_set_curswant = FALSE;
19799 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019800
Bram Moolenaar82c25852014-05-28 16:47:16 +020019801 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19802 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019803#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019804 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19805 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019806#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019807 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19808 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19809 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19810 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019811
19812 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019813 win_new_height(curwin, curwin->w_height);
19814# ifdef FEAT_VERTSPLIT
19815 win_new_width(curwin, W_WIDTH(curwin));
19816# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019817 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019818
Bram Moolenaarb851a962014-10-31 15:45:52 +010019819 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019820 curwin->w_topline = 1;
19821 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19822 curwin->w_topline = curbuf->b_ml.ml_line_count;
19823#ifdef FEAT_DIFF
19824 check_topfill(curwin, TRUE);
19825#endif
19826 }
19827}
19828
19829/*
19830 * "winsaveview()" function
19831 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019832 static void
19833f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019834 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019835 typval_T *rettv;
19836{
19837 dict_T *dict;
19838
Bram Moolenaara800b422010-06-27 01:15:55 +020019839 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019840 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019841 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019842
19843 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19844 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19845#ifdef FEAT_VIRTUALEDIT
19846 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19847#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019848 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019849 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19850
19851 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19852#ifdef FEAT_DIFF
19853 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19854#endif
19855 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19856 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19857}
19858
19859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 * "winwidth(nr)" function
19861 */
19862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019863f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 typval_T *argvars;
19865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866{
19867 win_T *wp;
19868
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019869 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019871 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 else
19873#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019874 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019876 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877#endif
19878}
19879
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019881 * Write list of strings to file
19882 */
19883 static int
19884write_list(fd, list, binary)
19885 FILE *fd;
19886 list_T *list;
19887 int binary;
19888{
19889 listitem_T *li;
19890 int c;
19891 int ret = OK;
19892 char_u *s;
19893
19894 for (li = list->lv_first; li != NULL; li = li->li_next)
19895 {
19896 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19897 {
19898 if (*s == '\n')
19899 c = putc(NUL, fd);
19900 else
19901 c = putc(*s, fd);
19902 if (c == EOF)
19903 {
19904 ret = FAIL;
19905 break;
19906 }
19907 }
19908 if (!binary || li->li_next != NULL)
19909 if (putc('\n', fd) == EOF)
19910 {
19911 ret = FAIL;
19912 break;
19913 }
19914 if (ret == FAIL)
19915 {
19916 EMSG(_(e_write));
19917 break;
19918 }
19919 }
19920 return ret;
19921}
19922
19923/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019924 * "writefile()" function
19925 */
19926 static void
19927f_writefile(argvars, rettv)
19928 typval_T *argvars;
19929 typval_T *rettv;
19930{
19931 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019932 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019933 char_u *fname;
19934 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019935 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019936
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019937 if (check_restricted() || check_secure())
19938 return;
19939
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019940 if (argvars[0].v_type != VAR_LIST)
19941 {
19942 EMSG2(_(e_listarg), "writefile()");
19943 return;
19944 }
19945 if (argvars[0].vval.v_list == NULL)
19946 return;
19947
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019948 if (argvars[2].v_type != VAR_UNKNOWN)
19949 {
19950 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19951 binary = TRUE;
19952 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19953 append = TRUE;
19954 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019955
19956 /* Always open the file in binary mode, library functions have a mind of
19957 * their own about CR-LF conversion. */
19958 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019959 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19960 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019961 {
19962 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19963 ret = -1;
19964 }
19965 else
19966 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019967 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19968 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019969 fclose(fd);
19970 }
19971
19972 rettv->vval.v_number = ret;
19973}
19974
19975/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019976 * "xor(expr, expr)" function
19977 */
19978 static void
19979f_xor(argvars, rettv)
19980 typval_T *argvars;
19981 typval_T *rettv;
19982{
19983 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19984 ^ get_tv_number_chk(&argvars[1], NULL);
19985}
19986
19987
19988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019990 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 */
19992 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019993var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019995 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019996 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019998 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020000 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001
Bram Moolenaara5525202006-03-02 22:52:09 +000020002 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020003 if (varp->v_type == VAR_LIST)
20004 {
20005 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020006 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020007 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020008 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020009
20010 l = varp->vval.v_list;
20011 if (l == NULL)
20012 return NULL;
20013
20014 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020015 pos.lnum = list_find_nr(l, 0L, &error);
20016 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020017 return NULL; /* invalid line number */
20018
20019 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020020 pos.col = list_find_nr(l, 1L, &error);
20021 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020022 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020023 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020024
20025 /* We accept "$" for the column number: last column. */
20026 li = list_find(l, 1L);
20027 if (li != NULL && li->li_tv.v_type == VAR_STRING
20028 && li->li_tv.vval.v_string != NULL
20029 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20030 pos.col = len + 1;
20031
Bram Moolenaara5525202006-03-02 22:52:09 +000020032 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020033 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020034 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020035 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020036
Bram Moolenaara5525202006-03-02 22:52:09 +000020037#ifdef FEAT_VIRTUALEDIT
20038 /* Get the virtual offset. Defaults to zero. */
20039 pos.coladd = list_find_nr(l, 2L, &error);
20040 if (error)
20041 pos.coladd = 0;
20042#endif
20043
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020044 return &pos;
20045 }
20046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020047 name = get_tv_string_chk(varp);
20048 if (name == NULL)
20049 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020050 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020052 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20053 {
20054 if (VIsual_active)
20055 return &VIsual;
20056 return &curwin->w_cursor;
20057 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020058 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020060 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20062 return NULL;
20063 return pp;
20064 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020065
20066#ifdef FEAT_VIRTUALEDIT
20067 pos.coladd = 0;
20068#endif
20069
Bram Moolenaar477933c2007-07-17 14:32:23 +000020070 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020071 {
20072 pos.col = 0;
20073 if (name[1] == '0') /* "w0": first visible line */
20074 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020075 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020076 pos.lnum = curwin->w_topline;
20077 return &pos;
20078 }
20079 else if (name[1] == '$') /* "w$": last visible line */
20080 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020081 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020082 pos.lnum = curwin->w_botline - 1;
20083 return &pos;
20084 }
20085 }
20086 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020088 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 {
20090 pos.lnum = curbuf->b_ml.ml_line_count;
20091 pos.col = 0;
20092 }
20093 else
20094 {
20095 pos.lnum = curwin->w_cursor.lnum;
20096 pos.col = (colnr_T)STRLEN(ml_get_curline());
20097 }
20098 return &pos;
20099 }
20100 return NULL;
20101}
20102
20103/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020104 * Convert list in "arg" into a position and optional file number.
20105 * When "fnump" is NULL there is no file number, only 3 items.
20106 * Note that the column is passed on as-is, the caller may want to decrement
20107 * it to use 1 for the first column.
20108 * Return FAIL when conversion is not possible, doesn't check the position for
20109 * validity.
20110 */
20111 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020112list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020113 typval_T *arg;
20114 pos_T *posp;
20115 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020116 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020117{
20118 list_T *l = arg->vval.v_list;
20119 long i = 0;
20120 long n;
20121
Bram Moolenaar493c1782014-05-28 14:34:46 +020020122 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20123 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020124 if (arg->v_type != VAR_LIST
20125 || l == NULL
20126 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020127 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020128 return FAIL;
20129
20130 if (fnump != NULL)
20131 {
20132 n = list_find_nr(l, i++, NULL); /* fnum */
20133 if (n < 0)
20134 return FAIL;
20135 if (n == 0)
20136 n = curbuf->b_fnum; /* current buffer */
20137 *fnump = n;
20138 }
20139
20140 n = list_find_nr(l, i++, NULL); /* lnum */
20141 if (n < 0)
20142 return FAIL;
20143 posp->lnum = n;
20144
20145 n = list_find_nr(l, i++, NULL); /* col */
20146 if (n < 0)
20147 return FAIL;
20148 posp->col = n;
20149
20150#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020151 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020152 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020153 posp->coladd = 0;
20154 else
20155 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020156#endif
20157
Bram Moolenaar493c1782014-05-28 14:34:46 +020020158 if (curswantp != NULL)
20159 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20160
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020161 return OK;
20162}
20163
20164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 * Get the length of an environment variable name.
20166 * Advance "arg" to the first character after the name.
20167 * Return 0 for error.
20168 */
20169 static int
20170get_env_len(arg)
20171 char_u **arg;
20172{
20173 char_u *p;
20174 int len;
20175
20176 for (p = *arg; vim_isIDc(*p); ++p)
20177 ;
20178 if (p == *arg) /* no name found */
20179 return 0;
20180
20181 len = (int)(p - *arg);
20182 *arg = p;
20183 return len;
20184}
20185
20186/*
20187 * Get the length of the name of a function or internal variable.
20188 * "arg" is advanced to the first non-white character after the name.
20189 * Return 0 if something is wrong.
20190 */
20191 static int
20192get_id_len(arg)
20193 char_u **arg;
20194{
20195 char_u *p;
20196 int len;
20197
20198 /* Find the end of the name. */
20199 for (p = *arg; eval_isnamec(*p); ++p)
20200 ;
20201 if (p == *arg) /* no name found */
20202 return 0;
20203
20204 len = (int)(p - *arg);
20205 *arg = skipwhite(p);
20206
20207 return len;
20208}
20209
20210/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020211 * Get the length of the name of a variable or function.
20212 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020214 * Return -1 if curly braces expansion failed.
20215 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 * If the name contains 'magic' {}'s, expand them and return the
20217 * expanded name in an allocated string via 'alias' - caller must free.
20218 */
20219 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020220get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 char_u **arg;
20222 char_u **alias;
20223 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020224 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225{
20226 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 char_u *p;
20228 char_u *expr_start;
20229 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230
20231 *alias = NULL; /* default to no alias */
20232
20233 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20234 && (*arg)[2] == (int)KE_SNR)
20235 {
20236 /* hard coded <SNR>, already translated */
20237 *arg += 3;
20238 return get_id_len(arg) + 3;
20239 }
20240 len = eval_fname_script(*arg);
20241 if (len > 0)
20242 {
20243 /* literal "<SID>", "s:" or "<SNR>" */
20244 *arg += len;
20245 }
20246
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020248 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020250 p = find_name_end(*arg, &expr_start, &expr_end,
20251 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 if (expr_start != NULL)
20253 {
20254 char_u *temp_string;
20255
20256 if (!evaluate)
20257 {
20258 len += (int)(p - *arg);
20259 *arg = skipwhite(p);
20260 return len;
20261 }
20262
20263 /*
20264 * Include any <SID> etc in the expanded string:
20265 * Thus the -len here.
20266 */
20267 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20268 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020269 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 *alias = temp_string;
20271 *arg = skipwhite(p);
20272 return (int)STRLEN(temp_string);
20273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274
20275 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020276 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 EMSG2(_(e_invexpr2), *arg);
20278
20279 return len;
20280}
20281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020282/*
20283 * Find the end of a variable or function name, taking care of magic braces.
20284 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20285 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020286 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020287 * Return a pointer to just after the name. Equal to "arg" if there is no
20288 * valid name.
20289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020291find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 char_u *arg;
20293 char_u **expr_start;
20294 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020295 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020297 int mb_nest = 0;
20298 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 char_u *p;
20300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020301 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020303 *expr_start = NULL;
20304 *expr_end = NULL;
20305 }
20306
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020307 /* Quick check for valid starting character. */
20308 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20309 return arg;
20310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020311 for (p = arg; *p != NUL
20312 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020313 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020314 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020315 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020316 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020317 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020318 if (*p == '\'')
20319 {
20320 /* skip over 'string' to avoid counting [ and ] inside it. */
20321 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20322 ;
20323 if (*p == NUL)
20324 break;
20325 }
20326 else if (*p == '"')
20327 {
20328 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20329 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20330 if (*p == '\\' && p[1] != NUL)
20331 ++p;
20332 if (*p == NUL)
20333 break;
20334 }
20335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020336 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020338 if (*p == '[')
20339 ++br_nest;
20340 else if (*p == ']')
20341 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020344 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020346 if (*p == '{')
20347 {
20348 mb_nest++;
20349 if (expr_start != NULL && *expr_start == NULL)
20350 *expr_start = p;
20351 }
20352 else if (*p == '}')
20353 {
20354 mb_nest--;
20355 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20356 *expr_end = p;
20357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 }
20360
20361 return p;
20362}
20363
20364/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020365 * Expands out the 'magic' {}'s in a variable/function name.
20366 * Note that this can call itself recursively, to deal with
20367 * constructs like foo{bar}{baz}{bam}
20368 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20369 * "in_start" ^
20370 * "expr_start" ^
20371 * "expr_end" ^
20372 * "in_end" ^
20373 *
20374 * Returns a new allocated string, which the caller must free.
20375 * Returns NULL for failure.
20376 */
20377 static char_u *
20378make_expanded_name(in_start, expr_start, expr_end, in_end)
20379 char_u *in_start;
20380 char_u *expr_start;
20381 char_u *expr_end;
20382 char_u *in_end;
20383{
20384 char_u c1;
20385 char_u *retval = NULL;
20386 char_u *temp_result;
20387 char_u *nextcmd = NULL;
20388
20389 if (expr_end == NULL || in_end == NULL)
20390 return NULL;
20391 *expr_start = NUL;
20392 *expr_end = NUL;
20393 c1 = *in_end;
20394 *in_end = NUL;
20395
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020396 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020397 if (temp_result != NULL && nextcmd == NULL)
20398 {
20399 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20400 + (in_end - expr_end) + 1));
20401 if (retval != NULL)
20402 {
20403 STRCPY(retval, in_start);
20404 STRCAT(retval, temp_result);
20405 STRCAT(retval, expr_end + 1);
20406 }
20407 }
20408 vim_free(temp_result);
20409
20410 *in_end = c1; /* put char back for error messages */
20411 *expr_start = '{';
20412 *expr_end = '}';
20413
20414 if (retval != NULL)
20415 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020416 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020417 if (expr_start != NULL)
20418 {
20419 /* Further expansion! */
20420 temp_result = make_expanded_name(retval, expr_start,
20421 expr_end, temp_result);
20422 vim_free(retval);
20423 retval = temp_result;
20424 }
20425 }
20426
20427 return retval;
20428}
20429
20430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020432 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 */
20434 static int
20435eval_isnamec(c)
20436 int c;
20437{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020438 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20439}
20440
20441/*
20442 * Return TRUE if character "c" can be used as the first character in a
20443 * variable or function name (excluding '{' and '}').
20444 */
20445 static int
20446eval_isnamec1(c)
20447 int c;
20448{
20449 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450}
20451
20452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 * Set number v: variable to "val".
20454 */
20455 void
20456set_vim_var_nr(idx, val)
20457 int idx;
20458 long val;
20459{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020460 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461}
20462
20463/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020464 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 */
20466 long
20467get_vim_var_nr(idx)
20468 int idx;
20469{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020470 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471}
20472
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020473/*
20474 * Get string v: variable value. Uses a static buffer, can only be used once.
20475 */
20476 char_u *
20477get_vim_var_str(idx)
20478 int idx;
20479{
20480 return get_tv_string(&vimvars[idx].vv_tv);
20481}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020482
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020484 * Get List v: variable value. Caller must take care of reference count when
20485 * needed.
20486 */
20487 list_T *
20488get_vim_var_list(idx)
20489 int idx;
20490{
20491 return vimvars[idx].vv_list;
20492}
20493
20494/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020495 * Set v:char to character "c".
20496 */
20497 void
20498set_vim_var_char(c)
20499 int c;
20500{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020501 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020502
20503#ifdef FEAT_MBYTE
20504 if (has_mbyte)
20505 buf[(*mb_char2bytes)(c, buf)] = NUL;
20506 else
20507#endif
20508 {
20509 buf[0] = c;
20510 buf[1] = NUL;
20511 }
20512 set_vim_var_string(VV_CHAR, buf, -1);
20513}
20514
20515/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020516 * Set v:count to "count" and v:count1 to "count1".
20517 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 */
20519 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020520set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 long count;
20522 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020523 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020525 if (set_prevcount)
20526 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020527 vimvars[VV_COUNT].vv_nr = count;
20528 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529}
20530
20531/*
20532 * Set string v: variable to a copy of "val".
20533 */
20534 void
20535set_vim_var_string(idx, val, len)
20536 int idx;
20537 char_u *val;
20538 int len; /* length of "val" to use or -1 (whole string) */
20539{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020540 /* Need to do this (at least) once, since we can't initialize a union.
20541 * Will always be invoked when "v:progname" is set. */
20542 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20543
Bram Moolenaare9a41262005-01-15 22:18:47 +000020544 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020546 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020548 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020550 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551}
20552
20553/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020554 * Set List v: variable to "val".
20555 */
20556 void
20557set_vim_var_list(idx, val)
20558 int idx;
20559 list_T *val;
20560{
20561 list_unref(vimvars[idx].vv_list);
20562 vimvars[idx].vv_list = val;
20563 if (val != NULL)
20564 ++val->lv_refcount;
20565}
20566
20567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 * Set v:register if needed.
20569 */
20570 void
20571set_reg_var(c)
20572 int c;
20573{
20574 char_u regname;
20575
20576 if (c == 0 || c == ' ')
20577 regname = '"';
20578 else
20579 regname = c;
20580 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020581 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 set_vim_var_string(VV_REG, &regname, 1);
20583}
20584
20585/*
20586 * Get or set v:exception. If "oldval" == NULL, return the current value.
20587 * Otherwise, restore the value to "oldval" and return NULL.
20588 * Must always be called in pairs to save and restore v:exception! Does not
20589 * take care of memory allocations.
20590 */
20591 char_u *
20592v_exception(oldval)
20593 char_u *oldval;
20594{
20595 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020596 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597
Bram Moolenaare9a41262005-01-15 22:18:47 +000020598 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 return NULL;
20600}
20601
20602/*
20603 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20604 * Otherwise, restore the value to "oldval" and return NULL.
20605 * Must always be called in pairs to save and restore v:throwpoint! Does not
20606 * take care of memory allocations.
20607 */
20608 char_u *
20609v_throwpoint(oldval)
20610 char_u *oldval;
20611{
20612 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020613 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614
Bram Moolenaare9a41262005-01-15 22:18:47 +000020615 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 return NULL;
20617}
20618
20619#if defined(FEAT_AUTOCMD) || defined(PROTO)
20620/*
20621 * Set v:cmdarg.
20622 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20623 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20624 * Must always be called in pairs!
20625 */
20626 char_u *
20627set_cmdarg(eap, oldarg)
20628 exarg_T *eap;
20629 char_u *oldarg;
20630{
20631 char_u *oldval;
20632 char_u *newval;
20633 unsigned len;
20634
Bram Moolenaare9a41262005-01-15 22:18:47 +000020635 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020636 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020638 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020639 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020640 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 }
20642
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020643 if (eap->force_bin == FORCE_BIN)
20644 len = 6;
20645 else if (eap->force_bin == FORCE_NOBIN)
20646 len = 8;
20647 else
20648 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020649
20650 if (eap->read_edit)
20651 len += 7;
20652
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020653 if (eap->force_ff != 0)
20654 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20655# ifdef FEAT_MBYTE
20656 if (eap->force_enc != 0)
20657 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020658 if (eap->bad_char != 0)
20659 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020660# endif
20661
20662 newval = alloc(len + 1);
20663 if (newval == NULL)
20664 return NULL;
20665
20666 if (eap->force_bin == FORCE_BIN)
20667 sprintf((char *)newval, " ++bin");
20668 else if (eap->force_bin == FORCE_NOBIN)
20669 sprintf((char *)newval, " ++nobin");
20670 else
20671 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020672
20673 if (eap->read_edit)
20674 STRCAT(newval, " ++edit");
20675
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020676 if (eap->force_ff != 0)
20677 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20678 eap->cmd + eap->force_ff);
20679# ifdef FEAT_MBYTE
20680 if (eap->force_enc != 0)
20681 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20682 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020683 if (eap->bad_char == BAD_KEEP)
20684 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20685 else if (eap->bad_char == BAD_DROP)
20686 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20687 else if (eap->bad_char != 0)
20688 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020689# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020690 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020691 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692}
20693#endif
20694
20695/*
20696 * Get the value of internal variable "name".
20697 * Return OK or FAIL.
20698 */
20699 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020700get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 char_u *name;
20702 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020703 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020704 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020705 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020706 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707{
20708 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020709 typval_T *tv = NULL;
20710 typval_T atv;
20711 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713
20714 /* truncate the name, so that we can use strcmp() */
20715 cc = name[len];
20716 name[len] = NUL;
20717
20718 /*
20719 * Check for "b:changedtick".
20720 */
20721 if (STRCMP(name, "b:changedtick") == 0)
20722 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020723 atv.v_type = VAR_NUMBER;
20724 atv.vval.v_number = curbuf->b_changedtick;
20725 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 }
20727
20728 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 * Check for user-defined variables.
20730 */
20731 else
20732 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020733 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020735 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020736 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020020737 if (dip != NULL)
20738 *dip = v;
20739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 }
20741
Bram Moolenaare9a41262005-01-15 22:18:47 +000020742 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020744 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020745 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 ret = FAIL;
20747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020748 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020749 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750
20751 name[len] = cc;
20752
20753 return ret;
20754}
20755
20756/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020757 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20758 * Also handle function call with Funcref variable: func(expr)
20759 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20760 */
20761 static int
20762handle_subscript(arg, rettv, evaluate, verbose)
20763 char_u **arg;
20764 typval_T *rettv;
20765 int evaluate; /* do more than finding the end */
20766 int verbose; /* give error messages */
20767{
20768 int ret = OK;
20769 dict_T *selfdict = NULL;
20770 char_u *s;
20771 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020772 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020773
20774 while (ret == OK
20775 && (**arg == '['
20776 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020777 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020778 && !vim_iswhite(*(*arg - 1)))
20779 {
20780 if (**arg == '(')
20781 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020782 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020783 if (evaluate)
20784 {
20785 functv = *rettv;
20786 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020787
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020788 /* Invoke the function. Recursive! */
20789 s = functv.vval.v_string;
20790 }
20791 else
20792 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020793 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020794 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20795 &len, evaluate, selfdict);
20796
20797 /* Clear the funcref afterwards, so that deleting it while
20798 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020799 if (evaluate)
20800 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020801
20802 /* Stop the expression evaluation when immediately aborting on
20803 * error, or when an interrupt occurred or an exception was thrown
20804 * but not caught. */
20805 if (aborting())
20806 {
20807 if (ret == OK)
20808 clear_tv(rettv);
20809 ret = FAIL;
20810 }
20811 dict_unref(selfdict);
20812 selfdict = NULL;
20813 }
20814 else /* **arg == '[' || **arg == '.' */
20815 {
20816 dict_unref(selfdict);
20817 if (rettv->v_type == VAR_DICT)
20818 {
20819 selfdict = rettv->vval.v_dict;
20820 if (selfdict != NULL)
20821 ++selfdict->dv_refcount;
20822 }
20823 else
20824 selfdict = NULL;
20825 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20826 {
20827 clear_tv(rettv);
20828 ret = FAIL;
20829 }
20830 }
20831 }
20832 dict_unref(selfdict);
20833 return ret;
20834}
20835
20836/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020837 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020838 * value).
20839 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020840 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020841alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020842{
Bram Moolenaar33570922005-01-25 22:26:29 +000020843 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020844}
20845
20846/*
20847 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 * The string "s" must have been allocated, it is consumed.
20849 * Return NULL for out of memory, the variable otherwise.
20850 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020851 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020852alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 char_u *s;
20854{
Bram Moolenaar33570922005-01-25 22:26:29 +000020855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020857 rettv = alloc_tv();
20858 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020860 rettv->v_type = VAR_STRING;
20861 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 }
20863 else
20864 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020865 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866}
20867
20868/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020869 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020871 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020872free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020873 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874{
20875 if (varp != NULL)
20876 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020877 switch (varp->v_type)
20878 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020879 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020880 func_unref(varp->vval.v_string);
20881 /*FALLTHROUGH*/
20882 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020883 vim_free(varp->vval.v_string);
20884 break;
20885 case VAR_LIST:
20886 list_unref(varp->vval.v_list);
20887 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020888 case VAR_DICT:
20889 dict_unref(varp->vval.v_dict);
20890 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020891 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020892#ifdef FEAT_FLOAT
20893 case VAR_FLOAT:
20894#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020895 case VAR_UNKNOWN:
20896 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020897 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020898 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020899 break;
20900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 vim_free(varp);
20902 }
20903}
20904
20905/*
20906 * Free the memory for a variable value and set the value to NULL or 0.
20907 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020908 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020909clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020910 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911{
20912 if (varp != NULL)
20913 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020914 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020916 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020917 func_unref(varp->vval.v_string);
20918 /*FALLTHROUGH*/
20919 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020920 vim_free(varp->vval.v_string);
20921 varp->vval.v_string = NULL;
20922 break;
20923 case VAR_LIST:
20924 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020925 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020926 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020927 case VAR_DICT:
20928 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020929 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020930 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020931 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020932 varp->vval.v_number = 0;
20933 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020934#ifdef FEAT_FLOAT
20935 case VAR_FLOAT:
20936 varp->vval.v_float = 0.0;
20937 break;
20938#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020939 case VAR_UNKNOWN:
20940 break;
20941 default:
20942 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020944 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 }
20946}
20947
20948/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020949 * Set the value of a variable to NULL without freeing items.
20950 */
20951 static void
20952init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020953 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020954{
20955 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020956 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020957}
20958
20959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 * Get the number value of a variable.
20961 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020962 * For incompatible types, return 0.
20963 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20964 * caller of incompatible types: it sets *denote to TRUE if "denote"
20965 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 */
20967 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020969 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020971 int error = FALSE;
20972
20973 return get_tv_number_chk(varp, &error); /* return 0L on error */
20974}
20975
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020976 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020977get_tv_number_chk(varp, denote)
20978 typval_T *varp;
20979 int *denote;
20980{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020981 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020983 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020985 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020986 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020987#ifdef FEAT_FLOAT
20988 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020989 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020990 break;
20991#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020992 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020993 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020994 break;
20995 case VAR_STRING:
20996 if (varp->vval.v_string != NULL)
20997 vim_str2nr(varp->vval.v_string, NULL, NULL,
20998 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020999 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021000 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021001 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021002 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021003 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021004 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021005 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021006 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021007 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021008 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021010 if (denote == NULL) /* useful for values that must be unsigned */
21011 n = -1;
21012 else
21013 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021014 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015}
21016
21017/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021018 * Get the lnum from the first argument.
21019 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021020 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 */
21022 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021024 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025{
Bram Moolenaar33570922005-01-25 22:26:29 +000021026 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 linenr_T lnum;
21028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021029 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 if (lnum == 0) /* no valid number, try using line() */
21031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 rettv.v_type = VAR_NUMBER;
21033 f_line(argvars, &rettv);
21034 lnum = rettv.vval.v_number;
21035 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 }
21037 return lnum;
21038}
21039
21040/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021041 * Get the lnum from the first argument.
21042 * Also accepts "$", then "buf" is used.
21043 * Returns 0 on error.
21044 */
21045 static linenr_T
21046get_tv_lnum_buf(argvars, buf)
21047 typval_T *argvars;
21048 buf_T *buf;
21049{
21050 if (argvars[0].v_type == VAR_STRING
21051 && argvars[0].vval.v_string != NULL
21052 && argvars[0].vval.v_string[0] == '$'
21053 && buf != NULL)
21054 return buf->b_ml.ml_line_count;
21055 return get_tv_number_chk(&argvars[0], NULL);
21056}
21057
21058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 * Get the string value of a variable.
21060 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021061 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21062 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 * If the String variable has never been set, return an empty string.
21064 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021065 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21066 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 */
21068 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021069get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021070 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071{
21072 static char_u mybuf[NUMBUFLEN];
21073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021074 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075}
21076
21077 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021078get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021079 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021080 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021082 char_u *res = get_tv_string_buf_chk(varp, buf);
21083
21084 return res != NULL ? res : (char_u *)"";
21085}
21086
Bram Moolenaar7d647822014-04-05 21:28:56 +020021087/*
21088 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21089 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021090 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021091get_tv_string_chk(varp)
21092 typval_T *varp;
21093{
21094 static char_u mybuf[NUMBUFLEN];
21095
21096 return get_tv_string_buf_chk(varp, mybuf);
21097}
21098
21099 static char_u *
21100get_tv_string_buf_chk(varp, buf)
21101 typval_T *varp;
21102 char_u *buf;
21103{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021104 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021106 case VAR_NUMBER:
21107 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21108 return buf;
21109 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021110 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021111 break;
21112 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021113 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021114 break;
21115 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021116 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021117 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021118#ifdef FEAT_FLOAT
21119 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021120 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021121 break;
21122#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021123 case VAR_STRING:
21124 if (varp->vval.v_string != NULL)
21125 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021126 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021127 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021128 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021131 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132}
21133
21134/*
21135 * Find variable "name" in the list of variables.
21136 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021137 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021138 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021139 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021141 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021142find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021144 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021145 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021148 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149
Bram Moolenaara7043832005-01-21 11:56:39 +000021150 ht = find_var_ht(name, &varname);
21151 if (htp != NULL)
21152 *htp = ht;
21153 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021155 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156}
21157
21158/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021159 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021160 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021162 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021163find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021164 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021165 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021166 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021167 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021168{
Bram Moolenaar33570922005-01-25 22:26:29 +000021169 hashitem_T *hi;
21170
21171 if (*varname == NUL)
21172 {
21173 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021174 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021175 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021176 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021177 case 'g': return &globvars_var;
21178 case 'v': return &vimvars_var;
21179 case 'b': return &curbuf->b_bufvar;
21180 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021181#ifdef FEAT_WINDOWS
21182 case 't': return &curtab->tp_winvar;
21183#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021184 case 'l': return current_funccal == NULL
21185 ? NULL : &current_funccal->l_vars_var;
21186 case 'a': return current_funccal == NULL
21187 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021188 }
21189 return NULL;
21190 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021191
21192 hi = hash_find(ht, varname);
21193 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021194 {
21195 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021196 * worked find the variable again. Don't auto-load a script if it was
21197 * loaded already, otherwise it would be loaded every time when
21198 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021199 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021200 {
21201 /* Note: script_autoload() may make "hi" invalid. It must either
21202 * be obtained again or not used. */
21203 if (!script_autoload(varname, FALSE) || aborting())
21204 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021205 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021206 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021207 if (HASHITEM_EMPTY(hi))
21208 return NULL;
21209 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021210 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021211}
21212
21213/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021214 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021215 * Set "varname" to the start of name without ':'.
21216 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021217 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021218find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 char_u *name;
21220 char_u **varname;
21221{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021222 hashitem_T *hi;
21223
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 if (name[1] != ':')
21225 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021226 /* The name must not start with a colon or #. */
21227 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 return NULL;
21229 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021230
21231 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021232 hi = hash_find(&compat_hashtab, name);
21233 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021234 return &compat_hashtab;
21235
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021237 return &globvarht; /* global variable */
21238 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 }
21240 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021241 if (*name == 'g') /* global variable */
21242 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021243 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21244 */
21245 if (vim_strchr(name + 2, ':') != NULL
21246 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021247 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021249 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021251 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021252#ifdef FEAT_WINDOWS
21253 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021254 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021256 if (*name == 'v') /* v: variable */
21257 return &vimvarht;
21258 if (*name == 'a' && current_funccal != NULL) /* function argument */
21259 return &current_funccal->l_avars.dv_hashtab;
21260 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21261 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 if (*name == 's' /* script variable */
21263 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21264 return &SCRIPT_VARS(current_SID);
21265 return NULL;
21266}
21267
21268/*
21269 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021270 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 * Returns NULL when it doesn't exist.
21272 */
21273 char_u *
21274get_var_value(name)
21275 char_u *name;
21276{
Bram Moolenaar33570922005-01-25 22:26:29 +000021277 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021279 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 if (v == NULL)
21281 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021282 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283}
21284
21285/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021286 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 * sourcing this script and when executing functions defined in the script.
21288 */
21289 void
21290new_script_vars(id)
21291 scid_T id;
21292{
Bram Moolenaara7043832005-01-21 11:56:39 +000021293 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021294 hashtab_T *ht;
21295 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021296
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21298 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021299 /* Re-allocating ga_data means that an ht_array pointing to
21300 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021301 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021302 for (i = 1; i <= ga_scripts.ga_len; ++i)
21303 {
21304 ht = &SCRIPT_VARS(i);
21305 if (ht->ht_mask == HT_INIT_SIZE - 1)
21306 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021307 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021308 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021309 }
21310
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 while (ga_scripts.ga_len < id)
21312 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021313 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021314 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021315 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 }
21318 }
21319}
21320
21321/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021322 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21323 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 */
21325 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021326init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021327 dict_T *dict;
21328 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021329 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330{
Bram Moolenaar33570922005-01-25 22:26:29 +000021331 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021332 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021333 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021334 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021335 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021336 dict_var->di_tv.vval.v_dict = dict;
21337 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021338 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021339 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21340 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341}
21342
21343/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021344 * Unreference a dictionary initialized by init_var_dict().
21345 */
21346 void
21347unref_var_dict(dict)
21348 dict_T *dict;
21349{
21350 /* Now the dict needs to be freed if no one else is using it, go back to
21351 * normal reference counting. */
21352 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21353 dict_unref(dict);
21354}
21355
21356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021358 * Frees all allocated variables and the value they contain.
21359 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 */
21361 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021362vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021363 hashtab_T *ht;
21364{
21365 vars_clear_ext(ht, TRUE);
21366}
21367
21368/*
21369 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21370 */
21371 static void
21372vars_clear_ext(ht, free_val)
21373 hashtab_T *ht;
21374 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375{
Bram Moolenaara7043832005-01-21 11:56:39 +000021376 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 hashitem_T *hi;
21378 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379
Bram Moolenaar33570922005-01-25 22:26:29 +000021380 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021381 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021382 for (hi = ht->ht_array; todo > 0; ++hi)
21383 {
21384 if (!HASHITEM_EMPTY(hi))
21385 {
21386 --todo;
21387
Bram Moolenaar33570922005-01-25 22:26:29 +000021388 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021389 * ht_array might change then. hash_clear() takes care of it
21390 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021391 v = HI2DI(hi);
21392 if (free_val)
21393 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021394 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021395 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021396 }
21397 }
21398 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021399 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400}
21401
Bram Moolenaara7043832005-01-21 11:56:39 +000021402/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021403 * Delete a variable from hashtab "ht" at item "hi".
21404 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021407delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021408 hashtab_T *ht;
21409 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410{
Bram Moolenaar33570922005-01-25 22:26:29 +000021411 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021412
21413 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021414 clear_tv(&di->di_tv);
21415 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416}
21417
21418/*
21419 * List the value of one internal variable.
21420 */
21421 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021422list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021423 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021425 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021427 char_u *tofree;
21428 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021429 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021430
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021431 current_copyID += COPYID_INC;
21432 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021433 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021434 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021435 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436}
21437
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021439list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 char_u *prefix;
21441 char_u *name;
21442 int type;
21443 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021444 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445{
Bram Moolenaar31859182007-08-14 20:41:13 +000021446 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21447 msg_start();
21448 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 if (name != NULL) /* "a:" vars don't have a name stored */
21450 msg_puts(name);
21451 msg_putchar(' ');
21452 msg_advance(22);
21453 if (type == VAR_NUMBER)
21454 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021455 else if (type == VAR_FUNC)
21456 msg_putchar('*');
21457 else if (type == VAR_LIST)
21458 {
21459 msg_putchar('[');
21460 if (*string == '[')
21461 ++string;
21462 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021463 else if (type == VAR_DICT)
21464 {
21465 msg_putchar('{');
21466 if (*string == '{')
21467 ++string;
21468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 else
21470 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021471
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021473
21474 if (type == VAR_FUNC)
21475 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021476 if (*first)
21477 {
21478 msg_clr_eos();
21479 *first = FALSE;
21480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481}
21482
21483/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021484 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 * If the variable already exists, the value is updated.
21486 * Otherwise the variable is created.
21487 */
21488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021489set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021491 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021492 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493{
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021496 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021498 ht = find_var_ht(name, &varname);
21499 if (ht == NULL || *varname == NUL)
21500 {
21501 EMSG2(_(e_illvar), name);
21502 return;
21503 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021504 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021505
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021506 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21507 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021508
Bram Moolenaar33570922005-01-25 22:26:29 +000021509 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021511 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021512 if (var_check_ro(v->di_flags, name, FALSE)
21513 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021514 return;
21515 if (v->di_tv.v_type != tv->v_type
21516 && !((v->di_tv.v_type == VAR_STRING
21517 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021518 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021519 || tv->v_type == VAR_NUMBER))
21520#ifdef FEAT_FLOAT
21521 && !((v->di_tv.v_type == VAR_NUMBER
21522 || v->di_tv.v_type == VAR_FLOAT)
21523 && (tv->v_type == VAR_NUMBER
21524 || tv->v_type == VAR_FLOAT))
21525#endif
21526 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021527 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021528 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021529 return;
21530 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021531
21532 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021533 * Handle setting internal v: variables separately where needed to
21534 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021535 */
21536 if (ht == &vimvarht)
21537 {
21538 if (v->di_tv.v_type == VAR_STRING)
21539 {
21540 vim_free(v->di_tv.vval.v_string);
21541 if (copy || tv->v_type != VAR_STRING)
21542 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21543 else
21544 {
21545 /* Take over the string to avoid an extra alloc/free. */
21546 v->di_tv.vval.v_string = tv->vval.v_string;
21547 tv->vval.v_string = NULL;
21548 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021549 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021550 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021551 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021552 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021554 if (STRCMP(varname, "searchforward") == 0)
21555 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021556#ifdef FEAT_SEARCH_EXTRA
21557 else if (STRCMP(varname, "hlsearch") == 0)
21558 {
21559 no_hlsearch = !v->di_tv.vval.v_number;
21560 redraw_all_later(SOME_VALID);
21561 }
21562#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021563 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021564 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021565 else if (v->di_tv.v_type != tv->v_type)
21566 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021567 }
21568
21569 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 }
21571 else /* add a new variable */
21572 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021573 /* Can't add "v:" variable. */
21574 if (ht == &vimvarht)
21575 {
21576 EMSG2(_(e_illvar), name);
21577 return;
21578 }
21579
Bram Moolenaar92124a32005-06-17 22:03:40 +000021580 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021581 if (!valid_varname(varname))
21582 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021583
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021584 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21585 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021586 if (v == NULL)
21587 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021588 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021589 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021591 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 return;
21593 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021594 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021596
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021597 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021598 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021599 else
21600 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021601 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021602 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021603 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605}
21606
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021607/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021608 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021609 * Also give an error message.
21610 */
21611 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021612var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000021613 int flags;
21614 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021615 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000021616{
21617 if (flags & DI_FLAGS_RO)
21618 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021619 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021620 return TRUE;
21621 }
21622 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21623 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021624 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000021625 return TRUE;
21626 }
21627 return FALSE;
21628}
21629
21630/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021631 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21632 * Also give an error message.
21633 */
21634 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021635var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021636 int flags;
21637 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021638 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021639{
21640 if (flags & DI_FLAGS_FIX)
21641 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020021642 EMSG2(_("E795: Cannot delete variable %s"),
21643 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021644 return TRUE;
21645 }
21646 return FALSE;
21647}
21648
21649/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021650 * Check if a funcref is assigned to a valid variable name.
21651 * Return TRUE and give an error if not.
21652 */
21653 static int
21654var_check_func_name(name, new_var)
21655 char_u *name; /* points to start of variable name */
21656 int new_var; /* TRUE when creating the variable */
21657{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021658 /* Allow for w: b: s: and t:. */
21659 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021660 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21661 ? name[2] : name[0]))
21662 {
21663 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21664 name);
21665 return TRUE;
21666 }
21667 /* Don't allow hiding a function. When "v" is not NULL we might be
21668 * assigning another function to the same var, the type is checked
21669 * below. */
21670 if (new_var && function_exists(name))
21671 {
21672 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21673 name);
21674 return TRUE;
21675 }
21676 return FALSE;
21677}
21678
21679/*
21680 * Check if a variable name is valid.
21681 * Return FALSE and give an error if not.
21682 */
21683 static int
21684valid_varname(varname)
21685 char_u *varname;
21686{
21687 char_u *p;
21688
21689 for (p = varname; *p != NUL; ++p)
21690 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21691 && *p != AUTOLOAD_CHAR)
21692 {
21693 EMSG2(_(e_illvar), varname);
21694 return FALSE;
21695 }
21696 return TRUE;
21697}
21698
21699/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021700 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020021701 * Also give an error message, using "name" or _("name") when use_gettext is
21702 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021703 */
21704 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020021705tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021706 int lock;
21707 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020021708 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021709{
21710 if (lock & VAR_LOCKED)
21711 {
21712 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021713 name == NULL ? (char_u *)_("Unknown")
21714 : use_gettext ? (char_u *)_(name)
21715 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021716 return TRUE;
21717 }
21718 if (lock & VAR_FIXED)
21719 {
21720 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020021721 name == NULL ? (char_u *)_("Unknown")
21722 : use_gettext ? (char_u *)_(name)
21723 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021724 return TRUE;
21725 }
21726 return FALSE;
21727}
21728
21729/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021730 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021731 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021732 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021733 * It is OK for "from" and "to" to point to the same item. This is used to
21734 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021735 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021736 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021737copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 typval_T *from;
21739 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021741 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021742 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021743 switch (from->v_type)
21744 {
21745 case VAR_NUMBER:
21746 to->vval.v_number = from->vval.v_number;
21747 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021748#ifdef FEAT_FLOAT
21749 case VAR_FLOAT:
21750 to->vval.v_float = from->vval.v_float;
21751 break;
21752#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021753 case VAR_STRING:
21754 case VAR_FUNC:
21755 if (from->vval.v_string == NULL)
21756 to->vval.v_string = NULL;
21757 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021758 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021759 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021760 if (from->v_type == VAR_FUNC)
21761 func_ref(to->vval.v_string);
21762 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021763 break;
21764 case VAR_LIST:
21765 if (from->vval.v_list == NULL)
21766 to->vval.v_list = NULL;
21767 else
21768 {
21769 to->vval.v_list = from->vval.v_list;
21770 ++to->vval.v_list->lv_refcount;
21771 }
21772 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021773 case VAR_DICT:
21774 if (from->vval.v_dict == NULL)
21775 to->vval.v_dict = NULL;
21776 else
21777 {
21778 to->vval.v_dict = from->vval.v_dict;
21779 ++to->vval.v_dict->dv_refcount;
21780 }
21781 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021782 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021783 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021784 break;
21785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786}
21787
21788/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021789 * Make a copy of an item.
21790 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021791 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21792 * reference to an already copied list/dict can be used.
21793 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021794 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021795 static int
21796item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021797 typval_T *from;
21798 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021799 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021800 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021801{
21802 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021803 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021804
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021806 {
21807 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021808 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021809 }
21810 ++recurse;
21811
21812 switch (from->v_type)
21813 {
21814 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021815#ifdef FEAT_FLOAT
21816 case VAR_FLOAT:
21817#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021818 case VAR_STRING:
21819 case VAR_FUNC:
21820 copy_tv(from, to);
21821 break;
21822 case VAR_LIST:
21823 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021824 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021825 if (from->vval.v_list == NULL)
21826 to->vval.v_list = NULL;
21827 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21828 {
21829 /* use the copy made earlier */
21830 to->vval.v_list = from->vval.v_list->lv_copylist;
21831 ++to->vval.v_list->lv_refcount;
21832 }
21833 else
21834 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21835 if (to->vval.v_list == NULL)
21836 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021837 break;
21838 case VAR_DICT:
21839 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021840 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021841 if (from->vval.v_dict == NULL)
21842 to->vval.v_dict = NULL;
21843 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21844 {
21845 /* use the copy made earlier */
21846 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21847 ++to->vval.v_dict->dv_refcount;
21848 }
21849 else
21850 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21851 if (to->vval.v_dict == NULL)
21852 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021853 break;
21854 default:
21855 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021856 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021857 }
21858 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021859 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021860}
21861
21862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 * ":echo expr1 ..." print each argument separated with a space, add a
21864 * newline at the end.
21865 * ":echon expr1 ..." print each argument plain.
21866 */
21867 void
21868ex_echo(eap)
21869 exarg_T *eap;
21870{
21871 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021872 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021873 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 char_u *p;
21875 int needclr = TRUE;
21876 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021877 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878
21879 if (eap->skip)
21880 ++emsg_skip;
21881 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021883 /* If eval1() causes an error message the text from the command may
21884 * still need to be cleared. E.g., "echo 22,44". */
21885 need_clr_eos = needclr;
21886
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021888 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 {
21890 /*
21891 * Report the invalid expression unless the expression evaluation
21892 * has been cancelled due to an aborting error, an interrupt, or an
21893 * exception.
21894 */
21895 if (!aborting())
21896 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021897 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 break;
21899 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021900 need_clr_eos = FALSE;
21901
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 if (!eap->skip)
21903 {
21904 if (atstart)
21905 {
21906 atstart = FALSE;
21907 /* Call msg_start() after eval1(), evaluating the expression
21908 * may cause a message to appear. */
21909 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021910 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021911 /* Mark the saved text as finishing the line, so that what
21912 * follows is displayed on a new line when scrolling back
21913 * at the more prompt. */
21914 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917 }
21918 else if (eap->cmdidx == CMD_echo)
21919 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021920 current_copyID += COPYID_INC;
21921 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021922 if (p != NULL)
21923 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021925 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021927 if (*p != TAB && needclr)
21928 {
21929 /* remove any text still there from the command */
21930 msg_clr_eos();
21931 needclr = FALSE;
21932 }
21933 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 }
21935 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021936 {
21937#ifdef FEAT_MBYTE
21938 if (has_mbyte)
21939 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021940 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021941
21942 (void)msg_outtrans_len_attr(p, i, echo_attr);
21943 p += i - 1;
21944 }
21945 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021947 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021952 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 arg = skipwhite(arg);
21954 }
21955 eap->nextcmd = check_nextcmd(arg);
21956
21957 if (eap->skip)
21958 --emsg_skip;
21959 else
21960 {
21961 /* remove text that may still be there from the command */
21962 if (needclr)
21963 msg_clr_eos();
21964 if (eap->cmdidx == CMD_echo)
21965 msg_end();
21966 }
21967}
21968
21969/*
21970 * ":echohl {name}".
21971 */
21972 void
21973ex_echohl(eap)
21974 exarg_T *eap;
21975{
21976 int id;
21977
21978 id = syn_name2id(eap->arg);
21979 if (id == 0)
21980 echo_attr = 0;
21981 else
21982 echo_attr = syn_id2attr(id);
21983}
21984
21985/*
21986 * ":execute expr1 ..." execute the result of an expression.
21987 * ":echomsg expr1 ..." Print a message
21988 * ":echoerr expr1 ..." Print an error
21989 * Each gets spaces around each argument and a newline at the end for
21990 * echo commands
21991 */
21992 void
21993ex_execute(eap)
21994 exarg_T *eap;
21995{
21996 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021997 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 int ret = OK;
21999 char_u *p;
22000 garray_T ga;
22001 int len;
22002 int save_did_emsg;
22003
22004 ga_init2(&ga, 1, 80);
22005
22006 if (eap->skip)
22007 ++emsg_skip;
22008 while (*arg != NUL && *arg != '|' && *arg != '\n')
22009 {
22010 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022011 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 {
22013 /*
22014 * Report the invalid expression unless the expression evaluation
22015 * has been cancelled due to an aborting error, an interrupt, or an
22016 * exception.
22017 */
22018 if (!aborting())
22019 EMSG2(_(e_invexpr2), p);
22020 ret = FAIL;
22021 break;
22022 }
22023
22024 if (!eap->skip)
22025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022026 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 len = (int)STRLEN(p);
22028 if (ga_grow(&ga, len + 2) == FAIL)
22029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022030 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 ret = FAIL;
22032 break;
22033 }
22034 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 ga.ga_len += len;
22038 }
22039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022040 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 arg = skipwhite(arg);
22042 }
22043
22044 if (ret != FAIL && ga.ga_data != NULL)
22045 {
22046 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022049 out_flush();
22050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 else if (eap->cmdidx == CMD_echoerr)
22052 {
22053 /* We don't want to abort following commands, restore did_emsg. */
22054 save_did_emsg = did_emsg;
22055 EMSG((char_u *)ga.ga_data);
22056 if (!force_abort)
22057 did_emsg = save_did_emsg;
22058 }
22059 else if (eap->cmdidx == CMD_execute)
22060 do_cmdline((char_u *)ga.ga_data,
22061 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22062 }
22063
22064 ga_clear(&ga);
22065
22066 if (eap->skip)
22067 --emsg_skip;
22068
22069 eap->nextcmd = check_nextcmd(arg);
22070}
22071
22072/*
22073 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22074 * "arg" points to the "&" or '+' when called, to "option" when returning.
22075 * Returns NULL when no option name found. Otherwise pointer to the char
22076 * after the option name.
22077 */
22078 static char_u *
22079find_option_end(arg, opt_flags)
22080 char_u **arg;
22081 int *opt_flags;
22082{
22083 char_u *p = *arg;
22084
22085 ++p;
22086 if (*p == 'g' && p[1] == ':')
22087 {
22088 *opt_flags = OPT_GLOBAL;
22089 p += 2;
22090 }
22091 else if (*p == 'l' && p[1] == ':')
22092 {
22093 *opt_flags = OPT_LOCAL;
22094 p += 2;
22095 }
22096 else
22097 *opt_flags = 0;
22098
22099 if (!ASCII_ISALPHA(*p))
22100 return NULL;
22101 *arg = p;
22102
22103 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22104 p += 4; /* termcap option */
22105 else
22106 while (ASCII_ISALPHA(*p))
22107 ++p;
22108 return p;
22109}
22110
22111/*
22112 * ":function"
22113 */
22114 void
22115ex_function(eap)
22116 exarg_T *eap;
22117{
22118 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022119 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 int j;
22121 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022123 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 char_u *name = NULL;
22125 char_u *p;
22126 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022127 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 garray_T newargs;
22129 garray_T newlines;
22130 int varargs = FALSE;
22131 int mustend = FALSE;
22132 int flags = 0;
22133 ufunc_T *fp;
22134 int indent;
22135 int nesting;
22136 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022137 dictitem_T *v;
22138 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022139 static int func_nr = 0; /* number for nameless function */
22140 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022141 hashtab_T *ht;
22142 int todo;
22143 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022144 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145
22146 /*
22147 * ":function" without argument: list functions.
22148 */
22149 if (ends_excmd(*eap->arg))
22150 {
22151 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022152 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022153 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022154 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022155 {
22156 if (!HASHITEM_EMPTY(hi))
22157 {
22158 --todo;
22159 fp = HI2UF(hi);
22160 if (!isdigit(*fp->uf_name))
22161 list_func_head(fp, FALSE);
22162 }
22163 }
22164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 eap->nextcmd = check_nextcmd(eap->arg);
22166 return;
22167 }
22168
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022169 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022170 * ":function /pat": list functions matching pattern.
22171 */
22172 if (*eap->arg == '/')
22173 {
22174 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22175 if (!eap->skip)
22176 {
22177 regmatch_T regmatch;
22178
22179 c = *p;
22180 *p = NUL;
22181 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22182 *p = c;
22183 if (regmatch.regprog != NULL)
22184 {
22185 regmatch.rm_ic = p_ic;
22186
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022187 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022188 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22189 {
22190 if (!HASHITEM_EMPTY(hi))
22191 {
22192 --todo;
22193 fp = HI2UF(hi);
22194 if (!isdigit(*fp->uf_name)
22195 && vim_regexec(&regmatch, fp->uf_name, 0))
22196 list_func_head(fp, FALSE);
22197 }
22198 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022199 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022200 }
22201 }
22202 if (*p == '/')
22203 ++p;
22204 eap->nextcmd = check_nextcmd(p);
22205 return;
22206 }
22207
22208 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022209 * Get the function name. There are these situations:
22210 * func normal function name
22211 * "name" == func, "fudi.fd_dict" == NULL
22212 * dict.func new dictionary entry
22213 * "name" == NULL, "fudi.fd_dict" set,
22214 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22215 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022216 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022217 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22218 * dict.func existing dict entry that's not a Funcref
22219 * "name" == NULL, "fudi.fd_dict" set,
22220 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022221 * s:func script-local function name
22222 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022225 name = trans_function_name(&p, eap->skip, 0, &fudi);
22226 paren = (vim_strchr(p, '(') != NULL);
22227 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 {
22229 /*
22230 * Return on an invalid expression in braces, unless the expression
22231 * evaluation has been cancelled due to an aborting error, an
22232 * interrupt, or an exception.
22233 */
22234 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022235 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022236 if (!eap->skip && fudi.fd_newkey != NULL)
22237 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022238 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 else
22242 eap->skip = TRUE;
22243 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022244
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 /* An error in a function call during evaluation of an expression in magic
22246 * braces should not cause the function not to be defined. */
22247 saved_did_emsg = did_emsg;
22248 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249
22250 /*
22251 * ":function func" with only function name: list function.
22252 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022253 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254 {
22255 if (!ends_excmd(*skipwhite(p)))
22256 {
22257 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022258 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 }
22260 eap->nextcmd = check_nextcmd(p);
22261 if (eap->nextcmd != NULL)
22262 *p = NUL;
22263 if (!eap->skip && !got_int)
22264 {
22265 fp = find_func(name);
22266 if (fp != NULL)
22267 {
22268 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022269 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022271 if (FUNCLINE(fp, j) == NULL)
22272 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 msg_putchar('\n');
22274 msg_outnum((long)(j + 1));
22275 if (j < 9)
22276 msg_putchar(' ');
22277 if (j < 99)
22278 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022279 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 out_flush(); /* show a line at a time */
22281 ui_breakcheck();
22282 }
22283 if (!got_int)
22284 {
22285 msg_putchar('\n');
22286 msg_puts((char_u *)" endfunction");
22287 }
22288 }
22289 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022290 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022292 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 }
22294
22295 /*
22296 * ":function name(arg1, arg2)" Define function.
22297 */
22298 p = skipwhite(p);
22299 if (*p != '(')
22300 {
22301 if (!eap->skip)
22302 {
22303 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022304 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 }
22306 /* attempt to continue by skipping some text */
22307 if (vim_strchr(p, '(') != NULL)
22308 p = vim_strchr(p, '(');
22309 }
22310 p = skipwhite(p + 1);
22311
22312 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22313 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22314
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022315 if (!eap->skip)
22316 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022317 /* Check the name of the function. Unless it's a dictionary function
22318 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022319 if (name != NULL)
22320 arg = name;
22321 else
22322 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022323 if (arg != NULL && (fudi.fd_di == NULL
22324 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022325 {
22326 if (*arg == K_SPECIAL)
22327 j = 3;
22328 else
22329 j = 0;
22330 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22331 : eval_isnamec(arg[j])))
22332 ++j;
22333 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022334 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022335 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022336 /* Disallow using the g: dict. */
22337 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22338 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022339 }
22340
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 /*
22342 * Isolate the arguments: "arg1, arg2, ...)"
22343 */
22344 while (*p != ')')
22345 {
22346 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22347 {
22348 varargs = TRUE;
22349 p += 3;
22350 mustend = TRUE;
22351 }
22352 else
22353 {
22354 arg = p;
22355 while (ASCII_ISALNUM(*p) || *p == '_')
22356 ++p;
22357 if (arg == p || isdigit(*arg)
22358 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22359 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22360 {
22361 if (!eap->skip)
22362 EMSG2(_("E125: Illegal argument: %s"), arg);
22363 break;
22364 }
22365 if (ga_grow(&newargs, 1) == FAIL)
22366 goto erret;
22367 c = *p;
22368 *p = NUL;
22369 arg = vim_strsave(arg);
22370 if (arg == NULL)
22371 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022372
22373 /* Check for duplicate argument name. */
22374 for (i = 0; i < newargs.ga_len; ++i)
22375 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22376 {
22377 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022378 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022379 goto erret;
22380 }
22381
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22383 *p = c;
22384 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 if (*p == ',')
22386 ++p;
22387 else
22388 mustend = TRUE;
22389 }
22390 p = skipwhite(p);
22391 if (mustend && *p != ')')
22392 {
22393 if (!eap->skip)
22394 EMSG2(_(e_invarg2), eap->arg);
22395 break;
22396 }
22397 }
22398 ++p; /* skip the ')' */
22399
Bram Moolenaare9a41262005-01-15 22:18:47 +000022400 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 for (;;)
22402 {
22403 p = skipwhite(p);
22404 if (STRNCMP(p, "range", 5) == 0)
22405 {
22406 flags |= FC_RANGE;
22407 p += 5;
22408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022409 else if (STRNCMP(p, "dict", 4) == 0)
22410 {
22411 flags |= FC_DICT;
22412 p += 4;
22413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 else if (STRNCMP(p, "abort", 5) == 0)
22415 {
22416 flags |= FC_ABORT;
22417 p += 5;
22418 }
22419 else
22420 break;
22421 }
22422
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022423 /* When there is a line break use what follows for the function body.
22424 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22425 if (*p == '\n')
22426 line_arg = p + 1;
22427 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 EMSG(_(e_trailing));
22429
22430 /*
22431 * Read the body of the function, until ":endfunction" is found.
22432 */
22433 if (KeyTyped)
22434 {
22435 /* Check if the function already exists, don't let the user type the
22436 * whole function before telling him it doesn't work! For a script we
22437 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022438 if (!eap->skip && !eap->forceit)
22439 {
22440 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22441 EMSG(_(e_funcdict));
22442 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022443 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022446 if (!eap->skip && did_emsg)
22447 goto erret;
22448
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 msg_putchar('\n'); /* don't overwrite the function name */
22450 cmdline_row = msg_row;
22451 }
22452
22453 indent = 2;
22454 nesting = 0;
22455 for (;;)
22456 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022457 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022458 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022459 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022460 saved_wait_return = FALSE;
22461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022463 sourcing_lnum_off = sourcing_lnum;
22464
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022465 if (line_arg != NULL)
22466 {
22467 /* Use eap->arg, split up in parts by line breaks. */
22468 theline = line_arg;
22469 p = vim_strchr(theline, '\n');
22470 if (p == NULL)
22471 line_arg += STRLEN(line_arg);
22472 else
22473 {
22474 *p = NUL;
22475 line_arg = p + 1;
22476 }
22477 }
22478 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 theline = getcmdline(':', 0L, indent);
22480 else
22481 theline = eap->getline(':', eap->cookie, indent);
22482 if (KeyTyped)
22483 lines_left = Rows - 1;
22484 if (theline == NULL)
22485 {
22486 EMSG(_("E126: Missing :endfunction"));
22487 goto erret;
22488 }
22489
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022490 /* Detect line continuation: sourcing_lnum increased more than one. */
22491 if (sourcing_lnum > sourcing_lnum_off + 1)
22492 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22493 else
22494 sourcing_lnum_off = 0;
22495
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 if (skip_until != NULL)
22497 {
22498 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22499 * don't check for ":endfunc". */
22500 if (STRCMP(theline, skip_until) == 0)
22501 {
22502 vim_free(skip_until);
22503 skip_until = NULL;
22504 }
22505 }
22506 else
22507 {
22508 /* skip ':' and blanks*/
22509 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22510 ;
22511
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022512 /* Check for "endfunction". */
22513 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022515 if (line_arg == NULL)
22516 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 break;
22518 }
22519
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022520 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 * at "end". */
22522 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22523 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022524 else if (STRNCMP(p, "if", 2) == 0
22525 || STRNCMP(p, "wh", 2) == 0
22526 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 || STRNCMP(p, "try", 3) == 0)
22528 indent += 2;
22529
22530 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022531 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022533 if (*p == '!')
22534 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022536 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22537 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022539 ++nesting;
22540 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 }
22542 }
22543
22544 /* Check for ":append" or ":insert". */
22545 p = skip_range(p, NULL);
22546 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22547 || (p[0] == 'i'
22548 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22549 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22550 skip_until = vim_strsave((char_u *)".");
22551
22552 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22553 arg = skipwhite(skiptowhite(p));
22554 if (arg[0] == '<' && arg[1] =='<'
22555 && ((p[0] == 'p' && p[1] == 'y'
22556 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22557 || (p[0] == 'p' && p[1] == 'e'
22558 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22559 || (p[0] == 't' && p[1] == 'c'
22560 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022561 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22562 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22564 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022565 || (p[0] == 'm' && p[1] == 'z'
22566 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 ))
22568 {
22569 /* ":python <<" continues until a dot, like ":append" */
22570 p = skipwhite(arg + 2);
22571 if (*p == NUL)
22572 skip_until = vim_strsave((char_u *)".");
22573 else
22574 skip_until = vim_strsave(p);
22575 }
22576 }
22577
22578 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022579 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022580 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022581 if (line_arg == NULL)
22582 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022584 }
22585
22586 /* Copy the line to newly allocated memory. get_one_sourceline()
22587 * allocates 250 bytes per line, this saves 80% on average. The cost
22588 * is an extra alloc/free. */
22589 p = vim_strsave(theline);
22590 if (p != NULL)
22591 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022592 if (line_arg == NULL)
22593 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022594 theline = p;
22595 }
22596
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022597 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22598
22599 /* Add NULL lines for continuation lines, so that the line count is
22600 * equal to the index in the growarray. */
22601 while (sourcing_lnum_off-- > 0)
22602 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022603
22604 /* Check for end of eap->arg. */
22605 if (line_arg != NULL && *line_arg == NUL)
22606 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 }
22608
22609 /* Don't define the function when skipping commands or when an error was
22610 * detected. */
22611 if (eap->skip || did_emsg)
22612 goto erret;
22613
22614 /*
22615 * If there are no errors, add the function
22616 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022617 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022618 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022619 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022621 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022622 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022623 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022624 goto erret;
22625 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022626
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022627 fp = find_func(name);
22628 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022630 if (!eap->forceit)
22631 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022632 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022633 goto erret;
22634 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022635 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022636 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022637 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022638 name);
22639 goto erret;
22640 }
22641 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022642 ga_clear_strings(&(fp->uf_args));
22643 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022644 vim_free(name);
22645 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 }
22648 else
22649 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022650 char numbuf[20];
22651
22652 fp = NULL;
22653 if (fudi.fd_newkey == NULL && !eap->forceit)
22654 {
22655 EMSG(_(e_funcdict));
22656 goto erret;
22657 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022658 if (fudi.fd_di == NULL)
22659 {
22660 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022661 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022662 goto erret;
22663 }
22664 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022665 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000022666 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022667
22668 /* Give the function a sequential number. Can only be used with a
22669 * Funcref! */
22670 vim_free(name);
22671 sprintf(numbuf, "%d", ++func_nr);
22672 name = vim_strsave((char_u *)numbuf);
22673 if (name == NULL)
22674 goto erret;
22675 }
22676
22677 if (fp == NULL)
22678 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022679 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022680 {
22681 int slen, plen;
22682 char_u *scriptname;
22683
22684 /* Check that the autoload name matches the script name. */
22685 j = FAIL;
22686 if (sourcing_name != NULL)
22687 {
22688 scriptname = autoload_name(name);
22689 if (scriptname != NULL)
22690 {
22691 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022692 plen = (int)STRLEN(p);
22693 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022694 if (slen > plen && fnamecmp(p,
22695 sourcing_name + slen - plen) == 0)
22696 j = OK;
22697 vim_free(scriptname);
22698 }
22699 }
22700 if (j == FAIL)
22701 {
22702 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22703 goto erret;
22704 }
22705 }
22706
22707 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 if (fp == NULL)
22709 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022710
22711 if (fudi.fd_dict != NULL)
22712 {
22713 if (fudi.fd_di == NULL)
22714 {
22715 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022716 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022717 if (fudi.fd_di == NULL)
22718 {
22719 vim_free(fp);
22720 goto erret;
22721 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022722 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22723 {
22724 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022725 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022726 goto erret;
22727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022728 }
22729 else
22730 /* overwrite existing dict entry */
22731 clear_tv(&fudi.fd_di->di_tv);
22732 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022733 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022734 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022735 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022736
22737 /* behave like "dict" was used */
22738 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022739 }
22740
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022742 STRCPY(fp->uf_name, name);
22743 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022745 fp->uf_args = newargs;
22746 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022747#ifdef FEAT_PROFILE
22748 fp->uf_tml_count = NULL;
22749 fp->uf_tml_total = NULL;
22750 fp->uf_tml_self = NULL;
22751 fp->uf_profiling = FALSE;
22752 if (prof_def_func())
22753 func_do_profile(fp);
22754#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022755 fp->uf_varargs = varargs;
22756 fp->uf_flags = flags;
22757 fp->uf_calls = 0;
22758 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022759 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760
22761erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 ga_clear_strings(&newargs);
22763 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022764ret_free:
22765 vim_free(skip_until);
22766 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022769 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770}
22771
22772/*
22773 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022774 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022776 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022777 * TFN_INT: internal function name OK
22778 * TFN_QUIET: be quiet
22779 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 * Advances "pp" to just after the function name (if no error).
22781 */
22782 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022783trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784 char_u **pp;
22785 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022786 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022789 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 char_u *start;
22791 char_u *end;
22792 int lead;
22793 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022795 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022796
22797 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022798 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022800
22801 /* Check for hard coded <SNR>: already translated function ID (from a user
22802 * command). */
22803 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22804 && (*pp)[2] == (int)KE_SNR)
22805 {
22806 *pp += 3;
22807 len = get_id_len(pp) + 3;
22808 return vim_strnsave(start, len);
22809 }
22810
22811 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22812 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022814 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022816
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022817 /* Note that TFN_ flags use the same values as GLV_ flags. */
22818 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022819 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022820 if (end == start)
22821 {
22822 if (!skip)
22823 EMSG(_("E129: Function name required"));
22824 goto theend;
22825 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022826 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022827 {
22828 /*
22829 * Report an invalid expression in braces, unless the expression
22830 * evaluation has been cancelled due to an aborting error, an
22831 * interrupt, or an exception.
22832 */
22833 if (!aborting())
22834 {
22835 if (end != NULL)
22836 EMSG2(_(e_invarg2), start);
22837 }
22838 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022839 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022840 goto theend;
22841 }
22842
22843 if (lv.ll_tv != NULL)
22844 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022845 if (fdp != NULL)
22846 {
22847 fdp->fd_dict = lv.ll_dict;
22848 fdp->fd_newkey = lv.ll_newkey;
22849 lv.ll_newkey = NULL;
22850 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022851 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022852 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22853 {
22854 name = vim_strsave(lv.ll_tv->vval.v_string);
22855 *pp = end;
22856 }
22857 else
22858 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022859 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22860 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022861 EMSG(_(e_funcref));
22862 else
22863 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022864 name = NULL;
22865 }
22866 goto theend;
22867 }
22868
22869 if (lv.ll_name == NULL)
22870 {
22871 /* Error found, but continue after the function name. */
22872 *pp = end;
22873 goto theend;
22874 }
22875
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022876 /* Check if the name is a Funcref. If so, use the value. */
22877 if (lv.ll_exp_name != NULL)
22878 {
22879 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022880 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022881 if (name == lv.ll_exp_name)
22882 name = NULL;
22883 }
22884 else
22885 {
22886 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022887 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022888 if (name == *pp)
22889 name = NULL;
22890 }
22891 if (name != NULL)
22892 {
22893 name = vim_strsave(name);
22894 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022895 if (STRNCMP(name, "<SNR>", 5) == 0)
22896 {
22897 /* Change "<SNR>" to the byte sequence. */
22898 name[0] = K_SPECIAL;
22899 name[1] = KS_EXTRA;
22900 name[2] = (int)KE_SNR;
22901 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22902 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022903 goto theend;
22904 }
22905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022906 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022907 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022908 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022909 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22910 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22911 {
22912 /* When there was "s:" already or the name expanded to get a
22913 * leading "s:" then remove it. */
22914 lv.ll_name += 2;
22915 len -= 2;
22916 lead = 2;
22917 }
22918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022919 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022920 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022921 /* skip over "s:" and "g:" */
22922 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022923 lv.ll_name += 2;
22924 len = (int)(end - lv.ll_name);
22925 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022926
22927 /*
22928 * Copy the function name to allocated memory.
22929 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22930 * Accept <SNR>123_name() outside a script.
22931 */
22932 if (skip)
22933 lead = 0; /* do nothing */
22934 else if (lead > 0)
22935 {
22936 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022937 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22938 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022939 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022940 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022941 if (current_SID <= 0)
22942 {
22943 EMSG(_(e_usingsid));
22944 goto theend;
22945 }
22946 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22947 lead += (int)STRLEN(sid_buf);
22948 }
22949 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022950 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022951 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022952 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022953 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022954 goto theend;
22955 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022956 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022957 {
22958 char_u *cp = vim_strchr(lv.ll_name, ':');
22959
22960 if (cp != NULL && cp < end)
22961 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022962 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022963 goto theend;
22964 }
22965 }
22966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022967 name = alloc((unsigned)(len + lead + 1));
22968 if (name != NULL)
22969 {
22970 if (lead > 0)
22971 {
22972 name[0] = K_SPECIAL;
22973 name[1] = KS_EXTRA;
22974 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022975 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022976 STRCPY(name + 3, sid_buf);
22977 }
22978 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022979 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022980 }
22981 *pp = end;
22982
22983theend:
22984 clear_lval(&lv);
22985 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986}
22987
22988/*
22989 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22990 * Return 2 if "p" starts with "s:".
22991 * Return 0 otherwise.
22992 */
22993 static int
22994eval_fname_script(p)
22995 char_u *p;
22996{
22997 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22998 || STRNICMP(p + 1, "SNR>", 4) == 0))
22999 return 5;
23000 if (p[0] == 's' && p[1] == ':')
23001 return 2;
23002 return 0;
23003}
23004
23005/*
23006 * Return TRUE if "p" starts with "<SID>" or "s:".
23007 * Only works if eval_fname_script() returned non-zero for "p"!
23008 */
23009 static int
23010eval_fname_sid(p)
23011 char_u *p;
23012{
23013 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23014}
23015
23016/*
23017 * List the head of the function: "name(arg1, arg2)".
23018 */
23019 static void
23020list_func_head(fp, indent)
23021 ufunc_T *fp;
23022 int indent;
23023{
23024 int j;
23025
23026 msg_start();
23027 if (indent)
23028 MSG_PUTS(" ");
23029 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023030 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031 {
23032 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023033 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 }
23035 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023036 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023038 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039 {
23040 if (j)
23041 MSG_PUTS(", ");
23042 msg_puts(FUNCARG(fp, j));
23043 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023044 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 {
23046 if (j)
23047 MSG_PUTS(", ");
23048 MSG_PUTS("...");
23049 }
23050 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023051 if (fp->uf_flags & FC_ABORT)
23052 MSG_PUTS(" abort");
23053 if (fp->uf_flags & FC_RANGE)
23054 MSG_PUTS(" range");
23055 if (fp->uf_flags & FC_DICT)
23056 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023057 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023058 if (p_verbose > 0)
23059 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060}
23061
23062/*
23063 * Find a function by name, return pointer to it in ufuncs.
23064 * Return NULL for unknown function.
23065 */
23066 static ufunc_T *
23067find_func(name)
23068 char_u *name;
23069{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023070 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023072 hi = hash_find(&func_hashtab, name);
23073 if (!HASHITEM_EMPTY(hi))
23074 return HI2UF(hi);
23075 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076}
23077
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023078#if defined(EXITFREE) || defined(PROTO)
23079 void
23080free_all_functions()
23081{
23082 hashitem_T *hi;
23083
23084 /* Need to start all over every time, because func_free() may change the
23085 * hash table. */
23086 while (func_hashtab.ht_used > 0)
23087 for (hi = func_hashtab.ht_array; ; ++hi)
23088 if (!HASHITEM_EMPTY(hi))
23089 {
23090 func_free(HI2UF(hi));
23091 break;
23092 }
23093}
23094#endif
23095
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023096 int
23097translated_function_exists(name)
23098 char_u *name;
23099{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023100 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023101 return find_internal_func(name) >= 0;
23102 return find_func(name) != NULL;
23103}
23104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023105/*
23106 * Return TRUE if a function "name" exists.
23107 */
23108 static int
23109function_exists(name)
23110 char_u *name;
23111{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023112 char_u *nm = name;
23113 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023114 int n = FALSE;
23115
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023116 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23117 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023118 nm = skipwhite(nm);
23119
23120 /* Only accept "funcname", "funcname ", "funcname (..." and
23121 * "funcname(...", not "funcname!...". */
23122 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023123 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023124 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023125 return n;
23126}
23127
Bram Moolenaara1544c02013-05-30 12:35:52 +020023128 char_u *
23129get_expanded_name(name, check)
23130 char_u *name;
23131 int check;
23132{
23133 char_u *nm = name;
23134 char_u *p;
23135
23136 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23137
23138 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023139 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023140 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023141
Bram Moolenaara1544c02013-05-30 12:35:52 +020023142 vim_free(p);
23143 return NULL;
23144}
23145
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023146/*
23147 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023148 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23149 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023150 */
23151 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023152builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023153 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023154 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023155{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023156 char_u *p;
23157
23158 if (!ASCII_ISLOWER(name[0]))
23159 return FALSE;
23160 p = vim_strchr(name, AUTOLOAD_CHAR);
23161 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023162}
23163
Bram Moolenaar05159a02005-02-26 23:04:13 +000023164#if defined(FEAT_PROFILE) || defined(PROTO)
23165/*
23166 * Start profiling function "fp".
23167 */
23168 static void
23169func_do_profile(fp)
23170 ufunc_T *fp;
23171{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023172 int len = fp->uf_lines.ga_len;
23173
23174 if (len == 0)
23175 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023176 fp->uf_tm_count = 0;
23177 profile_zero(&fp->uf_tm_self);
23178 profile_zero(&fp->uf_tm_total);
23179 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023180 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023181 if (fp->uf_tml_total == NULL)
23182 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023183 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023184 if (fp->uf_tml_self == NULL)
23185 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023186 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023187 fp->uf_tml_idx = -1;
23188 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23189 || fp->uf_tml_self == NULL)
23190 return; /* out of memory */
23191
23192 fp->uf_profiling = TRUE;
23193}
23194
23195/*
23196 * Dump the profiling results for all functions in file "fd".
23197 */
23198 void
23199func_dump_profile(fd)
23200 FILE *fd;
23201{
23202 hashitem_T *hi;
23203 int todo;
23204 ufunc_T *fp;
23205 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023206 ufunc_T **sorttab;
23207 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023208
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023209 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023210 if (todo == 0)
23211 return; /* nothing to dump */
23212
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023213 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023214
Bram Moolenaar05159a02005-02-26 23:04:13 +000023215 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23216 {
23217 if (!HASHITEM_EMPTY(hi))
23218 {
23219 --todo;
23220 fp = HI2UF(hi);
23221 if (fp->uf_profiling)
23222 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023223 if (sorttab != NULL)
23224 sorttab[st_len++] = fp;
23225
Bram Moolenaar05159a02005-02-26 23:04:13 +000023226 if (fp->uf_name[0] == K_SPECIAL)
23227 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23228 else
23229 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23230 if (fp->uf_tm_count == 1)
23231 fprintf(fd, "Called 1 time\n");
23232 else
23233 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23234 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23235 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23236 fprintf(fd, "\n");
23237 fprintf(fd, "count total (s) self (s)\n");
23238
23239 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23240 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023241 if (FUNCLINE(fp, i) == NULL)
23242 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023243 prof_func_line(fd, fp->uf_tml_count[i],
23244 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023245 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23246 }
23247 fprintf(fd, "\n");
23248 }
23249 }
23250 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023251
23252 if (sorttab != NULL && st_len > 0)
23253 {
23254 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23255 prof_total_cmp);
23256 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23257 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23258 prof_self_cmp);
23259 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23260 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023261
23262 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023263}
Bram Moolenaar73830342005-02-28 22:48:19 +000023264
23265 static void
23266prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23267 FILE *fd;
23268 ufunc_T **sorttab;
23269 int st_len;
23270 char *title;
23271 int prefer_self; /* when equal print only self time */
23272{
23273 int i;
23274 ufunc_T *fp;
23275
23276 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23277 fprintf(fd, "count total (s) self (s) function\n");
23278 for (i = 0; i < 20 && i < st_len; ++i)
23279 {
23280 fp = sorttab[i];
23281 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23282 prefer_self);
23283 if (fp->uf_name[0] == K_SPECIAL)
23284 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23285 else
23286 fprintf(fd, " %s()\n", fp->uf_name);
23287 }
23288 fprintf(fd, "\n");
23289}
23290
23291/*
23292 * Print the count and times for one function or function line.
23293 */
23294 static void
23295prof_func_line(fd, count, total, self, prefer_self)
23296 FILE *fd;
23297 int count;
23298 proftime_T *total;
23299 proftime_T *self;
23300 int prefer_self; /* when equal print only self time */
23301{
23302 if (count > 0)
23303 {
23304 fprintf(fd, "%5d ", count);
23305 if (prefer_self && profile_equal(total, self))
23306 fprintf(fd, " ");
23307 else
23308 fprintf(fd, "%s ", profile_msg(total));
23309 if (!prefer_self && profile_equal(total, self))
23310 fprintf(fd, " ");
23311 else
23312 fprintf(fd, "%s ", profile_msg(self));
23313 }
23314 else
23315 fprintf(fd, " ");
23316}
23317
23318/*
23319 * Compare function for total time sorting.
23320 */
23321 static int
23322#ifdef __BORLANDC__
23323_RTLENTRYF
23324#endif
23325prof_total_cmp(s1, s2)
23326 const void *s1;
23327 const void *s2;
23328{
23329 ufunc_T *p1, *p2;
23330
23331 p1 = *(ufunc_T **)s1;
23332 p2 = *(ufunc_T **)s2;
23333 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23334}
23335
23336/*
23337 * Compare function for self time sorting.
23338 */
23339 static int
23340#ifdef __BORLANDC__
23341_RTLENTRYF
23342#endif
23343prof_self_cmp(s1, s2)
23344 const void *s1;
23345 const void *s2;
23346{
23347 ufunc_T *p1, *p2;
23348
23349 p1 = *(ufunc_T **)s1;
23350 p2 = *(ufunc_T **)s2;
23351 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23352}
23353
Bram Moolenaar05159a02005-02-26 23:04:13 +000023354#endif
23355
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023356/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023357 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023358 * Return TRUE if a package was loaded.
23359 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023360 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023361script_autoload(name, reload)
23362 char_u *name;
23363 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023364{
23365 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023366 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023367 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023368 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023369
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023370 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023371 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023372 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023373 return FALSE;
23374
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023375 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023376
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023377 /* Find the name in the list of previously loaded package names. Skip
23378 * "autoload/", it's always the same. */
23379 for (i = 0; i < ga_loaded.ga_len; ++i)
23380 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23381 break;
23382 if (!reload && i < ga_loaded.ga_len)
23383 ret = FALSE; /* was loaded already */
23384 else
23385 {
23386 /* Remember the name if it wasn't loaded already. */
23387 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23388 {
23389 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23390 tofree = NULL;
23391 }
23392
23393 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023394 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023395 ret = TRUE;
23396 }
23397
23398 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023399 return ret;
23400}
23401
23402/*
23403 * Return the autoload script name for a function or variable name.
23404 * Returns NULL when out of memory.
23405 */
23406 static char_u *
23407autoload_name(name)
23408 char_u *name;
23409{
23410 char_u *p;
23411 char_u *scriptname;
23412
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023413 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023414 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23415 if (scriptname == NULL)
23416 return FALSE;
23417 STRCPY(scriptname, "autoload/");
23418 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023419 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023420 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023421 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023422 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023423 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023424}
23425
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23427
23428/*
23429 * Function given to ExpandGeneric() to obtain the list of user defined
23430 * function names.
23431 */
23432 char_u *
23433get_user_func_name(xp, idx)
23434 expand_T *xp;
23435 int idx;
23436{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023437 static long_u done;
23438 static hashitem_T *hi;
23439 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440
23441 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023443 done = 0;
23444 hi = func_hashtab.ht_array;
23445 }
23446 if (done < func_hashtab.ht_used)
23447 {
23448 if (done++ > 0)
23449 ++hi;
23450 while (HASHITEM_EMPTY(hi))
23451 ++hi;
23452 fp = HI2UF(hi);
23453
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023454 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023455 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023456
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023457 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23458 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459
23460 cat_func_name(IObuff, fp);
23461 if (xp->xp_context != EXPAND_USER_FUNC)
23462 {
23463 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023464 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465 STRCAT(IObuff, ")");
23466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 return IObuff;
23468 }
23469 return NULL;
23470}
23471
23472#endif /* FEAT_CMDL_COMPL */
23473
23474/*
23475 * Copy the function name of "fp" to buffer "buf".
23476 * "buf" must be able to hold the function name plus three bytes.
23477 * Takes care of script-local function names.
23478 */
23479 static void
23480cat_func_name(buf, fp)
23481 char_u *buf;
23482 ufunc_T *fp;
23483{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023484 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 {
23486 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023487 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 }
23489 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023490 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491}
23492
23493/*
23494 * ":delfunction {name}"
23495 */
23496 void
23497ex_delfunction(eap)
23498 exarg_T *eap;
23499{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023500 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 char_u *p;
23502 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023503 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504
23505 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023506 name = trans_function_name(&p, eap->skip, 0, &fudi);
23507 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023509 {
23510 if (fudi.fd_dict != NULL && !eap->skip)
23511 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 if (!ends_excmd(*skipwhite(p)))
23515 {
23516 vim_free(name);
23517 EMSG(_(e_trailing));
23518 return;
23519 }
23520 eap->nextcmd = check_nextcmd(p);
23521 if (eap->nextcmd != NULL)
23522 *p = NUL;
23523
23524 if (!eap->skip)
23525 fp = find_func(name);
23526 vim_free(name);
23527
23528 if (!eap->skip)
23529 {
23530 if (fp == NULL)
23531 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023532 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 return;
23534 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023535 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 {
23537 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23538 return;
23539 }
23540
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023541 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023543 /* Delete the dict item that refers to the function, it will
23544 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023545 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023547 else
23548 func_free(fp);
23549 }
23550}
23551
23552/*
23553 * Free a function and remove it from the list of functions.
23554 */
23555 static void
23556func_free(fp)
23557 ufunc_T *fp;
23558{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023559 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023560
23561 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023562 ga_clear_strings(&(fp->uf_args));
23563 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023564#ifdef FEAT_PROFILE
23565 vim_free(fp->uf_tml_count);
23566 vim_free(fp->uf_tml_total);
23567 vim_free(fp->uf_tml_self);
23568#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023569
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023570 /* remove the function from the function hashtable */
23571 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23572 if (HASHITEM_EMPTY(hi))
23573 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023574 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023575 hash_remove(&func_hashtab, hi);
23576
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023577 vim_free(fp);
23578}
23579
23580/*
23581 * Unreference a Function: decrement the reference count and free it when it
23582 * becomes zero. Only for numbered functions.
23583 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023584 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023585func_unref(name)
23586 char_u *name;
23587{
23588 ufunc_T *fp;
23589
23590 if (name != NULL && isdigit(*name))
23591 {
23592 fp = find_func(name);
23593 if (fp == NULL)
23594 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023595 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023596 {
23597 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023598 * when "uf_calls" becomes zero. */
23599 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023600 func_free(fp);
23601 }
23602 }
23603}
23604
23605/*
23606 * Count a reference to a Function.
23607 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023608 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023609func_ref(name)
23610 char_u *name;
23611{
23612 ufunc_T *fp;
23613
23614 if (name != NULL && isdigit(*name))
23615 {
23616 fp = find_func(name);
23617 if (fp == NULL)
23618 EMSG2(_(e_intern2), "func_ref()");
23619 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023620 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 }
23622}
23623
23624/*
23625 * Call a user function.
23626 */
23627 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023628call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 ufunc_T *fp; /* pointer to function */
23630 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023631 typval_T *argvars; /* arguments */
23632 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 linenr_T firstline; /* first line of range */
23634 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023635 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636{
Bram Moolenaar33570922005-01-25 22:26:29 +000023637 char_u *save_sourcing_name;
23638 linenr_T save_sourcing_lnum;
23639 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023640 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023641 int save_did_emsg;
23642 static int depth = 0;
23643 dictitem_T *v;
23644 int fixvar_idx = 0; /* index in fixvar[] */
23645 int i;
23646 int ai;
23647 char_u numbuf[NUMBUFLEN];
23648 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023649#ifdef FEAT_PROFILE
23650 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023651 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653
23654 /* If depth of calling is getting too high, don't execute the function */
23655 if (depth >= p_mfd)
23656 {
23657 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023658 rettv->v_type = VAR_NUMBER;
23659 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 return;
23661 }
23662 ++depth;
23663
23664 line_breakcheck(); /* check for CTRL-C hit */
23665
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023666 fc = (funccall_T *)alloc(sizeof(funccall_T));
23667 fc->caller = current_funccal;
23668 current_funccal = fc;
23669 fc->func = fp;
23670 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023671 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023672 fc->linenr = 0;
23673 fc->returned = FALSE;
23674 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023676 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23677 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678
Bram Moolenaar33570922005-01-25 22:26:29 +000023679 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023680 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023681 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23682 * each argument variable and saves a lot of time.
23683 */
23684 /*
23685 * Init l: variables.
23686 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023687 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023688 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023689 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023690 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23691 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023692 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023693 name = v->di_key;
23694 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023695 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023696 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023697 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023698 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023699 v->di_tv.vval.v_dict = selfdict;
23700 ++selfdict->dv_refcount;
23701 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023702
Bram Moolenaar33570922005-01-25 22:26:29 +000023703 /*
23704 * Init a: variables.
23705 * Set a:0 to "argcount".
23706 * Set a:000 to a list with room for the "..." arguments.
23707 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023708 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023709 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023710 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023711 /* Use "name" to avoid a warning from some compiler that checks the
23712 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023713 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023714 name = v->di_key;
23715 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023716 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023717 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023718 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023719 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023720 v->di_tv.vval.v_list = &fc->l_varlist;
23721 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23722 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23723 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023724
23725 /*
23726 * Set a:firstline to "firstline" and a:lastline to "lastline".
23727 * Set a:name to named arguments.
23728 * Set a:N to the "..." arguments.
23729 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023730 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023731 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023732 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023733 (varnumber_T)lastline);
23734 for (i = 0; i < argcount; ++i)
23735 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023736 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023737 if (ai < 0)
23738 /* named argument a:name */
23739 name = FUNCARG(fp, i);
23740 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023741 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023742 /* "..." argument a:1, a:2, etc. */
23743 sprintf((char *)numbuf, "%d", ai + 1);
23744 name = numbuf;
23745 }
23746 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23747 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023748 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023749 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23750 }
23751 else
23752 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023753 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23754 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023755 if (v == NULL)
23756 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023757 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000023758 }
23759 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023760 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023762 /* Note: the values are copied directly to avoid alloc/free.
23763 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023764 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023765 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023766
23767 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23768 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023769 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23770 fc->l_listitems[ai].li_tv = argvars[i];
23771 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023772 }
23773 }
23774
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 /* Don't redraw while executing the function. */
23776 ++RedrawingDisabled;
23777 save_sourcing_name = sourcing_name;
23778 save_sourcing_lnum = sourcing_lnum;
23779 sourcing_lnum = 1;
23780 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023781 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 if (sourcing_name != NULL)
23783 {
23784 if (save_sourcing_name != NULL
23785 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23786 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23787 else
23788 STRCPY(sourcing_name, "function ");
23789 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23790
23791 if (p_verbose >= 12)
23792 {
23793 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023794 verbose_enter_scroll();
23795
Bram Moolenaar555b2802005-05-19 21:08:39 +000023796 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 if (p_verbose >= 14)
23798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023800 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023801 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023802 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803
23804 msg_puts((char_u *)"(");
23805 for (i = 0; i < argcount; ++i)
23806 {
23807 if (i > 0)
23808 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023809 if (argvars[i].v_type == VAR_NUMBER)
23810 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 else
23812 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023813 /* Do not want errors such as E724 here. */
23814 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023815 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023816 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023817 if (s != NULL)
23818 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023819 if (vim_strsize(s) > MSG_BUF_CLEN)
23820 {
23821 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23822 s = buf;
23823 }
23824 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023825 vim_free(tofree);
23826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 }
23828 }
23829 msg_puts((char_u *)")");
23830 }
23831 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023832
23833 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 --no_wait_return;
23835 }
23836 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023837#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023838 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023839 {
23840 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23841 func_do_profile(fp);
23842 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023843 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023844 {
23845 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023846 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023847 profile_zero(&fp->uf_tm_children);
23848 }
23849 script_prof_save(&wait_start);
23850 }
23851#endif
23852
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023854 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 save_did_emsg = did_emsg;
23856 did_emsg = FALSE;
23857
23858 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023859 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23861
23862 --RedrawingDisabled;
23863
23864 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023865 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023867 clear_tv(rettv);
23868 rettv->v_type = VAR_NUMBER;
23869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870 }
23871
Bram Moolenaar05159a02005-02-26 23:04:13 +000023872#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023873 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023874 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023875 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023876 profile_end(&call_start);
23877 profile_sub_wait(&wait_start, &call_start);
23878 profile_add(&fp->uf_tm_total, &call_start);
23879 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023880 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023881 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023882 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23883 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023884 }
23885 }
23886#endif
23887
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 /* when being verbose, mention the return value */
23889 if (p_verbose >= 12)
23890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023892 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023895 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023896 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023897 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023898 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023901 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023902 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023903 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023904 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023905
Bram Moolenaar555b2802005-05-19 21:08:39 +000023906 /* The value may be very long. Skip the middle part, so that we
23907 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023908 * truncate it at the end. Don't want errors such as E724 here. */
23909 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023910 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023911 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023912 if (s != NULL)
23913 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023914 if (vim_strsize(s) > MSG_BUF_CLEN)
23915 {
23916 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23917 s = buf;
23918 }
23919 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023920 vim_free(tofree);
23921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 }
23923 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023924
23925 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 --no_wait_return;
23927 }
23928
23929 vim_free(sourcing_name);
23930 sourcing_name = save_sourcing_name;
23931 sourcing_lnum = save_sourcing_lnum;
23932 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023933#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023934 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023935 script_prof_restore(&wait_start);
23936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937
23938 if (p_verbose >= 12 && sourcing_name != NULL)
23939 {
23940 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023941 verbose_enter_scroll();
23942
Bram Moolenaar555b2802005-05-19 21:08:39 +000023943 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023945
23946 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 --no_wait_return;
23948 }
23949
23950 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023951 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023953
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023954 /* If the a:000 list and the l: and a: dicts are not referenced we can
23955 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023956 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23957 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23958 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23959 {
23960 free_funccal(fc, FALSE);
23961 }
23962 else
23963 {
23964 hashitem_T *hi;
23965 listitem_T *li;
23966 int todo;
23967
23968 /* "fc" is still in use. This can happen when returning "a:000" or
23969 * assigning "l:" to a global variable.
23970 * Link "fc" in the list for garbage collection later. */
23971 fc->caller = previous_funccal;
23972 previous_funccal = fc;
23973
23974 /* Make a copy of the a: variables, since we didn't do that above. */
23975 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23976 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23977 {
23978 if (!HASHITEM_EMPTY(hi))
23979 {
23980 --todo;
23981 v = HI2DI(hi);
23982 copy_tv(&v->di_tv, &v->di_tv);
23983 }
23984 }
23985
23986 /* Make a copy of the a:000 items, since we didn't do that above. */
23987 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23988 copy_tv(&li->li_tv, &li->li_tv);
23989 }
23990}
23991
23992/*
23993 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023994 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023995 */
23996 static int
23997can_free_funccal(fc, copyID)
23998 funccall_T *fc;
23999 int copyID;
24000{
24001 return (fc->l_varlist.lv_copyID != copyID
24002 && fc->l_vars.dv_copyID != copyID
24003 && fc->l_avars.dv_copyID != copyID);
24004}
24005
24006/*
24007 * Free "fc" and what it contains.
24008 */
24009 static void
24010free_funccal(fc, free_val)
24011 funccall_T *fc;
24012 int free_val; /* a: vars were allocated */
24013{
24014 listitem_T *li;
24015
24016 /* The a: variables typevals may not have been allocated, only free the
24017 * allocated variables. */
24018 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24019
24020 /* free all l: variables */
24021 vars_clear(&fc->l_vars.dv_hashtab);
24022
24023 /* Free the a:000 variables if they were allocated. */
24024 if (free_val)
24025 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24026 clear_tv(&li->li_tv);
24027
24028 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029}
24030
24031/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024032 * Add a number variable "name" to dict "dp" with value "nr".
24033 */
24034 static void
24035add_nr_var(dp, v, name, nr)
24036 dict_T *dp;
24037 dictitem_T *v;
24038 char *name;
24039 varnumber_T nr;
24040{
24041 STRCPY(v->di_key, name);
24042 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24043 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24044 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024045 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024046 v->di_tv.vval.v_number = nr;
24047}
24048
24049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 * ":return [expr]"
24051 */
24052 void
24053ex_return(eap)
24054 exarg_T *eap;
24055{
24056 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024057 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 int returning = FALSE;
24059
24060 if (current_funccal == NULL)
24061 {
24062 EMSG(_("E133: :return not inside a function"));
24063 return;
24064 }
24065
24066 if (eap->skip)
24067 ++emsg_skip;
24068
24069 eap->nextcmd = NULL;
24070 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024071 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 {
24073 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024074 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024076 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 }
24078 /* It's safer to return also on error. */
24079 else if (!eap->skip)
24080 {
24081 /*
24082 * Return unless the expression evaluation has been cancelled due to an
24083 * aborting error, an interrupt, or an exception.
24084 */
24085 if (!aborting())
24086 returning = do_return(eap, FALSE, TRUE, NULL);
24087 }
24088
24089 /* When skipping or the return gets pending, advance to the next command
24090 * in this line (!returning). Otherwise, ignore the rest of the line.
24091 * Following lines will be ignored by get_func_line(). */
24092 if (returning)
24093 eap->nextcmd = NULL;
24094 else if (eap->nextcmd == NULL) /* no argument */
24095 eap->nextcmd = check_nextcmd(arg);
24096
24097 if (eap->skip)
24098 --emsg_skip;
24099}
24100
24101/*
24102 * Return from a function. Possibly makes the return pending. Also called
24103 * for a pending return at the ":endtry" or after returning from an extra
24104 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024105 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024106 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 * FALSE when the return gets pending.
24108 */
24109 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024110do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 exarg_T *eap;
24112 int reanimate;
24113 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024114 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115{
24116 int idx;
24117 struct condstack *cstack = eap->cstack;
24118
24119 if (reanimate)
24120 /* Undo the return. */
24121 current_funccal->returned = FALSE;
24122
24123 /*
24124 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24125 * not in its finally clause (which then is to be executed next) is found.
24126 * In this case, make the ":return" pending for execution at the ":endtry".
24127 * Otherwise, return normally.
24128 */
24129 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24130 if (idx >= 0)
24131 {
24132 cstack->cs_pending[idx] = CSTP_RETURN;
24133
24134 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024135 /* A pending return again gets pending. "rettv" points to an
24136 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024138 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139 else
24140 {
24141 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024142 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024144 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024146 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147 {
24148 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024149 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024150 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 else
24152 EMSG(_(e_outofmem));
24153 }
24154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024155 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156
24157 if (reanimate)
24158 {
24159 /* The pending return value could be overwritten by a ":return"
24160 * without argument in a finally clause; reset the default
24161 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024162 current_funccal->rettv->v_type = VAR_NUMBER;
24163 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 }
24165 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024166 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 }
24168 else
24169 {
24170 current_funccal->returned = TRUE;
24171
24172 /* If the return is carried out now, store the return value. For
24173 * a return immediately after reanimation, the value is already
24174 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024175 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024177 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024178 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024180 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 }
24182 }
24183
24184 return idx < 0;
24185}
24186
24187/*
24188 * Free the variable with a pending return value.
24189 */
24190 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024191discard_pending_return(rettv)
24192 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193{
Bram Moolenaar33570922005-01-25 22:26:29 +000024194 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195}
24196
24197/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024198 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 * is an allocated string. Used by report_pending() for verbose messages.
24200 */
24201 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024202get_return_cmd(rettv)
24203 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024205 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024206 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024207 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024209 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024210 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024211 if (s == NULL)
24212 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024213
24214 STRCPY(IObuff, ":return ");
24215 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24216 if (STRLEN(s) + 8 >= IOSIZE)
24217 STRCPY(IObuff + IOSIZE - 4, "...");
24218 vim_free(tofree);
24219 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220}
24221
24222/*
24223 * Get next function line.
24224 * Called by do_cmdline() to get the next line.
24225 * Returns allocated string, or NULL for end of function.
24226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 char_u *
24228get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024229 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024231 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232{
Bram Moolenaar33570922005-01-25 22:26:29 +000024233 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024234 ufunc_T *fp = fcp->func;
24235 char_u *retval;
24236 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237
24238 /* If breakpoints have been added/deleted need to check for it. */
24239 if (fcp->dbg_tick != debug_tick)
24240 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024241 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 sourcing_lnum);
24243 fcp->dbg_tick = debug_tick;
24244 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024245#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024246 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024247 func_line_end(cookie);
24248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249
Bram Moolenaar05159a02005-02-26 23:04:13 +000024250 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024251 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24252 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 retval = NULL;
24254 else
24255 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024256 /* Skip NULL lines (continuation lines). */
24257 while (fcp->linenr < gap->ga_len
24258 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24259 ++fcp->linenr;
24260 if (fcp->linenr >= gap->ga_len)
24261 retval = NULL;
24262 else
24263 {
24264 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24265 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024266#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024267 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024268 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024269#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 }
24272
24273 /* Did we encounter a breakpoint? */
24274 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24275 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024276 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024278 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 sourcing_lnum);
24280 fcp->dbg_tick = debug_tick;
24281 }
24282
24283 return retval;
24284}
24285
Bram Moolenaar05159a02005-02-26 23:04:13 +000024286#if defined(FEAT_PROFILE) || defined(PROTO)
24287/*
24288 * Called when starting to read a function line.
24289 * "sourcing_lnum" must be correct!
24290 * When skipping lines it may not actually be executed, but we won't find out
24291 * until later and we need to store the time now.
24292 */
24293 void
24294func_line_start(cookie)
24295 void *cookie;
24296{
24297 funccall_T *fcp = (funccall_T *)cookie;
24298 ufunc_T *fp = fcp->func;
24299
24300 if (fp->uf_profiling && sourcing_lnum >= 1
24301 && sourcing_lnum <= fp->uf_lines.ga_len)
24302 {
24303 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024304 /* Skip continuation lines. */
24305 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24306 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024307 fp->uf_tml_execed = FALSE;
24308 profile_start(&fp->uf_tml_start);
24309 profile_zero(&fp->uf_tml_children);
24310 profile_get_wait(&fp->uf_tml_wait);
24311 }
24312}
24313
24314/*
24315 * Called when actually executing a function line.
24316 */
24317 void
24318func_line_exec(cookie)
24319 void *cookie;
24320{
24321 funccall_T *fcp = (funccall_T *)cookie;
24322 ufunc_T *fp = fcp->func;
24323
24324 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24325 fp->uf_tml_execed = TRUE;
24326}
24327
24328/*
24329 * Called when done with a function line.
24330 */
24331 void
24332func_line_end(cookie)
24333 void *cookie;
24334{
24335 funccall_T *fcp = (funccall_T *)cookie;
24336 ufunc_T *fp = fcp->func;
24337
24338 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24339 {
24340 if (fp->uf_tml_execed)
24341 {
24342 ++fp->uf_tml_count[fp->uf_tml_idx];
24343 profile_end(&fp->uf_tml_start);
24344 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024345 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024346 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24347 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024348 }
24349 fp->uf_tml_idx = -1;
24350 }
24351}
24352#endif
24353
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354/*
24355 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024356 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 */
24358 int
24359func_has_ended(cookie)
24360 void *cookie;
24361{
Bram Moolenaar33570922005-01-25 22:26:29 +000024362 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363
24364 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24365 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024366 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 || fcp->returned);
24368}
24369
24370/*
24371 * return TRUE if cookie indicates a function which "abort"s on errors.
24372 */
24373 int
24374func_has_abort(cookie)
24375 void *cookie;
24376{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024377 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378}
24379
24380#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24381typedef enum
24382{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024383 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24384 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24385 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386} var_flavour_T;
24387
24388static var_flavour_T var_flavour __ARGS((char_u *varname));
24389
24390 static var_flavour_T
24391var_flavour(varname)
24392 char_u *varname;
24393{
24394 char_u *p = varname;
24395
24396 if (ASCII_ISUPPER(*p))
24397 {
24398 while (*(++p))
24399 if (ASCII_ISLOWER(*p))
24400 return VAR_FLAVOUR_SESSION;
24401 return VAR_FLAVOUR_VIMINFO;
24402 }
24403 else
24404 return VAR_FLAVOUR_DEFAULT;
24405}
24406#endif
24407
24408#if defined(FEAT_VIMINFO) || defined(PROTO)
24409/*
24410 * Restore global vars that start with a capital from the viminfo file
24411 */
24412 int
24413read_viminfo_varlist(virp, writing)
24414 vir_T *virp;
24415 int writing;
24416{
24417 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024418 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024419 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420
24421 if (!writing && (find_viminfo_parameter('!') != NULL))
24422 {
24423 tab = vim_strchr(virp->vir_line + 1, '\t');
24424 if (tab != NULL)
24425 {
24426 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024427 switch (*tab)
24428 {
24429 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024430#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024431 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024432#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024433 case 'D': type = VAR_DICT; break;
24434 case 'L': type = VAR_LIST; break;
24435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436
24437 tab = vim_strchr(tab, '\t');
24438 if (tab != NULL)
24439 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024440 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024441 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024442 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024444#ifdef FEAT_FLOAT
24445 else if (type == VAR_FLOAT)
24446 (void)string2float(tab + 1, &tv.vval.v_float);
24447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024449 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024450 if (type == VAR_DICT || type == VAR_LIST)
24451 {
24452 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24453
24454 if (etv == NULL)
24455 /* Failed to parse back the dict or list, use it as a
24456 * string. */
24457 tv.v_type = VAR_STRING;
24458 else
24459 {
24460 vim_free(tv.vval.v_string);
24461 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024462 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024463 }
24464 }
24465
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024466 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024467
24468 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024469 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024470 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24471 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472 }
24473 }
24474 }
24475
24476 return viminfo_readline(virp);
24477}
24478
24479/*
24480 * Write global vars that start with a capital to the viminfo file
24481 */
24482 void
24483write_viminfo_varlist(fp)
24484 FILE *fp;
24485{
Bram Moolenaar33570922005-01-25 22:26:29 +000024486 hashitem_T *hi;
24487 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024488 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024489 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024490 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024491 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024492 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024493
24494 if (find_viminfo_parameter('!') == NULL)
24495 return;
24496
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024497 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024498
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024499 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024500 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024501 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024502 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024504 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024505 this_var = HI2DI(hi);
24506 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024507 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024508 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024509 {
24510 case VAR_STRING: s = "STR"; break;
24511 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024512#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024513 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024514#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024515 case VAR_DICT: s = "DIC"; break;
24516 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024517 default: continue;
24518 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024519 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024520 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024521 if (p != NULL)
24522 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024523 vim_free(tofree);
24524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525 }
24526 }
24527}
24528#endif
24529
24530#if defined(FEAT_SESSION) || defined(PROTO)
24531 int
24532store_session_globals(fd)
24533 FILE *fd;
24534{
Bram Moolenaar33570922005-01-25 22:26:29 +000024535 hashitem_T *hi;
24536 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024537 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 char_u *p, *t;
24539
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024540 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024541 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024543 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024544 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024545 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024546 this_var = HI2DI(hi);
24547 if ((this_var->di_tv.v_type == VAR_NUMBER
24548 || this_var->di_tv.v_type == VAR_STRING)
24549 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024550 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024551 /* Escape special characters with a backslash. Turn a LF and
24552 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024553 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024554 (char_u *)"\\\"\n\r");
24555 if (p == NULL) /* out of memory */
24556 break;
24557 for (t = p; *t != NUL; ++t)
24558 if (*t == '\n')
24559 *t = 'n';
24560 else if (*t == '\r')
24561 *t = 'r';
24562 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024563 this_var->di_key,
24564 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24565 : ' ',
24566 p,
24567 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24568 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024569 || put_eol(fd) == FAIL)
24570 {
24571 vim_free(p);
24572 return FAIL;
24573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 vim_free(p);
24575 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024576#ifdef FEAT_FLOAT
24577 else if (this_var->di_tv.v_type == VAR_FLOAT
24578 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24579 {
24580 float_T f = this_var->di_tv.vval.v_float;
24581 int sign = ' ';
24582
24583 if (f < 0)
24584 {
24585 f = -f;
24586 sign = '-';
24587 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024588 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024589 this_var->di_key, sign, f) < 0)
24590 || put_eol(fd) == FAIL)
24591 return FAIL;
24592 }
24593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594 }
24595 }
24596 return OK;
24597}
24598#endif
24599
Bram Moolenaar661b1822005-07-28 22:36:45 +000024600/*
24601 * Display script name where an item was last set.
24602 * Should only be invoked when 'verbose' is non-zero.
24603 */
24604 void
24605last_set_msg(scriptID)
24606 scid_T scriptID;
24607{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024608 char_u *p;
24609
Bram Moolenaar661b1822005-07-28 22:36:45 +000024610 if (scriptID != 0)
24611 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024612 p = home_replace_save(NULL, get_scriptname(scriptID));
24613 if (p != NULL)
24614 {
24615 verbose_enter();
24616 MSG_PUTS(_("\n\tLast set from "));
24617 MSG_PUTS(p);
24618 vim_free(p);
24619 verbose_leave();
24620 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024621 }
24622}
24623
Bram Moolenaard812df62008-11-09 12:46:09 +000024624/*
24625 * List v:oldfiles in a nice way.
24626 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024627 void
24628ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024629 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024630{
24631 list_T *l = vimvars[VV_OLDFILES].vv_list;
24632 listitem_T *li;
24633 int nr = 0;
24634
24635 if (l == NULL)
24636 msg((char_u *)_("No old files"));
24637 else
24638 {
24639 msg_start();
24640 msg_scroll = TRUE;
24641 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24642 {
24643 msg_outnum((long)++nr);
24644 MSG_PUTS(": ");
24645 msg_outtrans(get_tv_string(&li->li_tv));
24646 msg_putchar('\n');
24647 out_flush(); /* output one line at a time */
24648 ui_breakcheck();
24649 }
24650 /* Assume "got_int" was set to truncate the listing. */
24651 got_int = FALSE;
24652
24653#ifdef FEAT_BROWSE_CMD
24654 if (cmdmod.browse)
24655 {
24656 quit_more = FALSE;
24657 nr = prompt_for_number(FALSE);
24658 msg_starthere();
24659 if (nr > 0)
24660 {
24661 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24662 (long)nr);
24663
24664 if (p != NULL)
24665 {
24666 p = expand_env_save(p);
24667 eap->arg = p;
24668 eap->cmdidx = CMD_edit;
24669 cmdmod.browse = FALSE;
24670 do_exedit(eap, NULL);
24671 vim_free(p);
24672 }
24673 }
24674 }
24675#endif
24676 }
24677}
24678
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679#endif /* FEAT_EVAL */
24680
Bram Moolenaar071d4272004-06-13 20:20:40 +000024681
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024682#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024683
24684#ifdef WIN3264
24685/*
24686 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24687 */
24688static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24689static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24690static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24691
24692/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024693 * Get the short path (8.3) for the filename in "fnamep".
24694 * Only works for a valid file name.
24695 * When the path gets longer "fnamep" is changed and the allocated buffer
24696 * is put in "bufp".
24697 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24698 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699 */
24700 static int
24701get_short_pathname(fnamep, bufp, fnamelen)
24702 char_u **fnamep;
24703 char_u **bufp;
24704 int *fnamelen;
24705{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024706 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 char_u *newbuf;
24708
24709 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710 l = GetShortPathName(*fnamep, *fnamep, len);
24711 if (l > len - 1)
24712 {
24713 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024714 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 newbuf = vim_strnsave(*fnamep, l);
24716 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024717 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718
24719 vim_free(*bufp);
24720 *fnamep = *bufp = newbuf;
24721
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024722 /* Really should always succeed, as the buffer is big enough. */
24723 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024724 }
24725
24726 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024727 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728}
24729
24730/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024731 * Get the short path (8.3) for the filename in "fname". The converted
24732 * path is returned in "bufp".
24733 *
24734 * Some of the directories specified in "fname" may not exist. This function
24735 * will shorten the existing directories at the beginning of the path and then
24736 * append the remaining non-existing path.
24737 *
24738 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024739 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024740 * bufp - Pointer to an allocated buffer for the filename.
24741 * fnamelen - Length of the filename pointed to by fname
24742 *
24743 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744 */
24745 static int
24746shortpath_for_invalid_fname(fname, bufp, fnamelen)
24747 char_u **fname;
24748 char_u **bufp;
24749 int *fnamelen;
24750{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024751 char_u *short_fname, *save_fname, *pbuf_unused;
24752 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024754 int old_len, len;
24755 int new_len, sfx_len;
24756 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757
24758 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024759 old_len = *fnamelen;
24760 save_fname = vim_strnsave(*fname, old_len);
24761 pbuf_unused = NULL;
24762 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024764 endp = save_fname + old_len - 1; /* Find the end of the copy */
24765 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024767 /*
24768 * Try shortening the supplied path till it succeeds by removing one
24769 * directory at a time from the tail of the path.
24770 */
24771 len = 0;
24772 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024774 /* go back one path-separator */
24775 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24776 --endp;
24777 if (endp <= save_fname)
24778 break; /* processed the complete path */
24779
24780 /*
24781 * Replace the path separator with a NUL and try to shorten the
24782 * resulting path.
24783 */
24784 ch = *endp;
24785 *endp = 0;
24786 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024787 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024788 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24789 {
24790 retval = FAIL;
24791 goto theend;
24792 }
24793 *endp = ch; /* preserve the string */
24794
24795 if (len > 0)
24796 break; /* successfully shortened the path */
24797
24798 /* failed to shorten the path. Skip the path separator */
24799 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800 }
24801
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024802 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024804 /*
24805 * Succeeded in shortening the path. Now concatenate the shortened
24806 * path with the remaining path at the tail.
24807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024809 /* Compute the length of the new path. */
24810 sfx_len = (int)(save_endp - endp) + 1;
24811 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024813 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024815 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024817 /* There is not enough space in the currently allocated string,
24818 * copy it to a buffer big enough. */
24819 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024820 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024821 {
24822 retval = FAIL;
24823 goto theend;
24824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024825 }
24826 else
24827 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024828 /* Transfer short_fname to the main buffer (it's big enough),
24829 * unless get_short_pathname() did its work in-place. */
24830 *fname = *bufp = save_fname;
24831 if (short_fname != save_fname)
24832 vim_strncpy(save_fname, short_fname, len);
24833 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024835
24836 /* concat the not-shortened part of the path */
24837 vim_strncpy(*fname + len, endp, sfx_len);
24838 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024840
24841theend:
24842 vim_free(pbuf_unused);
24843 vim_free(save_fname);
24844
24845 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846}
24847
24848/*
24849 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024850 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 */
24852 static int
24853shortpath_for_partial(fnamep, bufp, fnamelen)
24854 char_u **fnamep;
24855 char_u **bufp;
24856 int *fnamelen;
24857{
24858 int sepcount, len, tflen;
24859 char_u *p;
24860 char_u *pbuf, *tfname;
24861 int hasTilde;
24862
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024863 /* Count up the path separators from the RHS.. so we know which part
24864 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024866 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867 if (vim_ispathsep(*p))
24868 ++sepcount;
24869
24870 /* Need full path first (use expand_env() to remove a "~/") */
24871 hasTilde = (**fnamep == '~');
24872 if (hasTilde)
24873 pbuf = tfname = expand_env_save(*fnamep);
24874 else
24875 pbuf = tfname = FullName_save(*fnamep, FALSE);
24876
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024877 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024879 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24880 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881
24882 if (len == 0)
24883 {
24884 /* Don't have a valid filename, so shorten the rest of the
24885 * path if we can. This CAN give us invalid 8.3 filenames, but
24886 * there's not a lot of point in guessing what it might be.
24887 */
24888 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024889 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24890 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891 }
24892
24893 /* Count the paths backward to find the beginning of the desired string. */
24894 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024895 {
24896#ifdef FEAT_MBYTE
24897 if (has_mbyte)
24898 p -= mb_head_off(tfname, p);
24899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 if (vim_ispathsep(*p))
24901 {
24902 if (sepcount == 0 || (hasTilde && sepcount == 1))
24903 break;
24904 else
24905 sepcount --;
24906 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 if (hasTilde)
24909 {
24910 --p;
24911 if (p >= tfname)
24912 *p = '~';
24913 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024914 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915 }
24916 else
24917 ++p;
24918
24919 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24920 vim_free(*bufp);
24921 *fnamelen = (int)STRLEN(p);
24922 *bufp = pbuf;
24923 *fnamep = p;
24924
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024925 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024926}
24927#endif /* WIN3264 */
24928
24929/*
24930 * Adjust a filename, according to a string of modifiers.
24931 * *fnamep must be NUL terminated when called. When returning, the length is
24932 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024933 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 * When there is an error, *fnamep is set to NULL.
24935 */
24936 int
24937modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24938 char_u *src; /* string with modifiers */
24939 int *usedlen; /* characters after src that are used */
24940 char_u **fnamep; /* file name so far */
24941 char_u **bufp; /* buffer for allocated file name or NULL */
24942 int *fnamelen; /* length of fnamep */
24943{
24944 int valid = 0;
24945 char_u *tail;
24946 char_u *s, *p, *pbuf;
24947 char_u dirname[MAXPATHL];
24948 int c;
24949 int has_fullname = 0;
24950#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024951 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 int has_shortname = 0;
24953#endif
24954
24955repeat:
24956 /* ":p" - full path/file_name */
24957 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24958 {
24959 has_fullname = 1;
24960
24961 valid |= VALID_PATH;
24962 *usedlen += 2;
24963
24964 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24965 if ((*fnamep)[0] == '~'
24966#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24967 && ((*fnamep)[1] == '/'
24968# ifdef BACKSLASH_IN_FILENAME
24969 || (*fnamep)[1] == '\\'
24970# endif
24971 || (*fnamep)[1] == NUL)
24972
24973#endif
24974 )
24975 {
24976 *fnamep = expand_env_save(*fnamep);
24977 vim_free(*bufp); /* free any allocated file name */
24978 *bufp = *fnamep;
24979 if (*fnamep == NULL)
24980 return -1;
24981 }
24982
24983 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024984 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 {
24986 if (vim_ispathsep(*p)
24987 && p[1] == '.'
24988 && (p[2] == NUL
24989 || vim_ispathsep(p[2])
24990 || (p[2] == '.'
24991 && (p[3] == NUL || vim_ispathsep(p[3])))))
24992 break;
24993 }
24994
24995 /* FullName_save() is slow, don't use it when not needed. */
24996 if (*p != NUL || !vim_isAbsName(*fnamep))
24997 {
24998 *fnamep = FullName_save(*fnamep, *p != NUL);
24999 vim_free(*bufp); /* free any allocated file name */
25000 *bufp = *fnamep;
25001 if (*fnamep == NULL)
25002 return -1;
25003 }
25004
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025005#ifdef WIN3264
25006# if _WIN32_WINNT >= 0x0500
25007 if (vim_strchr(*fnamep, '~') != NULL)
25008 {
25009 /* Expand 8.3 filename to full path. Needed to make sure the same
25010 * file does not have two different names.
25011 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25012 p = alloc(_MAX_PATH + 1);
25013 if (p != NULL)
25014 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025015 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025016 {
25017 vim_free(*bufp);
25018 *bufp = *fnamep = p;
25019 }
25020 else
25021 vim_free(p);
25022 }
25023 }
25024# endif
25025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026 /* Append a path separator to a directory. */
25027 if (mch_isdir(*fnamep))
25028 {
25029 /* Make room for one or two extra characters. */
25030 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25031 vim_free(*bufp); /* free any allocated file name */
25032 *bufp = *fnamep;
25033 if (*fnamep == NULL)
25034 return -1;
25035 add_pathsep(*fnamep);
25036 }
25037 }
25038
25039 /* ":." - path relative to the current directory */
25040 /* ":~" - path relative to the home directory */
25041 /* ":8" - shortname path - postponed till after */
25042 while (src[*usedlen] == ':'
25043 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25044 {
25045 *usedlen += 2;
25046 if (c == '8')
25047 {
25048#ifdef WIN3264
25049 has_shortname = 1; /* Postpone this. */
25050#endif
25051 continue;
25052 }
25053 pbuf = NULL;
25054 /* Need full path first (use expand_env() to remove a "~/") */
25055 if (!has_fullname)
25056 {
25057 if (c == '.' && **fnamep == '~')
25058 p = pbuf = expand_env_save(*fnamep);
25059 else
25060 p = pbuf = FullName_save(*fnamep, FALSE);
25061 }
25062 else
25063 p = *fnamep;
25064
25065 has_fullname = 0;
25066
25067 if (p != NULL)
25068 {
25069 if (c == '.')
25070 {
25071 mch_dirname(dirname, MAXPATHL);
25072 s = shorten_fname(p, dirname);
25073 if (s != NULL)
25074 {
25075 *fnamep = s;
25076 if (pbuf != NULL)
25077 {
25078 vim_free(*bufp); /* free any allocated file name */
25079 *bufp = pbuf;
25080 pbuf = NULL;
25081 }
25082 }
25083 }
25084 else
25085 {
25086 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25087 /* Only replace it when it starts with '~' */
25088 if (*dirname == '~')
25089 {
25090 s = vim_strsave(dirname);
25091 if (s != NULL)
25092 {
25093 *fnamep = s;
25094 vim_free(*bufp);
25095 *bufp = s;
25096 }
25097 }
25098 }
25099 vim_free(pbuf);
25100 }
25101 }
25102
25103 tail = gettail(*fnamep);
25104 *fnamelen = (int)STRLEN(*fnamep);
25105
25106 /* ":h" - head, remove "/file_name", can be repeated */
25107 /* Don't remove the first "/" or "c:\" */
25108 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25109 {
25110 valid |= VALID_HEAD;
25111 *usedlen += 2;
25112 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025113 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025114 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 *fnamelen = (int)(tail - *fnamep);
25116#ifdef VMS
25117 if (*fnamelen > 0)
25118 *fnamelen += 1; /* the path separator is part of the path */
25119#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025120 if (*fnamelen == 0)
25121 {
25122 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25123 p = vim_strsave((char_u *)".");
25124 if (p == NULL)
25125 return -1;
25126 vim_free(*bufp);
25127 *bufp = *fnamep = tail = p;
25128 *fnamelen = 1;
25129 }
25130 else
25131 {
25132 while (tail > s && !after_pathsep(s, tail))
25133 mb_ptr_back(*fnamep, tail);
25134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135 }
25136
25137 /* ":8" - shortname */
25138 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25139 {
25140 *usedlen += 2;
25141#ifdef WIN3264
25142 has_shortname = 1;
25143#endif
25144 }
25145
25146#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025147 /*
25148 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149 */
25150 if (has_shortname)
25151 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025152 /* Copy the string if it is shortened by :h and when it wasn't copied
25153 * yet, because we are going to change it in place. Avoids changing
25154 * the buffer name for "%:8". */
25155 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156 {
25157 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025158 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159 return -1;
25160 vim_free(*bufp);
25161 *bufp = *fnamep = p;
25162 }
25163
25164 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025165 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 if (!has_fullname && !vim_isAbsName(*fnamep))
25167 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025168 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169 return -1;
25170 }
25171 else
25172 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025173 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025174
Bram Moolenaardc935552011-08-17 15:23:23 +020025175 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025176 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025177 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178 return -1;
25179
25180 if (l == 0)
25181 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025182 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025184 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 return -1;
25186 }
25187 *fnamelen = l;
25188 }
25189 }
25190#endif /* WIN3264 */
25191
25192 /* ":t" - tail, just the basename */
25193 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25194 {
25195 *usedlen += 2;
25196 *fnamelen -= (int)(tail - *fnamep);
25197 *fnamep = tail;
25198 }
25199
25200 /* ":e" - extension, can be repeated */
25201 /* ":r" - root, without extension, can be repeated */
25202 while (src[*usedlen] == ':'
25203 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25204 {
25205 /* find a '.' in the tail:
25206 * - for second :e: before the current fname
25207 * - otherwise: The last '.'
25208 */
25209 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25210 s = *fnamep - 2;
25211 else
25212 s = *fnamep + *fnamelen - 1;
25213 for ( ; s > tail; --s)
25214 if (s[0] == '.')
25215 break;
25216 if (src[*usedlen + 1] == 'e') /* :e */
25217 {
25218 if (s > tail)
25219 {
25220 *fnamelen += (int)(*fnamep - (s + 1));
25221 *fnamep = s + 1;
25222#ifdef VMS
25223 /* cut version from the extension */
25224 s = *fnamep + *fnamelen - 1;
25225 for ( ; s > *fnamep; --s)
25226 if (s[0] == ';')
25227 break;
25228 if (s > *fnamep)
25229 *fnamelen = s - *fnamep;
25230#endif
25231 }
25232 else if (*fnamep <= tail)
25233 *fnamelen = 0;
25234 }
25235 else /* :r */
25236 {
25237 if (s > tail) /* remove one extension */
25238 *fnamelen = (int)(s - *fnamep);
25239 }
25240 *usedlen += 2;
25241 }
25242
25243 /* ":s?pat?foo?" - substitute */
25244 /* ":gs?pat?foo?" - global substitute */
25245 if (src[*usedlen] == ':'
25246 && (src[*usedlen + 1] == 's'
25247 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25248 {
25249 char_u *str;
25250 char_u *pat;
25251 char_u *sub;
25252 int sep;
25253 char_u *flags;
25254 int didit = FALSE;
25255
25256 flags = (char_u *)"";
25257 s = src + *usedlen + 2;
25258 if (src[*usedlen + 1] == 'g')
25259 {
25260 flags = (char_u *)"g";
25261 ++s;
25262 }
25263
25264 sep = *s++;
25265 if (sep)
25266 {
25267 /* find end of pattern */
25268 p = vim_strchr(s, sep);
25269 if (p != NULL)
25270 {
25271 pat = vim_strnsave(s, (int)(p - s));
25272 if (pat != NULL)
25273 {
25274 s = p + 1;
25275 /* find end of substitution */
25276 p = vim_strchr(s, sep);
25277 if (p != NULL)
25278 {
25279 sub = vim_strnsave(s, (int)(p - s));
25280 str = vim_strnsave(*fnamep, *fnamelen);
25281 if (sub != NULL && str != NULL)
25282 {
25283 *usedlen = (int)(p + 1 - src);
25284 s = do_string_sub(str, pat, sub, flags);
25285 if (s != NULL)
25286 {
25287 *fnamep = s;
25288 *fnamelen = (int)STRLEN(s);
25289 vim_free(*bufp);
25290 *bufp = s;
25291 didit = TRUE;
25292 }
25293 }
25294 vim_free(sub);
25295 vim_free(str);
25296 }
25297 vim_free(pat);
25298 }
25299 }
25300 /* after using ":s", repeat all the modifiers */
25301 if (didit)
25302 goto repeat;
25303 }
25304 }
25305
Bram Moolenaar26df0922014-02-23 23:39:13 +010025306 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25307 {
25308 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25309 if (p == NULL)
25310 return -1;
25311 vim_free(*bufp);
25312 *bufp = *fnamep = p;
25313 *fnamelen = (int)STRLEN(p);
25314 *usedlen += 2;
25315 }
25316
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 return valid;
25318}
25319
25320/*
25321 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25322 * "flags" can be "g" to do a global substitute.
25323 * Returns an allocated string, NULL for error.
25324 */
25325 char_u *
25326do_string_sub(str, pat, sub, flags)
25327 char_u *str;
25328 char_u *pat;
25329 char_u *sub;
25330 char_u *flags;
25331{
25332 int sublen;
25333 regmatch_T regmatch;
25334 int i;
25335 int do_all;
25336 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025337 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 garray_T ga;
25339 char_u *ret;
25340 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025341 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342
25343 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25344 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025345 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346
25347 ga_init2(&ga, 1, 200);
25348
25349 do_all = (flags[0] == 'g');
25350
25351 regmatch.rm_ic = p_ic;
25352 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25353 if (regmatch.regprog != NULL)
25354 {
25355 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025356 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25358 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025359 /* Skip empty match except for first match. */
25360 if (regmatch.startp[0] == regmatch.endp[0])
25361 {
25362 if (zero_width == regmatch.startp[0])
25363 {
25364 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025365 i = MB_PTR2LEN(tail);
25366 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25367 (size_t)i);
25368 ga.ga_len += i;
25369 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025370 continue;
25371 }
25372 zero_width = regmatch.startp[0];
25373 }
25374
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375 /*
25376 * Get some space for a temporary buffer to do the substitution
25377 * into. It will contain:
25378 * - The text up to where the match is.
25379 * - The substituted text.
25380 * - The text after the match.
25381 */
25382 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025383 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025384 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25385 {
25386 ga_clear(&ga);
25387 break;
25388 }
25389
25390 /* copy the text up to where the match is */
25391 i = (int)(regmatch.startp[0] - tail);
25392 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25393 /* add the substituted text */
25394 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25395 + ga.ga_len + i, TRUE, TRUE, FALSE);
25396 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025397 tail = regmatch.endp[0];
25398 if (*tail == NUL)
25399 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400 if (!do_all)
25401 break;
25402 }
25403
25404 if (ga.ga_data != NULL)
25405 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25406
Bram Moolenaar473de612013-06-08 18:19:48 +020025407 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408 }
25409
25410 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25411 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025412 if (p_cpo == empty_option)
25413 p_cpo = save_cpo;
25414 else
25415 /* Darn, evaluating {sub} expression changed the value. */
25416 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417
25418 return ret;
25419}
25420
25421#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */