blob: dd19492286b24fe86ce1d1d908d9de51147e24e7 [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 Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000372};
373
374/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_type vv_di.di_tv.v_type
376#define vv_nr vv_di.di_tv.vval.v_number
377#define vv_float vv_di.di_tv.vval.v_float
378#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000379#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200380#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000382
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200383static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000384#define vimvarht vimvardict.dv_hashtab
385
Bram Moolenaara40058a2005-07-11 22:42:07 +0000386static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
387static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100404static 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 +0000405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000434static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
438static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100441static 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 +0000442static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000443static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200444static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
452static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static int string2float __ARGS((char_u *text, float_T *value));
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static 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 +0200461static 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 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100470static void f_alloc_fail __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100471static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200475static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100477static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200481static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100494static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100496static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#ifdef FEAT_FLOAT
499static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
500#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000501static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000504static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000506#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000507static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000508static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
510#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#ifdef FEAT_FLOAT
514static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200515static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000516#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
520static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200530static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000531static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
533static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000537static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
544static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000548static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000557static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000559static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200563static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000566static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200567static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000575static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000576static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200577static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000578static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000579static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200582static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000583static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100589static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000592static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000606static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100611static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000613static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000625#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200626static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000627static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
628#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200629#ifdef FEAT_LUA
630static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000636static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200637static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000638static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000639static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000645#ifdef vim_mkdir
646static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100649#ifdef FEAT_MZSCHEME
650static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
651#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100654static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000655static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000656#ifdef FEAT_FLOAT
657static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
658#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000660static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000661static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200662#ifdef FEAT_PYTHON3
663static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
664#endif
665#ifdef FEAT_PYTHON
666static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
667#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000669static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000670static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000682#ifdef FEAT_FLOAT
683static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
684#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200685static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100687static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
688static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000690static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000692static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200697static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000700static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000701static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000702static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000703static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200705static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000706static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100708#ifdef FEAT_CRYPT
709static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
710#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000711static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200712static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000714#ifdef FEAT_FLOAT
715static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200716static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000717#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000719static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000720static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000723#ifdef FEAT_FLOAT
724static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000727static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200728static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729#ifdef HAVE_STRFTIME
730static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
731#endif
732static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200738static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200739static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000745static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200746static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200748static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000749static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000750static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000751static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000752static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000753static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000755static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200756#ifdef FEAT_FLOAT
757static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
759#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#ifdef FEAT_FLOAT
764static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
765#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000766static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200767static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200768static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100769static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
771static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100773static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
775static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000780static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
781static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000783static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100784static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100785static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786
Bram Moolenaar493c1782014-05-28 14:34:46 +0200787static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000788static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000789static int get_env_len __ARGS((char_u **arg));
790static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000791static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000792static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
793#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
794#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
795 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000796static 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 +0000797static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000798static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200799static 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 +0000800static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static typval_T *alloc_tv __ARGS((void));
802static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static void init_tv __ARGS((typval_T *varp));
804static long get_tv_number __ARGS((typval_T *varp));
805static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000806static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000807static char_u *get_tv_string __ARGS((typval_T *varp));
808static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000809static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100810static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
811static 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 +0000812static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
813static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
814static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000815static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
816static 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 +0000817static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200818static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
819static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200820static int var_check_func_name __ARGS((char_u *name, int new_var));
821static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200822static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000823static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
825static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
826static int eval_fname_script __ARGS((char_u *p));
827static int eval_fname_sid __ARGS((char_u *p));
828static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static ufunc_T *find_func __ARGS((char_u *name));
830static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200831static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000832#ifdef FEAT_PROFILE
833static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000834static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
835static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
836static int
837# ifdef __BORLANDC__
838 _RTLENTRYF
839# endif
840 prof_total_cmp __ARGS((const void *s1, const void *s2));
841static int
842# ifdef __BORLANDC__
843 _RTLENTRYF
844# endif
845 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200847static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000848static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000849static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000850static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000851static 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 +0000852static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
853static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000854static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000855static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
856static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000857static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000858static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000859static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200860static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200861static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000862
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200863
864#ifdef EBCDIC
865static int compare_func_name __ARGS((const void *s1, const void *s2));
866static void sortFunctions __ARGS(());
867#endif
868
Bram Moolenaar33570922005-01-25 22:26:29 +0000869/*
870 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000871 */
872 void
873eval_init()
874{
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 int i;
876 struct vimvar *p;
877
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200878 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
879 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200880 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000881 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000882 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000883
884 for (i = 0; i < VV_LEN; ++i)
885 {
886 p = &vimvars[i];
887 STRCPY(p->vv_di.di_key, p->vv_name);
888 if (p->vv_flags & VV_RO)
889 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
890 else if (p->vv_flags & VV_RO_SBX)
891 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
892 else
893 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000894
895 /* add to v: scope dict, unless the value is not always available */
896 if (p->vv_type != VAR_UNKNOWN)
897 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000898 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000899 /* add to compat scope dict */
900 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000901 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000902 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100903 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200904 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100905 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200906 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200907
908#ifdef EBCDIC
909 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100910 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200911 */
912 sortFunctions();
913#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000914}
915
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000916#if defined(EXITFREE) || defined(PROTO)
917 void
918eval_clear()
919{
920 int i;
921 struct vimvar *p;
922
923 for (i = 0; i < VV_LEN; ++i)
924 {
925 p = &vimvars[i];
926 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000927 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000928 vim_free(p->vv_str);
929 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000930 }
931 else if (p->vv_di.di_tv.v_type == VAR_LIST)
932 {
933 list_unref(p->vv_list);
934 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000935 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000936 }
937 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000938 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939 hash_clear(&compat_hashtab);
940
Bram Moolenaard9fba312005-06-26 22:34:35 +0000941 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100942# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200943 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100944# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945
946 /* global variables */
947 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000948
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000949 /* autoloaded script names */
950 ga_clear_strings(&ga_loaded);
951
Bram Moolenaarcca74132013-09-25 21:00:28 +0200952 /* Script-local variables. First clear all the variables and in a second
953 * loop free the scriptvar_T, because a variable in one script might hold
954 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200955 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200956 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200957 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200958 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200959 ga_clear(&ga_scripts);
960
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000961 /* unreferenced lists and dicts */
962 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000963
964 /* functions */
965 free_all_functions();
966 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000967}
968#endif
969
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 * Return the name of the executed function.
972 */
973 char_u *
974func_name(cookie)
975 void *cookie;
976{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000977 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979
980/*
981 * Return the address holding the next breakpoint line for a funccall cookie.
982 */
983 linenr_T *
984func_breakpoint(cookie)
985 void *cookie;
986{
Bram Moolenaar33570922005-01-25 22:26:29 +0000987 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988}
989
990/*
991 * Return the address holding the debug tick for a funccall cookie.
992 */
993 int *
994func_dbg_tick(cookie)
995 void *cookie;
996{
Bram Moolenaar33570922005-01-25 22:26:29 +0000997 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998}
999
1000/*
1001 * Return the nesting level for a funccall cookie.
1002 */
1003 int
1004func_level(cookie)
1005 void *cookie;
1006{
Bram Moolenaar33570922005-01-25 22:26:29 +00001007 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008}
1009
1010/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001011funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001013/* pointer to list of previously used funccal, still around because some
1014 * item in it is still being used. */
1015funccall_T *previous_funccal = NULL;
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017/*
1018 * Return TRUE when a function was ended by a ":return" command.
1019 */
1020 int
1021current_func_returned()
1022{
1023 return current_funccal->returned;
1024}
1025
1026
1027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 * Set an internal variable to a string value. Creates the variable if it does
1029 * not already exist.
1030 */
1031 void
1032set_internal_string_var(name, value)
1033 char_u *name;
1034 char_u *value;
1035{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001036 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001037 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
1039 val = vim_strsave(value);
1040 if (val != NULL)
1041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001042 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001043 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001045 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001046 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048 }
1049}
1050
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001052static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053static char_u *redir_endp = NULL;
1054static char_u *redir_varname = NULL;
1055
1056/*
1057 * Start recording command output to a variable
1058 * Returns OK if successfully completed the setup. FAIL otherwise.
1059 */
1060 int
1061var_redir_start(name, append)
1062 char_u *name;
1063 int append; /* append to an existing variable */
1064{
1065 int save_emsg;
1066 int err;
1067 typval_T tv;
1068
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001069 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001070 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 {
1072 EMSG(_(e_invarg));
1073 return FAIL;
1074 }
1075
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001076 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 redir_varname = vim_strsave(name);
1078 if (redir_varname == NULL)
1079 return FAIL;
1080
1081 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1082 if (redir_lval == NULL)
1083 {
1084 var_redir_stop();
1085 return FAIL;
1086 }
1087
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001088 /* The output is stored in growarray "redir_ga" until redirection ends. */
1089 ga_init2(&redir_ga, (int)sizeof(char), 500);
1090
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001092 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001093 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1095 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001096 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 if (redir_endp != NULL && *redir_endp != NUL)
1098 /* Trailing characters are present after the variable name */
1099 EMSG(_(e_trailing));
1100 else
1101 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001102 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 var_redir_stop();
1104 return FAIL;
1105 }
1106
1107 /* check if we can write to the variable: set it to or append an empty
1108 * string */
1109 save_emsg = did_emsg;
1110 did_emsg = FALSE;
1111 tv.v_type = VAR_STRING;
1112 tv.vval.v_string = (char_u *)"";
1113 if (append)
1114 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1115 else
1116 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001117 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001119 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 if (err)
1121 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001122 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
1124 return FAIL;
1125 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126
1127 return OK;
1128}
1129
1130/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131 * Append "value[value_len]" to the variable set by var_redir_start().
1132 * The actual appending is postponed until redirection ends, because the value
1133 * appended may in fact be the string we write to, changing it may cause freed
1134 * memory to be used:
1135 * :redir => foo
1136 * :let foo
1137 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 */
1139 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001144 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145
1146 if (redir_lval == NULL)
1147 return;
1148
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001150 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001152 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001154 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 {
1156 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001157 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158 }
1159 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161}
1162
1163/*
1164 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 */
1167 void
1168var_redir_stop()
1169{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170 typval_T tv;
1171
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 if (redir_lval != NULL)
1173 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* If there was no error: assign the text to the variable. */
1175 if (redir_endp != NULL)
1176 {
1177 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1178 tv.v_type = VAR_STRING;
1179 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001180 /* Call get_lval() again, if it's inside a Dict or List it may
1181 * have changed. */
1182 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001183 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001184 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1185 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1186 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001187 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001188
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001189 /* free the collected output */
1190 vim_free(redir_ga.ga_data);
1191 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001192
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193 vim_free(redir_lval);
1194 redir_lval = NULL;
1195 }
1196 vim_free(redir_varname);
1197 redir_varname = NULL;
1198}
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200# if defined(FEAT_MBYTE) || defined(PROTO)
1201 int
1202eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1203 char_u *enc_from;
1204 char_u *enc_to;
1205 char_u *fname_from;
1206 char_u *fname_to;
1207{
1208 int err = FALSE;
1209
1210 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1211 set_vim_var_string(VV_CC_TO, enc_to, -1);
1212 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1213 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1214 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1215 err = TRUE;
1216 set_vim_var_string(VV_CC_FROM, NULL, -1);
1217 set_vim_var_string(VV_CC_TO, NULL, -1);
1218 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1219 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1220
1221 if (err)
1222 return FAIL;
1223 return OK;
1224}
1225# endif
1226
1227# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1228 int
1229eval_printexpr(fname, args)
1230 char_u *fname;
1231 char_u *args;
1232{
1233 int err = FALSE;
1234
1235 set_vim_var_string(VV_FNAME_IN, fname, -1);
1236 set_vim_var_string(VV_CMDARG, args, -1);
1237 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1238 err = TRUE;
1239 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1240 set_vim_var_string(VV_CMDARG, NULL, -1);
1241
1242 if (err)
1243 {
1244 mch_remove(fname);
1245 return FAIL;
1246 }
1247 return OK;
1248}
1249# endif
1250
1251# if defined(FEAT_DIFF) || defined(PROTO)
1252 void
1253eval_diff(origfile, newfile, outfile)
1254 char_u *origfile;
1255 char_u *newfile;
1256 char_u *outfile;
1257{
1258 int err = FALSE;
1259
1260 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1261 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1262 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1263 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1264 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1265 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1266 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1267}
1268
1269 void
1270eval_patch(origfile, difffile, outfile)
1271 char_u *origfile;
1272 char_u *difffile;
1273 char_u *outfile;
1274{
1275 int err;
1276
1277 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1278 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1279 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1280 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1281 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1282 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1283 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1284}
1285# endif
1286
1287/*
1288 * Top level evaluation function, returning a boolean.
1289 * Sets "error" to TRUE if there was an error.
1290 * Return TRUE or FALSE.
1291 */
1292 int
1293eval_to_bool(arg, error, nextcmd, skip)
1294 char_u *arg;
1295 int *error;
1296 char_u **nextcmd;
1297 int skip; /* only parse, don't execute */
1298{
Bram Moolenaar33570922005-01-25 22:26:29 +00001299 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 int retval = FALSE;
1301
1302 if (skip)
1303 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 else
1307 {
1308 *error = FALSE;
1309 if (!skip)
1310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001311 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001312 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 }
1315 if (skip)
1316 --emsg_skip;
1317
1318 return retval;
1319}
1320
1321/*
1322 * Top level evaluation function, returning a string. If "skip" is TRUE,
1323 * only parsing to "nextcmd" is done, without reporting errors. Return
1324 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1325 */
1326 char_u *
1327eval_to_string_skip(arg, nextcmd, skip)
1328 char_u *arg;
1329 char_u **nextcmd;
1330 int skip; /* only parse, don't execute */
1331{
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 char_u *retval;
1334
1335 if (skip)
1336 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 retval = NULL;
1339 else
1340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 retval = vim_strsave(get_tv_string(&tv));
1342 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344 if (skip)
1345 --emsg_skip;
1346
1347 return retval;
1348}
1349
1350/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001351 * Skip over an expression at "*pp".
1352 * Return FAIL for an error, OK otherwise.
1353 */
1354 int
1355skip_expr(pp)
1356 char_u **pp;
1357{
Bram Moolenaar33570922005-01-25 22:26:29 +00001358 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001359
1360 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001362}
1363
1364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001366 * When "convert" is TRUE convert a List into a sequence of lines and convert
1367 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 * Return pointer to allocated memory, or NULL for failure.
1369 */
1370 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char_u *arg;
1373 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001374 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
Bram Moolenaar33570922005-01-25 22:26:29 +00001376 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001379#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001380 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 retval = NULL;
1385 else
1386 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001387 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001388 {
1389 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001390 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001391 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001392 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001393 if (tv.vval.v_list->lv_len > 0)
1394 ga_append(&ga, NL);
1395 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 ga_append(&ga, NUL);
1397 retval = (char_u *)ga.ga_data;
1398 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001399#ifdef FEAT_FLOAT
1400 else if (convert && tv.v_type == VAR_FLOAT)
1401 {
1402 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1403 retval = vim_strsave(numbuf);
1404 }
1405#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001406 else
1407 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
1410
1411 return retval;
1412}
1413
1414/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001415 * Call eval_to_string() without using current local variables and using
1416 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 */
1418 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001419eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 char_u *arg;
1421 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001422 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 char_u *retval;
1425 void *save_funccalp;
1426
1427 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001428 if (use_sandbox)
1429 ++sandbox;
1430 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001431 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001432 if (use_sandbox)
1433 --sandbox;
1434 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 restore_funccal(save_funccalp);
1436 return retval;
1437}
1438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439/*
1440 * Top level evaluation function, returning a number.
1441 * Evaluates "expr" silently.
1442 * Returns -1 for an error.
1443 */
1444 int
1445eval_to_number(expr)
1446 char_u *expr;
1447{
Bram Moolenaar33570922005-01-25 22:26:29 +00001448 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001450 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
1452 ++emsg_off;
1453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001454 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 retval = -1;
1456 else
1457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001458 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001459 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461 --emsg_off;
1462
1463 return retval;
1464}
1465
Bram Moolenaara40058a2005-07-11 22:42:07 +00001466/*
1467 * Prepare v: variable "idx" to be used.
1468 * Save the current typeval in "save_tv".
1469 * When not used yet add the variable to the v: hashtable.
1470 */
1471 static void
1472prepare_vimvar(idx, save_tv)
1473 int idx;
1474 typval_T *save_tv;
1475{
1476 *save_tv = vimvars[idx].vv_tv;
1477 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1478 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1479}
1480
1481/*
1482 * Restore v: variable "idx" to typeval "save_tv".
1483 * When no longer defined, remove the variable from the v: hashtable.
1484 */
1485 static void
1486restore_vimvar(idx, save_tv)
1487 int idx;
1488 typval_T *save_tv;
1489{
1490 hashitem_T *hi;
1491
Bram Moolenaara40058a2005-07-11 22:42:07 +00001492 vimvars[idx].vv_tv = *save_tv;
1493 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1494 {
1495 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1496 if (HASHITEM_EMPTY(hi))
1497 EMSG2(_(e_intern2), "restore_vimvar()");
1498 else
1499 hash_remove(&vimvarht, hi);
1500 }
1501}
1502
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001503#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001504/*
1505 * Evaluate an expression to a list with suggestions.
1506 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001507 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508 */
1509 list_T *
1510eval_spell_expr(badword, expr)
1511 char_u *badword;
1512 char_u *expr;
1513{
1514 typval_T save_val;
1515 typval_T rettv;
1516 list_T *list = NULL;
1517 char_u *p = skipwhite(expr);
1518
1519 /* Set "v:val" to the bad word. */
1520 prepare_vimvar(VV_VAL, &save_val);
1521 vimvars[VV_VAL].vv_type = VAR_STRING;
1522 vimvars[VV_VAL].vv_str = badword;
1523 if (p_verbose == 0)
1524 ++emsg_off;
1525
1526 if (eval1(&p, &rettv, TRUE) == OK)
1527 {
1528 if (rettv.v_type != VAR_LIST)
1529 clear_tv(&rettv);
1530 else
1531 list = rettv.vval.v_list;
1532 }
1533
1534 if (p_verbose == 0)
1535 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001536 restore_vimvar(VV_VAL, &save_val);
1537
1538 return list;
1539}
1540
1541/*
1542 * "list" is supposed to contain two items: a word and a number. Return the
1543 * word in "pp" and the number as the return value.
1544 * Return -1 if anything isn't right.
1545 * Used to get the good word and score from the eval_spell_expr() result.
1546 */
1547 int
1548get_spellword(list, pp)
1549 list_T *list;
1550 char_u **pp;
1551{
1552 listitem_T *li;
1553
1554 li = list->lv_first;
1555 if (li == NULL)
1556 return -1;
1557 *pp = get_tv_string(&li->li_tv);
1558
1559 li = li->li_next;
1560 if (li == NULL)
1561 return -1;
1562 return get_tv_number(&li->li_tv);
1563}
1564#endif
1565
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001566/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 * Top level evaluation function.
1568 * Returns an allocated typval_T with the result.
1569 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001570 */
1571 typval_T *
1572eval_expr(arg, nextcmd)
1573 char_u *arg;
1574 char_u **nextcmd;
1575{
1576 typval_T *tv;
1577
1578 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001579 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001580 {
1581 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001582 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001583 }
1584
1585 return tv;
1586}
1587
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 * Uses argv[argc] for the function arguments. Only Number and String
1592 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001595 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001596call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 char_u *func;
1598 int argc;
1599 char_u **argv;
1600 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001601 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 long n;
1606 int len;
1607 int i;
1608 int doesrange;
1609 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001612 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 for (i = 0; i < argc; i++)
1617 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001618 /* Pass a NULL or empty argument as an empty string */
1619 if (argv[i] == NULL || *argv[i] == NUL)
1620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001621 argvars[i].v_type = VAR_STRING;
1622 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001623 continue;
1624 }
1625
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001626 if (str_arg_only)
1627 len = 0;
1628 else
1629 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001630 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (len != 0 && len == (int)STRLEN(argv[i]))
1632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001633 argvars[i].v_type = VAR_NUMBER;
1634 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 else
1637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001638 argvars[i].v_type = VAR_STRING;
1639 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 }
1641 }
1642
1643 if (safe)
1644 {
1645 save_funccalp = save_funccal();
1646 ++sandbox;
1647 }
1648
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1650 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (safe)
1654 {
1655 --sandbox;
1656 restore_funccal(save_funccalp);
1657 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 vim_free(argvars);
1659
1660 if (ret == FAIL)
1661 clear_tv(rettv);
1662
1663 return ret;
1664}
1665
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001666/*
1667 * Call vimL function "func" and return the result as a number.
1668 * Returns -1 when calling the function fails.
1669 * Uses argv[argc] for the function arguments.
1670 */
1671 long
1672call_func_retnr(func, argc, argv, safe)
1673 char_u *func;
1674 int argc;
1675 char_u **argv;
1676 int safe; /* use the sandbox */
1677{
1678 typval_T rettv;
1679 long retval;
1680
1681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1683 return -1;
1684
1685 retval = get_tv_number_chk(&rettv, NULL);
1686 clear_tv(&rettv);
1687 return retval;
1688}
1689
1690#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1691 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1692
Bram Moolenaar4f688582007-07-24 12:34:30 +00001693# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001695 * Call vimL function "func" and return the result as a string.
1696 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 * Uses argv[argc] for the function arguments.
1698 */
1699 void *
1700call_func_retstr(func, argc, argv, safe)
1701 char_u *func;
1702 int argc;
1703 char_u **argv;
1704 int safe; /* use the sandbox */
1705{
1706 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001707 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 return NULL;
1712
1713 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return retval;
1716}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001719/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001720 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001722 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 */
1724 void *
1725call_func_retlist(func, argc, argv, safe)
1726 char_u *func;
1727 int argc;
1728 char_u **argv;
1729 int safe; /* use the sandbox */
1730{
1731 typval_T rettv;
1732
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001733 /* All arguments are passed as strings, no conversion to number. */
1734 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 return NULL;
1736
1737 if (rettv.v_type != VAR_LIST)
1738 {
1739 clear_tv(&rettv);
1740 return NULL;
1741 }
1742
1743 return rettv.vval.v_list;
1744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
1746
1747/*
1748 * Save the current function call pointer, and set it to NULL.
1749 * Used when executing autocommands and for ":source".
1750 */
1751 void *
1752save_funccal()
1753{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 current_funccal = NULL;
1757 return (void *)fc;
1758}
1759
1760 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001761restore_funccal(vfc)
1762 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001764 funccall_T *fc = (funccall_T *)vfc;
1765
1766 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767}
1768
Bram Moolenaar05159a02005-02-26 23:04:13 +00001769#if defined(FEAT_PROFILE) || defined(PROTO)
1770/*
1771 * Prepare profiling for entering a child or something else that is not
1772 * counted for the script/function itself.
1773 * Should always be called in pair with prof_child_exit().
1774 */
1775 void
1776prof_child_enter(tm)
1777 proftime_T *tm; /* place to store waittime */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 profile_start(&fc->prof_child);
1783 script_prof_save(tm);
1784}
1785
1786/*
1787 * Take care of time spent in a child.
1788 * Should always be called after prof_child_enter().
1789 */
1790 void
1791prof_child_exit(tm)
1792 proftime_T *tm; /* where waittime was stored */
1793{
1794 funccall_T *fc = current_funccal;
1795
1796 if (fc != NULL && fc->func->uf_profiling)
1797 {
1798 profile_end(&fc->prof_child);
1799 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1800 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1801 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1802 }
1803 script_prof_restore(tm);
1804}
1805#endif
1806
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808#ifdef FEAT_FOLDING
1809/*
1810 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1811 * it in "*cp". Doesn't give error messages.
1812 */
1813 int
1814eval_foldexpr(arg, cp)
1815 char_u *arg;
1816 int *cp;
1817{
Bram Moolenaar33570922005-01-25 22:26:29 +00001818 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 int retval;
1820 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001821 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1822 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001825 if (use_sandbox)
1826 ++sandbox;
1827 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 retval = 0;
1831 else
1832 {
1833 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 if (tv.v_type == VAR_NUMBER)
1835 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001836 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 retval = 0;
1838 else
1839 {
1840 /* If the result is a string, check if there is a non-digit before
1841 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (!VIM_ISDIGIT(*s) && *s != '-')
1844 *cp = *s++;
1845 retval = atol((char *)s);
1846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001850 if (use_sandbox)
1851 --sandbox;
1852 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 return retval;
1855}
1856#endif
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 * ":let" list all variable values
1860 * ":let var1 var2" list variable values
1861 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 * ":let var += expr" assignment command.
1863 * ":let var -= expr" assignment command.
1864 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 */
1867 void
1868ex_let(eap)
1869 exarg_T *eap;
1870{
1871 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001873 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 int var_count = 0;
1876 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001878 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001879 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
Bram Moolenaardb552d602006-03-23 22:59:57 +00001881 argend = skip_var_list(arg, &var_count, &semicolon);
1882 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001884 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1885 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001886 expr = skipwhite(argend);
1887 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1888 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001890 /*
1891 * ":let" without "=": list variables
1892 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 if (*arg == '[')
1894 EMSG(_(e_invarg));
1895 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 list_glob_vars(&first);
1902 list_buf_vars(&first);
1903 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001904#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001906#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001907 list_script_vars(&first);
1908 list_func_vars(&first);
1909 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 eap->nextcmd = check_nextcmd(arg);
1912 }
1913 else
1914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 op[0] = '=';
1916 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001917 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001919 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1920 op[0] = *expr; /* +=, -= or .= */
1921 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001923 else
1924 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (eap->skip)
1927 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (eap->skip)
1930 {
1931 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 --emsg_skip;
1934 }
1935 else if (i != FAIL)
1936 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
1941 }
1942}
1943
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944/*
1945 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1946 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 * When "nextchars" is not NULL it points to a string with characters that
1948 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1949 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 * Returns OK or FAIL;
1951 */
1952 static int
1953ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1954 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 int copy; /* copy values from "tv", don't move */
1957 int semicolon; /* from skip_var_list() */
1958 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960{
1961 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001962 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001964 listitem_T *item;
1965 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966
1967 if (*arg != '[')
1968 {
1969 /*
1970 * ":let var = expr" or ":for var in list"
1971 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001972 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return FAIL;
1974 return OK;
1975 }
1976
1977 /*
1978 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1979 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001980 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 {
1982 EMSG(_(e_listreq));
1983 return FAIL;
1984 }
1985
1986 i = list_len(l);
1987 if (semicolon == 0 && var_count < i)
1988 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001989 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 return FAIL;
1991 }
1992 if (var_count - semicolon > i)
1993 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001994 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 return FAIL;
1996 }
1997
1998 item = l->lv_first;
1999 while (*arg != ']')
2000 {
2001 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002002 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 item = item->li_next;
2004 if (arg == NULL)
2005 return FAIL;
2006
2007 arg = skipwhite(arg);
2008 if (*arg == ';')
2009 {
2010 /* Put the rest of the list (may be empty) in the var after ';'.
2011 * Create a new list for this. */
2012 l = list_alloc();
2013 if (l == NULL)
2014 return FAIL;
2015 while (item != NULL)
2016 {
2017 list_append_tv(l, &item->li_tv);
2018 item = item->li_next;
2019 }
2020
2021 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002022 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 ltv.vval.v_list = l;
2024 l->lv_refcount = 1;
2025
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002026 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2027 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 clear_tv(&ltv);
2029 if (arg == NULL)
2030 return FAIL;
2031 break;
2032 }
2033 else if (*arg != ',' && *arg != ']')
2034 {
2035 EMSG2(_(e_intern2), "ex_let_vars()");
2036 return FAIL;
2037 }
2038 }
2039
2040 return OK;
2041}
2042
2043/*
2044 * Skip over assignable variable "var" or list of variables "[var, var]".
2045 * Used for ":let varvar = expr" and ":for varvar in expr".
2046 * For "[var, var]" increment "*var_count" for each variable.
2047 * for "[var, var; var]" set "semicolon".
2048 * Return NULL for an error.
2049 */
2050 static char_u *
2051skip_var_list(arg, var_count, semicolon)
2052 char_u *arg;
2053 int *var_count;
2054 int *semicolon;
2055{
2056 char_u *p, *s;
2057
2058 if (*arg == '[')
2059 {
2060 /* "[var, var]": find the matching ']'. */
2061 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002062 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 {
2064 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2065 s = skip_var_one(p);
2066 if (s == p)
2067 {
2068 EMSG2(_(e_invarg2), p);
2069 return NULL;
2070 }
2071 ++*var_count;
2072
2073 p = skipwhite(s);
2074 if (*p == ']')
2075 break;
2076 else if (*p == ';')
2077 {
2078 if (*semicolon == 1)
2079 {
2080 EMSG(_("Double ; in list of variables"));
2081 return NULL;
2082 }
2083 *semicolon = 1;
2084 }
2085 else if (*p != ',')
2086 {
2087 EMSG2(_(e_invarg2), p);
2088 return NULL;
2089 }
2090 }
2091 return p + 1;
2092 }
2093 else
2094 return skip_var_one(arg);
2095}
2096
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002097/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002098 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002099 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002101 static char_u *
2102skip_var_one(arg)
2103 char_u *arg;
2104{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002105 if (*arg == '@' && arg[1] != NUL)
2106 return arg + 2;
2107 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2108 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002109}
2110
Bram Moolenaara7043832005-01-21 11:56:39 +00002111/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002112 * List variables for hashtab "ht" with prefix "prefix".
2113 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002114 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002119 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar33570922005-01-25 22:26:29 +00002122 hashitem_T *hi;
2123 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124 int todo;
2125
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002126 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002127 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2128 {
2129 if (!HASHITEM_EMPTY(hi))
2130 {
2131 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002132 di = HI2DI(hi);
2133 if (empty || di->di_tv.v_type != VAR_STRING
2134 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 }
2137 }
2138}
2139
2140/*
2141 * List global variables.
2142 */
2143 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144list_glob_vars(first)
2145 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002148}
2149
2150/*
2151 * List buffer variables.
2152 */
2153 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154list_buf_vars(first)
2155 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002156{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002157 char_u numbuf[NUMBUFLEN];
2158
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002161
2162 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2164 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002165}
2166
2167/*
2168 * List window variables.
2169 */
2170 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171list_win_vars(first)
2172 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002173{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002174 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002176}
2177
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002178#ifdef FEAT_WINDOWS
2179/*
2180 * List tab page variables.
2181 */
2182 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183list_tab_vars(first)
2184 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002185{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002186 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002188}
2189#endif
2190
Bram Moolenaara7043832005-01-21 11:56:39 +00002191/*
2192 * List Vim variables.
2193 */
2194 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195list_vim_vars(first)
2196 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199}
2200
2201/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002202 * List script-local variables, if there is a script.
2203 */
2204 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205list_script_vars(first)
2206 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002207{
2208 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2210 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002211}
2212
2213/*
2214 * List function variables, if there is a function.
2215 */
2216 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217list_func_vars(first)
2218 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002219{
2220 if (current_funccal != NULL)
2221 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223}
2224
2225/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 * List variables in "arg".
2227 */
2228 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 exarg_T *eap;
2231 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233{
2234 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 char_u *name_start;
2238 char_u *arg_subsc;
2239 char_u *tofree;
2240 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241
2242 while (!ends_excmd(*arg) && !got_int)
2243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002246 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2248 {
2249 emsg_severe = TRUE;
2250 EMSG(_(e_trailing));
2251 break;
2252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 /* get_name_len() takes care of expanding curly braces */
2257 name_start = name = arg;
2258 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2259 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 /* This is mainly to keep test 49 working: when expanding
2262 * curly braces fails overrule the exception error message. */
2263 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 emsg_severe = TRUE;
2266 EMSG2(_(e_invarg2), arg);
2267 break;
2268 }
2269 error = TRUE;
2270 }
2271 else
2272 {
2273 if (tofree != NULL)
2274 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002275 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 else
2278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 /* handle d.key, l[idx], f(expr) */
2280 arg_subsc = arg;
2281 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002282 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 case 'g': list_glob_vars(first); break;
2290 case 'b': list_buf_vars(first); break;
2291 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002292#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002293 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002294#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002295 case 'v': list_vim_vars(first); break;
2296 case 's': list_script_vars(first); break;
2297 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 default:
2299 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 }
2302 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 {
2304 char_u numbuf[NUMBUFLEN];
2305 char_u *tf;
2306 int c;
2307 char_u *s;
2308
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002309 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 c = *arg;
2311 *arg = NUL;
2312 list_one_var_a((char_u *)"",
2313 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002314 tv.v_type,
2315 s == NULL ? (char_u *)"" : s,
2316 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 *arg = c;
2318 vim_free(tf);
2319 }
2320 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 }
2323 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002324
2325 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002327
2328 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
2330
2331 return arg;
2332}
2333
2334/*
2335 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2336 * Returns a pointer to the char just after the var name.
2337 * Returns NULL if there is an error.
2338 */
2339 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002340ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002342 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002344 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002345 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346{
2347 int c1;
2348 char_u *name;
2349 char_u *p;
2350 char_u *arg_end = NULL;
2351 int len;
2352 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354
2355 /*
2356 * ":let $VAR = expr": Set environment variable.
2357 */
2358 if (*arg == '$')
2359 {
2360 /* Find the end of the name. */
2361 ++arg;
2362 name = arg;
2363 len = get_env_len(&arg);
2364 if (len == 0)
2365 EMSG2(_(e_invarg2), name - 1);
2366 else
2367 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 if (op != NULL && (*op == '+' || *op == '-'))
2369 EMSG2(_(e_letwrong), op);
2370 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002371 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002373 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 {
2375 c1 = name[len];
2376 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002377 p = get_tv_string_chk(tv);
2378 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 {
2380 int mustfree = FALSE;
2381 char_u *s = vim_getenv(name, &mustfree);
2382
2383 if (s != NULL)
2384 {
2385 p = tofree = concat_str(s, p);
2386 if (mustfree)
2387 vim_free(s);
2388 }
2389 }
2390 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002391 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 if (STRICMP(name, "HOME") == 0)
2394 init_homedir();
2395 else if (didset_vim && STRICMP(name, "VIM") == 0)
2396 didset_vim = FALSE;
2397 else if (didset_vimruntime
2398 && STRICMP(name, "VIMRUNTIME") == 0)
2399 didset_vimruntime = FALSE;
2400 arg_end = arg;
2401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 }
2405 }
2406 }
2407
2408 /*
2409 * ":let &option = expr": Set option value.
2410 * ":let &l:option = expr": Set local option value.
2411 * ":let &g:option = expr": Set global option value.
2412 */
2413 else if (*arg == '&')
2414 {
2415 /* Find the end of the name. */
2416 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002417 if (p == NULL || (endchars != NULL
2418 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 EMSG(_(e_letunexp));
2420 else
2421 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 long n;
2423 int opt_type;
2424 long numval;
2425 char_u *stringval = NULL;
2426 char_u *s;
2427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 c1 = *p;
2429 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430
2431 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 s = get_tv_string_chk(tv); /* != NULL if number or string */
2433 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 {
2435 opt_type = get_option_value(arg, &numval,
2436 &stringval, opt_flags);
2437 if ((opt_type == 1 && *op == '.')
2438 || (opt_type == 0 && *op != '.'))
2439 EMSG2(_(e_letwrong), op);
2440 else
2441 {
2442 if (opt_type == 1) /* number */
2443 {
2444 if (*op == '+')
2445 n = numval + n;
2446 else
2447 n = numval - n;
2448 }
2449 else if (opt_type == 0 && stringval != NULL) /* string */
2450 {
2451 s = concat_str(stringval, s);
2452 vim_free(stringval);
2453 stringval = s;
2454 }
2455 }
2456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 if (s != NULL)
2458 {
2459 set_option_value(arg, n, s, opt_flags);
2460 arg_end = p;
2461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465 }
2466
2467 /*
2468 * ":let @r = expr": Set register contents.
2469 */
2470 else if (*arg == '@')
2471 {
2472 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 if (op != NULL && (*op == '+' || *op == '-'))
2474 EMSG2(_(e_letwrong), op);
2475 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002476 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 EMSG(_(e_letunexp));
2478 else
2479 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002480 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 char_u *s;
2482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002483 p = get_tv_string_chk(tv);
2484 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002486 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002487 if (s != NULL)
2488 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 vim_free(s);
2491 }
2492 }
2493 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 arg_end = arg + 1;
2497 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002499 }
2500 }
2501
2502 /*
2503 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002506 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002510 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2514 EMSG(_(e_letunexp));
2515 else
2516 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 arg_end = p;
2519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 }
2523
2524 else
2525 EMSG2(_(e_invarg2), arg);
2526
2527 return arg_end;
2528}
2529
2530/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002531 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2532 */
2533 static int
2534check_changedtick(arg)
2535 char_u *arg;
2536{
2537 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2538 {
2539 EMSG2(_(e_readonlyvar), arg);
2540 return TRUE;
2541 }
2542 return FALSE;
2543}
2544
2545/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Get an lval: variable, Dict item or List item that can be assigned a value
2547 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2548 * "name.key", "name.key[expr]" etc.
2549 * Indexing only works if "name" is an existing List or Dictionary.
2550 * "name" points to the start of the name.
2551 * If "rettv" is not NULL it points to the value to be assigned.
2552 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2553 * wrong; must end in space or cmd separator.
2554 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 * flags:
2556 * GLV_QUIET: do not give error messages
2557 * GLV_NO_AUTOLOAD: do not use script autoloading
2558 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002560 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 */
2563 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002564get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 typval_T *rettv;
2567 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 int unlet;
2569 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002570 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002571 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 char_u *expr_start, *expr_end;
2575 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002576 dictitem_T *v;
2577 typval_T var1;
2578 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002583 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002584 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588
2589 if (skip)
2590 {
2591 /* When skipping just find the end of the name. */
2592 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002593 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 }
2595
2596 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002597 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 if (expr_start != NULL)
2599 {
2600 /* Don't expand the name when we already know there is an error. */
2601 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2602 && *p != '[' && *p != '.')
2603 {
2604 EMSG(_(e_trailing));
2605 return NULL;
2606 }
2607
2608 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2609 if (lp->ll_exp_name == NULL)
2610 {
2611 /* Report an invalid expression in braces, unless the
2612 * expression evaluation has been cancelled due to an
2613 * aborting error, an interrupt, or an exception. */
2614 if (!aborting() && !quiet)
2615 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002616 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 EMSG2(_(e_invarg2), name);
2618 return NULL;
2619 }
2620 }
2621 lp->ll_name = lp->ll_exp_name;
2622 }
2623 else
2624 lp->ll_name = name;
2625
2626 /* Without [idx] or .key we are done. */
2627 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2628 return p;
2629
2630 cc = *p;
2631 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002632 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (v == NULL && !quiet)
2634 EMSG2(_(e_undefvar), lp->ll_name);
2635 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 if (v == NULL)
2637 return NULL;
2638
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 /*
2640 * Loop until no more [idx] or .key is following.
2641 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2646 && !(lp->ll_tv->v_type == VAR_DICT
2647 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
2650 EMSG(_("E689: Can only index a List or Dictionary"));
2651 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_("E708: [:] must come last"));
2657 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 len = -1;
2661 if (*p == '.')
2662 {
2663 key = p + 1;
2664 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2665 ;
2666 if (len == 0)
2667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
2669 EMSG(_(e_emptykey));
2670 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 }
2672 p = key + len;
2673 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674 else
2675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (*p == ':')
2679 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 else
2681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 empty1 = FALSE;
2683 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002685 if (get_tv_string_chk(&var1) == NULL)
2686 {
2687 /* not a number or string */
2688 clear_tv(&var1);
2689 return NULL;
2690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 }
2692
2693 /* Optionally get the second index [ :expr]. */
2694 if (*p == ':')
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002699 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002703 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (rettv != NULL && (rettv->v_type != VAR_LIST
2705 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
2708 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 }
2713 p = skipwhite(p + 1);
2714 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 else
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2720 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002725 if (get_tv_string_chk(&var2) == NULL)
2726 {
2727 /* not a number or string */
2728 if (!empty1)
2729 clear_tv(&var1);
2730 clear_tv(&var2);
2731 return NULL;
2732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002738
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (!quiet)
2742 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (!empty1)
2744 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 }
2749
2750 /* Skip to past ']'. */
2751 ++p;
2752 }
2753
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
2756 if (len == -1)
2757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002759 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 if (*key == NUL)
2761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (!quiet)
2763 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 }
2767 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002768 lp->ll_list = NULL;
2769 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002770 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002771
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 /* When assigning to a scope dictionary check that a function and
2773 * variable name is valid (only variable name unless it is l: or
2774 * g: dictionary). Disallow overwriting a builtin function. */
2775 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002776 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002777 int prevval;
2778 int wrong;
2779
2780 if (len != -1)
2781 {
2782 prevval = key[len];
2783 key[len] = NUL;
2784 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002785 else
2786 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2788 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002789 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002790 || !valid_varname(key);
2791 if (len != -1)
2792 key[len] = prevval;
2793 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 return NULL;
2795 }
2796
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002799 /* Can't add "v:" variable. */
2800 if (lp->ll_dict == &vimvardict)
2801 {
2802 EMSG2(_(e_illvar), name);
2803 return NULL;
2804 }
2805
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002806 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002810 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 if (len == -1)
2812 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 }
2815 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 if (len == -1)
2820 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 p = NULL;
2823 break;
2824 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002825 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002826 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002827 return NULL;
2828
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 if (len == -1)
2830 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833 else
2834 {
2835 /*
2836 * Get the number and item for the only or first index of the List.
2837 */
2838 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 else
2841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var1);
2844 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 lp->ll_list = lp->ll_tv->vval.v_list;
2847 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2848 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002850 if (lp->ll_n1 < 0)
2851 {
2852 lp->ll_n1 = 0;
2853 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2854 }
2855 }
2856 if (lp->ll_li == NULL)
2857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002860 if (!quiet)
2861 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864
2865 /*
2866 * May need to find the item or absolute index for the second
2867 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 * When no index given: "lp->ll_empty2" is TRUE.
2869 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002873 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 {
2880 if (!quiet)
2881 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002883 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2888 if (lp->ll_n1 < 0)
2889 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2890 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002891 {
2892 if (!quiet)
2893 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002896 }
2897
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002900 }
2901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 return p;
2903}
2904
2905/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002906 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 */
2908 static void
2909clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911{
2912 vim_free(lp->ll_exp_name);
2913 vim_free(lp->ll_newkey);
2914}
2915
2916/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002917 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 */
2921 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002925 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928{
2929 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002930 listitem_T *ri;
2931 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932
2933 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 cc = *endp;
2938 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002939 if (op != NULL && *op != '=')
2940 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002943 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002944 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002945 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002946 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002948 if ((di == NULL
2949 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2950 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2951 FALSE)))
2952 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 set_var(lp->ll_name, &tv, FALSE);
2954 clear_tv(&tv);
2955 }
2956 }
2957 else
2958 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002960 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002962 else if (tv_check_lock(lp->ll_newkey == NULL
2963 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002964 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002965 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 else if (lp->ll_range)
2967 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002968 listitem_T *ll_li = lp->ll_li;
2969 int ll_n1 = lp->ll_n1;
2970
2971 /*
2972 * Check whether any of the list items is locked
2973 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002974 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002975 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002976 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002977 return;
2978 ri = ri->li_next;
2979 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2980 break;
2981 ll_li = ll_li->li_next;
2982 ++ll_n1;
2983 }
2984
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /*
2986 * Assign the List values to the list items.
2987 */
2988 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 if (op != NULL && *op != '=')
2991 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2992 else
2993 {
2994 clear_tv(&lp->ll_li->li_tv);
2995 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 ri = ri->li_next;
2998 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2999 break;
3000 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003003 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 ri = NULL;
3006 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 lp->ll_li = lp->ll_li->li_next;
3010 ++lp->ll_n1;
3011 }
3012 if (ri != NULL)
3013 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003014 else if (lp->ll_empty2
3015 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 : lp->ll_n1 != lp->ll_n2)
3017 EMSG(_("E711: List value has not enough items"));
3018 }
3019 else
3020 {
3021 /*
3022 * Assign to a List or Dictionary item.
3023 */
3024 if (lp->ll_newkey != NULL)
3025 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 if (op != NULL && *op != '=')
3027 {
3028 EMSG2(_(e_letwrong), op);
3029 return;
3030 }
3031
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003033 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 if (di == NULL)
3035 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003036 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3037 {
3038 vim_free(di);
3039 return;
3040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003042 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003043 else if (op != NULL && *op != '=')
3044 {
3045 tv_op(lp->ll_tv, rettv, op);
3046 return;
3047 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003048 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003050
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 /*
3052 * Assign the value to the variable or list item.
3053 */
3054 if (copy)
3055 copy_tv(rettv, lp->ll_tv);
3056 else
3057 {
3058 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003059 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 }
3062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063}
3064
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3067 * Returns OK or FAIL.
3068 */
3069 static int
3070tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003071 typval_T *tv1;
3072 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 char_u *op;
3074{
3075 long n;
3076 char_u numbuf[NUMBUFLEN];
3077 char_u *s;
3078
3079 /* Can't do anything with a Funcref or a Dict on the right. */
3080 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3081 {
3082 switch (tv1->v_type)
3083 {
3084 case VAR_DICT:
3085 case VAR_FUNC:
3086 break;
3087
3088 case VAR_LIST:
3089 if (*op != '+' || tv2->v_type != VAR_LIST)
3090 break;
3091 /* List += List */
3092 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3093 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3094 return OK;
3095
3096 case VAR_NUMBER:
3097 case VAR_STRING:
3098 if (tv2->v_type == VAR_LIST)
3099 break;
3100 if (*op == '+' || *op == '-')
3101 {
3102 /* nr += nr or nr -= nr*/
3103 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003104#ifdef FEAT_FLOAT
3105 if (tv2->v_type == VAR_FLOAT)
3106 {
3107 float_T f = n;
3108
3109 if (*op == '+')
3110 f += tv2->vval.v_float;
3111 else
3112 f -= tv2->vval.v_float;
3113 clear_tv(tv1);
3114 tv1->v_type = VAR_FLOAT;
3115 tv1->vval.v_float = f;
3116 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003118#endif
3119 {
3120 if (*op == '+')
3121 n += get_tv_number(tv2);
3122 else
3123 n -= get_tv_number(tv2);
3124 clear_tv(tv1);
3125 tv1->v_type = VAR_NUMBER;
3126 tv1->vval.v_number = n;
3127 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 }
3129 else
3130 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131 if (tv2->v_type == VAR_FLOAT)
3132 break;
3133
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003134 /* str .= str */
3135 s = get_tv_string(tv1);
3136 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3137 clear_tv(tv1);
3138 tv1->v_type = VAR_STRING;
3139 tv1->vval.v_string = s;
3140 }
3141 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003142
3143#ifdef FEAT_FLOAT
3144 case VAR_FLOAT:
3145 {
3146 float_T f;
3147
3148 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3149 && tv2->v_type != VAR_NUMBER
3150 && tv2->v_type != VAR_STRING))
3151 break;
3152 if (tv2->v_type == VAR_FLOAT)
3153 f = tv2->vval.v_float;
3154 else
3155 f = get_tv_number(tv2);
3156 if (*op == '+')
3157 tv1->vval.v_float += f;
3158 else
3159 tv1->vval.v_float -= f;
3160 }
3161 return OK;
3162#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 }
3164 }
3165
3166 EMSG2(_(e_letwrong), op);
3167 return FAIL;
3168}
3169
3170/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171 * Add a watcher to a list.
3172 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003173 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 list_T *l;
3176 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177{
3178 lw->lw_next = l->lv_watch;
3179 l->lv_watch = lw;
3180}
3181
3182/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003183 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 * No warning when it isn't found...
3185 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003186 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 list_T *l;
3189 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190{
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192
3193 lwp = &l->lv_watch;
3194 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3195 {
3196 if (lw == lwrem)
3197 {
3198 *lwp = lw->lw_next;
3199 break;
3200 }
3201 lwp = &lw->lw_next;
3202 }
3203}
3204
3205/*
3206 * Just before removing an item from a list: advance watchers to the next
3207 * item.
3208 */
3209 static void
3210list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 list_T *l;
3212 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213{
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215
3216 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3217 if (lw->lw_item == item)
3218 lw->lw_item = item->li_next;
3219}
3220
3221/*
3222 * Evaluate the expression used in a ":for var in expr" command.
3223 * "arg" points to "var".
3224 * Set "*errp" to TRUE for an error, FALSE otherwise;
3225 * Return a pointer that holds the info. Null when there is an error.
3226 */
3227 void *
3228eval_for_line(arg, errp, nextcmdp, skip)
3229 char_u *arg;
3230 int *errp;
3231 char_u **nextcmdp;
3232 int skip;
3233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 typval_T tv;
3237 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238
3239 *errp = TRUE; /* default: there is an error */
3240
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 if (fi == NULL)
3243 return NULL;
3244
3245 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3246 if (expr == NULL)
3247 return fi;
3248
3249 expr = skipwhite(expr);
3250 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3251 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003252 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 return fi;
3254 }
3255
3256 if (skip)
3257 ++emsg_skip;
3258 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3259 {
3260 *errp = FALSE;
3261 if (!skip)
3262 {
3263 l = tv.vval.v_list;
3264 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003265 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003267 clear_tv(&tv);
3268 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 else
3270 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003271 /* No need to increment the refcount, it's already set for the
3272 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 fi->fi_list = l;
3274 list_add_watch(l, &fi->fi_lw);
3275 fi->fi_lw.lw_item = l->lv_first;
3276 }
3277 }
3278 }
3279 if (skip)
3280 --emsg_skip;
3281
3282 return fi;
3283}
3284
3285/*
3286 * Use the first item in a ":for" list. Advance to the next.
3287 * Assign the values to the variable (list). "arg" points to the first one.
3288 * Return TRUE when a valid item was found, FALSE when at end of list or
3289 * something wrong.
3290 */
3291 int
3292next_for_item(fi_void, arg)
3293 void *fi_void;
3294 char_u *arg;
3295{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003296 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003298 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299
3300 item = fi->fi_lw.lw_item;
3301 if (item == NULL)
3302 result = FALSE;
3303 else
3304 {
3305 fi->fi_lw.lw_item = item->li_next;
3306 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3307 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3308 }
3309 return result;
3310}
3311
3312/*
3313 * Free the structure used to store info used by ":for".
3314 */
3315 void
3316free_for_info(fi_void)
3317 void *fi_void;
3318{
Bram Moolenaar33570922005-01-25 22:26:29 +00003319 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003321 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 list_unref(fi->fi_list);
3325 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 vim_free(fi);
3327}
3328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3330
3331 void
3332set_context_for_expression(xp, arg, cmdidx)
3333 expand_T *xp;
3334 char_u *arg;
3335 cmdidx_T cmdidx;
3336{
3337 int got_eq = FALSE;
3338 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 if (cmdidx == CMD_let)
3342 {
3343 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003344 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 {
3346 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 if (vim_iswhite(*p))
3352 break;
3353 }
3354 return;
3355 }
3356 }
3357 else
3358 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3359 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 while ((xp->xp_pattern = vim_strpbrk(arg,
3361 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3362 {
3363 c = *xp->xp_pattern;
3364 if (c == '&')
3365 {
3366 c = xp->xp_pattern[1];
3367 if (c == '&')
3368 {
3369 ++xp->xp_pattern;
3370 xp->xp_context = cmdidx != CMD_let || got_eq
3371 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3372 }
3373 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003376 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3377 xp->xp_pattern += 2;
3378
3379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 else if (c == '$')
3382 {
3383 /* environment variable */
3384 xp->xp_context = EXPAND_ENV_VARS;
3385 }
3386 else if (c == '=')
3387 {
3388 got_eq = TRUE;
3389 xp->xp_context = EXPAND_EXPRESSION;
3390 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003391 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 && xp->xp_context == EXPAND_FUNCTIONS
3393 && vim_strchr(xp->xp_pattern, '(') == NULL)
3394 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 break;
3397 }
3398 else if (cmdidx != CMD_let || got_eq)
3399 {
3400 if (c == '"') /* string */
3401 {
3402 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3403 if (c == '\\' && xp->xp_pattern[1] != NUL)
3404 ++xp->xp_pattern;
3405 xp->xp_context = EXPAND_NOTHING;
3406 }
3407 else if (c == '\'') /* literal string */
3408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003409 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3411 /* skip */ ;
3412 xp->xp_context = EXPAND_NOTHING;
3413 }
3414 else if (c == '|')
3415 {
3416 if (xp->xp_pattern[1] == '|')
3417 {
3418 ++xp->xp_pattern;
3419 xp->xp_context = EXPAND_EXPRESSION;
3420 }
3421 else
3422 xp->xp_context = EXPAND_COMMANDS;
3423 }
3424 else
3425 xp->xp_context = EXPAND_EXPRESSION;
3426 }
3427 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003428 /* Doesn't look like something valid, expand as an expression
3429 * anyway. */
3430 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 arg = xp->xp_pattern;
3432 if (*arg != NUL)
3433 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3434 /* skip */ ;
3435 }
3436 xp->xp_pattern = arg;
3437}
3438
3439#endif /* FEAT_CMDL_COMPL */
3440
3441/*
3442 * ":1,25call func(arg1, arg2)" function call.
3443 */
3444 void
3445ex_call(eap)
3446 exarg_T *eap;
3447{
3448 char_u *arg = eap->arg;
3449 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003451 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003453 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 linenr_T lnum;
3455 int doesrange;
3456 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003457 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003459 if (eap->skip)
3460 {
3461 /* trans_function_name() doesn't work well when skipping, use eval0()
3462 * instead to skip to any following command, e.g. for:
3463 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003464 ++emsg_skip;
3465 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3466 clear_tv(&rettv);
3467 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003468 return;
3469 }
3470
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003471 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003472 if (fudi.fd_newkey != NULL)
3473 {
3474 /* Still need to give an error message for missing key. */
3475 EMSG2(_(e_dictkey), fudi.fd_newkey);
3476 vim_free(fudi.fd_newkey);
3477 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003478 if (tofree == NULL)
3479 return;
3480
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003481 /* Increase refcount on dictionary, it could get deleted when evaluating
3482 * the arguments. */
3483 if (fudi.fd_dict != NULL)
3484 ++fudi.fd_dict->dv_refcount;
3485
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003486 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003487 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003488 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar532c7802005-01-27 14:44:31 +00003490 /* Skip white space to allow ":call func ()". Not good, but required for
3491 * backward compatibility. */
3492 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 if (*startarg != '(')
3496 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003497 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 goto end;
3499 }
3500
3501 /*
3502 * When skipping, evaluate the function once, to find the end of the
3503 * arguments.
3504 * When the function takes a range, this is discovered after the first
3505 * call, and the loop is broken.
3506 */
3507 if (eap->skip)
3508 {
3509 ++emsg_skip;
3510 lnum = eap->line2; /* do it once, also with an invalid range */
3511 }
3512 else
3513 lnum = eap->line1;
3514 for ( ; lnum <= eap->line2; ++lnum)
3515 {
3516 if (!eap->skip && eap->addr_count > 0)
3517 {
3518 curwin->w_cursor.lnum = lnum;
3519 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003520#ifdef FEAT_VIRTUALEDIT
3521 curwin->w_cursor.coladd = 0;
3522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003525 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003526 eap->line1, eap->line2, &doesrange,
3527 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 failed = TRUE;
3530 break;
3531 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003532
3533 /* Handle a function returning a Funcref, Dictionary or List. */
3534 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3535 {
3536 failed = TRUE;
3537 break;
3538 }
3539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (doesrange || eap->skip)
3542 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003546 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (aborting())
3549 break;
3550 }
3551 if (eap->skip)
3552 --emsg_skip;
3553
3554 if (!failed)
3555 {
3556 /* Check for trailing illegal characters and a following command. */
3557 if (!ends_excmd(*arg))
3558 {
3559 emsg_severe = TRUE;
3560 EMSG(_(e_trailing));
3561 }
3562 else
3563 eap->nextcmd = check_nextcmd(arg);
3564 }
3565
3566end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003567 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003568 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569}
3570
3571/*
3572 * ":unlet[!] var1 ... " command.
3573 */
3574 void
3575ex_unlet(eap)
3576 exarg_T *eap;
3577{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578 ex_unletlock(eap, eap->arg, 0);
3579}
3580
3581/*
3582 * ":lockvar" and ":unlockvar" commands
3583 */
3584 void
3585ex_lockvar(eap)
3586 exarg_T *eap;
3587{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 int deep = 2;
3590
3591 if (eap->forceit)
3592 deep = -1;
3593 else if (vim_isdigit(*arg))
3594 {
3595 deep = getdigits(&arg);
3596 arg = skipwhite(arg);
3597 }
3598
3599 ex_unletlock(eap, arg, deep);
3600}
3601
3602/*
3603 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3604 */
3605 static void
3606ex_unletlock(eap, argstart, deep)
3607 exarg_T *eap;
3608 char_u *argstart;
3609 int deep;
3610{
3611 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 do
3617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003619 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003620 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (lv.ll_name == NULL)
3622 error = TRUE; /* error but continue parsing */
3623 if (name_end == NULL || (!vim_iswhite(*name_end)
3624 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 if (name_end != NULL)
3627 {
3628 emsg_severe = TRUE;
3629 EMSG(_(e_trailing));
3630 }
3631 if (!(eap->skip || error))
3632 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 break;
3634 }
3635
3636 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 {
3638 if (eap->cmdidx == CMD_unlet)
3639 {
3640 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3641 error = TRUE;
3642 }
3643 else
3644 {
3645 if (do_lock_var(&lv, name_end, deep,
3646 eap->cmdidx == CMD_lockvar) == FAIL)
3647 error = TRUE;
3648 }
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 if (!eap->skip)
3652 clear_lval(&lv);
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 arg = skipwhite(name_end);
3655 } while (!ends_excmd(*arg));
3656
3657 eap->nextcmd = check_nextcmd(arg);
3658}
3659
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664 int forceit;
3665{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 int ret = OK;
3667 int cc;
3668
3669 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 cc = *name_end;
3672 *name_end = NUL;
3673
3674 /* Normal name or expanded name. */
3675 if (check_changedtick(lp->ll_name))
3676 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003683 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003684 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 else if (lp->ll_range)
3687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003688 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 listitem_T *ll_li = lp->ll_li;
3690 int ll_n1 = lp->ll_n1;
3691
3692 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3693 {
3694 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003696 return FAIL;
3697 ll_li = li;
3698 ++ll_n1;
3699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700
3701 /* Delete a range of List items. */
3702 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3703 {
3704 li = lp->ll_li->li_next;
3705 listitem_remove(lp->ll_list, lp->ll_li);
3706 lp->ll_li = li;
3707 ++lp->ll_n1;
3708 }
3709 }
3710 else
3711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a List item. */
3714 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003717 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 }
3719
3720 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721}
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723/*
3724 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
3727 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003728do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003730 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
Bram Moolenaar33570922005-01-25 22:26:29 +00003732 hashtab_T *ht;
3733 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003734 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003735 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003736 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 ht = find_var_ht(name, &varname);
3739 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003741 if (ht == &globvarht)
3742 d = &globvardict;
3743 else if (current_funccal != NULL
3744 && ht == &current_funccal->l_vars.dv_hashtab)
3745 d = &current_funccal->l_vars;
3746 else if (ht == &compat_hashtab)
3747 d = &vimvardict;
3748 else
3749 {
3750 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3751 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3752 }
3753 if (d == NULL)
3754 {
3755 EMSG2(_(e_intern2), "do_unlet()");
3756 return FAIL;
3757 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003758 hi = hash_find(ht, varname);
3759 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003760 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003761 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003762 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003763 || var_check_ro(di->di_flags, name, FALSE)
3764 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003765 return FAIL;
3766
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003767 delete_var(ht, hi);
3768 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771 if (forceit)
3772 return OK;
3773 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 return FAIL;
3775}
3776
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003777/*
3778 * Lock or unlock variable indicated by "lp".
3779 * "deep" is the levels to go (-1 for unlimited);
3780 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3781 */
3782 static int
3783do_lock_var(lp, name_end, deep, lock)
3784 lval_T *lp;
3785 char_u *name_end;
3786 int deep;
3787 int lock;
3788{
3789 int ret = OK;
3790 int cc;
3791 dictitem_T *di;
3792
3793 if (deep == 0) /* nothing to do */
3794 return OK;
3795
3796 if (lp->ll_tv == NULL)
3797 {
3798 cc = *name_end;
3799 *name_end = NUL;
3800
3801 /* Normal name or expanded name. */
3802 if (check_changedtick(lp->ll_name))
3803 ret = FAIL;
3804 else
3805 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003806 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003807 if (di == NULL)
3808 ret = FAIL;
3809 else
3810 {
3811 if (lock)
3812 di->di_flags |= DI_FLAGS_LOCK;
3813 else
3814 di->di_flags &= ~DI_FLAGS_LOCK;
3815 item_lock(&di->di_tv, deep, lock);
3816 }
3817 }
3818 *name_end = cc;
3819 }
3820 else if (lp->ll_range)
3821 {
3822 listitem_T *li = lp->ll_li;
3823
3824 /* (un)lock a range of List items. */
3825 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3826 {
3827 item_lock(&li->li_tv, deep, lock);
3828 li = li->li_next;
3829 ++lp->ll_n1;
3830 }
3831 }
3832 else if (lp->ll_list != NULL)
3833 /* (un)lock a List item. */
3834 item_lock(&lp->ll_li->li_tv, deep, lock);
3835 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003836 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003837 item_lock(&lp->ll_di->di_tv, deep, lock);
3838
3839 return ret;
3840}
3841
3842/*
3843 * Lock or unlock an item. "deep" is nr of levels to go.
3844 */
3845 static void
3846item_lock(tv, deep, lock)
3847 typval_T *tv;
3848 int deep;
3849 int lock;
3850{
3851 static int recurse = 0;
3852 list_T *l;
3853 listitem_T *li;
3854 dict_T *d;
3855 hashitem_T *hi;
3856 int todo;
3857
3858 if (recurse >= DICT_MAXNEST)
3859 {
3860 EMSG(_("E743: variable nested too deep for (un)lock"));
3861 return;
3862 }
3863 if (deep == 0)
3864 return;
3865 ++recurse;
3866
3867 /* lock/unlock the item itself */
3868 if (lock)
3869 tv->v_lock |= VAR_LOCKED;
3870 else
3871 tv->v_lock &= ~VAR_LOCKED;
3872
3873 switch (tv->v_type)
3874 {
3875 case VAR_LIST:
3876 if ((l = tv->vval.v_list) != NULL)
3877 {
3878 if (lock)
3879 l->lv_lock |= VAR_LOCKED;
3880 else
3881 l->lv_lock &= ~VAR_LOCKED;
3882 if (deep < 0 || deep > 1)
3883 /* recursive: lock/unlock the items the List contains */
3884 for (li = l->lv_first; li != NULL; li = li->li_next)
3885 item_lock(&li->li_tv, deep - 1, lock);
3886 }
3887 break;
3888 case VAR_DICT:
3889 if ((d = tv->vval.v_dict) != NULL)
3890 {
3891 if (lock)
3892 d->dv_lock |= VAR_LOCKED;
3893 else
3894 d->dv_lock &= ~VAR_LOCKED;
3895 if (deep < 0 || deep > 1)
3896 {
3897 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3900 {
3901 if (!HASHITEM_EMPTY(hi))
3902 {
3903 --todo;
3904 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3905 }
3906 }
3907 }
3908 }
3909 }
3910 --recurse;
3911}
3912
Bram Moolenaara40058a2005-07-11 22:42:07 +00003913/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003914 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3915 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003916 */
3917 static int
3918tv_islocked(tv)
3919 typval_T *tv;
3920{
3921 return (tv->v_lock & VAR_LOCKED)
3922 || (tv->v_type == VAR_LIST
3923 && tv->vval.v_list != NULL
3924 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3925 || (tv->v_type == VAR_DICT
3926 && tv->vval.v_dict != NULL
3927 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3928}
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3931/*
3932 * Delete all "menutrans_" variables.
3933 */
3934 void
3935del_menutrans_vars()
3936{
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003941 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 {
3944 if (!HASHITEM_EMPTY(hi))
3945 {
3946 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3948 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 }
3950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952}
3953#endif
3954
3955#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3956
3957/*
3958 * Local string buffer for the next two functions to store a variable name
3959 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3960 * get_user_var_name().
3961 */
3962
3963static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3964
3965static char_u *varnamebuf = NULL;
3966static int varnamebuflen = 0;
3967
3968/*
3969 * Function to concatenate a prefix and a variable name.
3970 */
3971 static char_u *
3972cat_prefix_varname(prefix, name)
3973 int prefix;
3974 char_u *name;
3975{
3976 int len;
3977
3978 len = (int)STRLEN(name) + 3;
3979 if (len > varnamebuflen)
3980 {
3981 vim_free(varnamebuf);
3982 len += 10; /* some additional space */
3983 varnamebuf = alloc(len);
3984 if (varnamebuf == NULL)
3985 {
3986 varnamebuflen = 0;
3987 return NULL;
3988 }
3989 varnamebuflen = len;
3990 }
3991 *varnamebuf = prefix;
3992 varnamebuf[1] = ':';
3993 STRCPY(varnamebuf + 2, name);
3994 return varnamebuf;
3995}
3996
3997/*
3998 * Function given to ExpandGeneric() to obtain the list of user defined
3999 * (global/buffer/window/built-in) variable names.
4000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 char_u *
4002get_user_var_name(xp, idx)
4003 expand_T *xp;
4004 int idx;
4005{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 static long_u gdone;
4007 static long_u bdone;
4008 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009#ifdef FEAT_WINDOWS
4010 static long_u tdone;
4011#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 static int vidx;
4013 static hashitem_T *hi;
4014 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019#ifdef FEAT_WINDOWS
4020 tdone = 0;
4021#endif
4022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004023
4024 /* Global variables */
4025 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4034 return cat_prefix_varname('g', hi->hi_key);
4035 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004037
4038 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004039 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004044 else
4045 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 while (HASHITEM_EMPTY(hi))
4047 ++hi;
4048 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return (char_u *)"b:changedtick";
4054 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004055
4056 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004057 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004060 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 else
4063 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 while (HASHITEM_EMPTY(hi))
4065 ++hi;
4066 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004068
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004071 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 if (tdone < ht->ht_used)
4073 {
4074 if (tdone++ == 0)
4075 hi = ht->ht_array;
4076 else
4077 ++hi;
4078 while (HASHITEM_EMPTY(hi))
4079 ++hi;
4080 return cat_prefix_varname('t', hi->hi_key);
4081 }
4082#endif
4083
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 /* v: variables */
4085 if (vidx < VV_LEN)
4086 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 vim_free(varnamebuf);
4089 varnamebuf = NULL;
4090 varnamebuflen = 0;
4091 return NULL;
4092}
4093
4094#endif /* FEAT_CMDL_COMPL */
4095
4096/*
4097 * types for expressions.
4098 */
4099typedef enum
4100{
4101 TYPE_UNKNOWN = 0
4102 , TYPE_EQUAL /* == */
4103 , TYPE_NEQUAL /* != */
4104 , TYPE_GREATER /* > */
4105 , TYPE_GEQUAL /* >= */
4106 , TYPE_SMALLER /* < */
4107 , TYPE_SEQUAL /* <= */
4108 , TYPE_MATCH /* =~ */
4109 , TYPE_NOMATCH /* !~ */
4110} exptype_T;
4111
4112/*
4113 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4116 */
4117
4118/*
4119 * Handle zero level expression.
4120 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004122 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 * Return OK or FAIL.
4124 */
4125 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 char_u **nextcmd;
4130 int evaluate;
4131{
4132 int ret;
4133 char_u *p;
4134
4135 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (ret == FAIL || !ends_excmd(*p))
4138 {
4139 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /*
4142 * Report the invalid expression unless the expression evaluation has
4143 * been cancelled due to an aborting error, an interrupt, or an
4144 * exception.
4145 */
4146 if (!aborting())
4147 EMSG2(_(e_invexpr2), arg);
4148 ret = FAIL;
4149 }
4150 if (nextcmd != NULL)
4151 *nextcmd = check_nextcmd(p);
4152
4153 return ret;
4154}
4155
4156/*
4157 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004158 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 *
4160 * "arg" must point to the first non-white of the expression.
4161 * "arg" is advanced to the next non-white after the recognized expression.
4162 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004163 * Note: "rettv.v_lock" is not set.
4164 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * Return OK or FAIL.
4166 */
4167 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 int evaluate;
4172{
4173 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004174 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
4176 /*
4177 * Get the first variable.
4178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return FAIL;
4181
4182 if ((*arg)[0] == '?')
4183 {
4184 result = FALSE;
4185 if (evaluate)
4186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 int error = FALSE;
4188
4189 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 if (error)
4193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195
4196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return FAIL;
4202
4203 /*
4204 * Check for the ":".
4205 */
4206 if ((*arg)[0] != ':')
4207 {
4208 EMSG(_("E109: Missing ':' after '?'"));
4209 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 return FAIL;
4212 }
4213
4214 /*
4215 * Get the third variable.
4216 */
4217 *arg = skipwhite(*arg + 1);
4218 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4219 {
4220 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return FAIL;
4223 }
4224 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227
4228 return OK;
4229}
4230
4231/*
4232 * Handle first level expression:
4233 * expr2 || expr2 || expr2 logical OR
4234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
4238 * Return OK or FAIL.
4239 */
4240 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 int evaluate;
4245{
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 long result;
4248 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /*
4252 * Get the first variable.
4253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return FAIL;
4256
4257 /*
4258 * Repeat until there is no following "||".
4259 */
4260 first = TRUE;
4261 result = FALSE;
4262 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4263 {
4264 if (evaluate && first)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (error)
4270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 first = FALSE;
4272 }
4273
4274 /*
4275 * Get the second variable.
4276 */
4277 *arg = skipwhite(*arg + 2);
4278 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4279 return FAIL;
4280
4281 /*
4282 * Compute the result.
4283 */
4284 if (evaluate && !result)
4285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (error)
4290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 if (evaluate)
4293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 rettv->v_type = VAR_NUMBER;
4295 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298
4299 return OK;
4300}
4301
4302/*
4303 * Handle second level expression:
4304 * expr3 && expr3 && expr3 logical AND
4305 *
4306 * "arg" must point to the first non-white of the expression.
4307 * "arg" is advanced to the next non-white after the recognized expression.
4308 *
4309 * Return OK or FAIL.
4310 */
4311 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int evaluate;
4316{
Bram Moolenaar33570922005-01-25 22:26:29 +00004317 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 long result;
4319 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 /*
4329 * Repeat until there is no following "&&".
4330 */
4331 first = TRUE;
4332 result = TRUE;
4333 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4334 {
4335 if (evaluate && first)
4336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (error)
4341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 first = FALSE;
4343 }
4344
4345 /*
4346 * Get the second variable.
4347 */
4348 *arg = skipwhite(*arg + 2);
4349 if (eval4(arg, &var2, evaluate && result) == FAIL)
4350 return FAIL;
4351
4352 /*
4353 * Compute the result.
4354 */
4355 if (evaluate && result)
4356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (error)
4361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 if (evaluate)
4364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 rettv->v_type = VAR_NUMBER;
4366 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 }
4369
4370 return OK;
4371}
4372
4373/*
4374 * Handle third level expression:
4375 * var1 == var2
4376 * var1 =~ var2
4377 * var1 != var2
4378 * var1 !~ var2
4379 * var1 > var2
4380 * var1 >= var2
4381 * var1 < var2
4382 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 * var1 is var2
4384 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 *
4386 * "arg" must point to the first non-white of the expression.
4387 * "arg" is advanced to the next non-white after the recognized expression.
4388 *
4389 * Return OK or FAIL.
4390 */
4391 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 int evaluate;
4396{
Bram Moolenaar33570922005-01-25 22:26:29 +00004397 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 char_u *p;
4399 int i;
4400 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004401 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int len = 2;
4403 long n1, n2;
4404 char_u *s1, *s2;
4405 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4406 regmatch_T regmatch;
4407 int ic;
4408 char_u *save_cpo;
4409
4410 /*
4411 * Get the first variable.
4412 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 return FAIL;
4415
4416 p = *arg;
4417 switch (p[0])
4418 {
4419 case '=': if (p[1] == '=')
4420 type = TYPE_EQUAL;
4421 else if (p[1] == '~')
4422 type = TYPE_MATCH;
4423 break;
4424 case '!': if (p[1] == '=')
4425 type = TYPE_NEQUAL;
4426 else if (p[1] == '~')
4427 type = TYPE_NOMATCH;
4428 break;
4429 case '>': if (p[1] != '=')
4430 {
4431 type = TYPE_GREATER;
4432 len = 1;
4433 }
4434 else
4435 type = TYPE_GEQUAL;
4436 break;
4437 case '<': if (p[1] != '=')
4438 {
4439 type = TYPE_SMALLER;
4440 len = 1;
4441 }
4442 else
4443 type = TYPE_SEQUAL;
4444 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 case 'i': if (p[1] == 's')
4446 {
4447 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4448 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004449 i = p[len];
4450 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 {
4452 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4453 type_is = TRUE;
4454 }
4455 }
4456 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458
4459 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004460 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
4462 if (type != TYPE_UNKNOWN)
4463 {
4464 /* extra question mark appended: ignore case */
4465 if (p[len] == '?')
4466 {
4467 ic = TRUE;
4468 ++len;
4469 }
4470 /* extra '#' appended: match case */
4471 else if (p[len] == '#')
4472 {
4473 ic = FALSE;
4474 ++len;
4475 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004476 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 else
4478 ic = p_ic;
4479
4480 /*
4481 * Get the second variable.
4482 */
4483 *arg = skipwhite(p + len);
4484 if (eval5(arg, &var2, evaluate) == FAIL)
4485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 return FAIL;
4488 }
4489
4490 if (evaluate)
4491 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004492 if (type_is && rettv->v_type != var2.v_type)
4493 {
4494 /* For "is" a different type always means FALSE, for "notis"
4495 * it means TRUE. */
4496 n1 = (type == TYPE_NEQUAL);
4497 }
4498 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4499 {
4500 if (type_is)
4501 {
4502 n1 = (rettv->v_type == var2.v_type
4503 && rettv->vval.v_list == var2.vval.v_list);
4504 if (type == TYPE_NEQUAL)
4505 n1 = !n1;
4506 }
4507 else if (rettv->v_type != var2.v_type
4508 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4509 {
4510 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004511 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004513 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 clear_tv(rettv);
4515 clear_tv(&var2);
4516 return FAIL;
4517 }
4518 else
4519 {
4520 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004521 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4522 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 if (type == TYPE_NEQUAL)
4524 n1 = !n1;
4525 }
4526 }
4527
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004528 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4529 {
4530 if (type_is)
4531 {
4532 n1 = (rettv->v_type == var2.v_type
4533 && rettv->vval.v_dict == var2.vval.v_dict);
4534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 else if (rettv->v_type != var2.v_type
4538 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4539 {
4540 if (rettv->v_type != var2.v_type)
4541 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4542 else
4543 EMSG(_("E736: Invalid operation for Dictionary"));
4544 clear_tv(rettv);
4545 clear_tv(&var2);
4546 return FAIL;
4547 }
4548 else
4549 {
4550 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004551 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4552 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004553 if (type == TYPE_NEQUAL)
4554 n1 = !n1;
4555 }
4556 }
4557
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004558 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4559 {
4560 if (rettv->v_type != var2.v_type
4561 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4562 {
4563 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004564 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004565 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004566 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004567 clear_tv(rettv);
4568 clear_tv(&var2);
4569 return FAIL;
4570 }
4571 else
4572 {
4573 /* Compare two Funcrefs for being equal or unequal. */
4574 if (rettv->vval.v_string == NULL
4575 || var2.vval.v_string == NULL)
4576 n1 = FALSE;
4577 else
4578 n1 = STRCMP(rettv->vval.v_string,
4579 var2.vval.v_string) == 0;
4580 if (type == TYPE_NEQUAL)
4581 n1 = !n1;
4582 }
4583 }
4584
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004585#ifdef FEAT_FLOAT
4586 /*
4587 * If one of the two variables is a float, compare as a float.
4588 * When using "=~" or "!~", always compare as string.
4589 */
4590 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4591 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4592 {
4593 float_T f1, f2;
4594
4595 if (rettv->v_type == VAR_FLOAT)
4596 f1 = rettv->vval.v_float;
4597 else
4598 f1 = get_tv_number(rettv);
4599 if (var2.v_type == VAR_FLOAT)
4600 f2 = var2.vval.v_float;
4601 else
4602 f2 = get_tv_number(&var2);
4603 n1 = FALSE;
4604 switch (type)
4605 {
4606 case TYPE_EQUAL: n1 = (f1 == f2); break;
4607 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4608 case TYPE_GREATER: n1 = (f1 > f2); break;
4609 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4610 case TYPE_SMALLER: n1 = (f1 < f2); break;
4611 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4612 case TYPE_UNKNOWN:
4613 case TYPE_MATCH:
4614 case TYPE_NOMATCH: break; /* avoid gcc warning */
4615 }
4616 }
4617#endif
4618
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 /*
4620 * If one of the two variables is a number, compare as a number.
4621 * When using "=~" or "!~", always compare as string.
4622 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 n1 = get_tv_number(rettv);
4627 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 switch (type)
4629 {
4630 case TYPE_EQUAL: n1 = (n1 == n2); break;
4631 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4632 case TYPE_GREATER: n1 = (n1 > n2); break;
4633 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4634 case TYPE_SMALLER: n1 = (n1 < n2); break;
4635 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4636 case TYPE_UNKNOWN:
4637 case TYPE_MATCH:
4638 case TYPE_NOMATCH: break; /* avoid gcc warning */
4639 }
4640 }
4641 else
4642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004643 s1 = get_tv_string_buf(rettv, buf1);
4644 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4646 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4647 else
4648 i = 0;
4649 n1 = FALSE;
4650 switch (type)
4651 {
4652 case TYPE_EQUAL: n1 = (i == 0); break;
4653 case TYPE_NEQUAL: n1 = (i != 0); break;
4654 case TYPE_GREATER: n1 = (i > 0); break;
4655 case TYPE_GEQUAL: n1 = (i >= 0); break;
4656 case TYPE_SMALLER: n1 = (i < 0); break;
4657 case TYPE_SEQUAL: n1 = (i <= 0); break;
4658
4659 case TYPE_MATCH:
4660 case TYPE_NOMATCH:
4661 /* avoid 'l' flag in 'cpoptions' */
4662 save_cpo = p_cpo;
4663 p_cpo = (char_u *)"";
4664 regmatch.regprog = vim_regcomp(s2,
4665 RE_MAGIC + RE_STRING);
4666 regmatch.rm_ic = ic;
4667 if (regmatch.regprog != NULL)
4668 {
4669 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004670 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (type == TYPE_NOMATCH)
4672 n1 = !n1;
4673 }
4674 p_cpo = save_cpo;
4675 break;
4676
4677 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4678 }
4679 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
4681 clear_tv(&var2);
4682 rettv->v_type = VAR_NUMBER;
4683 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
4685 }
4686
4687 return OK;
4688}
4689
4690/*
4691 * Handle fourth level expression:
4692 * + number addition
4693 * - number subtraction
4694 * . string concatenation
4695 *
4696 * "arg" must point to the first non-white of the expression.
4697 * "arg" is advanced to the next non-white after the recognized expression.
4698 *
4699 * Return OK or FAIL.
4700 */
4701 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 int evaluate;
4706{
Bram Moolenaar33570922005-01-25 22:26:29 +00004707 typval_T var2;
4708 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 int op;
4710 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711#ifdef FEAT_FLOAT
4712 float_T f1 = 0, f2 = 0;
4713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 char_u *s1, *s2;
4715 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4716 char_u *p;
4717
4718 /*
4719 * Get the first variable.
4720 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004721 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 return FAIL;
4723
4724 /*
4725 * Repeat computing, until no '+', '-' or '.' is following.
4726 */
4727 for (;;)
4728 {
4729 op = **arg;
4730 if (op != '+' && op != '-' && op != '.')
4731 break;
4732
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733 if ((op != '+' || rettv->v_type != VAR_LIST)
4734#ifdef FEAT_FLOAT
4735 && (op == '.' || rettv->v_type != VAR_FLOAT)
4736#endif
4737 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004738 {
4739 /* For "list + ...", an illegal use of the first operand as
4740 * a number cannot be determined before evaluating the 2nd
4741 * operand: if this is also a list, all is ok.
4742 * For "something . ...", "something - ..." or "non-list + ...",
4743 * we know that the first operand needs to be a string or number
4744 * without evaluating the 2nd operand. So check before to avoid
4745 * side effects after an error. */
4746 if (evaluate && get_tv_string_chk(rettv) == NULL)
4747 {
4748 clear_tv(rettv);
4749 return FAIL;
4750 }
4751 }
4752
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 /*
4754 * Get the second variable.
4755 */
4756 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004757 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 return FAIL;
4761 }
4762
4763 if (evaluate)
4764 {
4765 /*
4766 * Compute the result.
4767 */
4768 if (op == '.')
4769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4771 s2 = get_tv_string_buf_chk(&var2, buf2);
4772 if (s2 == NULL) /* type error ? */
4773 {
4774 clear_tv(rettv);
4775 clear_tv(&var2);
4776 return FAIL;
4777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004778 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 clear_tv(rettv);
4780 rettv->v_type = VAR_STRING;
4781 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004783 else if (op == '+' && rettv->v_type == VAR_LIST
4784 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004785 {
4786 /* concatenate Lists */
4787 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4788 &var3) == FAIL)
4789 {
4790 clear_tv(rettv);
4791 clear_tv(&var2);
4792 return FAIL;
4793 }
4794 clear_tv(rettv);
4795 *rettv = var3;
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 else
4798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004799 int error = FALSE;
4800
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801#ifdef FEAT_FLOAT
4802 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004803 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804 f1 = rettv->vval.v_float;
4805 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004806 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807 else
4808#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004809 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004810 n1 = get_tv_number_chk(rettv, &error);
4811 if (error)
4812 {
4813 /* This can only happen for "list + non-list". For
4814 * "non-list + ..." or "something - ...", we returned
4815 * before evaluating the 2nd operand. */
4816 clear_tv(rettv);
4817 return FAIL;
4818 }
4819#ifdef FEAT_FLOAT
4820 if (var2.v_type == VAR_FLOAT)
4821 f1 = n1;
4822#endif
4823 }
4824#ifdef FEAT_FLOAT
4825 if (var2.v_type == VAR_FLOAT)
4826 {
4827 f2 = var2.vval.v_float;
4828 n2 = 0;
4829 }
4830 else
4831#endif
4832 {
4833 n2 = get_tv_number_chk(&var2, &error);
4834 if (error)
4835 {
4836 clear_tv(rettv);
4837 clear_tv(&var2);
4838 return FAIL;
4839 }
4840#ifdef FEAT_FLOAT
4841 if (rettv->v_type == VAR_FLOAT)
4842 f2 = n2;
4843#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004845 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846
4847#ifdef FEAT_FLOAT
4848 /* If there is a float on either side the result is a float. */
4849 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4850 {
4851 if (op == '+')
4852 f1 = f1 + f2;
4853 else
4854 f1 = f1 - f2;
4855 rettv->v_type = VAR_FLOAT;
4856 rettv->vval.v_float = f1;
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004859#endif
4860 {
4861 if (op == '+')
4862 n1 = n1 + n2;
4863 else
4864 n1 = n1 - n2;
4865 rettv->v_type = VAR_NUMBER;
4866 rettv->vval.v_number = n1;
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 }
4872 return OK;
4873}
4874
4875/*
4876 * Handle fifth level expression:
4877 * * number multiplication
4878 * / number division
4879 * % number modulo
4880 *
4881 * "arg" must point to the first non-white of the expression.
4882 * "arg" is advanced to the next non-white after the recognized expression.
4883 *
4884 * Return OK or FAIL.
4885 */
4886 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004887eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004891 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892{
Bram Moolenaar33570922005-01-25 22:26:29 +00004893 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 int op;
4895 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004896#ifdef FEAT_FLOAT
4897 int use_float = FALSE;
4898 float_T f1 = 0, f2;
4899#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004900 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901
4902 /*
4903 * Get the first variable.
4904 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004905 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 return FAIL;
4907
4908 /*
4909 * Repeat computing, until no '*', '/' or '%' is following.
4910 */
4911 for (;;)
4912 {
4913 op = **arg;
4914 if (op != '*' && op != '/' && op != '%')
4915 break;
4916
4917 if (evaluate)
4918 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919#ifdef FEAT_FLOAT
4920 if (rettv->v_type == VAR_FLOAT)
4921 {
4922 f1 = rettv->vval.v_float;
4923 use_float = TRUE;
4924 n1 = 0;
4925 }
4926 else
4927#endif
4928 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004930 if (error)
4931 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 else
4934 n1 = 0;
4935
4936 /*
4937 * Get the second variable.
4938 */
4939 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004940 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return FAIL;
4942
4943 if (evaluate)
4944 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945#ifdef FEAT_FLOAT
4946 if (var2.v_type == VAR_FLOAT)
4947 {
4948 if (!use_float)
4949 {
4950 f1 = n1;
4951 use_float = TRUE;
4952 }
4953 f2 = var2.vval.v_float;
4954 n2 = 0;
4955 }
4956 else
4957#endif
4958 {
4959 n2 = get_tv_number_chk(&var2, &error);
4960 clear_tv(&var2);
4961 if (error)
4962 return FAIL;
4963#ifdef FEAT_FLOAT
4964 if (use_float)
4965 f2 = n2;
4966#endif
4967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 /*
4970 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973#ifdef FEAT_FLOAT
4974 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 if (op == '*')
4977 f1 = f1 * f2;
4978 else if (op == '/')
4979 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004980# ifdef VMS
4981 /* VMS crashes on divide by zero, work around it */
4982 if (f2 == 0.0)
4983 {
4984 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004985 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004986 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004987 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004988 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004989 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004990 }
4991 else
4992 f1 = f1 / f2;
4993# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 /* We rely on the floating point library to handle divide
4995 * by zero to result in "inf" and not a crash. */
4996 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004997# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005000 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005001 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002 return FAIL;
5003 }
5004 rettv->v_type = VAR_FLOAT;
5005 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 if (op == '*')
5011 n1 = n1 * n2;
5012 else if (op == '/')
5013 {
5014 if (n2 == 0) /* give an error message? */
5015 {
5016 if (n1 == 0)
5017 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5018 else if (n1 < 0)
5019 n1 = -0x7fffffffL;
5020 else
5021 n1 = 0x7fffffffL;
5022 }
5023 else
5024 n1 = n1 / n2;
5025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005027 {
5028 if (n2 == 0) /* give an error message? */
5029 n1 = 0;
5030 else
5031 n1 = n1 % n2;
5032 }
5033 rettv->v_type = VAR_NUMBER;
5034 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 }
5038
5039 return OK;
5040}
5041
5042/*
5043 * Handle sixth level expression:
5044 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005045 * "string" string constant
5046 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 * &option-name option value
5048 * @r register contents
5049 * identifier variable value
5050 * function() function call
5051 * $VAR environment variable
5052 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005053 * [expr, expr] List
5054 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 *
5056 * Also handle:
5057 * ! in front logical NOT
5058 * - in front unary minus
5059 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060 * trailing [] subscript in String or List
5061 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 *
5063 * "arg" must point to the first non-white of the expression.
5064 * "arg" is advanced to the next non-white after the recognized expression.
5065 *
5066 * Return OK or FAIL.
5067 */
5068 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005069eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005073 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 long n;
5076 int len;
5077 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 char_u *start_leader, *end_leader;
5079 int ret = OK;
5080 char_u *alias;
5081
5082 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 /*
5089 * Skip '!' and '-' characters. They are handled later.
5090 */
5091 start_leader = *arg;
5092 while (**arg == '!' || **arg == '-' || **arg == '+')
5093 *arg = skipwhite(*arg + 1);
5094 end_leader = *arg;
5095
5096 switch (**arg)
5097 {
5098 /*
5099 * Number constant.
5100 */
5101 case '0':
5102 case '1':
5103 case '2':
5104 case '3':
5105 case '4':
5106 case '5':
5107 case '6':
5108 case '7':
5109 case '8':
5110 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005111 {
5112#ifdef FEAT_FLOAT
5113 char_u *p = skipdigits(*arg + 1);
5114 int get_float = FALSE;
5115
5116 /* We accept a float when the format matches
5117 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005118 * strict to avoid backwards compatibility problems.
5119 * Don't look for a float after the "." operator, so that
5120 * ":let vers = 1.2.3" doesn't fail. */
5121 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005123 get_float = TRUE;
5124 p = skipdigits(p + 2);
5125 if (*p == 'e' || *p == 'E')
5126 {
5127 ++p;
5128 if (*p == '-' || *p == '+')
5129 ++p;
5130 if (!vim_isdigit(*p))
5131 get_float = FALSE;
5132 else
5133 p = skipdigits(p + 1);
5134 }
5135 if (ASCII_ISALPHA(*p) || *p == '.')
5136 get_float = FALSE;
5137 }
5138 if (get_float)
5139 {
5140 float_T f;
5141
5142 *arg += string2float(*arg, &f);
5143 if (evaluate)
5144 {
5145 rettv->v_type = VAR_FLOAT;
5146 rettv->vval.v_float = f;
5147 }
5148 }
5149 else
5150#endif
5151 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005152 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005153 *arg += len;
5154 if (evaluate)
5155 {
5156 rettv->v_type = VAR_NUMBER;
5157 rettv->vval.v_number = n;
5158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162
5163 /*
5164 * String constant: "string".
5165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005166 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168
5169 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 break;
5174
5175 /*
5176 * List: [expr, expr]
5177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180
5181 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 * Dictionary: {key: val, key: val}
5183 */
5184 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5185 break;
5186
5187 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005188 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005190 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 break;
5192
5193 /*
5194 * Environment variable: $VAR.
5195 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 break;
5198
5199 /*
5200 * Register contents: @r.
5201 */
5202 case '@': ++*arg;
5203 if (evaluate)
5204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005206 rettv->vval.v_string = get_reg_contents(**arg,
5207 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
5209 if (**arg != NUL)
5210 ++*arg;
5211 break;
5212
5213 /*
5214 * nested expression: (expression).
5215 */
5216 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (**arg == ')')
5219 ++*arg;
5220 else if (ret == OK)
5221 {
5222 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005223 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 ret = FAIL;
5225 }
5226 break;
5227
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 break;
5230 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231
5232 if (ret == NOTDONE)
5233 {
5234 /*
5235 * Must be a variable or function name.
5236 * Can also be a curly-braces kind of name: {expr}.
5237 */
5238 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005239 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 if (alias != NULL)
5241 s = alias;
5242
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005243 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 ret = FAIL;
5245 else
5246 {
5247 if (**arg == '(') /* recursive! */
5248 {
5249 /* If "s" is the name of a variable of type VAR_FUNC
5250 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005251 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252
5253 /* Invoke the function. */
5254 ret = get_func_tv(s, len, rettv, arg,
5255 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005256 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005257
5258 /* If evaluate is FALSE rettv->v_type was not set in
5259 * get_func_tv, but it's needed in handle_subscript() to parse
5260 * what follows. So set it here. */
5261 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5262 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005263 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005264 rettv->v_type = VAR_FUNC;
5265 }
5266
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 /* Stop the expression evaluation when immediately
5268 * aborting on error, or when an interrupt occurred or
5269 * an exception was thrown but not caught. */
5270 if (aborting())
5271 {
5272 if (ret == OK)
5273 clear_tv(rettv);
5274 ret = FAIL;
5275 }
5276 }
5277 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005278 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005279 else
5280 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005282 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 }
5284
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 *arg = skipwhite(*arg);
5286
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005287 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5288 * expr(expr). */
5289 if (ret == OK)
5290 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
5292 /*
5293 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5294 */
5295 if (ret == OK && evaluate && end_leader > start_leader)
5296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005298 int val = 0;
5299#ifdef FEAT_FLOAT
5300 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005301
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005302 if (rettv->v_type == VAR_FLOAT)
5303 f = rettv->vval.v_float;
5304 else
5305#endif
5306 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 clear_tv(rettv);
5310 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 else
5313 {
5314 while (end_leader > start_leader)
5315 {
5316 --end_leader;
5317 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318 {
5319#ifdef FEAT_FLOAT
5320 if (rettv->v_type == VAR_FLOAT)
5321 f = !f;
5322 else
5323#endif
5324 val = !val;
5325 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005327 {
5328#ifdef FEAT_FLOAT
5329 if (rettv->v_type == VAR_FLOAT)
5330 f = -f;
5331 else
5332#endif
5333 val = -val;
5334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005336#ifdef FEAT_FLOAT
5337 if (rettv->v_type == VAR_FLOAT)
5338 {
5339 clear_tv(rettv);
5340 rettv->vval.v_float = f;
5341 }
5342 else
5343#endif
5344 {
5345 clear_tv(rettv);
5346 rettv->v_type = VAR_NUMBER;
5347 rettv->vval.v_number = val;
5348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
5351
5352 return ret;
5353}
5354
5355/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005356 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5357 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5359 */
5360 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005361eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005365 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366{
5367 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005368 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 long len = -1;
5371 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005375 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005377 if (verbose)
5378 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 return FAIL;
5380 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005381#ifdef FEAT_FLOAT
5382 else if (rettv->v_type == VAR_FLOAT)
5383 {
5384 if (verbose)
5385 EMSG(_(e_float_as_string));
5386 return FAIL;
5387 }
5388#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005390 init_tv(&var1);
5391 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 /*
5395 * dict.name
5396 */
5397 key = *arg + 1;
5398 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5399 ;
5400 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 /*
5407 * something[idx]
5408 *
5409 * Get the (first) variable from inside the [].
5410 */
5411 *arg = skipwhite(*arg + 1);
5412 if (**arg == ':')
5413 empty1 = TRUE;
5414 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5415 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005416 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5417 {
5418 /* not a number or string */
5419 clear_tv(&var1);
5420 return FAIL;
5421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422
5423 /*
5424 * Get the second variable from inside the [:].
5425 */
5426 if (**arg == ':')
5427 {
5428 range = TRUE;
5429 *arg = skipwhite(*arg + 1);
5430 if (**arg == ']')
5431 empty2 = TRUE;
5432 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005434 if (!empty1)
5435 clear_tv(&var1);
5436 return FAIL;
5437 }
5438 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5439 {
5440 /* not a number or string */
5441 if (!empty1)
5442 clear_tv(&var1);
5443 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 return FAIL;
5445 }
5446 }
5447
5448 /* Check for the ']'. */
5449 if (**arg != ']')
5450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451 if (verbose)
5452 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 clear_tv(&var1);
5454 if (range)
5455 clear_tv(&var2);
5456 return FAIL;
5457 }
5458 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460
5461 if (evaluate)
5462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 n1 = 0;
5464 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 n1 = get_tv_number(&var1);
5467 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 if (range)
5470 {
5471 if (empty2)
5472 n2 = -1;
5473 else
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 n2 = get_tv_number(&var2);
5476 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 }
5478 }
5479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 {
5482 case VAR_NUMBER:
5483 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 len = (long)STRLEN(s);
5486 if (range)
5487 {
5488 /* The resulting variable is a substring. If the indexes
5489 * are out of range the result is empty. */
5490 if (n1 < 0)
5491 {
5492 n1 = len + n1;
5493 if (n1 < 0)
5494 n1 = 0;
5495 }
5496 if (n2 < 0)
5497 n2 = len + n2;
5498 else if (n2 >= len)
5499 n2 = len;
5500 if (n1 >= len || n2 < 0 || n1 > n2)
5501 s = NULL;
5502 else
5503 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5504 }
5505 else
5506 {
5507 /* The resulting variable is a string of a single
5508 * character. If the index is too big or negative the
5509 * result is empty. */
5510 if (n1 >= len || n1 < 0)
5511 s = NULL;
5512 else
5513 s = vim_strnsave(s + n1, 1);
5514 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 clear_tv(rettv);
5516 rettv->v_type = VAR_STRING;
5517 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 break;
5519
5520 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 if (n1 < 0)
5523 n1 = len + n1;
5524 if (!empty1 && (n1 < 0 || n1 >= len))
5525 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005526 /* For a range we allow invalid values and return an empty
5527 * list. A list index out of range is an error. */
5528 if (!range)
5529 {
5530 if (verbose)
5531 EMSGN(_(e_listidx), n1);
5532 return FAIL;
5533 }
5534 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 }
5536 if (range)
5537 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005538 list_T *l;
5539 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540
5541 if (n2 < 0)
5542 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005543 else if (n2 >= len)
5544 n2 = len - 1;
5545 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005546 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 l = list_alloc();
5548 if (l == NULL)
5549 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 n1 <= n2; ++n1)
5552 {
5553 if (list_append_tv(l, &item->li_tv) == FAIL)
5554 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005555 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 return FAIL;
5557 }
5558 item = item->li_next;
5559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 clear_tv(rettv);
5561 rettv->v_type = VAR_LIST;
5562 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005563 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 else
5566 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005567 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 clear_tv(rettv);
5569 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 }
5571 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572
5573 case VAR_DICT:
5574 if (range)
5575 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005576 if (verbose)
5577 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578 if (len == -1)
5579 clear_tv(&var1);
5580 return FAIL;
5581 }
5582 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005583 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584
5585 if (len == -1)
5586 {
5587 key = get_tv_string(&var1);
5588 if (*key == NUL)
5589 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005590 if (verbose)
5591 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 clear_tv(&var1);
5593 return FAIL;
5594 }
5595 }
5596
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005597 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005599 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005600 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 if (len == -1)
5602 clear_tv(&var1);
5603 if (item == NULL)
5604 return FAIL;
5605
5606 copy_tv(&item->di_tv, &var1);
5607 clear_tv(rettv);
5608 *rettv = var1;
5609 }
5610 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 }
5612 }
5613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 return OK;
5615}
5616
5617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 * Get an option value.
5619 * "arg" points to the '&' or '+' before the option name.
5620 * "arg" is advanced to character after the option name.
5621 * Return OK or FAIL.
5622 */
5623 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005626 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 int evaluate;
5628{
5629 char_u *option_end;
5630 long numval;
5631 char_u *stringval;
5632 int opt_type;
5633 int c;
5634 int working = (**arg == '+'); /* has("+option") */
5635 int ret = OK;
5636 int opt_flags;
5637
5638 /*
5639 * Isolate the option name and find its value.
5640 */
5641 option_end = find_option_end(arg, &opt_flags);
5642 if (option_end == NULL)
5643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 EMSG2(_("E112: Option name missing: %s"), *arg);
5646 return FAIL;
5647 }
5648
5649 if (!evaluate)
5650 {
5651 *arg = option_end;
5652 return OK;
5653 }
5654
5655 c = *option_end;
5656 *option_end = NUL;
5657 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005658 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 if (opt_type == -3) /* invalid name */
5661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005662 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 EMSG2(_("E113: Unknown option: %s"), *arg);
5664 ret = FAIL;
5665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
5668 if (opt_type == -2) /* hidden string option */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv->v_type = VAR_STRING;
5671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 else if (opt_type == -1) /* hidden number option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_NUMBER;
5676 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 else if (opt_type == 1) /* number option */
5679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 rettv->v_type = VAR_NUMBER;
5681 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683 else /* string option */
5684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 rettv->v_type = VAR_STRING;
5686 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 }
5689 else if (working && (opt_type == -2 || opt_type == -1))
5690 ret = FAIL;
5691
5692 *option_end = c; /* put back for error messages */
5693 *arg = option_end;
5694
5695 return ret;
5696}
5697
5698/*
5699 * Allocate a variable for a string constant.
5700 * Return OK or FAIL.
5701 */
5702 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 int evaluate;
5707{
5708 char_u *p;
5709 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 int extra = 0;
5711
5712 /*
5713 * Find the end of the string, skipping backslashed characters.
5714 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 if (*p == '\\' && p[1] != NUL)
5718 {
5719 ++p;
5720 /* A "\<x>" form occupies at least 4 characters, and produces up
5721 * to 6 characters: reserve space for 2 extra */
5722 if (*p == '<')
5723 extra += 2;
5724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726
5727 if (*p != '"')
5728 {
5729 EMSG2(_("E114: Missing quote: %s"), *arg);
5730 return FAIL;
5731 }
5732
5733 /* If only parsing, set *arg and return here */
5734 if (!evaluate)
5735 {
5736 *arg = p + 1;
5737 return OK;
5738 }
5739
5740 /*
5741 * Copy the string into allocated memory, handling backslashed
5742 * characters.
5743 */
5744 name = alloc((unsigned)(p - *arg + extra));
5745 if (name == NULL)
5746 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 rettv->v_type = VAR_STRING;
5748 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 if (*p == '\\')
5753 {
5754 switch (*++p)
5755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 case 'b': *name++ = BS; ++p; break;
5757 case 'e': *name++ = ESC; ++p; break;
5758 case 'f': *name++ = FF; ++p; break;
5759 case 'n': *name++ = NL; ++p; break;
5760 case 'r': *name++ = CAR; ++p; break;
5761 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 case 'X': /* hex: "\x1", "\x12" */
5764 case 'x':
5765 case 'u': /* Unicode: "\u0023" */
5766 case 'U':
5767 if (vim_isxdigit(p[1]))
5768 {
5769 int n, nr;
5770 int c = toupper(*p);
5771
5772 if (c == 'X')
5773 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005774 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005776 else
5777 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 nr = 0;
5779 while (--n >= 0 && vim_isxdigit(p[1]))
5780 {
5781 ++p;
5782 nr = (nr << 4) + hex2nr(*p);
5783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#ifdef FEAT_MBYTE
5786 /* For "\u" store the number according to
5787 * 'encoding'. */
5788 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 else
5791#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795
5796 /* octal: "\1", "\12", "\123" */
5797 case '0':
5798 case '1':
5799 case '2':
5800 case '3':
5801 case '4':
5802 case '5':
5803 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 case '7': *name = *p++ - '0';
5805 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 *name = (*name << 3) + *p++ - '0';
5808 if (*p >= '0' && *p <= '7')
5809 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 break;
5813
5814 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (extra != 0)
5817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 break;
5820 }
5821 /* FALLTHROUGH */
5822
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 }
5826 }
5827 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 *arg = p + 1;
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 return OK;
5835}
5836
5837/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * Return OK or FAIL.
5840 */
5841 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005842get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 int evaluate;
5846{
5847 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 int reduce = 0;
5850
5851 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 */
5854 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 break;
5860 ++reduce;
5861 ++p;
5862 }
5863 }
5864
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 return FAIL;
5869 }
5870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 if (!evaluate)
5873 {
5874 *arg = p + 1;
5875 return OK;
5876 }
5877
5878 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 */
5881 str = alloc((unsigned)((p - *arg) - reduce));
5882 if (str == NULL)
5883 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 rettv->v_type = VAR_STRING;
5885 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 break;
5893 ++p;
5894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 *arg = p + 1;
5899
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 return OK;
5901}
5902
5903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 * Allocate a variable for a List and fill it from "*arg".
5905 * Return OK or FAIL.
5906 */
5907 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005910 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 int evaluate;
5912{
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 list_T *l = NULL;
5914 typval_T tv;
5915 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916
5917 if (evaluate)
5918 {
5919 l = list_alloc();
5920 if (l == NULL)
5921 return FAIL;
5922 }
5923
5924 *arg = skipwhite(*arg + 1);
5925 while (**arg != ']' && **arg != NUL)
5926 {
5927 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5928 goto failret;
5929 if (evaluate)
5930 {
5931 item = listitem_alloc();
5932 if (item != NULL)
5933 {
5934 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005935 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 list_append(l, item);
5937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005938 else
5939 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
5941
5942 if (**arg == ']')
5943 break;
5944 if (**arg != ',')
5945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 goto failret;
5948 }
5949 *arg = skipwhite(*arg + 1);
5950 }
5951
5952 if (**arg != ']')
5953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955failret:
5956 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005957 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 return FAIL;
5959 }
5960
5961 *arg = skipwhite(*arg + 1);
5962 if (evaluate)
5963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 rettv->v_type = VAR_LIST;
5965 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 ++l->lv_refcount;
5967 }
5968
5969 return OK;
5970}
5971
5972/*
5973 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005974 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005976 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977list_alloc()
5978{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005979 list_T *l;
5980
5981 l = (list_T *)alloc_clear(sizeof(list_T));
5982 if (l != NULL)
5983 {
5984 /* Prepend the list to the list of lists for garbage collection. */
5985 if (first_list != NULL)
5986 first_list->lv_used_prev = l;
5987 l->lv_used_prev = NULL;
5988 l->lv_used_next = first_list;
5989 first_list = l;
5990 }
5991 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992}
5993
5994/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005995 * Allocate an empty list for a return value.
5996 * Returns OK or FAIL.
5997 */
5998 static int
5999rettv_list_alloc(rettv)
6000 typval_T *rettv;
6001{
6002 list_T *l = list_alloc();
6003
6004 if (l == NULL)
6005 return FAIL;
6006
6007 rettv->vval.v_list = l;
6008 rettv->v_type = VAR_LIST;
6009 ++l->lv_refcount;
6010 return OK;
6011}
6012
6013/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 * Unreference a list: decrement the reference count and free it when it
6015 * becomes zero.
6016 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006017 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006019 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006021 if (l != NULL && --l->lv_refcount <= 0)
6022 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023}
6024
6025/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006026 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027 * Ignores the reference count.
6028 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006029 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006030list_free(l, recurse)
6031 list_T *l;
6032 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033{
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006036 /* Remove the list from the list of lists for garbage collection. */
6037 if (l->lv_used_prev == NULL)
6038 first_list = l->lv_used_next;
6039 else
6040 l->lv_used_prev->lv_used_next = l->lv_used_next;
6041 if (l->lv_used_next != NULL)
6042 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6043
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006046 /* Remove the item before deleting it. */
6047 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006048 if (recurse || (item->li_tv.v_type != VAR_LIST
6049 && item->li_tv.v_type != VAR_DICT))
6050 clear_tv(&item->li_tv);
6051 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 }
6053 vim_free(l);
6054}
6055
6056/*
6057 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006058 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006060 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061listitem_alloc()
6062{
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064}
6065
6066/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006067 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006069 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006071 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006073 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 vim_free(item);
6075}
6076
6077/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078 * Remove a list item from a List and free it. Also clears the value.
6079 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006080 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006081listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 list_T *l;
6083 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006084{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006085 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006086 listitem_free(item);
6087}
6088
6089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 * Get the number of items in a list.
6091 */
6092 static long
6093list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006094 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096 if (l == NULL)
6097 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006098 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099}
6100
6101/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102 * Return TRUE when two lists have exactly the same values.
6103 */
6104 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 list_T *l1;
6107 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110{
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006113 if (l1 == NULL || l2 == NULL)
6114 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006115 if (l1 == l2)
6116 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006117 if (list_len(l1) != list_len(l2))
6118 return FALSE;
6119
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006120 for (item1 = l1->lv_first, item2 = l2->lv_first;
6121 item1 != NULL && item2 != NULL;
6122 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006124 return FALSE;
6125 return item1 == NULL && item2 == NULL;
6126}
6127
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006128#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6129 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006130/*
6131 * Return the dictitem that an entry in a hashtable points to.
6132 */
6133 dictitem_T *
6134dict_lookup(hi)
6135 hashitem_T *hi;
6136{
6137 return HI2DI(hi);
6138}
6139#endif
6140
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006141/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006142 * Return TRUE when two dictionaries have exactly the same key/values.
6143 */
6144 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 dict_T *d1;
6147 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006149 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006150{
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 hashitem_T *hi;
6152 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006155 if (d1 == NULL || d2 == NULL)
6156 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006157 if (d1 == d2)
6158 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006159 if (dict_len(d1) != dict_len(d2))
6160 return FALSE;
6161
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006162 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 if (!HASHITEM_EMPTY(hi))
6166 {
6167 item2 = dict_find(d2, hi->hi_key, -1);
6168 if (item2 == NULL)
6169 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006170 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006171 return FALSE;
6172 --todo;
6173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006174 }
6175 return TRUE;
6176}
6177
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006178static int tv_equal_recurse_limit;
6179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006180/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006182 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006183 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 */
6185 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 typval_T *tv1;
6188 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 int ic; /* ignore case */
6190 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006191{
6192 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006193 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006195 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006200 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 * recursiveness to a limit. We guess they are equal then.
6202 * A fixed limit has the problem of still taking an awful long time.
6203 * Reduce the limit every time running into it. That should work fine for
6204 * deeply linked structures that are not recursively linked and catch
6205 * recursiveness quickly. */
6206 if (!recursive)
6207 tv_equal_recurse_limit = 1000;
6208 if (recursive_cnt >= tv_equal_recurse_limit)
6209 {
6210 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006211 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213
6214 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006215 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006217 ++recursive_cnt;
6218 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6219 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006220 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006221
6222 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006223 ++recursive_cnt;
6224 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6225 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006226 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006227
6228 case VAR_FUNC:
6229 return (tv1->vval.v_string != NULL
6230 && tv2->vval.v_string != NULL
6231 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6232
6233 case VAR_NUMBER:
6234 return tv1->vval.v_number == tv2->vval.v_number;
6235
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006236#ifdef FEAT_FLOAT
6237 case VAR_FLOAT:
6238 return tv1->vval.v_float == tv2->vval.v_float;
6239#endif
6240
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006241 case VAR_STRING:
6242 s1 = get_tv_string_buf(tv1, buf1);
6243 s2 = get_tv_string_buf(tv2, buf2);
6244 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006246
6247 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 return TRUE;
6249}
6250
6251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 * Locate item with index "n" in list "l" and return it.
6253 * A negative index is counted from the end; -1 is the last item.
6254 * Returns NULL when "n" is out of range.
6255 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006257list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259 long n;
6260{
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 long idx;
6263
6264 if (l == NULL)
6265 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006266
6267 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006269 n = l->lv_len + n;
6270
6271 /* Check for index out of range. */
6272 if (n < 0 || n >= l->lv_len)
6273 return NULL;
6274
6275 /* When there is a cached index may start search from there. */
6276 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006278 if (n < l->lv_idx / 2)
6279 {
6280 /* closest to the start of the list */
6281 item = l->lv_first;
6282 idx = 0;
6283 }
6284 else if (n > (l->lv_idx + l->lv_len) / 2)
6285 {
6286 /* closest to the end of the list */
6287 item = l->lv_last;
6288 idx = l->lv_len - 1;
6289 }
6290 else
6291 {
6292 /* closest to the cached index */
6293 item = l->lv_idx_item;
6294 idx = l->lv_idx;
6295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 }
6297 else
6298 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006299 if (n < l->lv_len / 2)
6300 {
6301 /* closest to the start of the list */
6302 item = l->lv_first;
6303 idx = 0;
6304 }
6305 else
6306 {
6307 /* closest to the end of the list */
6308 item = l->lv_last;
6309 idx = l->lv_len - 1;
6310 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006312
6313 while (n > idx)
6314 {
6315 /* search forward */
6316 item = item->li_next;
6317 ++idx;
6318 }
6319 while (n < idx)
6320 {
6321 /* search backward */
6322 item = item->li_prev;
6323 --idx;
6324 }
6325
6326 /* cache the used index */
6327 l->lv_idx = idx;
6328 l->lv_idx_item = item;
6329
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 return item;
6331}
6332
6333/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006334 * Get list item "l[idx]" as a number.
6335 */
6336 static long
6337list_find_nr(l, idx, errorp)
6338 list_T *l;
6339 long idx;
6340 int *errorp; /* set to TRUE when something wrong */
6341{
6342 listitem_T *li;
6343
6344 li = list_find(l, idx);
6345 if (li == NULL)
6346 {
6347 if (errorp != NULL)
6348 *errorp = TRUE;
6349 return -1L;
6350 }
6351 return get_tv_number_chk(&li->li_tv, errorp);
6352}
6353
6354/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006355 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6356 */
6357 char_u *
6358list_find_str(l, idx)
6359 list_T *l;
6360 long idx;
6361{
6362 listitem_T *li;
6363
6364 li = list_find(l, idx - 1);
6365 if (li == NULL)
6366 {
6367 EMSGN(_(e_listidx), idx);
6368 return NULL;
6369 }
6370 return get_tv_string(&li->li_tv);
6371}
6372
6373/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374 * Locate "item" list "l" and return its index.
6375 * Returns -1 when "item" is not in the list.
6376 */
6377 static long
6378list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 list_T *l;
6380 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006381{
6382 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006384
6385 if (l == NULL)
6386 return -1;
6387 idx = 0;
6388 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6389 ++idx;
6390 if (li == NULL)
6391 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006392 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006393}
6394
6395/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396 * Append item "item" to the end of list "l".
6397 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006398 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 list_T *l;
6401 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402{
6403 if (l->lv_last == NULL)
6404 {
6405 /* empty list */
6406 l->lv_first = item;
6407 l->lv_last = item;
6408 item->li_prev = NULL;
6409 }
6410 else
6411 {
6412 l->lv_last->li_next = item;
6413 item->li_prev = l->lv_last;
6414 l->lv_last = item;
6415 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006416 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 item->li_next = NULL;
6418}
6419
6420/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006424 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 list_T *l;
6427 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006428{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006429 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430
Bram Moolenaar05159a02005-02-26 23:04:13 +00006431 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006433 copy_tv(tv, &li->li_tv);
6434 list_append(l, li);
6435 return OK;
6436}
6437
6438/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006439 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006440 * Return FAIL when out of memory.
6441 */
6442 int
6443list_append_dict(list, dict)
6444 list_T *list;
6445 dict_T *dict;
6446{
6447 listitem_T *li = listitem_alloc();
6448
6449 if (li == NULL)
6450 return FAIL;
6451 li->li_tv.v_type = VAR_DICT;
6452 li->li_tv.v_lock = 0;
6453 li->li_tv.vval.v_dict = dict;
6454 list_append(list, li);
6455 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456 return OK;
6457}
6458
6459/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006460 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006461 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006462 * Returns FAIL when out of memory.
6463 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006464 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006465list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466 list_T *l;
6467 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006468 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006469{
6470 listitem_T *li = listitem_alloc();
6471
6472 if (li == NULL)
6473 return FAIL;
6474 list_append(l, li);
6475 li->li_tv.v_type = VAR_STRING;
6476 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006477 if (str == NULL)
6478 li->li_tv.vval.v_string = NULL;
6479 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006480 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006481 return FAIL;
6482 return OK;
6483}
6484
6485/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006486 * Append "n" to list "l".
6487 * Returns FAIL when out of memory.
6488 */
6489 static int
6490list_append_number(l, n)
6491 list_T *l;
6492 varnumber_T n;
6493{
6494 listitem_T *li;
6495
6496 li = listitem_alloc();
6497 if (li == NULL)
6498 return FAIL;
6499 li->li_tv.v_type = VAR_NUMBER;
6500 li->li_tv.v_lock = 0;
6501 li->li_tv.vval.v_number = n;
6502 list_append(l, li);
6503 return OK;
6504}
6505
6506/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 * If "item" is NULL append at the end.
6509 * Return FAIL when out of memory.
6510 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006511 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 list_T *l;
6514 typval_T *tv;
6515 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516{
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518
6519 if (ni == NULL)
6520 return FAIL;
6521 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006522 list_insert(l, ni, item);
6523 return OK;
6524}
6525
6526 void
6527list_insert(l, ni, item)
6528 list_T *l;
6529 listitem_T *ni;
6530 listitem_T *item;
6531{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 if (item == NULL)
6533 /* Append new item at end of list. */
6534 list_append(l, ni);
6535 else
6536 {
6537 /* Insert new item before existing item. */
6538 ni->li_prev = item->li_prev;
6539 ni->li_next = item;
6540 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006541 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006543 ++l->lv_idx;
6544 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006546 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006548 l->lv_idx_item = NULL;
6549 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006551 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553}
6554
6555/*
6556 * Extend "l1" with "l2".
6557 * If "bef" is NULL append at the end, otherwise insert before this item.
6558 * Returns FAIL when out of memory.
6559 */
6560 static int
6561list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 list_T *l1;
6563 list_T *l2;
6564 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565{
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006567 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006569 /* We also quit the loop when we have inserted the original item count of
6570 * the list, avoid a hang when we extend a list with itself. */
6571 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6573 return FAIL;
6574 return OK;
6575}
6576
6577/*
6578 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6579 * Return FAIL when out of memory.
6580 */
6581 static int
6582list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *l1;
6584 list_T *l2;
6585 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586{
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006589 if (l1 == NULL || l2 == NULL)
6590 return FAIL;
6591
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006594 if (l == NULL)
6595 return FAIL;
6596 tv->v_type = VAR_LIST;
6597 tv->vval.v_list = l;
6598
6599 /* append all items from the second list */
6600 return list_extend(l, l2, NULL);
6601}
6602
6603/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006606 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 * Returns NULL when out of memory.
6608 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006609 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006613 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614{
Bram Moolenaar33570922005-01-25 22:26:29 +00006615 list_T *copy;
6616 listitem_T *item;
6617 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618
6619 if (orig == NULL)
6620 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621
6622 copy = list_alloc();
6623 if (copy != NULL)
6624 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006625 if (copyID != 0)
6626 {
6627 /* Do this before adding the items, because one of the items may
6628 * refer back to this list. */
6629 orig->lv_copyID = copyID;
6630 orig->lv_copylist = copy;
6631 }
6632 for (item = orig->lv_first; item != NULL && !got_int;
6633 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 {
6635 ni = listitem_alloc();
6636 if (ni == NULL)
6637 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006638 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006639 {
6640 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6641 {
6642 vim_free(ni);
6643 break;
6644 }
6645 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006647 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 list_append(copy, ni);
6649 }
6650 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006651 if (item != NULL)
6652 {
6653 list_unref(copy);
6654 copy = NULL;
6655 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656 }
6657
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658 return copy;
6659}
6660
6661/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006662 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006663 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006664 * This used to be called list_remove, but that conflicts with a Sun header
6665 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006667 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006668vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006669 list_T *l;
6670 listitem_T *item;
6671 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006672{
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006675 /* notify watchers */
6676 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006678 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006679 list_fix_watch(l, ip);
6680 if (ip == item2)
6681 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006683
6684 if (item2->li_next == NULL)
6685 l->lv_last = item->li_prev;
6686 else
6687 item2->li_next->li_prev = item->li_prev;
6688 if (item->li_prev == NULL)
6689 l->lv_first = item2->li_next;
6690 else
6691 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006692 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693}
6694
6695/*
6696 * Return an allocated string with the string representation of a list.
6697 * May return NULL.
6698 */
6699 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006700list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006701 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006702 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703{
6704 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006705
6706 if (tv->vval.v_list == NULL)
6707 return NULL;
6708 ga_init2(&ga, (int)sizeof(char), 80);
6709 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006710 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006711 {
6712 vim_free(ga.ga_data);
6713 return NULL;
6714 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006715 ga_append(&ga, ']');
6716 ga_append(&ga, NUL);
6717 return (char_u *)ga.ga_data;
6718}
6719
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006720typedef struct join_S {
6721 char_u *s;
6722 char_u *tofree;
6723} join_T;
6724
6725 static int
6726list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6727 garray_T *gap; /* to store the result in */
6728 list_T *l;
6729 char_u *sep;
6730 int echo_style;
6731 int copyID;
6732 garray_T *join_gap; /* to keep each list item string */
6733{
6734 int i;
6735 join_T *p;
6736 int len;
6737 int sumlen = 0;
6738 int first = TRUE;
6739 char_u *tofree;
6740 char_u numbuf[NUMBUFLEN];
6741 listitem_T *item;
6742 char_u *s;
6743
6744 /* Stringify each item in the list. */
6745 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6746 {
6747 if (echo_style)
6748 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6749 else
6750 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6751 if (s == NULL)
6752 return FAIL;
6753
6754 len = (int)STRLEN(s);
6755 sumlen += len;
6756
Bram Moolenaarcde88542015-08-11 19:14:00 +02006757 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006758 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6759 if (tofree != NULL || s != numbuf)
6760 {
6761 p->s = s;
6762 p->tofree = tofree;
6763 }
6764 else
6765 {
6766 p->s = vim_strnsave(s, len);
6767 p->tofree = p->s;
6768 }
6769
6770 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006771 if (did_echo_string_emsg) /* recursion error, bail out */
6772 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006773 }
6774
6775 /* Allocate result buffer with its total size, avoid re-allocation and
6776 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6777 if (join_gap->ga_len >= 2)
6778 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6779 if (ga_grow(gap, sumlen + 2) == FAIL)
6780 return FAIL;
6781
6782 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6783 {
6784 if (first)
6785 first = FALSE;
6786 else
6787 ga_concat(gap, sep);
6788 p = ((join_T *)join_gap->ga_data) + i;
6789
6790 if (p->s != NULL)
6791 ga_concat(gap, p->s);
6792 line_breakcheck();
6793 }
6794
6795 return OK;
6796}
6797
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006798/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006800 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006801 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006802 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006803 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006804list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006806 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006807 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006808 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006809 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006810{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006811 garray_T join_ga;
6812 int retval;
6813 join_T *p;
6814 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006815
Bram Moolenaard39a7512015-04-16 22:51:22 +02006816 if (l->lv_len < 1)
6817 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006818 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6819 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6820
6821 /* Dispose each item in join_ga. */
6822 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006824 p = (join_T *)join_ga.ga_data;
6825 for (i = 0; i < join_ga.ga_len; ++i)
6826 {
6827 vim_free(p->tofree);
6828 ++p;
6829 }
6830 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006831 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006832
6833 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006834}
6835
6836/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 * Garbage collection for lists and dictionaries.
6838 *
6839 * We use reference counts to be able to free most items right away when they
6840 * are no longer used. But for composite items it's possible that it becomes
6841 * unused while the reference count is > 0: When there is a recursive
6842 * reference. Example:
6843 * :let l = [1, 2, 3]
6844 * :let d = {9: l}
6845 * :let l[1] = d
6846 *
6847 * Since this is quite unusual we handle this with garbage collection: every
6848 * once in a while find out which lists and dicts are not referenced from any
6849 * variable.
6850 *
6851 * Here is a good reference text about garbage collection (refers to Python
6852 * but it applies to all reference-counting mechanisms):
6853 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006854 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855
6856/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857 * Do garbage collection for lists and dicts.
6858 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006859 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 int
6861garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006862{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006864 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 buf_T *buf;
6866 win_T *wp;
6867 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006868 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006869 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006871#ifdef FEAT_WINDOWS
6872 tabpage_T *tp;
6873#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006875 /* Only do this once. */
6876 want_garbage_collect = FALSE;
6877 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006878 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006879
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 /* We advance by two because we add one for items referenced through
6881 * previous_funccal. */
6882 current_copyID += COPYID_INC;
6883 copyID = current_copyID;
6884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /*
6886 * 1. Go through all accessible variables and mark all lists and dicts
6887 * with copyID.
6888 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889
6890 /* Don't free variables in the previous_funccal list unless they are only
6891 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006892 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6894 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6896 NULL);
6897 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6898 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 }
6900
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 /* script-local variables */
6902 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904
6905 /* buffer-local variables */
6906 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6908 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
6910 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006911 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6913 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006914#ifdef FEAT_AUTOCMD
6915 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6917 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006918#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006920#ifdef FEAT_WINDOWS
6921 /* tabpage-local variables */
6922 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6924 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006925#endif
6926
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929
6930 /* function-local variables */
6931 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6932 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6934 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 }
6936
Bram Moolenaard812df62008-11-09 12:46:09 +00006937 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006939
Bram Moolenaar1dced572012-04-05 16:54:08 +02006940#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006942#endif
6943
Bram Moolenaardb913952012-06-29 12:54:53 +02006944#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006946#endif
6947
6948#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006950#endif
6951
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006953 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 /*
6955 * 2. Free lists and dictionaries that are not referenced.
6956 */
6957 did_free = free_unref_items(copyID);
6958
6959 /*
6960 * 3. Check if any funccal can be freed now.
6961 */
6962 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006964 if (can_free_funccal(*pfc, copyID))
6965 {
6966 fc = *pfc;
6967 *pfc = fc->caller;
6968 free_funccal(fc, TRUE);
6969 did_free = TRUE;
6970 did_free_funccal = TRUE;
6971 }
6972 else
6973 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006975 if (did_free_funccal)
6976 /* When a funccal was freed some more items might be garbage
6977 * collected, so run again. */
6978 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980 else if (p_verbose > 0)
6981 {
6982 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6983 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984
6985 return did_free;
6986}
6987
6988/*
6989 * Free lists and dictionaries that are no longer referenced.
6990 */
6991 static int
6992free_unref_items(copyID)
6993 int copyID;
6994{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006995 dict_T *dd, *dd_next;
6996 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 int did_free = FALSE;
6998
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007000 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 */
7002 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007003 {
7004 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007005 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007007 /* Free the Dictionary and ordinary items it contains, but don't
7008 * recurse into Lists and Dictionaries, they will be in the list
7009 * of dicts or list of lists. */
7010 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 dd = dd_next;
7014 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015
7016 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007017 * Go through the list of lists and free items without the copyID.
7018 * But don't free a list that has a watcher (used in a for loop), these
7019 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 */
7021 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007022 {
7023 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007024 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7025 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007027 /* Free the List and ordinary items it contains, but don't recurse
7028 * into Lists and Dictionaries, they will be in the list of dicts
7029 * or list of lists. */
7030 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007033 ll = ll_next;
7034 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 return did_free;
7036}
7037
7038/*
7039 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 * "list_stack" is used to add lists to be marked. Can be NULL.
7041 *
7042 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 int
7045set_ref_in_ht(ht, copyID, list_stack)
7046 hashtab_T *ht;
7047 int copyID;
7048 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049{
7050 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 hashtab_T *cur_ht;
7054 ht_stack_T *ht_stack = NULL;
7055 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 cur_ht = ht;
7058 for (;;)
7059 {
7060 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 /* Mark each item in the hashtab. If the item contains a hashtab
7063 * it is added to ht_stack, if it contains a list it is added to
7064 * list_stack. */
7065 todo = (int)cur_ht->ht_used;
7066 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7067 if (!HASHITEM_EMPTY(hi))
7068 {
7069 --todo;
7070 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7071 &ht_stack, list_stack);
7072 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074
7075 if (ht_stack == NULL)
7076 break;
7077
7078 /* take an item from the stack */
7079 cur_ht = ht_stack->ht;
7080 tempitem = ht_stack;
7081 ht_stack = ht_stack->prev;
7082 free(tempitem);
7083 }
7084
7085 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086}
7087
7088/*
7089 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007090 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7091 *
7092 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 int
7095set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096 list_T *l;
7097 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 listitem_T *li;
7101 int abort = FALSE;
7102 list_T *cur_l;
7103 list_stack_T *list_stack = NULL;
7104 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007106 cur_l = l;
7107 for (;;)
7108 {
7109 if (!abort)
7110 /* Mark each item in the list. If the item contains a hashtab
7111 * it is added to ht_stack, if it contains a list it is added to
7112 * list_stack. */
7113 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7114 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7115 ht_stack, &list_stack);
7116 if (list_stack == NULL)
7117 break;
7118
7119 /* take an item from the stack */
7120 cur_l = list_stack->list;
7121 tempitem = list_stack;
7122 list_stack = list_stack->prev;
7123 free(tempitem);
7124 }
7125
7126 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007127}
7128
7129/*
7130 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 * "list_stack" is used to add lists to be marked. Can be NULL.
7132 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7133 *
7134 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007135 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007136 int
7137set_ref_in_item(tv, copyID, ht_stack, list_stack)
7138 typval_T *tv;
7139 int copyID;
7140 ht_stack_T **ht_stack;
7141 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007142{
7143 dict_T *dd;
7144 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007145 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146
7147 switch (tv->v_type)
7148 {
7149 case VAR_DICT:
7150 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007151 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007153 /* Didn't see this dict yet. */
7154 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 if (ht_stack == NULL)
7156 {
7157 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7158 }
7159 else
7160 {
7161 ht_stack_T *newitem = (ht_stack_T*)malloc(
7162 sizeof(ht_stack_T));
7163 if (newitem == NULL)
7164 abort = TRUE;
7165 else
7166 {
7167 newitem->ht = &dd->dv_hashtab;
7168 newitem->prev = *ht_stack;
7169 *ht_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
7175 case VAR_LIST:
7176 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007177 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007178 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007179 /* Didn't see this list yet. */
7180 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 if (list_stack == NULL)
7182 {
7183 abort = set_ref_in_list(ll, copyID, ht_stack);
7184 }
7185 else
7186 {
7187 list_stack_T *newitem = (list_stack_T*)malloc(
7188 sizeof(list_stack_T));
7189 if (newitem == NULL)
7190 abort = TRUE;
7191 else
7192 {
7193 newitem->list = ll;
7194 newitem->prev = *list_stack;
7195 *list_stack = newitem;
7196 }
7197 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007198 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007199 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007200 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007201 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007202}
7203
7204/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007205 * Allocate an empty header for a dictionary.
7206 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007207 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208dict_alloc()
7209{
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007214 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007215 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007216 if (first_dict != NULL)
7217 first_dict->dv_used_prev = d;
7218 d->dv_used_next = first_dict;
7219 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007220 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007221
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007223 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007224 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007225 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007226 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007228 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229}
7230
7231/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007232 * Allocate an empty dict for a return value.
7233 * Returns OK or FAIL.
7234 */
7235 static int
7236rettv_dict_alloc(rettv)
7237 typval_T *rettv;
7238{
7239 dict_T *d = dict_alloc();
7240
7241 if (d == NULL)
7242 return FAIL;
7243
7244 rettv->vval.v_dict = d;
7245 rettv->v_type = VAR_DICT;
7246 ++d->dv_refcount;
7247 return OK;
7248}
7249
7250
7251/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 * Unreference a Dictionary: decrement the reference count and free it when it
7253 * becomes zero.
7254 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007255 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007259 if (d != NULL && --d->dv_refcount <= 0)
7260 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261}
7262
7263/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007264 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 * Ignores the reference count.
7266 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007267 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007268dict_free(d, recurse)
7269 dict_T *d;
7270 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007274 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007276 /* Remove the dict from the list of dicts for garbage collection. */
7277 if (d->dv_used_prev == NULL)
7278 first_dict = d->dv_used_next;
7279 else
7280 d->dv_used_prev->dv_used_next = d->dv_used_next;
7281 if (d->dv_used_next != NULL)
7282 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7283
7284 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007285 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 if (!HASHITEM_EMPTY(hi))
7290 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007291 /* Remove the item before deleting it, just in case there is
7292 * something recursive causing trouble. */
7293 di = HI2DI(hi);
7294 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007295 if (recurse || (di->di_tv.v_type != VAR_LIST
7296 && di->di_tv.v_type != VAR_DICT))
7297 clear_tv(&di->di_tv);
7298 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 --todo;
7300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303 vim_free(d);
7304}
7305
7306/*
7307 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 * The "key" is copied to the new item.
7309 * Note that the value of the item "di_tv" still needs to be initialized!
7310 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007312 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313dictitem_alloc(key)
7314 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315{
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007318 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007320 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007322 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007323 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325}
7326
7327/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328 * Make a copy of a Dictionary item.
7329 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007330 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333{
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007336 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7337 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007338 if (di != NULL)
7339 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007341 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 copy_tv(&org->di_tv, &di->di_tv);
7343 }
7344 return di;
7345}
7346
7347/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 * Remove item "item" from Dictionary "dict" and free it.
7349 */
7350 static void
7351dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 dict_T *dict;
7353 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007354{
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 if (HASHITEM_EMPTY(hi))
7359 EMSG2(_(e_intern2), "dictitem_remove()");
7360 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 dictitem_free(item);
7363}
7364
7365/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 * Free a dict item. Also clears the value.
7367 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007368 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007371{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007373 if (item->di_flags & DI_FLAGS_ALLOC)
7374 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375}
7376
7377/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7379 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007380 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 * Returns NULL when out of memory.
7382 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007384dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007387 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388{
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 dict_T *copy;
7390 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393
7394 if (orig == NULL)
7395 return NULL;
7396
7397 copy = dict_alloc();
7398 if (copy != NULL)
7399 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007400 if (copyID != 0)
7401 {
7402 orig->dv_copyID = copyID;
7403 orig->dv_copydict = copy;
7404 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007405 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007406 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 --todo;
7411
7412 di = dictitem_alloc(hi->hi_key);
7413 if (di == NULL)
7414 break;
7415 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007416 {
7417 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7418 copyID) == FAIL)
7419 {
7420 vim_free(di);
7421 break;
7422 }
7423 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007424 else
7425 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7426 if (dict_add(copy, di) == FAIL)
7427 {
7428 dictitem_free(di);
7429 break;
7430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007435 if (todo > 0)
7436 {
7437 dict_unref(copy);
7438 copy = NULL;
7439 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007440 }
7441
7442 return copy;
7443}
7444
7445/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007447 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007449 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 dict_T *d;
7452 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453{
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007455}
7456
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007458 * Add a number or string entry to dictionary "d".
7459 * When "str" is NULL use number "nr", otherwise use "str".
7460 * Returns FAIL when out of memory and when key already exists.
7461 */
7462 int
7463dict_add_nr_str(d, key, nr, str)
7464 dict_T *d;
7465 char *key;
7466 long nr;
7467 char_u *str;
7468{
7469 dictitem_T *item;
7470
7471 item = dictitem_alloc((char_u *)key);
7472 if (item == NULL)
7473 return FAIL;
7474 item->di_tv.v_lock = 0;
7475 if (str == NULL)
7476 {
7477 item->di_tv.v_type = VAR_NUMBER;
7478 item->di_tv.vval.v_number = nr;
7479 }
7480 else
7481 {
7482 item->di_tv.v_type = VAR_STRING;
7483 item->di_tv.vval.v_string = vim_strsave(str);
7484 }
7485 if (dict_add(d, item) == FAIL)
7486 {
7487 dictitem_free(item);
7488 return FAIL;
7489 }
7490 return OK;
7491}
7492
7493/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007494 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007495 * Returns FAIL when out of memory and when key already exists.
7496 */
7497 int
7498dict_add_list(d, key, list)
7499 dict_T *d;
7500 char *key;
7501 list_T *list;
7502{
7503 dictitem_T *item;
7504
7505 item = dictitem_alloc((char_u *)key);
7506 if (item == NULL)
7507 return FAIL;
7508 item->di_tv.v_lock = 0;
7509 item->di_tv.v_type = VAR_LIST;
7510 item->di_tv.vval.v_list = list;
7511 if (dict_add(d, item) == FAIL)
7512 {
7513 dictitem_free(item);
7514 return FAIL;
7515 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007516 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007517 return OK;
7518}
7519
7520/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007521 * Get the number of items in a Dictionary.
7522 */
7523 static long
7524dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007526{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527 if (d == NULL)
7528 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007529 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007530}
7531
7532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 * Find item "key[len]" in Dictionary "d".
7534 * If "len" is negative use strlen(key).
7535 * Returns NULL when not found.
7536 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007537 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007538dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007539 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 char_u *key;
7541 int len;
7542{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543#define AKEYLEN 200
7544 char_u buf[AKEYLEN];
7545 char_u *akey;
7546 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007549 if (len < 0)
7550 akey = key;
7551 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007552 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 tofree = akey = vim_strnsave(key, len);
7554 if (akey == NULL)
7555 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 else
7558 {
7559 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007560 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 akey = buf;
7562 }
7563
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 vim_free(tofree);
7566 if (HASHITEM_EMPTY(hi))
7567 return NULL;
7568 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569}
7570
7571/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007572 * Get a string item from a dictionary.
7573 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007574 * Returns NULL if the entry doesn't exist or out of memory.
7575 */
7576 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007577get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007578 dict_T *d;
7579 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007580 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007581{
7582 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007583 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007584
7585 di = dict_find(d, key, -1);
7586 if (di == NULL)
7587 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007588 s = get_tv_string(&di->di_tv);
7589 if (save && s != NULL)
7590 s = vim_strsave(s);
7591 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007592}
7593
7594/*
7595 * Get a number item from a dictionary.
7596 * Returns 0 if the entry doesn't exist or out of memory.
7597 */
7598 long
7599get_dict_number(d, key)
7600 dict_T *d;
7601 char_u *key;
7602{
7603 dictitem_T *di;
7604
7605 di = dict_find(d, key, -1);
7606 if (di == NULL)
7607 return 0;
7608 return get_tv_number(&di->di_tv);
7609}
7610
7611/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 * Return an allocated string with the string representation of a Dictionary.
7613 * May return NULL.
7614 */
7615 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007616dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619{
7620 garray_T ga;
7621 int first = TRUE;
7622 char_u *tofree;
7623 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 return NULL;
7631 ga_init2(&ga, (int)sizeof(char), 80);
7632 ga_append(&ga, '{');
7633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007634 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007635 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 --todo;
7640
7641 if (first)
7642 first = FALSE;
7643 else
7644 ga_concat(&ga, (char_u *)", ");
7645
7646 tofree = string_quote(hi->hi_key, FALSE);
7647 if (tofree != NULL)
7648 {
7649 ga_concat(&ga, tofree);
7650 vim_free(tofree);
7651 }
7652 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007653 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 if (s != NULL)
7655 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007657 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007658 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007659 line_breakcheck();
7660
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007663 if (todo > 0)
7664 {
7665 vim_free(ga.ga_data);
7666 return NULL;
7667 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668
7669 ga_append(&ga, '}');
7670 ga_append(&ga, NUL);
7671 return (char_u *)ga.ga_data;
7672}
7673
7674/*
7675 * Allocate a variable for a Dictionary and fill it from "*arg".
7676 * Return OK or FAIL. Returns NOTDONE for {expr}.
7677 */
7678 static int
7679get_dict_tv(arg, rettv, evaluate)
7680 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007681 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 int evaluate;
7683{
Bram Moolenaar33570922005-01-25 22:26:29 +00007684 dict_T *d = NULL;
7685 typval_T tvkey;
7686 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007687 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691
7692 /*
7693 * First check if it's not a curly-braces thing: {expr}.
7694 * Must do this without evaluating, otherwise a function may be called
7695 * twice. Unfortunately this means we need to call eval1() twice for the
7696 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007699 if (*start != '}')
7700 {
7701 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7702 return FAIL;
7703 if (*start == '}')
7704 return NOTDONE;
7705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706
7707 if (evaluate)
7708 {
7709 d = dict_alloc();
7710 if (d == NULL)
7711 return FAIL;
7712 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 tvkey.v_type = VAR_UNKNOWN;
7714 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715
7716 *arg = skipwhite(*arg + 1);
7717 while (**arg != '}' && **arg != NUL)
7718 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007719 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 goto failret;
7721 if (**arg != ':')
7722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007723 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 goto failret;
7726 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007727 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007729 key = get_tv_string_buf_chk(&tvkey, buf);
7730 if (key == NULL || *key == NUL)
7731 {
7732 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7733 if (key != NULL)
7734 EMSG(_(e_emptykey));
7735 clear_tv(&tvkey);
7736 goto failret;
7737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739
7740 *arg = skipwhite(*arg + 1);
7741 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7742 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007743 if (evaluate)
7744 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 goto failret;
7746 }
7747 if (evaluate)
7748 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007749 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 if (item != NULL)
7751 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007752 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 clear_tv(&tv);
7755 goto failret;
7756 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 item = dictitem_alloc(key);
7758 clear_tv(&tvkey);
7759 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007762 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007763 if (dict_add(d, item) == FAIL)
7764 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765 }
7766 }
7767
7768 if (**arg == '}')
7769 break;
7770 if (**arg != ',')
7771 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007772 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007773 goto failret;
7774 }
7775 *arg = skipwhite(*arg + 1);
7776 }
7777
7778 if (**arg != '}')
7779 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007780 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781failret:
7782 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007783 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784 return FAIL;
7785 }
7786
7787 *arg = skipwhite(*arg + 1);
7788 if (evaluate)
7789 {
7790 rettv->v_type = VAR_DICT;
7791 rettv->vval.v_dict = d;
7792 ++d->dv_refcount;
7793 }
7794
7795 return OK;
7796}
7797
Bram Moolenaar8c711452005-01-14 21:53:12 +00007798/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 * Return a string with the string representation of a variable.
7800 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007801 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007802 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007803 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007804 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007805 */
7806 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007807echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007810 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007811 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813 static int recurse = 0;
7814 char_u *r = NULL;
7815
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007818 if (!did_echo_string_emsg)
7819 {
7820 /* Only give this message once for a recursive call to avoid
7821 * flooding the user with errors. And stop iterating over lists
7822 * and dicts. */
7823 did_echo_string_emsg = TRUE;
7824 EMSG(_("E724: variable nested too deep for displaying"));
7825 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007827 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 }
7829 ++recurse;
7830
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007831 switch (tv->v_type)
7832 {
7833 case VAR_FUNC:
7834 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 r = tv->vval.v_string;
7836 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007837
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007838 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007839 if (tv->vval.v_list == NULL)
7840 {
7841 *tofree = NULL;
7842 r = NULL;
7843 }
7844 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7845 {
7846 *tofree = NULL;
7847 r = (char_u *)"[...]";
7848 }
7849 else
7850 {
7851 tv->vval.v_list->lv_copyID = copyID;
7852 *tofree = list2string(tv, copyID);
7853 r = *tofree;
7854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007856
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007858 if (tv->vval.v_dict == NULL)
7859 {
7860 *tofree = NULL;
7861 r = NULL;
7862 }
7863 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7864 {
7865 *tofree = NULL;
7866 r = (char_u *)"{...}";
7867 }
7868 else
7869 {
7870 tv->vval.v_dict->dv_copyID = copyID;
7871 *tofree = dict2string(tv, copyID);
7872 r = *tofree;
7873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007876 case VAR_STRING:
7877 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007878 *tofree = NULL;
7879 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007881
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882#ifdef FEAT_FLOAT
7883 case VAR_FLOAT:
7884 *tofree = NULL;
7885 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7886 r = numbuf;
7887 break;
7888#endif
7889
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007892 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894
Bram Moolenaar8502c702014-06-17 12:51:16 +02007895 if (--recurse == 0)
7896 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007897 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007898}
7899
7900/*
7901 * Return a string with the string representation of a variable.
7902 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7903 * "numbuf" is used for a number.
7904 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007905 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 */
7907 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007908tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 char_u **tofree;
7911 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007912 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007913{
7914 switch (tv->v_type)
7915 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 case VAR_FUNC:
7917 *tofree = string_quote(tv->vval.v_string, TRUE);
7918 return *tofree;
7919 case VAR_STRING:
7920 *tofree = string_quote(tv->vval.v_string, FALSE);
7921 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007922#ifdef FEAT_FLOAT
7923 case VAR_FLOAT:
7924 *tofree = NULL;
7925 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7926 return numbuf;
7927#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007928 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007930 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007931 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007932 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007933 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007934 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007935 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007936}
7937
7938/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 * Return string "str" in ' quotes, doubling ' characters.
7940 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 */
7943 static char_u *
7944string_quote(str, function)
7945 char_u *str;
7946 int function;
7947{
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007949 char_u *p, *r, *s;
7950
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 len = (function ? 13 : 3);
7952 if (str != NULL)
7953 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007954 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 for (p = str; *p != NUL; mb_ptr_adv(p))
7956 if (*p == '\'')
7957 ++len;
7958 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 s = r = alloc(len);
7960 if (r != NULL)
7961 {
7962 if (function)
7963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007964 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007965 r += 10;
7966 }
7967 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007969 if (str != NULL)
7970 for (p = str; *p != NUL; )
7971 {
7972 if (*p == '\'')
7973 *r++ = '\'';
7974 MB_COPY_CHAR(p, r);
7975 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977 if (function)
7978 *r++ = ')';
7979 *r++ = NUL;
7980 }
7981 return s;
7982}
7983
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007984#ifdef FEAT_FLOAT
7985/*
7986 * Convert the string "text" to a floating point number.
7987 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7988 * this always uses a decimal point.
7989 * Returns the length of the text that was consumed.
7990 */
7991 static int
7992string2float(text, value)
7993 char_u *text;
7994 float_T *value; /* result stored here */
7995{
7996 char *s = (char *)text;
7997 float_T f;
7998
7999 f = strtod(s, &s);
8000 *value = f;
8001 return (int)((char_u *)s - text);
8002}
8003#endif
8004
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 * Get the value of an environment variable.
8007 * "arg" is pointing to the '$'. It is advanced to after the name.
8008 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008009 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 */
8011 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 int evaluate;
8016{
8017 char_u *string = NULL;
8018 int len;
8019 int cc;
8020 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008021 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022
8023 ++*arg;
8024 name = *arg;
8025 len = get_env_len(arg);
8026 if (evaluate)
8027 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008028 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008029 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008030
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008031 cc = name[len];
8032 name[len] = NUL;
8033 /* first try vim_getenv(), fast for normal environment vars */
8034 string = vim_getenv(name, &mustfree);
8035 if (string != NULL && *string != NUL)
8036 {
8037 if (!mustfree)
8038 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008040 else
8041 {
8042 if (mustfree)
8043 vim_free(string);
8044
8045 /* next try expanding things like $VIM and ${HOME} */
8046 string = expand_env_save(name - 1);
8047 if (string != NULL && *string == '$')
8048 {
8049 vim_free(string);
8050 string = NULL;
8051 }
8052 }
8053 name[len] = cc;
8054
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055 rettv->v_type = VAR_STRING;
8056 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 }
8058
8059 return OK;
8060}
8061
8062/*
8063 * Array with names and number of arguments of all internal functions
8064 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8065 */
8066static struct fst
8067{
8068 char *f_name; /* function name */
8069 char f_min_argc; /* minimal number of arguments */
8070 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008072 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073} functions[] =
8074{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008077 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008079 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008080 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008081 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"append", 2, 2, f_append},
8083 {"argc", 0, 0, f_argc},
8084 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008085 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008086 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008087#ifdef FEAT_FLOAT
8088 {"asin", 1, 1, f_asin}, /* WJMc */
8089#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008090 {"assert_equal", 2, 3, f_assert_equal},
8091 {"assert_false", 1, 2, f_assert_false},
8092 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008093#ifdef FEAT_FLOAT
8094 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008095 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008098 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"bufexists", 1, 1, f_bufexists},
8100 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8101 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8102 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8103 {"buflisted", 1, 1, f_buflisted},
8104 {"bufloaded", 1, 1, f_bufloaded},
8105 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008106 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"bufwinnr", 1, 1, f_bufwinnr},
8108 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008109 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008110 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008111 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008112#ifdef FEAT_FLOAT
8113 {"ceil", 1, 1, f_ceil},
8114#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008115 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008116 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008118 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008120#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008121 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008122 {"complete_add", 1, 1, f_complete_add},
8123 {"complete_check", 0, 0, f_complete_check},
8124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008127#ifdef FEAT_FLOAT
8128 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008129 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008130#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008131 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008133 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008134 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"delete", 1, 1, f_delete},
8136 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008137 {"diff_filler", 1, 1, f_diff_filler},
8138 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008139 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008141 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"eventhandler", 0, 0, f_eventhandler},
8143 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008144 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008146#ifdef FEAT_FLOAT
8147 {"exp", 1, 1, f_exp},
8148#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008149 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008150 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008151 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8153 {"filereadable", 1, 1, f_filereadable},
8154 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008155 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008156 {"finddir", 1, 3, f_finddir},
8157 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008158#ifdef FEAT_FLOAT
8159 {"float2nr", 1, 1, f_float2nr},
8160 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008161 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008162#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008163 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"fnamemodify", 2, 2, f_fnamemodify},
8165 {"foldclosed", 1, 1, f_foldclosed},
8166 {"foldclosedend", 1, 1, f_foldclosedend},
8167 {"foldlevel", 1, 1, f_foldlevel},
8168 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008169 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008171 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008172 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008173 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008174 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008175 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"getchar", 0, 1, f_getchar},
8177 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008178 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"getcmdline", 0, 0, f_getcmdline},
8180 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008181 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008182 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008183 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008185 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008186 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"getfsize", 1, 1, f_getfsize},
8188 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008189 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008190 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008191 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008192 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008193 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008194 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008195 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008196 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008198 {"gettabvar", 2, 3, f_gettabvar},
8199 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"getwinposx", 0, 0, f_getwinposx},
8201 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008202 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008203 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008204 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008205 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008207 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008208 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008209 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8211 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8212 {"histadd", 2, 2, f_histadd},
8213 {"histdel", 1, 2, f_histdel},
8214 {"histget", 1, 2, f_histget},
8215 {"histnr", 1, 1, f_histnr},
8216 {"hlID", 1, 1, f_hlID},
8217 {"hlexists", 1, 1, f_hlexists},
8218 {"hostname", 0, 0, f_hostname},
8219 {"iconv", 3, 3, f_iconv},
8220 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008221 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008222 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008224 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"inputrestore", 0, 0, f_inputrestore},
8226 {"inputsave", 0, 0, f_inputsave},
8227 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008228 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008229 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008231 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008232 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008233 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008234 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008236 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"libcall", 3, 3, f_libcall},
8238 {"libcallnr", 3, 3, f_libcallnr},
8239 {"line", 1, 1, f_line},
8240 {"line2byte", 1, 1, f_line2byte},
8241 {"lispindent", 1, 1, f_lispindent},
8242 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008243#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008244 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008245 {"log10", 1, 1, f_log10},
8246#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008247#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008248 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008249#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008250 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008251 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008252 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008253 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008254 {"matchadd", 2, 5, f_matchadd},
8255 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008256 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008257 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008258 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008259 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008260 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008261 {"max", 1, 1, f_max},
8262 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008263#ifdef vim_mkdir
8264 {"mkdir", 1, 3, f_mkdir},
8265#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008266 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008267#ifdef FEAT_MZSCHEME
8268 {"mzeval", 1, 1, f_mzeval},
8269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008271 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008272 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008273 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008274#ifdef FEAT_FLOAT
8275 {"pow", 2, 2, f_pow},
8276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008278 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008279 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008280#ifdef FEAT_PYTHON3
8281 {"py3eval", 1, 1, f_py3eval},
8282#endif
8283#ifdef FEAT_PYTHON
8284 {"pyeval", 1, 1, f_pyeval},
8285#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008286 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008287 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008288 {"reltime", 0, 2, f_reltime},
8289 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"remote_expr", 2, 3, f_remote_expr},
8291 {"remote_foreground", 1, 1, f_remote_foreground},
8292 {"remote_peek", 1, 2, f_remote_peek},
8293 {"remote_read", 1, 1, f_remote_read},
8294 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008295 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008297 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008299 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300#ifdef FEAT_FLOAT
8301 {"round", 1, 1, f_round},
8302#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008303 {"screenattr", 2, 2, f_screenattr},
8304 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008305 {"screencol", 0, 0, f_screencol},
8306 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008307 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008308 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008309 {"searchpair", 3, 7, f_searchpair},
8310 {"searchpairpos", 3, 7, f_searchpairpos},
8311 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"server2client", 2, 2, f_server2client},
8313 {"serverlist", 0, 0, f_serverlist},
8314 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008315 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"setcmdpos", 1, 1, f_setcmdpos},
8317 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008318 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008319 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008320 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008321 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008323 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008324 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008326#ifdef FEAT_CRYPT
8327 {"sha256", 1, 1, f_sha256},
8328#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008329 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008330 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008332#ifdef FEAT_FLOAT
8333 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008334 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008335#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008336 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008337 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008338 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008339 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008340 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008341#ifdef FEAT_FLOAT
8342 {"sqrt", 1, 1, f_sqrt},
8343 {"str2float", 1, 1, f_str2float},
8344#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008345 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008346 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008347 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348#ifdef HAVE_STRFTIME
8349 {"strftime", 1, 2, f_strftime},
8350#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"strlen", 1, 1, f_strlen},
8354 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008355 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008357 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008358 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"substitute", 4, 4, f_substitute},
8360 {"synID", 3, 3, f_synID},
8361 {"synIDattr", 2, 3, f_synIDattr},
8362 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008363 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008364 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008365 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008366 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008367 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008368 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008369 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008370 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008371 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008372#ifdef FEAT_FLOAT
8373 {"tan", 1, 1, f_tan},
8374 {"tanh", 1, 1, f_tanh},
8375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008377 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"tolower", 1, 1, f_tolower},
8379 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008380 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008381#ifdef FEAT_FLOAT
8382 {"trunc", 1, 1, f_trunc},
8383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008385 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008386 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008387 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008388 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"virtcol", 1, 1, f_virtcol},
8390 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008391 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 {"winbufnr", 1, 1, f_winbufnr},
8393 {"wincol", 0, 0, f_wincol},
8394 {"winheight", 1, 1, f_winheight},
8395 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008396 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008398 {"winrestview", 1, 1, f_winrestview},
8399 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008401 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008402 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008403 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404};
8405
8406#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8407
8408/*
8409 * Function given to ExpandGeneric() to obtain the list of internal
8410 * or user defined function names.
8411 */
8412 char_u *
8413get_function_name(xp, idx)
8414 expand_T *xp;
8415 int idx;
8416{
8417 static int intidx = -1;
8418 char_u *name;
8419
8420 if (idx == 0)
8421 intidx = -1;
8422 if (intidx < 0)
8423 {
8424 name = get_user_func_name(xp, idx);
8425 if (name != NULL)
8426 return name;
8427 }
8428 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8429 {
8430 STRCPY(IObuff, functions[intidx].f_name);
8431 STRCAT(IObuff, "(");
8432 if (functions[intidx].f_max_argc == 0)
8433 STRCAT(IObuff, ")");
8434 return IObuff;
8435 }
8436
8437 return NULL;
8438}
8439
8440/*
8441 * Function given to ExpandGeneric() to obtain the list of internal or
8442 * user defined variable or function names.
8443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 char_u *
8445get_expr_name(xp, idx)
8446 expand_T *xp;
8447 int idx;
8448{
8449 static int intidx = -1;
8450 char_u *name;
8451
8452 if (idx == 0)
8453 intidx = -1;
8454 if (intidx < 0)
8455 {
8456 name = get_function_name(xp, idx);
8457 if (name != NULL)
8458 return name;
8459 }
8460 return get_user_var_name(xp, ++intidx);
8461}
8462
8463#endif /* FEAT_CMDL_COMPL */
8464
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008465#if defined(EBCDIC) || defined(PROTO)
8466/*
8467 * Compare struct fst by function name.
8468 */
8469 static int
8470compare_func_name(s1, s2)
8471 const void *s1;
8472 const void *s2;
8473{
8474 struct fst *p1 = (struct fst *)s1;
8475 struct fst *p2 = (struct fst *)s2;
8476
8477 return STRCMP(p1->f_name, p2->f_name);
8478}
8479
8480/*
8481 * Sort the function table by function name.
8482 * The sorting of the table above is ASCII dependant.
8483 * On machines using EBCDIC we have to sort it.
8484 */
8485 static void
8486sortFunctions()
8487{
8488 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8489
8490 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8491}
8492#endif
8493
8494
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495/*
8496 * Find internal function in table above.
8497 * Return index, or -1 if not found
8498 */
8499 static int
8500find_internal_func(name)
8501 char_u *name; /* name of the function */
8502{
8503 int first = 0;
8504 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8505 int cmp;
8506 int x;
8507
8508 /*
8509 * Find the function name in the table. Binary search.
8510 */
8511 while (first <= last)
8512 {
8513 x = first + ((unsigned)(last - first) >> 1);
8514 cmp = STRCMP(name, functions[x].f_name);
8515 if (cmp < 0)
8516 last = x - 1;
8517 else if (cmp > 0)
8518 first = x + 1;
8519 else
8520 return x;
8521 }
8522 return -1;
8523}
8524
8525/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8527 * name it contains, otherwise return "name".
8528 */
8529 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008530deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 char_u *name;
8532 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008533 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008534{
Bram Moolenaar33570922005-01-25 22:26:29 +00008535 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008536 int cc;
8537
8538 cc = name[*lenp];
8539 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008540 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008541 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008544 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 {
8546 *lenp = 0;
8547 return (char_u *)""; /* just in case */
8548 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008549 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 }
8552
8553 return name;
8554}
8555
8556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 * Allocate a variable for the result of a function.
8558 * Return OK or FAIL.
8559 */
8560 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8562 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 char_u *name; /* name of the function */
8564 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 char_u **arg; /* argument, pointing to the '(' */
8567 linenr_T firstline; /* first line of range */
8568 linenr_T lastline; /* last line of range */
8569 int *doesrange; /* return: function handled range */
8570 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572{
8573 char_u *argp;
8574 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008575 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 int argcount = 0; /* number of arguments found */
8577
8578 /*
8579 * Get the arguments.
8580 */
8581 argp = *arg;
8582 while (argcount < MAX_FUNC_ARGS)
8583 {
8584 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8585 if (*argp == ')' || *argp == ',' || *argp == NUL)
8586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8588 {
8589 ret = FAIL;
8590 break;
8591 }
8592 ++argcount;
8593 if (*argp != ',')
8594 break;
8595 }
8596 if (*argp == ')')
8597 ++argp;
8598 else
8599 ret = FAIL;
8600
8601 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008605 {
8606 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008607 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008608 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008609 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
8612 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614
8615 *arg = skipwhite(argp);
8616 return ret;
8617}
8618
8619
8620/*
8621 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008622 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008623 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 */
8625 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008626call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008627 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008628 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008630 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008632 typval_T *argvars; /* vars for arguments, must have "argcount"
8633 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 linenr_T firstline; /* first line of range */
8635 linenr_T lastline; /* last line of range */
8636 int *doesrange; /* return: function handled range */
8637 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639{
8640 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641#define ERROR_UNKNOWN 0
8642#define ERROR_TOOMANY 1
8643#define ERROR_TOOFEW 2
8644#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008645#define ERROR_DICT 4
8646#define ERROR_NONE 5
8647#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 int error = ERROR_NONE;
8649 int i;
8650 int llen;
8651 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652#define FLEN_FIXED 40
8653 char_u fname_buf[FLEN_FIXED + 1];
8654 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008655 char_u *name;
8656
8657 /* Make a copy of the name, if it comes from a funcref variable it could
8658 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008659 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008660 if (name == NULL)
8661 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
8663 /*
8664 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8665 * Change <SNR>123_name() to K_SNR 123_name().
8666 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 llen = eval_fname_script(name);
8669 if (llen > 0)
8670 {
8671 fname_buf[0] = K_SPECIAL;
8672 fname_buf[1] = KS_EXTRA;
8673 fname_buf[2] = (int)KE_SNR;
8674 i = 3;
8675 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8676 {
8677 if (current_SID <= 0)
8678 error = ERROR_SCRIPT;
8679 else
8680 {
8681 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8682 i = (int)STRLEN(fname_buf);
8683 }
8684 }
8685 if (i + STRLEN(name + llen) < FLEN_FIXED)
8686 {
8687 STRCPY(fname_buf + i, name + llen);
8688 fname = fname_buf;
8689 }
8690 else
8691 {
8692 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8693 if (fname == NULL)
8694 error = ERROR_OTHER;
8695 else
8696 {
8697 mch_memmove(fname, fname_buf, (size_t)i);
8698 STRCPY(fname + i, name + llen);
8699 }
8700 }
8701 }
8702 else
8703 fname = name;
8704
8705 *doesrange = FALSE;
8706
8707
8708 /* execute the function if no errors detected and executing */
8709 if (evaluate && error == ERROR_NONE)
8710 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008711 char_u *rfname = fname;
8712
8713 /* Ignore "g:" before a function name. */
8714 if (fname[0] == 'g' && fname[1] == ':')
8715 rfname = fname + 2;
8716
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008717 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8718 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 error = ERROR_UNKNOWN;
8720
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008721 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {
8723 /*
8724 * User defined function.
8725 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008726 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008727
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008729 /* Trigger FuncUndefined event, may load the function. */
8730 if (fp == NULL
8731 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008732 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008733 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008735 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008736 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 }
8738#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008739 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008740 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008741 {
8742 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008743 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008744 }
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 if (fp != NULL)
8747 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008748 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008750 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008752 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008754 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008755 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 else
8757 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008758 int did_save_redo = FALSE;
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 /*
8761 * Call the user function.
8762 * Save and restore search patterns, script variables and
8763 * redo buffer.
8764 */
8765 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008766#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008767 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008768#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008769 {
8770 saveRedobuff();
8771 did_save_redo = TRUE;
8772 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008773 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008775 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008776 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8777 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8778 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008779 /* Function was unreferenced while being used, free it
8780 * now. */
8781 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008782 if (did_save_redo)
8783 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 restore_search_patterns();
8785 error = ERROR_NONE;
8786 }
8787 }
8788 }
8789 else
8790 {
8791 /*
8792 * Find the function name in the table, call its implementation.
8793 */
8794 i = find_internal_func(fname);
8795 if (i >= 0)
8796 {
8797 if (argcount < functions[i].f_min_argc)
8798 error = ERROR_TOOFEW;
8799 else if (argcount > functions[i].f_max_argc)
8800 error = ERROR_TOOMANY;
8801 else
8802 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008803 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008804 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 error = ERROR_NONE;
8806 }
8807 }
8808 }
8809 /*
8810 * The function call (or "FuncUndefined" autocommand sequence) might
8811 * have been aborted by an error, an interrupt, or an explicitly thrown
8812 * exception that has not been caught so far. This situation can be
8813 * tested for by calling aborting(). For an error in an internal
8814 * function or for the "E132" error in call_user_func(), however, the
8815 * throw point at which the "force_abort" flag (temporarily reset by
8816 * emsg()) is normally updated has not been reached yet. We need to
8817 * update that flag first to make aborting() reliable.
8818 */
8819 update_force_abort();
8820 }
8821 if (error == ERROR_NONE)
8822 ret = OK;
8823
8824 /*
8825 * Report an error unless the argument evaluation or function call has been
8826 * cancelled due to an aborting error, an interrupt, or an exception.
8827 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008828 if (!aborting())
8829 {
8830 switch (error)
8831 {
8832 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008833 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008834 break;
8835 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008836 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008837 break;
8838 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008839 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008840 name);
8841 break;
8842 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008843 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008844 name);
8845 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008846 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008847 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 name);
8849 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008850 }
8851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 if (fname != name && fname != fname_buf)
8854 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008855 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
8857 return ret;
8858}
8859
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008860/*
8861 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008862 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008863 */
8864 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008865emsg_funcname(ermsg, name)
8866 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008867 char_u *name;
8868{
8869 char_u *p;
8870
8871 if (*name == K_SPECIAL)
8872 p = concat_str((char_u *)"<SNR>", name + 3);
8873 else
8874 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008875 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008876 if (p != name)
8877 vim_free(p);
8878}
8879
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008880/*
8881 * Return TRUE for a non-zero Number and a non-empty String.
8882 */
8883 static int
8884non_zero_arg(argvars)
8885 typval_T *argvars;
8886{
8887 return ((argvars[0].v_type == VAR_NUMBER
8888 && argvars[0].vval.v_number != 0)
8889 || (argvars[0].v_type == VAR_STRING
8890 && argvars[0].vval.v_string != NULL
8891 && *argvars[0].vval.v_string != NUL));
8892}
8893
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894/*********************************************
8895 * Implementation of the built-in functions
8896 */
8897
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008898#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008899static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8900
8901/*
8902 * Get the float value of "argvars[0]" into "f".
8903 * Returns FAIL when the argument is not a Number or Float.
8904 */
8905 static int
8906get_float_arg(argvars, f)
8907 typval_T *argvars;
8908 float_T *f;
8909{
8910 if (argvars[0].v_type == VAR_FLOAT)
8911 {
8912 *f = argvars[0].vval.v_float;
8913 return OK;
8914 }
8915 if (argvars[0].v_type == VAR_NUMBER)
8916 {
8917 *f = (float_T)argvars[0].vval.v_number;
8918 return OK;
8919 }
8920 EMSG(_("E808: Number or Float required"));
8921 return FAIL;
8922}
8923
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924/*
8925 * "abs(expr)" function
8926 */
8927 static void
8928f_abs(argvars, rettv)
8929 typval_T *argvars;
8930 typval_T *rettv;
8931{
8932 if (argvars[0].v_type == VAR_FLOAT)
8933 {
8934 rettv->v_type = VAR_FLOAT;
8935 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8936 }
8937 else
8938 {
8939 varnumber_T n;
8940 int error = FALSE;
8941
8942 n = get_tv_number_chk(&argvars[0], &error);
8943 if (error)
8944 rettv->vval.v_number = -1;
8945 else if (n > 0)
8946 rettv->vval.v_number = n;
8947 else
8948 rettv->vval.v_number = -n;
8949 }
8950}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008951
8952/*
8953 * "acos()" function
8954 */
8955 static void
8956f_acos(argvars, rettv)
8957 typval_T *argvars;
8958 typval_T *rettv;
8959{
8960 float_T f;
8961
8962 rettv->v_type = VAR_FLOAT;
8963 if (get_float_arg(argvars, &f) == OK)
8964 rettv->vval.v_float = acos(f);
8965 else
8966 rettv->vval.v_float = 0.0;
8967}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008968#endif
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008971 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 */
8973 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008974f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008975 typval_T *argvars;
8976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008981 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008983 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008984 && !tv_check_lock(l->lv_lock,
8985 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008986 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008987 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008988 }
8989 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008990 EMSG(_(e_listreq));
8991}
8992
8993/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008994 * "alloc_fail(id, countdown, repeat)" function
8995 */
8996 static void
8997f_alloc_fail(argvars, rettv)
8998 typval_T *argvars;
8999 typval_T *rettv UNUSED;
9000{
9001 if (argvars[0].v_type != VAR_NUMBER
9002 || argvars[0].vval.v_number <= 0
9003 || argvars[1].v_type != VAR_NUMBER
9004 || argvars[1].vval.v_number < 0
9005 || argvars[2].v_type != VAR_NUMBER)
9006 EMSG(_(e_invarg));
9007 else
9008 {
9009 alloc_fail_id = argvars[0].vval.v_number;
9010 alloc_fail_countdown = argvars[1].vval.v_number;
9011 alloc_fail_repeat = argvars[2].vval.v_number;
9012 }
9013}
9014
9015/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009016 * "and(expr, expr)" function
9017 */
9018 static void
9019f_and(argvars, rettv)
9020 typval_T *argvars;
9021 typval_T *rettv;
9022{
9023 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9024 & get_tv_number_chk(&argvars[1], NULL);
9025}
9026
9027/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009028 * "append(lnum, string/list)" function
9029 */
9030 static void
9031f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009032 typval_T *argvars;
9033 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009034{
9035 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009036 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 list_T *l = NULL;
9038 listitem_T *li = NULL;
9039 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009040 long added = 0;
9041
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009042 /* When coming here from Insert mode, sync undo, so that this can be
9043 * undone separately from what was previously inserted. */
9044 if (u_sync_once == 2)
9045 {
9046 u_sync_once = 1; /* notify that u_sync() was called */
9047 u_sync(TRUE);
9048 }
9049
Bram Moolenaar0d660222005-01-07 21:51:51 +00009050 lnum = get_tv_lnum(argvars);
9051 if (lnum >= 0
9052 && lnum <= curbuf->b_ml.ml_line_count
9053 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009054 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009055 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009056 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 l = argvars[1].vval.v_list;
9058 if (l == NULL)
9059 return;
9060 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009061 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009062 for (;;)
9063 {
9064 if (l == NULL)
9065 tv = &argvars[1]; /* append a string */
9066 else if (li == NULL)
9067 break; /* end of list */
9068 else
9069 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009070 line = get_tv_string_chk(tv);
9071 if (line == NULL) /* type error */
9072 {
9073 rettv->vval.v_number = 1; /* Failed */
9074 break;
9075 }
9076 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009077 ++added;
9078 if (l == NULL)
9079 break;
9080 li = li->li_next;
9081 }
9082
9083 appended_lines_mark(lnum, added);
9084 if (curwin->w_cursor.lnum > lnum)
9085 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009087 else
9088 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089}
9090
9091/*
9092 * "argc()" function
9093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009096 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100}
9101
9102/*
9103 * "argidx()" function
9104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009107 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111}
9112
9113/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009114 * "arglistid()" function
9115 */
9116 static void
9117f_arglistid(argvars, rettv)
9118 typval_T *argvars UNUSED;
9119 typval_T *rettv;
9120{
9121 win_T *wp;
9122 tabpage_T *tp = NULL;
9123 long n;
9124
9125 rettv->vval.v_number = -1;
9126 if (argvars[0].v_type != VAR_UNKNOWN)
9127 {
9128 if (argvars[1].v_type != VAR_UNKNOWN)
9129 {
9130 n = get_tv_number(&argvars[1]);
9131 if (n >= 0)
9132 tp = find_tabpage(n);
9133 }
9134 else
9135 tp = curtab;
9136
9137 if (tp != NULL)
9138 {
9139 wp = find_win_by_nr(&argvars[0], tp);
9140 if (wp != NULL)
9141 rettv->vval.v_number = wp->w_alist->id;
9142 }
9143 }
9144 else
9145 rettv->vval.v_number = curwin->w_alist->id;
9146}
9147
9148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 * "argv(nr)" function
9150 */
9151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 typval_T *argvars;
9154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
9156 int idx;
9157
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009158 if (argvars[0].v_type != VAR_UNKNOWN)
9159 {
9160 idx = get_tv_number_chk(&argvars[0], NULL);
9161 if (idx >= 0 && idx < ARGCOUNT)
9162 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9163 else
9164 rettv->vval.v_string = NULL;
9165 rettv->v_type = VAR_STRING;
9166 }
9167 else if (rettv_list_alloc(rettv) == OK)
9168 for (idx = 0; idx < ARGCOUNT; ++idx)
9169 list_append_string(rettv->vval.v_list,
9170 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171}
9172
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009173static void prepare_assert_error __ARGS((garray_T*gap));
9174static void fill_assert_error __ARGS((garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv));
9175static void assert_error __ARGS((garray_T *gap));
9176static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9177
9178/*
9179 * Prepare "gap" for an assert error and add the sourcing position.
9180 */
9181 static void
9182prepare_assert_error(gap)
9183 garray_T *gap;
9184{
9185 char buf[NUMBUFLEN];
9186
9187 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009188 if (sourcing_name != NULL)
9189 {
9190 ga_concat(gap, sourcing_name);
9191 if (sourcing_lnum > 0)
9192 ga_concat(gap, (char_u *)" ");
9193 }
9194 if (sourcing_lnum > 0)
9195 {
9196 sprintf(buf, "line %ld", (long)sourcing_lnum);
9197 ga_concat(gap, (char_u *)buf);
9198 }
9199 if (sourcing_name != NULL || sourcing_lnum > 0)
9200 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009201}
9202
9203/*
9204 * Fill "gap" with information about an assert error.
9205 */
9206 static void
9207fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9208 garray_T *gap;
9209 typval_T *opt_msg_tv;
9210 char_u *exp_str;
9211 typval_T *exp_tv;
9212 typval_T *got_tv;
9213{
9214 char_u numbuf[NUMBUFLEN];
9215 char_u *tofree;
9216
9217 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9218 {
9219 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9220 vim_free(tofree);
9221 }
9222 else
9223 {
9224 ga_concat(gap, (char_u *)"Expected ");
9225 if (exp_str == NULL)
9226 {
9227 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9228 vim_free(tofree);
9229 }
9230 else
9231 ga_concat(gap, exp_str);
9232 ga_concat(gap, (char_u *)" but got ");
9233 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9234 vim_free(tofree);
9235 }
9236}
Bram Moolenaar43345542015-11-29 17:35:35 +01009237
9238/*
9239 * Add an assert error to v:errors.
9240 */
9241 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009242assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009243 garray_T *gap;
9244{
9245 struct vimvar *vp = &vimvars[VV_ERRORS];
9246
9247 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9248 /* Make sure v:errors is a list. */
9249 set_vim_var_list(VV_ERRORS, list_alloc());
9250 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9251}
9252
Bram Moolenaar43345542015-11-29 17:35:35 +01009253/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009254 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009255 */
9256 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009257f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009258 typval_T *argvars;
9259 typval_T *rettv UNUSED;
9260{
9261 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009262
9263 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9264 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009265 prepare_assert_error(&ga);
9266 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9267 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009268 ga_clear(&ga);
9269 }
9270}
9271
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009272/*
9273 * Common for assert_true() and assert_false().
9274 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009275 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009276assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009277 typval_T *argvars;
9278 int isTrue;
9279{
9280 int error = FALSE;
9281 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009282
9283 if (argvars[0].v_type != VAR_NUMBER
9284 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9285 || error)
9286 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009287 prepare_assert_error(&ga);
9288 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009289 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009290 NULL, &argvars[0]);
9291 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009292 ga_clear(&ga);
9293 }
9294}
9295
9296/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009297 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009298 */
9299 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009300f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009301 typval_T *argvars;
9302 typval_T *rettv UNUSED;
9303{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009304 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009305}
9306
9307/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009308 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009309 */
9310 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009311f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009312 typval_T *argvars;
9313 typval_T *rettv UNUSED;
9314{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009316}
9317
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009318#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009319/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009320 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009321 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009322 static void
9323f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009324 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009325 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009326{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009327 float_T f;
9328
9329 rettv->v_type = VAR_FLOAT;
9330 if (get_float_arg(argvars, &f) == OK)
9331 rettv->vval.v_float = asin(f);
9332 else
9333 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009334}
9335
9336/*
9337 * "atan()" function
9338 */
9339 static void
9340f_atan(argvars, rettv)
9341 typval_T *argvars;
9342 typval_T *rettv;
9343{
9344 float_T f;
9345
9346 rettv->v_type = VAR_FLOAT;
9347 if (get_float_arg(argvars, &f) == OK)
9348 rettv->vval.v_float = atan(f);
9349 else
9350 rettv->vval.v_float = 0.0;
9351}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009352
9353/*
9354 * "atan2()" function
9355 */
9356 static void
9357f_atan2(argvars, rettv)
9358 typval_T *argvars;
9359 typval_T *rettv;
9360{
9361 float_T fx, fy;
9362
9363 rettv->v_type = VAR_FLOAT;
9364 if (get_float_arg(argvars, &fx) == OK
9365 && get_float_arg(&argvars[1], &fy) == OK)
9366 rettv->vval.v_float = atan2(fx, fy);
9367 else
9368 rettv->vval.v_float = 0.0;
9369}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009370#endif
9371
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372/*
9373 * "browse(save, title, initdir, default)" function
9374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009376f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009377 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380#ifdef FEAT_BROWSE
9381 int save;
9382 char_u *title;
9383 char_u *initdir;
9384 char_u *defname;
9385 char_u buf[NUMBUFLEN];
9386 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009387 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009389 save = get_tv_number_chk(&argvars[0], &error);
9390 title = get_tv_string_chk(&argvars[1]);
9391 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9392 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009394 if (error || title == NULL || initdir == NULL || defname == NULL)
9395 rettv->vval.v_string = NULL;
9396 else
9397 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009398 do_browse(save ? BROWSE_SAVE : 0,
9399 title, defname, NULL, initdir, NULL, curbuf);
9400#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009402#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009403 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009404}
9405
9406/*
9407 * "browsedir(title, initdir)" function
9408 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009411 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009412 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009413{
9414#ifdef FEAT_BROWSE
9415 char_u *title;
9416 char_u *initdir;
9417 char_u buf[NUMBUFLEN];
9418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419 title = get_tv_string_chk(&argvars[0]);
9420 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009422 if (title == NULL || initdir == NULL)
9423 rettv->vval.v_string = NULL;
9424 else
9425 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009426 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431}
9432
Bram Moolenaar33570922005-01-25 22:26:29 +00009433static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009434
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435/*
9436 * Find a buffer by number or exact name.
9437 */
9438 static buf_T *
9439find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009440 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009444 if (avar->v_type == VAR_NUMBER)
9445 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009446 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009448 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009449 if (buf == NULL)
9450 {
9451 /* No full path name match, try a match with a URL or a "nofile"
9452 * buffer, these don't use the full path. */
9453 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9454 if (buf->b_fname != NULL
9455 && (path_with_url(buf->b_fname)
9456#ifdef FEAT_QUICKFIX
9457 || bt_nofile(buf)
9458#endif
9459 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009460 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009461 break;
9462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 }
9464 return buf;
9465}
9466
9467/*
9468 * "bufexists(expr)" function
9469 */
9470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009472 typval_T *argvars;
9473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478/*
9479 * "buflisted(expr)" function
9480 */
9481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009483 typval_T *argvars;
9484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
9486 buf_T *buf;
9487
9488 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490}
9491
9492/*
9493 * "bufloaded(expr)" function
9494 */
9495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009497 typval_T *argvars;
9498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
9500 buf_T *buf;
9501
9502 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504}
9505
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009506static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009507
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508/*
9509 * Get buffer by number or pattern.
9510 */
9511 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009512get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009514 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 int save_magic;
9518 char_u *save_cpo;
9519 buf_T *buf;
9520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 if (tv->v_type == VAR_NUMBER)
9522 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009523 if (tv->v_type != VAR_STRING)
9524 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 if (name == NULL || *name == NUL)
9526 return curbuf;
9527 if (name[0] == '$' && name[1] == NUL)
9528 return lastbuf;
9529
9530 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9531 save_magic = p_magic;
9532 p_magic = TRUE;
9533 save_cpo = p_cpo;
9534 p_cpo = (char_u *)"";
9535
9536 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009537 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538
9539 p_magic = save_magic;
9540 p_cpo = save_cpo;
9541
9542 /* If not found, try expanding the name, like done for bufexists(). */
9543 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546 return buf;
9547}
9548
9549/*
9550 * "bufname(expr)" function
9551 */
9552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009554 typval_T *argvars;
9555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556{
9557 buf_T *buf;
9558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009561 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 --emsg_off;
9568}
9569
9570/*
9571 * "bufnr(expr)" function
9572 */
9573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009575 typval_T *argvars;
9576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009579 int error = FALSE;
9580 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009584 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009585 --emsg_off;
9586
9587 /* If the buffer isn't found and the second argument is not zero create a
9588 * new buffer. */
9589 if (buf == NULL
9590 && argvars[1].v_type != VAR_UNKNOWN
9591 && get_tv_number_chk(&argvars[1], &error) != 0
9592 && !error
9593 && (name = get_tv_string_chk(&argvars[0])) != NULL
9594 && !error)
9595 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9596
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601}
9602
9603/*
9604 * "bufwinnr(nr)" function
9605 */
9606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009607f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009608 typval_T *argvars;
9609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611#ifdef FEAT_WINDOWS
9612 win_T *wp;
9613 int winnr = 0;
9614#endif
9615 buf_T *buf;
9616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009617 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009619 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620#ifdef FEAT_WINDOWS
9621 for (wp = firstwin; wp; wp = wp->w_next)
9622 {
9623 ++winnr;
9624 if (wp->w_buffer == buf)
9625 break;
9626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630#endif
9631 --emsg_off;
9632}
9633
9634/*
9635 * "byte2line(byte)" function
9636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644#else
9645 long boff = 0;
9646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009647 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 (linenr_T)0, &boff);
9653#endif
9654}
9655
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009656 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009657byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009658 typval_T *argvars;
9659 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009660 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009661{
9662#ifdef FEAT_MBYTE
9663 char_u *t;
9664#endif
9665 char_u *str;
9666 long idx;
9667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009668 str = get_tv_string_chk(&argvars[0]);
9669 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009671 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009672 return;
9673
9674#ifdef FEAT_MBYTE
9675 t = str;
9676 for ( ; idx > 0; idx--)
9677 {
9678 if (*t == NUL) /* EOL reached */
9679 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009680 if (enc_utf8 && comp)
9681 t += utf_ptr2len(t);
9682 else
9683 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009684 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009685 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009686#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009687 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009689#endif
9690}
9691
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009692/*
9693 * "byteidx()" function
9694 */
9695 static void
9696f_byteidx(argvars, rettv)
9697 typval_T *argvars;
9698 typval_T *rettv;
9699{
9700 byteidx(argvars, rettv, FALSE);
9701}
9702
9703/*
9704 * "byteidxcomp()" function
9705 */
9706 static void
9707f_byteidxcomp(argvars, rettv)
9708 typval_T *argvars;
9709 typval_T *rettv;
9710{
9711 byteidx(argvars, rettv, TRUE);
9712}
9713
Bram Moolenaardb913952012-06-29 12:54:53 +02009714 int
9715func_call(name, args, selfdict, rettv)
9716 char_u *name;
9717 typval_T *args;
9718 dict_T *selfdict;
9719 typval_T *rettv;
9720{
9721 listitem_T *item;
9722 typval_T argv[MAX_FUNC_ARGS + 1];
9723 int argc = 0;
9724 int dummy;
9725 int r = 0;
9726
9727 for (item = args->vval.v_list->lv_first; item != NULL;
9728 item = item->li_next)
9729 {
9730 if (argc == MAX_FUNC_ARGS)
9731 {
9732 EMSG(_("E699: Too many arguments"));
9733 break;
9734 }
9735 /* Make a copy of each argument. This is needed to be able to set
9736 * v_lock to VAR_FIXED in the copy without changing the original list.
9737 */
9738 copy_tv(&item->li_tv, &argv[argc++]);
9739 }
9740
9741 if (item == NULL)
9742 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9743 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9744 &dummy, TRUE, selfdict);
9745
9746 /* Free the arguments. */
9747 while (argc > 0)
9748 clear_tv(&argv[--argc]);
9749
9750 return r;
9751}
9752
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009753/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009754 * "call(func, arglist)" function
9755 */
9756 static void
9757f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009758 typval_T *argvars;
9759 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009760{
9761 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009762 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009763
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009764 if (argvars[1].v_type != VAR_LIST)
9765 {
9766 EMSG(_(e_listreq));
9767 return;
9768 }
9769 if (argvars[1].vval.v_list == NULL)
9770 return;
9771
9772 if (argvars[0].v_type == VAR_FUNC)
9773 func = argvars[0].vval.v_string;
9774 else
9775 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009776 if (*func == NUL)
9777 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009778
Bram Moolenaare9a41262005-01-15 22:18:47 +00009779 if (argvars[2].v_type != VAR_UNKNOWN)
9780 {
9781 if (argvars[2].v_type != VAR_DICT)
9782 {
9783 EMSG(_(e_dictreq));
9784 return;
9785 }
9786 selfdict = argvars[2].vval.v_dict;
9787 }
9788
Bram Moolenaardb913952012-06-29 12:54:53 +02009789 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009790}
9791
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009792#ifdef FEAT_FLOAT
9793/*
9794 * "ceil({float})" function
9795 */
9796 static void
9797f_ceil(argvars, rettv)
9798 typval_T *argvars;
9799 typval_T *rettv;
9800{
9801 float_T f;
9802
9803 rettv->v_type = VAR_FLOAT;
9804 if (get_float_arg(argvars, &f) == OK)
9805 rettv->vval.v_float = ceil(f);
9806 else
9807 rettv->vval.v_float = 0.0;
9808}
9809#endif
9810
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009811/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009812 * "changenr()" function
9813 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009814 static void
9815f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009816 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009817 typval_T *rettv;
9818{
9819 rettv->vval.v_number = curbuf->b_u_seq_cur;
9820}
9821
9822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 * "char2nr(string)" function
9824 */
9825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009827 typval_T *argvars;
9828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830#ifdef FEAT_MBYTE
9831 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009832 {
9833 int utf8 = 0;
9834
9835 if (argvars[1].v_type != VAR_UNKNOWN)
9836 utf8 = get_tv_number_chk(&argvars[1], NULL);
9837
9838 if (utf8)
9839 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9840 else
9841 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 else
9844#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846}
9847
9848/*
9849 * "cindent(lnum)" function
9850 */
9851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855{
9856#ifdef FEAT_CINDENT
9857 pos_T pos;
9858 linenr_T lnum;
9859
9860 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9863 {
9864 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 curwin->w_cursor = pos;
9867 }
9868 else
9869#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871}
9872
9873/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009874 * "clearmatches()" function
9875 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009876 static void
9877f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009878 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009879 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009880{
9881#ifdef FEAT_SEARCH_EXTRA
9882 clear_matches(curwin);
9883#endif
9884}
9885
9886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 * "col(string)" function
9888 */
9889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009891 typval_T *argvars;
9892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
9894 colnr_T col = 0;
9895 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009896 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009898 fp = var2fpos(&argvars[0], FALSE, &fnum);
9899 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 {
9901 if (fp->col == MAXCOL)
9902 {
9903 /* '> can be MAXCOL, get the length of the line then */
9904 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009905 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 else
9907 col = MAXCOL;
9908 }
9909 else
9910 {
9911 col = fp->col + 1;
9912#ifdef FEAT_VIRTUALEDIT
9913 /* col(".") when the cursor is on the NUL at the end of the line
9914 * because of "coladd" can be seen as an extra column. */
9915 if (virtual_active() && fp == &curwin->w_cursor)
9916 {
9917 char_u *p = ml_get_cursor();
9918
9919 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9920 curwin->w_virtcol - curwin->w_cursor.coladd))
9921 {
9922# ifdef FEAT_MBYTE
9923 int l;
9924
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009925 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 col += l;
9927# else
9928 if (*p != NUL && p[1] == NUL)
9929 ++col;
9930# endif
9931 }
9932 }
9933#endif
9934 }
9935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937}
9938
Bram Moolenaar572cb562005-08-05 21:35:02 +00009939#if defined(FEAT_INS_EXPAND)
9940/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009941 * "complete()" function
9942 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009943 static void
9944f_complete(argvars, rettv)
9945 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009946 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009947{
9948 int startcol;
9949
9950 if ((State & INSERT) == 0)
9951 {
9952 EMSG(_("E785: complete() can only be used in Insert mode"));
9953 return;
9954 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009955
9956 /* Check for undo allowed here, because if something was already inserted
9957 * the line was already saved for undo and this check isn't done. */
9958 if (!undo_allowed())
9959 return;
9960
Bram Moolenaarade00832006-03-10 21:46:58 +00009961 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9962 {
9963 EMSG(_(e_invarg));
9964 return;
9965 }
9966
9967 startcol = get_tv_number_chk(&argvars[0], NULL);
9968 if (startcol <= 0)
9969 return;
9970
9971 set_completion(startcol - 1, argvars[1].vval.v_list);
9972}
9973
9974/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009975 * "complete_add()" function
9976 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009977 static void
9978f_complete_add(argvars, rettv)
9979 typval_T *argvars;
9980 typval_T *rettv;
9981{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009982 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009983}
9984
9985/*
9986 * "complete_check()" function
9987 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009988 static void
9989f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009990 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009991 typval_T *rettv;
9992{
9993 int saved = RedrawingDisabled;
9994
9995 RedrawingDisabled = 0;
9996 ins_compl_check_keys(0);
9997 rettv->vval.v_number = compl_interrupted;
9998 RedrawingDisabled = saved;
9999}
10000#endif
10001
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002/*
10003 * "confirm(message, buttons[, default [, type]])" function
10004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010007 typval_T *argvars UNUSED;
10008 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
10010#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10011 char_u *message;
10012 char_u *buttons = NULL;
10013 char_u buf[NUMBUFLEN];
10014 char_u buf2[NUMBUFLEN];
10015 int def = 1;
10016 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010017 char_u *typestr;
10018 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 message = get_tv_string_chk(&argvars[0]);
10021 if (message == NULL)
10022 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10026 if (buttons == NULL)
10027 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010028 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010030 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010031 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10034 if (typestr == NULL)
10035 error = TRUE;
10036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010038 switch (TOUPPER_ASC(*typestr))
10039 {
10040 case 'E': type = VIM_ERROR; break;
10041 case 'Q': type = VIM_QUESTION; break;
10042 case 'I': type = VIM_INFO; break;
10043 case 'W': type = VIM_WARNING; break;
10044 case 'G': type = VIM_GENERIC; break;
10045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 }
10047 }
10048 }
10049 }
10050
10051 if (buttons == NULL || *buttons == NUL)
10052 buttons = (char_u *)_("&Ok");
10053
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010054 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010055 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010056 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057#endif
10058}
10059
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010060/*
10061 * "copy()" function
10062 */
10063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010065 typval_T *argvars;
10066 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010067{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010068 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010069}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010071#ifdef FEAT_FLOAT
10072/*
10073 * "cos()" function
10074 */
10075 static void
10076f_cos(argvars, rettv)
10077 typval_T *argvars;
10078 typval_T *rettv;
10079{
10080 float_T f;
10081
10082 rettv->v_type = VAR_FLOAT;
10083 if (get_float_arg(argvars, &f) == OK)
10084 rettv->vval.v_float = cos(f);
10085 else
10086 rettv->vval.v_float = 0.0;
10087}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010088
10089/*
10090 * "cosh()" function
10091 */
10092 static void
10093f_cosh(argvars, rettv)
10094 typval_T *argvars;
10095 typval_T *rettv;
10096{
10097 float_T f;
10098
10099 rettv->v_type = VAR_FLOAT;
10100 if (get_float_arg(argvars, &f) == OK)
10101 rettv->vval.v_float = cosh(f);
10102 else
10103 rettv->vval.v_float = 0.0;
10104}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010105#endif
10106
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010108 * "count()" function
10109 */
10110 static void
10111f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010112 typval_T *argvars;
10113 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010114{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010115 long n = 0;
10116 int ic = FALSE;
10117
Bram Moolenaare9a41262005-01-15 22:18:47 +000010118 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010119 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010120 listitem_T *li;
10121 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010122 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010123
Bram Moolenaare9a41262005-01-15 22:18:47 +000010124 if ((l = argvars[0].vval.v_list) != NULL)
10125 {
10126 li = l->lv_first;
10127 if (argvars[2].v_type != VAR_UNKNOWN)
10128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 int error = FALSE;
10130
10131 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010132 if (argvars[3].v_type != VAR_UNKNOWN)
10133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 idx = get_tv_number_chk(&argvars[3], &error);
10135 if (!error)
10136 {
10137 li = list_find(l, idx);
10138 if (li == NULL)
10139 EMSGN(_(e_listidx), idx);
10140 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 if (error)
10143 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010144 }
10145
10146 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010147 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010148 ++n;
10149 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010150 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010151 else if (argvars[0].v_type == VAR_DICT)
10152 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010153 int todo;
10154 dict_T *d;
10155 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010156
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010157 if ((d = argvars[0].vval.v_dict) != NULL)
10158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 int error = FALSE;
10160
Bram Moolenaare9a41262005-01-15 22:18:47 +000010161 if (argvars[2].v_type != VAR_UNKNOWN)
10162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010163 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010164 if (argvars[3].v_type != VAR_UNKNOWN)
10165 EMSG(_(e_invarg));
10166 }
10167
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010168 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010169 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010170 {
10171 if (!HASHITEM_EMPTY(hi))
10172 {
10173 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010174 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010175 ++n;
10176 }
10177 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 }
10179 }
10180 else
10181 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010182 rettv->vval.v_number = n;
10183}
10184
10185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10187 *
10188 * Checks the existence of a cscope connection.
10189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010192 typval_T *argvars UNUSED;
10193 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194{
10195#ifdef FEAT_CSCOPE
10196 int num = 0;
10197 char_u *dbpath = NULL;
10198 char_u *prepend = NULL;
10199 char_u buf[NUMBUFLEN];
10200
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010201 if (argvars[0].v_type != VAR_UNKNOWN
10202 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010204 num = (int)get_tv_number(&argvars[0]);
10205 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010206 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 }
10209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211#endif
10212}
10213
10214/*
10215 * "cursor(lnum, col)" function
10216 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010217 * Moves the cursor to the specified line and column.
10218 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 typval_T *argvars;
10223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224{
10225 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010226#ifdef FEAT_VIRTUALEDIT
10227 long coladd = 0;
10228#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010229 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010231 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010232 if (argvars[1].v_type == VAR_UNKNOWN)
10233 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010234 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010235 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010236
Bram Moolenaar493c1782014-05-28 14:34:46 +020010237 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010238 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010239 line = pos.lnum;
10240 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010241#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010242 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010243#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010244 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010245 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010246 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010247 set_curswant = FALSE;
10248 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010249 }
10250 else
10251 {
10252 line = get_tv_lnum(argvars);
10253 col = get_tv_number_chk(&argvars[1], NULL);
10254#ifdef FEAT_VIRTUALEDIT
10255 if (argvars[2].v_type != VAR_UNKNOWN)
10256 coladd = get_tv_number_chk(&argvars[2], NULL);
10257#endif
10258 }
10259 if (line < 0 || col < 0
10260#ifdef FEAT_VIRTUALEDIT
10261 || coladd < 0
10262#endif
10263 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010264 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 if (line > 0)
10266 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 if (col > 0)
10268 curwin->w_cursor.col = col - 1;
10269#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010270 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271#endif
10272
10273 /* Make sure the cursor is in a valid position. */
10274 check_cursor();
10275#ifdef FEAT_MBYTE
10276 /* Correct cursor for multi-byte character. */
10277 if (has_mbyte)
10278 mb_adjust_cursor();
10279#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010280
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010281 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010282 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283}
10284
10285/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010286 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 */
10288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010289f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010290 typval_T *argvars;
10291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010293 int noref = 0;
10294
10295 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010297 if (noref < 0 || noref > 1)
10298 EMSG(_(e_invarg));
10299 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010300 {
10301 current_copyID += COPYID_INC;
10302 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304}
10305
10306/*
10307 * "delete()" function
10308 */
10309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010311 typval_T *argvars;
10312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313{
10314 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318}
10319
10320/*
10321 * "did_filetype()" function
10322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010325 typval_T *argvars UNUSED;
10326 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327{
10328#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330#endif
10331}
10332
10333/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010334 * "diff_filler()" function
10335 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010337f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010338 typval_T *argvars UNUSED;
10339 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010340{
10341#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010342 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010343#endif
10344}
10345
10346/*
10347 * "diff_hlID()" function
10348 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010351 typval_T *argvars UNUSED;
10352 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010353{
10354#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010356 static linenr_T prev_lnum = 0;
10357 static int changedtick = 0;
10358 static int fnum = 0;
10359 static int change_start = 0;
10360 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010361 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010362 int filler_lines;
10363 int col;
10364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010365 if (lnum < 0) /* ignore type error in {lnum} arg */
10366 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010367 if (lnum != prev_lnum
10368 || changedtick != curbuf->b_changedtick
10369 || fnum != curbuf->b_fnum)
10370 {
10371 /* New line, buffer, change: need to get the values. */
10372 filler_lines = diff_check(curwin, lnum);
10373 if (filler_lines < 0)
10374 {
10375 if (filler_lines == -1)
10376 {
10377 change_start = MAXCOL;
10378 change_end = -1;
10379 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10380 hlID = HLF_ADD; /* added line */
10381 else
10382 hlID = HLF_CHD; /* changed line */
10383 }
10384 else
10385 hlID = HLF_ADD; /* added line */
10386 }
10387 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010388 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010389 prev_lnum = lnum;
10390 changedtick = curbuf->b_changedtick;
10391 fnum = curbuf->b_fnum;
10392 }
10393
10394 if (hlID == HLF_CHD || hlID == HLF_TXD)
10395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010396 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010397 if (col >= change_start && col <= change_end)
10398 hlID = HLF_TXD; /* changed text */
10399 else
10400 hlID = HLF_CHD; /* changed line */
10401 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010402 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010403#endif
10404}
10405
10406/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010407 * "empty({expr})" function
10408 */
10409 static void
10410f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010411 typval_T *argvars;
10412 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010413{
10414 int n;
10415
10416 switch (argvars[0].v_type)
10417 {
10418 case VAR_STRING:
10419 case VAR_FUNC:
10420 n = argvars[0].vval.v_string == NULL
10421 || *argvars[0].vval.v_string == NUL;
10422 break;
10423 case VAR_NUMBER:
10424 n = argvars[0].vval.v_number == 0;
10425 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010426#ifdef FEAT_FLOAT
10427 case VAR_FLOAT:
10428 n = argvars[0].vval.v_float == 0.0;
10429 break;
10430#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010431 case VAR_LIST:
10432 n = argvars[0].vval.v_list == NULL
10433 || argvars[0].vval.v_list->lv_first == NULL;
10434 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010435 case VAR_DICT:
10436 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010438 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010439 default:
10440 EMSG2(_(e_intern2), "f_empty()");
10441 n = 0;
10442 }
10443
10444 rettv->vval.v_number = n;
10445}
10446
10447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 * "escape({string}, {chars})" function
10449 */
10450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010452 typval_T *argvars;
10453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454{
10455 char_u buf[NUMBUFLEN];
10456
Bram Moolenaar758711c2005-02-02 23:11:38 +000010457 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10458 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460}
10461
10462/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010463 * "eval()" function
10464 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010465 static void
10466f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010467 typval_T *argvars;
10468 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010469{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010470 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 s = get_tv_string_chk(&argvars[0]);
10473 if (s != NULL)
10474 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010475
Bram Moolenaar615b9972015-01-14 17:15:05 +010010476 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010477 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10478 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010479 if (p != NULL && !aborting())
10480 EMSG2(_(e_invexpr2), p);
10481 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010483 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010485 else if (*s != NUL)
10486 EMSG(_(e_trailing));
10487}
10488
10489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 * "eventhandler()" function
10491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010493f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010494 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498}
10499
10500/*
10501 * "executable()" function
10502 */
10503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010505 typval_T *argvars;
10506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010508 char_u *name = get_tv_string(&argvars[0]);
10509
10510 /* Check in $PATH and also check directly if there is a directory name. */
10511 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10512 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010513}
10514
10515/*
10516 * "exepath()" function
10517 */
10518 static void
10519f_exepath(argvars, rettv)
10520 typval_T *argvars;
10521 typval_T *rettv;
10522{
10523 char_u *p = NULL;
10524
Bram Moolenaarb5971142015-03-21 17:32:19 +010010525 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010526 rettv->v_type = VAR_STRING;
10527 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528}
10529
10530/*
10531 * "exists()" function
10532 */
10533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010535 typval_T *argvars;
10536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537{
10538 char_u *p;
10539 char_u *name;
10540 int n = FALSE;
10541 int len = 0;
10542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 if (*p == '$') /* environment variable */
10545 {
10546 /* first try "normal" environment variables (fast) */
10547 if (mch_getenv(p + 1) != NULL)
10548 n = TRUE;
10549 else
10550 {
10551 /* try expanding things like $VIM and ${HOME} */
10552 p = expand_env_save(p);
10553 if (p != NULL && *p != '$')
10554 n = TRUE;
10555 vim_free(p);
10556 }
10557 }
10558 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010561 if (*skipwhite(p) != NUL)
10562 n = FALSE; /* trailing garbage */
10563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 else if (*p == '*') /* internal or user defined function */
10565 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010566 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 }
10568 else if (*p == ':')
10569 {
10570 n = cmd_exists(p + 1);
10571 }
10572 else if (*p == '#')
10573 {
10574#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010575 if (p[1] == '#')
10576 n = autocmd_supported(p + 2);
10577 else
10578 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579#endif
10580 }
10581 else /* internal variable */
10582 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010583 char_u *tofree;
10584 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010586 /* get_name_len() takes care of expanding curly braces */
10587 name = p;
10588 len = get_name_len(&p, &tofree, TRUE, FALSE);
10589 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010591 if (tofree != NULL)
10592 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010593 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010594 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010596 /* handle d.key, l[idx], f(expr) */
10597 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10598 if (n)
10599 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 }
10601 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010602 if (*p != NUL)
10603 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010605 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 }
10607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010608 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609}
10610
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010611#ifdef FEAT_FLOAT
10612/*
10613 * "exp()" function
10614 */
10615 static void
10616f_exp(argvars, rettv)
10617 typval_T *argvars;
10618 typval_T *rettv;
10619{
10620 float_T f;
10621
10622 rettv->v_type = VAR_FLOAT;
10623 if (get_float_arg(argvars, &f) == OK)
10624 rettv->vval.v_float = exp(f);
10625 else
10626 rettv->vval.v_float = 0.0;
10627}
10628#endif
10629
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630/*
10631 * "expand()" function
10632 */
10633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 typval_T *argvars;
10636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638 char_u *s;
10639 int len;
10640 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010641 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010644 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010647 if (argvars[1].v_type != VAR_UNKNOWN
10648 && argvars[2].v_type != VAR_UNKNOWN
10649 && get_tv_number_chk(&argvars[2], &error)
10650 && !error)
10651 {
10652 rettv->v_type = VAR_LIST;
10653 rettv->vval.v_list = NULL;
10654 }
10655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 if (*s == '%' || *s == '#' || *s == '<')
10658 {
10659 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010660 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010662 if (rettv->v_type == VAR_LIST)
10663 {
10664 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10665 list_append_string(rettv->vval.v_list, result, -1);
10666 else
10667 vim_free(result);
10668 }
10669 else
10670 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 }
10672 else
10673 {
10674 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010675 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010676 if (argvars[1].v_type != VAR_UNKNOWN
10677 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010678 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010679 if (!error)
10680 {
10681 ExpandInit(&xpc);
10682 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010683 if (p_wic)
10684 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010685 if (rettv->v_type == VAR_STRING)
10686 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10687 options, WILD_ALL);
10688 else if (rettv_list_alloc(rettv) != FAIL)
10689 {
10690 int i;
10691
10692 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10693 for (i = 0; i < xpc.xp_numfiles; i++)
10694 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10695 ExpandCleanup(&xpc);
10696 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697 }
10698 else
10699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 }
10701}
10702
10703/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010704 * Go over all entries in "d2" and add them to "d1".
10705 * When "action" is "error" then a duplicate key is an error.
10706 * When "action" is "force" then a duplicate key is overwritten.
10707 * Otherwise duplicate keys are ignored ("action" is "keep").
10708 */
10709 void
10710dict_extend(d1, d2, action)
10711 dict_T *d1;
10712 dict_T *d2;
10713 char_u *action;
10714{
10715 dictitem_T *di1;
10716 hashitem_T *hi2;
10717 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010718 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010719
10720 todo = (int)d2->dv_hashtab.ht_used;
10721 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10722 {
10723 if (!HASHITEM_EMPTY(hi2))
10724 {
10725 --todo;
10726 di1 = dict_find(d1, hi2->hi_key, -1);
10727 if (d1->dv_scope != 0)
10728 {
10729 /* Disallow replacing a builtin function in l: and g:.
10730 * Check the key to be valid when adding to any
10731 * scope. */
10732 if (d1->dv_scope == VAR_DEF_SCOPE
10733 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10734 && var_check_func_name(hi2->hi_key,
10735 di1 == NULL))
10736 break;
10737 if (!valid_varname(hi2->hi_key))
10738 break;
10739 }
10740 if (di1 == NULL)
10741 {
10742 di1 = dictitem_copy(HI2DI(hi2));
10743 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10744 dictitem_free(di1);
10745 }
10746 else if (*action == 'e')
10747 {
10748 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10749 break;
10750 }
10751 else if (*action == 'f' && HI2DI(hi2) != di1)
10752 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010753 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10754 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010755 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010756 clear_tv(&di1->di_tv);
10757 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10758 }
10759 }
10760 }
10761}
10762
10763/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010764 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010765 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010766 */
10767 static void
10768f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010769 typval_T *argvars;
10770 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010771{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010772 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010773
Bram Moolenaare9a41262005-01-15 22:18:47 +000010774 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010775 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010776 list_T *l1, *l2;
10777 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010778 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010780
Bram Moolenaare9a41262005-01-15 22:18:47 +000010781 l1 = argvars[0].vval.v_list;
10782 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010783 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010784 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010785 {
10786 if (argvars[2].v_type != VAR_UNKNOWN)
10787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 before = get_tv_number_chk(&argvars[2], &error);
10789 if (error)
10790 return; /* type error; errmsg already given */
10791
Bram Moolenaar758711c2005-02-02 23:11:38 +000010792 if (before == l1->lv_len)
10793 item = NULL;
10794 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010795 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010796 item = list_find(l1, before);
10797 if (item == NULL)
10798 {
10799 EMSGN(_(e_listidx), before);
10800 return;
10801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010802 }
10803 }
10804 else
10805 item = NULL;
10806 list_extend(l1, l2, item);
10807
Bram Moolenaare9a41262005-01-15 22:18:47 +000010808 copy_tv(&argvars[0], rettv);
10809 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010811 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10812 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010813 dict_T *d1, *d2;
10814 char_u *action;
10815 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816
10817 d1 = argvars[0].vval.v_dict;
10818 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010819 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010820 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010821 {
10822 /* Check the third argument. */
10823 if (argvars[2].v_type != VAR_UNKNOWN)
10824 {
10825 static char *(av[]) = {"keep", "force", "error"};
10826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010827 action = get_tv_string_chk(&argvars[2]);
10828 if (action == NULL)
10829 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010830 for (i = 0; i < 3; ++i)
10831 if (STRCMP(action, av[i]) == 0)
10832 break;
10833 if (i == 3)
10834 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010835 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010836 return;
10837 }
10838 }
10839 else
10840 action = (char_u *)"force";
10841
Bram Moolenaara9922d62013-05-30 13:01:18 +020010842 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010843
Bram Moolenaare9a41262005-01-15 22:18:47 +000010844 copy_tv(&argvars[0], rettv);
10845 }
10846 }
10847 else
10848 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010849}
10850
10851/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010852 * "feedkeys()" function
10853 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010854 static void
10855f_feedkeys(argvars, rettv)
10856 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010857 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010858{
10859 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010860 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010861 char_u *keys, *flags;
10862 char_u nbuf[NUMBUFLEN];
10863 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010864 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010865
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010866 /* This is not allowed in the sandbox. If the commands would still be
10867 * executed in the sandbox it would be OK, but it probably happens later,
10868 * when "sandbox" is no longer set. */
10869 if (check_secure())
10870 return;
10871
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010872 keys = get_tv_string(&argvars[0]);
10873 if (*keys != NUL)
10874 {
10875 if (argvars[1].v_type != VAR_UNKNOWN)
10876 {
10877 flags = get_tv_string_buf(&argvars[1], nbuf);
10878 for ( ; *flags != NUL; ++flags)
10879 {
10880 switch (*flags)
10881 {
10882 case 'n': remap = FALSE; break;
10883 case 'm': remap = TRUE; break;
10884 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010885 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010886 }
10887 }
10888 }
10889
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010890 /* Need to escape K_SPECIAL and CSI before putting the string in the
10891 * typeahead buffer. */
10892 keys_esc = vim_strsave_escape_csi(keys);
10893 if (keys_esc != NULL)
10894 {
10895 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010896 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010897 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010898 if (vgetc_busy)
10899 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010900 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010901 }
10902}
10903
10904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 * "filereadable()" function
10906 */
10907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010909 typval_T *argvars;
10910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010912 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 char_u *p;
10914 int n;
10915
Bram Moolenaarc236c162008-07-13 17:41:49 +000010916#ifndef O_NONBLOCK
10917# define O_NONBLOCK 0
10918#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010919 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010920 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10921 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 {
10923 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010924 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 }
10926 else
10927 n = FALSE;
10928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930}
10931
10932/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010933 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 * rights to write into.
10935 */
10936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010938 typval_T *argvars;
10939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010941 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942}
10943
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010944static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010945
10946 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010947findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010948 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010949 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010950 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010951{
10952#ifdef FEAT_SEARCHPATH
10953 char_u *fname;
10954 char_u *fresult = NULL;
10955 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10956 char_u *p;
10957 char_u pathbuf[NUMBUFLEN];
10958 int count = 1;
10959 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010960 int error = FALSE;
10961#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010962
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010963 rettv->vval.v_string = NULL;
10964 rettv->v_type = VAR_STRING;
10965
10966#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010968
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10972 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010973 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 else
10975 {
10976 if (*p != NUL)
10977 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010980 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010981 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010982 }
10983
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010984 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10985 error = TRUE;
10986
10987 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010989 do
10990 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010991 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010992 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010993 fresult = find_file_in_path_option(first ? fname : NULL,
10994 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010995 0, first, path,
10996 find_what,
10997 curbuf->b_ffname,
10998 find_what == FINDFILE_DIR
10999 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011000 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011001
11002 if (fresult != NULL && rettv->v_type == VAR_LIST)
11003 list_append_string(rettv->vval.v_list, fresult, -1);
11004
11005 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011006 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011007
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011008 if (rettv->v_type == VAR_STRING)
11009 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011010#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011011}
11012
Bram Moolenaar33570922005-01-25 22:26:29 +000011013static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11014static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011015
11016/*
11017 * Implementation of map() and filter().
11018 */
11019 static void
11020filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011021 typval_T *argvars;
11022 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011023 int map;
11024{
11025 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011026 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011027 listitem_T *li, *nli;
11028 list_T *l = NULL;
11029 dictitem_T *di;
11030 hashtab_T *ht;
11031 hashitem_T *hi;
11032 dict_T *d = NULL;
11033 typval_T save_val;
11034 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011035 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011036 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011037 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011038 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011039 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011040 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011041 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011042
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011044 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011045 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011046 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011047 return;
11048 }
11049 else if (argvars[0].v_type == VAR_DICT)
11050 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011051 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011052 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011053 return;
11054 }
11055 else
11056 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011057 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011058 return;
11059 }
11060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 expr = get_tv_string_buf_chk(&argvars[1], buf);
11062 /* On type errors, the preceding call has already displayed an error
11063 * message. Avoid a misleading error message for an empty string that
11064 * was not passed as argument. */
11065 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011067 prepare_vimvar(VV_VAL, &save_val);
11068 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011069
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011070 /* We reset "did_emsg" to be able to detect whether an error
11071 * occurred during evaluation of the expression. */
11072 save_did_emsg = did_emsg;
11073 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011074
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011075 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 vimvars[VV_KEY].vv_type = VAR_STRING;
11079
11080 ht = &d->dv_hashtab;
11081 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011082 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 if (!HASHITEM_EMPTY(hi))
11086 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011087 int r;
11088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 --todo;
11090 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011091 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011092 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11093 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 break;
11095 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011096 r = filter_map_one(&di->di_tv, expr, map, &rem);
11097 clear_tv(&vimvars[VV_KEY].vv_tv);
11098 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099 break;
11100 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011101 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011102 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11103 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011104 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011106 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 }
11108 }
11109 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110 }
11111 else
11112 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011113 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 for (li = l->lv_first; li != NULL; li = nli)
11116 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011117 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011118 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011119 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011120 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011121 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011122 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011123 break;
11124 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011125 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011126 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011127 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011128 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011129
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011130 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011131 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011132
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011133 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011134 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135
11136 copy_tv(&argvars[0], rettv);
11137}
11138
11139 static int
11140filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 char_u *expr;
11143 int map;
11144 int *remp;
11145{
Bram Moolenaar33570922005-01-25 22:26:29 +000011146 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011147 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011148 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011149
Bram Moolenaar33570922005-01-25 22:26:29 +000011150 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011151 s = expr;
11152 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011153 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011154 if (*s != NUL) /* check for trailing chars after expr */
11155 {
11156 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011157 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011158 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011159 }
11160 if (map)
11161 {
11162 /* map(): replace the list item value */
11163 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011164 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011165 *tv = rettv;
11166 }
11167 else
11168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011169 int error = FALSE;
11170
Bram Moolenaare9a41262005-01-15 22:18:47 +000011171 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 /* On type error, nothing has been removed; return FAIL to stop the
11175 * loop. The error message was given by get_tv_number_chk(). */
11176 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011177 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011178 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011179 retval = OK;
11180theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011182 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011183}
11184
11185/*
11186 * "filter()" function
11187 */
11188 static void
11189f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *argvars;
11191 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011192{
11193 filter_map(argvars, rettv, FALSE);
11194}
11195
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011197 * "finddir({fname}[, {path}[, {count}]])" function
11198 */
11199 static void
11200f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011201 typval_T *argvars;
11202 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011203{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011204 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011205}
11206
11207/*
11208 * "findfile({fname}[, {path}[, {count}]])" function
11209 */
11210 static void
11211f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 typval_T *argvars;
11213 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011214{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011215 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011216}
11217
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011218#ifdef FEAT_FLOAT
11219/*
11220 * "float2nr({float})" function
11221 */
11222 static void
11223f_float2nr(argvars, rettv)
11224 typval_T *argvars;
11225 typval_T *rettv;
11226{
11227 float_T f;
11228
11229 if (get_float_arg(argvars, &f) == OK)
11230 {
11231 if (f < -0x7fffffff)
11232 rettv->vval.v_number = -0x7fffffff;
11233 else if (f > 0x7fffffff)
11234 rettv->vval.v_number = 0x7fffffff;
11235 else
11236 rettv->vval.v_number = (varnumber_T)f;
11237 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011238}
11239
11240/*
11241 * "floor({float})" function
11242 */
11243 static void
11244f_floor(argvars, rettv)
11245 typval_T *argvars;
11246 typval_T *rettv;
11247{
11248 float_T f;
11249
11250 rettv->v_type = VAR_FLOAT;
11251 if (get_float_arg(argvars, &f) == OK)
11252 rettv->vval.v_float = floor(f);
11253 else
11254 rettv->vval.v_float = 0.0;
11255}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011256
11257/*
11258 * "fmod()" function
11259 */
11260 static void
11261f_fmod(argvars, rettv)
11262 typval_T *argvars;
11263 typval_T *rettv;
11264{
11265 float_T fx, fy;
11266
11267 rettv->v_type = VAR_FLOAT;
11268 if (get_float_arg(argvars, &fx) == OK
11269 && get_float_arg(&argvars[1], &fy) == OK)
11270 rettv->vval.v_float = fmod(fx, fy);
11271 else
11272 rettv->vval.v_float = 0.0;
11273}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011274#endif
11275
Bram Moolenaar0d660222005-01-07 21:51:51 +000011276/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011277 * "fnameescape({string})" function
11278 */
11279 static void
11280f_fnameescape(argvars, rettv)
11281 typval_T *argvars;
11282 typval_T *rettv;
11283{
11284 rettv->vval.v_string = vim_strsave_fnameescape(
11285 get_tv_string(&argvars[0]), FALSE);
11286 rettv->v_type = VAR_STRING;
11287}
11288
11289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 * "fnamemodify({fname}, {mods})" function
11291 */
11292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011294 typval_T *argvars;
11295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296{
11297 char_u *fname;
11298 char_u *mods;
11299 int usedlen = 0;
11300 int len;
11301 char_u *fbuf = NULL;
11302 char_u buf[NUMBUFLEN];
11303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 fname = get_tv_string_chk(&argvars[0]);
11305 mods = get_tv_string_buf_chk(&argvars[1], buf);
11306 if (fname == NULL || mods == NULL)
11307 fname = NULL;
11308 else
11309 {
11310 len = (int)STRLEN(fname);
11311 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011314 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 vim_free(fbuf);
11320}
11321
Bram Moolenaar33570922005-01-25 22:26:29 +000011322static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323
11324/*
11325 * "foldclosed()" function
11326 */
11327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011330 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011331 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332{
11333#ifdef FEAT_FOLDING
11334 linenr_T lnum;
11335 linenr_T first, last;
11336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11339 {
11340 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11341 {
11342 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 return;
11347 }
11348 }
11349#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351}
11352
11353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011354 * "foldclosed()" function
11355 */
11356 static void
11357f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011358 typval_T *argvars;
11359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011360{
11361 foldclosed_both(argvars, rettv, FALSE);
11362}
11363
11364/*
11365 * "foldclosedend()" function
11366 */
11367 static void
11368f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011369 typval_T *argvars;
11370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011371{
11372 foldclosed_both(argvars, rettv, TRUE);
11373}
11374
11375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376 * "foldlevel()" function
11377 */
11378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011380 typval_T *argvars UNUSED;
11381 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382{
11383#ifdef FEAT_FOLDING
11384 linenr_T lnum;
11385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390}
11391
11392/*
11393 * "foldtext()" function
11394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
11400#ifdef FEAT_FOLDING
11401 linenr_T lnum;
11402 char_u *s;
11403 char_u *r;
11404 int len;
11405 char *txt;
11406#endif
11407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408 rettv->v_type = VAR_STRING;
11409 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011411 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11412 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11413 <= curbuf->b_ml.ml_line_count
11414 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 {
11416 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011417 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11418 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 {
11420 if (!linewhite(lnum))
11421 break;
11422 ++lnum;
11423 }
11424
11425 /* Find interesting text in this line. */
11426 s = skipwhite(ml_get(lnum));
11427 /* skip C comment-start */
11428 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011429 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011431 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011432 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011433 {
11434 s = skipwhite(ml_get(lnum + 1));
11435 if (*s == '*')
11436 s = skipwhite(s + 1);
11437 }
11438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 txt = _("+-%s%3ld lines: ");
11440 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011441 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 + 20 /* for %3ld */
11443 + STRLEN(s))); /* concatenated */
11444 if (r != NULL)
11445 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011446 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11447 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11448 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 len = (int)STRLEN(r);
11450 STRCAT(r, s);
11451 /* remove 'foldmarker' and 'commentstring' */
11452 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 }
11455 }
11456#endif
11457}
11458
11459/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011460 * "foldtextresult(lnum)" function
11461 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011466{
11467#ifdef FEAT_FOLDING
11468 linenr_T lnum;
11469 char_u *text;
11470 char_u buf[51];
11471 foldinfo_T foldinfo;
11472 int fold_count;
11473#endif
11474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->v_type = VAR_STRING;
11476 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011477#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011479 /* treat illegal types and illegal string values for {lnum} the same */
11480 if (lnum < 0)
11481 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011482 fold_count = foldedCount(curwin, lnum, &foldinfo);
11483 if (fold_count > 0)
11484 {
11485 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11486 &foldinfo, buf);
11487 if (text == buf)
11488 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011490 }
11491#endif
11492}
11493
11494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 * "foreground()" function
11496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011499 typval_T *argvars UNUSED;
11500 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502#ifdef FEAT_GUI
11503 if (gui.in_use)
11504 gui_mch_set_foreground();
11505#else
11506# ifdef WIN32
11507 win32_set_foreground();
11508# endif
11509#endif
11510}
11511
11512/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011513 * "function()" function
11514 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 typval_T *argvars;
11518 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011519{
11520 char_u *s;
11521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011523 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011524 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011525 /* Don't check an autoload name for existence here. */
11526 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011527 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011528 else
11529 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011530 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011531 {
11532 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011533 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011534
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011535 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11536 * also be called from another script. Using trans_function_name()
11537 * would also work, but some plugins depend on the name being
11538 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011539 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011540 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011541 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011542 if (rettv->vval.v_string != NULL)
11543 {
11544 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011545 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011546 }
11547 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011548 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011549 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011551 }
11552}
11553
11554/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011555 * "garbagecollect()" function
11556 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011557 static void
11558f_garbagecollect(argvars, rettv)
11559 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011560 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011561{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011562 /* This is postponed until we are back at the toplevel, because we may be
11563 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11564 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011565
11566 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11567 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011568}
11569
11570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011571 * "get()" function
11572 */
11573 static void
11574f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011575 typval_T *argvars;
11576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011577{
Bram Moolenaar33570922005-01-25 22:26:29 +000011578 listitem_T *li;
11579 list_T *l;
11580 dictitem_T *di;
11581 dict_T *d;
11582 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011583
Bram Moolenaare9a41262005-01-15 22:18:47 +000011584 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011585 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011586 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011588 int error = FALSE;
11589
11590 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11591 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011592 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011593 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011594 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595 else if (argvars[0].v_type == VAR_DICT)
11596 {
11597 if ((d = argvars[0].vval.v_dict) != NULL)
11598 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011599 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011600 if (di != NULL)
11601 tv = &di->di_tv;
11602 }
11603 }
11604 else
11605 EMSG2(_(e_listdictarg), "get()");
11606
11607 if (tv == NULL)
11608 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011609 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011610 copy_tv(&argvars[2], rettv);
11611 }
11612 else
11613 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011614}
11615
Bram Moolenaar342337a2005-07-21 21:11:17 +000011616static 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 +000011617
11618/*
11619 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011620 * Return a range (from start to end) of lines in rettv from the specified
11621 * buffer.
11622 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011623 */
11624 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011625get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011626 buf_T *buf;
11627 linenr_T start;
11628 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011629 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011630 typval_T *rettv;
11631{
11632 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011633
Bram Moolenaar959a1432013-12-14 12:17:38 +010011634 rettv->v_type = VAR_STRING;
11635 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011636 if (retlist && rettv_list_alloc(rettv) == FAIL)
11637 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011638
11639 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11640 return;
11641
11642 if (!retlist)
11643 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011644 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11645 p = ml_get_buf(buf, start, FALSE);
11646 else
11647 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011648 rettv->vval.v_string = vim_strsave(p);
11649 }
11650 else
11651 {
11652 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011653 return;
11654
11655 if (start < 1)
11656 start = 1;
11657 if (end > buf->b_ml.ml_line_count)
11658 end = buf->b_ml.ml_line_count;
11659 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011660 if (list_append_string(rettv->vval.v_list,
11661 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011662 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011663 }
11664}
11665
11666/*
11667 * "getbufline()" function
11668 */
11669 static void
11670f_getbufline(argvars, rettv)
11671 typval_T *argvars;
11672 typval_T *rettv;
11673{
11674 linenr_T lnum;
11675 linenr_T end;
11676 buf_T *buf;
11677
11678 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11679 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011680 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011681 --emsg_off;
11682
Bram Moolenaar661b1822005-07-28 22:36:45 +000011683 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011684 if (argvars[2].v_type == VAR_UNKNOWN)
11685 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011686 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011687 end = get_tv_lnum_buf(&argvars[2], buf);
11688
Bram Moolenaar342337a2005-07-21 21:11:17 +000011689 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011690}
11691
Bram Moolenaar0d660222005-01-07 21:51:51 +000011692/*
11693 * "getbufvar()" function
11694 */
11695 static void
11696f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011697 typval_T *argvars;
11698 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699{
11700 buf_T *buf;
11701 buf_T *save_curbuf;
11702 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011703 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011704 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11707 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011708 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011709 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011711 rettv->v_type = VAR_STRING;
11712 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011713
11714 if (buf != NULL && varname != NULL)
11715 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011716 /* set curbuf to be our buf, temporarily */
11717 save_curbuf = curbuf;
11718 curbuf = buf;
11719
Bram Moolenaar0d660222005-01-07 21:51:51 +000011720 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011721 {
11722 if (get_option_tv(&varname, rettv, TRUE) == OK)
11723 done = TRUE;
11724 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011725 else if (STRCMP(varname, "changedtick") == 0)
11726 {
11727 rettv->v_type = VAR_NUMBER;
11728 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011729 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011730 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011731 else
11732 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011733 /* Look up the variable. */
11734 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11735 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11736 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011737 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011738 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011739 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011740 done = TRUE;
11741 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011742 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011743
11744 /* restore previous notion of curbuf */
11745 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011746 }
11747
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011748 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11749 /* use the default value */
11750 copy_tv(&argvars[2], rettv);
11751
Bram Moolenaar0d660222005-01-07 21:51:51 +000011752 --emsg_off;
11753}
11754
11755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 * "getchar()" function
11757 */
11758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011760 typval_T *argvars;
11761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762{
11763 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011766 /* Position the cursor. Needed after a message that ends in a space. */
11767 windgoto(msg_row, msg_col);
11768
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 ++no_mapping;
11770 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011771 for (;;)
11772 {
11773 if (argvars[0].v_type == VAR_UNKNOWN)
11774 /* getchar(): blocking wait. */
11775 n = safe_vgetc();
11776 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11777 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011778 n = vpeekc_any();
11779 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011780 /* illegal argument or getchar(0) and no char avail: return zero */
11781 n = 0;
11782 else
11783 /* getchar(0) and char avail: return char */
11784 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011785
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011786 if (n == K_IGNORE)
11787 continue;
11788 break;
11789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 --no_mapping;
11791 --allow_keys;
11792
Bram Moolenaar219b8702006-11-01 14:32:36 +000011793 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11794 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11795 vimvars[VV_MOUSE_COL].vv_nr = 0;
11796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 if (IS_SPECIAL(n) || mod_mask != 0)
11799 {
11800 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11801 int i = 0;
11802
11803 /* Turn a special key into three bytes, plus modifier. */
11804 if (mod_mask != 0)
11805 {
11806 temp[i++] = K_SPECIAL;
11807 temp[i++] = KS_MODIFIER;
11808 temp[i++] = mod_mask;
11809 }
11810 if (IS_SPECIAL(n))
11811 {
11812 temp[i++] = K_SPECIAL;
11813 temp[i++] = K_SECOND(n);
11814 temp[i++] = K_THIRD(n);
11815 }
11816#ifdef FEAT_MBYTE
11817 else if (has_mbyte)
11818 i += (*mb_char2bytes)(n, temp + i);
11819#endif
11820 else
11821 temp[i++] = n;
11822 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011823 rettv->v_type = VAR_STRING;
11824 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011825
11826#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011827 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011828 {
11829 int row = mouse_row;
11830 int col = mouse_col;
11831 win_T *win;
11832 linenr_T lnum;
11833# ifdef FEAT_WINDOWS
11834 win_T *wp;
11835# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011836 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011837
11838 if (row >= 0 && col >= 0)
11839 {
11840 /* Find the window at the mouse coordinates and compute the
11841 * text position. */
11842 win = mouse_find_win(&row, &col);
11843 (void)mouse_comp_pos(win, &row, &col, &lnum);
11844# ifdef FEAT_WINDOWS
11845 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011846 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011847# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011848 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011849 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11850 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11851 }
11852 }
11853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 }
11855}
11856
11857/*
11858 * "getcharmod()" function
11859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011862 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866}
11867
11868/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011869 * "getcharsearch()" function
11870 */
11871 static void
11872f_getcharsearch(argvars, rettv)
11873 typval_T *argvars UNUSED;
11874 typval_T *rettv;
11875{
11876 if (rettv_dict_alloc(rettv) != FAIL)
11877 {
11878 dict_T *dict = rettv->vval.v_dict;
11879
11880 dict_add_nr_str(dict, "char", 0L, last_csearch());
11881 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11882 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11883 }
11884}
11885
11886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 * "getcmdline()" function
11888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011891 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011894 rettv->v_type = VAR_STRING;
11895 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896}
11897
11898/*
11899 * "getcmdpos()" function
11900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011906 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907}
11908
11909/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011910 * "getcmdtype()" function
11911 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011912 static void
11913f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011914 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011915 typval_T *rettv;
11916{
11917 rettv->v_type = VAR_STRING;
11918 rettv->vval.v_string = alloc(2);
11919 if (rettv->vval.v_string != NULL)
11920 {
11921 rettv->vval.v_string[0] = get_cmdline_type();
11922 rettv->vval.v_string[1] = NUL;
11923 }
11924}
11925
11926/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011927 * "getcmdwintype()" function
11928 */
11929 static void
11930f_getcmdwintype(argvars, rettv)
11931 typval_T *argvars UNUSED;
11932 typval_T *rettv;
11933{
11934 rettv->v_type = VAR_STRING;
11935 rettv->vval.v_string = NULL;
11936#ifdef FEAT_CMDWIN
11937 rettv->vval.v_string = alloc(2);
11938 if (rettv->vval.v_string != NULL)
11939 {
11940 rettv->vval.v_string[0] = cmdwin_type;
11941 rettv->vval.v_string[1] = NUL;
11942 }
11943#endif
11944}
11945
11946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 * "getcwd()" function
11948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011950f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011951 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011954 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011957 rettv->vval.v_string = NULL;
11958 cwd = alloc(MAXPATHL);
11959 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011961 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11962 {
11963 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011965 if (rettv->vval.v_string != NULL)
11966 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011968 }
11969 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 }
11971}
11972
11973/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011974 * "getfontname()" function
11975 */
11976 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011978 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011979 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011980{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981 rettv->v_type = VAR_STRING;
11982 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011983#ifdef FEAT_GUI
11984 if (gui.in_use)
11985 {
11986 GuiFont font;
11987 char_u *name = NULL;
11988
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011989 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011990 {
11991 /* Get the "Normal" font. Either the name saved by
11992 * hl_set_font_name() or from the font ID. */
11993 font = gui.norm_font;
11994 name = hl_get_font_name();
11995 }
11996 else
11997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011999 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12000 return;
12001 font = gui_mch_get_font(name, FALSE);
12002 if (font == NOFONT)
12003 return; /* Invalid font name, return empty string. */
12004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012006 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012007 gui_mch_free_font(font);
12008 }
12009#endif
12010}
12011
12012/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012013 * "getfperm({fname})" function
12014 */
12015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012017 typval_T *argvars;
12018 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012019{
12020 char_u *fname;
12021 struct stat st;
12022 char_u *perm = NULL;
12023 char_u flags[] = "rwx";
12024 int i;
12025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012029 if (mch_stat((char *)fname, &st) >= 0)
12030 {
12031 perm = vim_strsave((char_u *)"---------");
12032 if (perm != NULL)
12033 {
12034 for (i = 0; i < 9; i++)
12035 {
12036 if (st.st_mode & (1 << (8 - i)))
12037 perm[i] = flags[i % 3];
12038 }
12039 }
12040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012042}
12043
12044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 * "getfsize({fname})" function
12046 */
12047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012049 typval_T *argvars;
12050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052 char_u *fname;
12053 struct stat st;
12054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058
12059 if (mch_stat((char *)fname, &st) >= 0)
12060 {
12061 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012064 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012066
12067 /* non-perfect check for overflow */
12068 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12069 rettv->vval.v_number = -2;
12070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 }
12072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074}
12075
12076/*
12077 * "getftime({fname})" function
12078 */
12079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012081 typval_T *argvars;
12082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083{
12084 char_u *fname;
12085 struct stat st;
12086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088
12089 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093}
12094
12095/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012096 * "getftype({fname})" function
12097 */
12098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012100 typval_T *argvars;
12101 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012102{
12103 char_u *fname;
12104 struct stat st;
12105 char_u *type = NULL;
12106 char *t;
12107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012110 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012111 if (mch_lstat((char *)fname, &st) >= 0)
12112 {
12113#ifdef S_ISREG
12114 if (S_ISREG(st.st_mode))
12115 t = "file";
12116 else if (S_ISDIR(st.st_mode))
12117 t = "dir";
12118# ifdef S_ISLNK
12119 else if (S_ISLNK(st.st_mode))
12120 t = "link";
12121# endif
12122# ifdef S_ISBLK
12123 else if (S_ISBLK(st.st_mode))
12124 t = "bdev";
12125# endif
12126# ifdef S_ISCHR
12127 else if (S_ISCHR(st.st_mode))
12128 t = "cdev";
12129# endif
12130# ifdef S_ISFIFO
12131 else if (S_ISFIFO(st.st_mode))
12132 t = "fifo";
12133# endif
12134# ifdef S_ISSOCK
12135 else if (S_ISSOCK(st.st_mode))
12136 t = "fifo";
12137# endif
12138 else
12139 t = "other";
12140#else
12141# ifdef S_IFMT
12142 switch (st.st_mode & S_IFMT)
12143 {
12144 case S_IFREG: t = "file"; break;
12145 case S_IFDIR: t = "dir"; break;
12146# ifdef S_IFLNK
12147 case S_IFLNK: t = "link"; break;
12148# endif
12149# ifdef S_IFBLK
12150 case S_IFBLK: t = "bdev"; break;
12151# endif
12152# ifdef S_IFCHR
12153 case S_IFCHR: t = "cdev"; break;
12154# endif
12155# ifdef S_IFIFO
12156 case S_IFIFO: t = "fifo"; break;
12157# endif
12158# ifdef S_IFSOCK
12159 case S_IFSOCK: t = "socket"; break;
12160# endif
12161 default: t = "other";
12162 }
12163# else
12164 if (mch_isdir(fname))
12165 t = "dir";
12166 else
12167 t = "file";
12168# endif
12169#endif
12170 type = vim_strsave((char_u *)t);
12171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012172 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012173}
12174
12175/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012176 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012177 */
12178 static void
12179f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012180 typval_T *argvars;
12181 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012182{
12183 linenr_T lnum;
12184 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012185 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012186
12187 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012188 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012189 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012190 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012191 retlist = FALSE;
12192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012193 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012194 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012196 retlist = TRUE;
12197 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012198
Bram Moolenaar342337a2005-07-21 21:11:17 +000012199 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012200}
12201
12202/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012203 * "getmatches()" function
12204 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012205 static void
12206f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012207 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012208 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012209{
12210#ifdef FEAT_SEARCH_EXTRA
12211 dict_T *dict;
12212 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012213 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012214
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012215 if (rettv_list_alloc(rettv) == OK)
12216 {
12217 while (cur != NULL)
12218 {
12219 dict = dict_alloc();
12220 if (dict == NULL)
12221 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012222 if (cur->match.regprog == NULL)
12223 {
12224 /* match added with matchaddpos() */
12225 for (i = 0; i < MAXPOSMATCH; ++i)
12226 {
12227 llpos_T *llpos;
12228 char buf[6];
12229 list_T *l;
12230
12231 llpos = &cur->pos.pos[i];
12232 if (llpos->lnum == 0)
12233 break;
12234 l = list_alloc();
12235 if (l == NULL)
12236 break;
12237 list_append_number(l, (varnumber_T)llpos->lnum);
12238 if (llpos->col > 0)
12239 {
12240 list_append_number(l, (varnumber_T)llpos->col);
12241 list_append_number(l, (varnumber_T)llpos->len);
12242 }
12243 sprintf(buf, "pos%d", i + 1);
12244 dict_add_list(dict, buf, l);
12245 }
12246 }
12247 else
12248 {
12249 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12250 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012251 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012252 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12253 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012254# ifdef FEAT_CONCEAL
12255 if (cur->conceal_char)
12256 {
12257 char_u buf[MB_MAXBYTES + 1];
12258
12259 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12260 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12261 }
12262# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012263 list_append_dict(rettv->vval.v_list, dict);
12264 cur = cur->next;
12265 }
12266 }
12267#endif
12268}
12269
12270/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012271 * "getpid()" function
12272 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012273 static void
12274f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012275 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012276 typval_T *rettv;
12277{
12278 rettv->vval.v_number = mch_get_pid();
12279}
12280
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012281static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12282
12283/*
12284 * "getcurpos()" function
12285 */
12286 static void
12287f_getcurpos(argvars, rettv)
12288 typval_T *argvars;
12289 typval_T *rettv;
12290{
12291 getpos_both(argvars, rettv, TRUE);
12292}
12293
Bram Moolenaar18081e32008-02-20 19:11:07 +000012294/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012295 * "getpos(string)" function
12296 */
12297 static void
12298f_getpos(argvars, rettv)
12299 typval_T *argvars;
12300 typval_T *rettv;
12301{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012302 getpos_both(argvars, rettv, FALSE);
12303}
12304
12305 static void
12306getpos_both(argvars, rettv, getcurpos)
12307 typval_T *argvars;
12308 typval_T *rettv;
12309 int getcurpos;
12310{
Bram Moolenaara5525202006-03-02 22:52:09 +000012311 pos_T *fp;
12312 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012313 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012314
12315 if (rettv_list_alloc(rettv) == OK)
12316 {
12317 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012318 if (getcurpos)
12319 fp = &curwin->w_cursor;
12320 else
12321 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012322 if (fnum != -1)
12323 list_append_number(l, (varnumber_T)fnum);
12324 else
12325 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012326 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12327 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012328 list_append_number(l, (fp != NULL)
12329 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012330 : (varnumber_T)0);
12331 list_append_number(l,
12332#ifdef FEAT_VIRTUALEDIT
12333 (fp != NULL) ? (varnumber_T)fp->coladd :
12334#endif
12335 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012336 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012337 list_append_number(l, curwin->w_curswant == MAXCOL ?
12338 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012339 }
12340 else
12341 rettv->vval.v_number = FALSE;
12342}
12343
12344/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012345 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012346 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012347 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012348f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012349 typval_T *argvars UNUSED;
12350 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012351{
12352#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012353 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012354#endif
12355
Bram Moolenaar2641f772005-03-25 21:58:17 +000012356#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012357 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012358 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012359 wp = NULL;
12360 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12361 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012362 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012363 if (wp == NULL)
12364 return;
12365 }
12366
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012367 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012368 }
12369#endif
12370}
12371
12372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373 * "getreg()" function
12374 */
12375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012377 typval_T *argvars;
12378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379{
12380 char_u *strregname;
12381 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012382 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012383 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012386 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 strregname = get_tv_string_chk(&argvars[0]);
12389 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012390 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012392 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012393 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12394 return_list = get_tv_number_chk(&argvars[2], &error);
12395 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012398 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012399
12400 if (error)
12401 return;
12402
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 regname = (strregname == NULL ? '"' : *strregname);
12404 if (regname == 0)
12405 regname = '"';
12406
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012407 if (return_list)
12408 {
12409 rettv->v_type = VAR_LIST;
12410 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12411 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012412 if (rettv->vval.v_list != NULL)
12413 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012414 }
12415 else
12416 {
12417 rettv->v_type = VAR_STRING;
12418 rettv->vval.v_string = get_reg_contents(regname,
12419 arg2 ? GREG_EXPR_SRC : 0);
12420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421}
12422
12423/*
12424 * "getregtype()" function
12425 */
12426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012427f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012428 typval_T *argvars;
12429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430{
12431 char_u *strregname;
12432 int regname;
12433 char_u buf[NUMBUFLEN + 2];
12434 long reglen = 0;
12435
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012436 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012437 {
12438 strregname = get_tv_string_chk(&argvars[0]);
12439 if (strregname == NULL) /* type error; errmsg already given */
12440 {
12441 rettv->v_type = VAR_STRING;
12442 rettv->vval.v_string = NULL;
12443 return;
12444 }
12445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 else
12447 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012448 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449
12450 regname = (strregname == NULL ? '"' : *strregname);
12451 if (regname == 0)
12452 regname = '"';
12453
12454 buf[0] = NUL;
12455 buf[1] = NUL;
12456 switch (get_reg_type(regname, &reglen))
12457 {
12458 case MLINE: buf[0] = 'V'; break;
12459 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460 case MBLOCK:
12461 buf[0] = Ctrl_V;
12462 sprintf((char *)buf + 1, "%ld", reglen + 1);
12463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465 rettv->v_type = VAR_STRING;
12466 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467}
12468
12469/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012470 * "gettabvar()" function
12471 */
12472 static void
12473f_gettabvar(argvars, rettv)
12474 typval_T *argvars;
12475 typval_T *rettv;
12476{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012477 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012478 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012479 dictitem_T *v;
12480 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012481 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012482
12483 rettv->v_type = VAR_STRING;
12484 rettv->vval.v_string = NULL;
12485
12486 varname = get_tv_string_chk(&argvars[1]);
12487 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12488 if (tp != NULL && varname != NULL)
12489 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012490 /* Set tp to be our tabpage, temporarily. Also set the window to the
12491 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012492 if (switch_win(&oldcurwin, &oldtabpage,
12493 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012494 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012495 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012496 /* look up the variable */
12497 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12498 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12499 if (v != NULL)
12500 {
12501 copy_tv(&v->di_tv, rettv);
12502 done = TRUE;
12503 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012504 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012505
12506 /* restore previous notion of curwin */
12507 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012508 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012509
12510 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012511 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012512}
12513
12514/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012515 * "gettabwinvar()" function
12516 */
12517 static void
12518f_gettabwinvar(argvars, rettv)
12519 typval_T *argvars;
12520 typval_T *rettv;
12521{
12522 getwinvar(argvars, rettv, 1);
12523}
12524
12525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012526 * "getwinposx()" function
12527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012530 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534#ifdef FEAT_GUI
12535 if (gui.in_use)
12536 {
12537 int x, y;
12538
12539 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 }
12542#endif
12543}
12544
12545/*
12546 * "getwinposy()" function
12547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012550 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554#ifdef FEAT_GUI
12555 if (gui.in_use)
12556 {
12557 int x, y;
12558
12559 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 }
12562#endif
12563}
12564
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012565/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012566 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012567 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012568 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012569find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012570 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012571 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012572{
12573#ifdef FEAT_WINDOWS
12574 win_T *wp;
12575#endif
12576 int nr;
12577
12578 nr = get_tv_number_chk(vp, NULL);
12579
12580#ifdef FEAT_WINDOWS
12581 if (nr < 0)
12582 return NULL;
12583 if (nr == 0)
12584 return curwin;
12585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012586 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12587 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012588 if (--nr <= 0)
12589 break;
12590 return wp;
12591#else
12592 if (nr == 0 || nr == 1)
12593 return curwin;
12594 return NULL;
12595#endif
12596}
12597
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598/*
12599 * "getwinvar()" function
12600 */
12601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012602f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012603 typval_T *argvars;
12604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012606 getwinvar(argvars, rettv, 0);
12607}
12608
12609/*
12610 * getwinvar() and gettabwinvar()
12611 */
12612 static void
12613getwinvar(argvars, rettv, off)
12614 typval_T *argvars;
12615 typval_T *rettv;
12616 int off; /* 1 for gettabwinvar() */
12617{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012618 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012620 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012621 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012622 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012623#ifdef FEAT_WINDOWS
12624 win_T *oldcurwin;
12625 tabpage_T *oldtabpage;
12626 int need_switch_win;
12627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012629#ifdef FEAT_WINDOWS
12630 if (off == 1)
12631 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12632 else
12633 tp = curtab;
12634#endif
12635 win = find_win_by_nr(&argvars[off], tp);
12636 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012637 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012639 rettv->v_type = VAR_STRING;
12640 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641
12642 if (win != NULL && varname != NULL)
12643 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012644#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012645 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012646 * otherwise the window is not valid. Only do this when needed,
12647 * autocommands get blocked. */
12648 need_switch_win = !(tp == curtab && win == curwin);
12649 if (!need_switch_win
12650 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12651#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012652 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012653 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012654 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012655 if (get_option_tv(&varname, rettv, 1) == OK)
12656 done = TRUE;
12657 }
12658 else
12659 {
12660 /* Look up the variable. */
12661 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12662 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12663 varname, FALSE);
12664 if (v != NULL)
12665 {
12666 copy_tv(&v->di_tv, rettv);
12667 done = TRUE;
12668 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012671
Bram Moolenaarba117c22015-09-29 16:53:22 +020012672#ifdef FEAT_WINDOWS
12673 if (need_switch_win)
12674 /* restore previous notion of curwin */
12675 restore_win(oldcurwin, oldtabpage, TRUE);
12676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 }
12678
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012679 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12680 /* use the default return value */
12681 copy_tv(&argvars[off + 2], rettv);
12682
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 --emsg_off;
12684}
12685
12686/*
12687 * "glob()" function
12688 */
12689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012690f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012691 typval_T *argvars;
12692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012694 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012696 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012698 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012699 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012701 if (argvars[1].v_type != VAR_UNKNOWN)
12702 {
12703 if (get_tv_number_chk(&argvars[1], &error))
12704 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012706 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012707 if (get_tv_number_chk(&argvars[2], &error))
12708 {
12709 rettv->v_type = VAR_LIST;
12710 rettv->vval.v_list = NULL;
12711 }
12712 if (argvars[3].v_type != VAR_UNKNOWN
12713 && get_tv_number_chk(&argvars[3], &error))
12714 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012715 }
12716 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012717 if (!error)
12718 {
12719 ExpandInit(&xpc);
12720 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012721 if (p_wic)
12722 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012723 if (rettv->v_type == VAR_STRING)
12724 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012725 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012726 else if (rettv_list_alloc(rettv) != FAIL)
12727 {
12728 int i;
12729
12730 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12731 NULL, options, WILD_ALL_KEEP);
12732 for (i = 0; i < xpc.xp_numfiles; i++)
12733 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12734
12735 ExpandCleanup(&xpc);
12736 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012737 }
12738 else
12739 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740}
12741
12742/*
12743 * "globpath()" function
12744 */
12745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012747 typval_T *argvars;
12748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012750 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012753 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012754 garray_T ga;
12755 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012757 /* When the optional second argument is non-zero, don't remove matches
12758 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012760 if (argvars[2].v_type != VAR_UNKNOWN)
12761 {
12762 if (get_tv_number_chk(&argvars[2], &error))
12763 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012764 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012765 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012766 if (get_tv_number_chk(&argvars[3], &error))
12767 {
12768 rettv->v_type = VAR_LIST;
12769 rettv->vval.v_list = NULL;
12770 }
12771 if (argvars[4].v_type != VAR_UNKNOWN
12772 && get_tv_number_chk(&argvars[4], &error))
12773 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012774 }
12775 }
12776 if (file != NULL && !error)
12777 {
12778 ga_init2(&ga, (int)sizeof(char_u *), 10);
12779 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12780 if (rettv->v_type == VAR_STRING)
12781 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12782 else if (rettv_list_alloc(rettv) != FAIL)
12783 for (i = 0; i < ga.ga_len; ++i)
12784 list_append_string(rettv->vval.v_list,
12785 ((char_u **)(ga.ga_data))[i], -1);
12786 ga_clear_strings(&ga);
12787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012789 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790}
12791
12792/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012793 * "glob2regpat()" function
12794 */
12795 static void
12796f_glob2regpat(argvars, rettv)
12797 typval_T *argvars;
12798 typval_T *rettv;
12799{
12800 char_u *pat = get_tv_string_chk(&argvars[0]);
12801
12802 rettv->v_type = VAR_STRING;
12803 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12804}
12805
12806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 * "has()" function
12808 */
12809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012811 typval_T *argvars;
12812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813{
12814 int i;
12815 char_u *name;
12816 int n = FALSE;
12817 static char *(has_list[]) =
12818 {
12819#ifdef AMIGA
12820 "amiga",
12821# ifdef FEAT_ARP
12822 "arp",
12823# endif
12824#endif
12825#ifdef __BEOS__
12826 "beos",
12827#endif
12828#ifdef MSDOS
12829# ifdef DJGPP
12830 "dos32",
12831# else
12832 "dos16",
12833# endif
12834#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012835#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 "mac",
12837#endif
12838#if defined(MACOS_X_UNIX)
12839 "macunix",
12840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841#ifdef __QNX__
12842 "qnx",
12843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844#ifdef UNIX
12845 "unix",
12846#endif
12847#ifdef VMS
12848 "vms",
12849#endif
12850#ifdef WIN16
12851 "win16",
12852#endif
12853#ifdef WIN32
12854 "win32",
12855#endif
12856#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12857 "win32unix",
12858#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012859#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 "win64",
12861#endif
12862#ifdef EBCDIC
12863 "ebcdic",
12864#endif
12865#ifndef CASE_INSENSITIVE_FILENAME
12866 "fname_case",
12867#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012868#ifdef HAVE_ACL
12869 "acl",
12870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871#ifdef FEAT_ARABIC
12872 "arabic",
12873#endif
12874#ifdef FEAT_AUTOCMD
12875 "autocmd",
12876#endif
12877#ifdef FEAT_BEVAL
12878 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012879# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12880 "balloon_multiline",
12881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882#endif
12883#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12884 "builtin_terms",
12885# ifdef ALL_BUILTIN_TCAPS
12886 "all_builtin_terms",
12887# endif
12888#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012889#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12890 || defined(FEAT_GUI_W32) \
12891 || defined(FEAT_GUI_MOTIF))
12892 "browsefilter",
12893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894#ifdef FEAT_BYTEOFF
12895 "byte_offset",
12896#endif
12897#ifdef FEAT_CINDENT
12898 "cindent",
12899#endif
12900#ifdef FEAT_CLIENTSERVER
12901 "clientserver",
12902#endif
12903#ifdef FEAT_CLIPBOARD
12904 "clipboard",
12905#endif
12906#ifdef FEAT_CMDL_COMPL
12907 "cmdline_compl",
12908#endif
12909#ifdef FEAT_CMDHIST
12910 "cmdline_hist",
12911#endif
12912#ifdef FEAT_COMMENTS
12913 "comments",
12914#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012915#ifdef FEAT_CONCEAL
12916 "conceal",
12917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918#ifdef FEAT_CRYPT
12919 "cryptv",
12920#endif
12921#ifdef FEAT_CSCOPE
12922 "cscope",
12923#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012924#ifdef FEAT_CURSORBIND
12925 "cursorbind",
12926#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012927#ifdef CURSOR_SHAPE
12928 "cursorshape",
12929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930#ifdef DEBUG
12931 "debug",
12932#endif
12933#ifdef FEAT_CON_DIALOG
12934 "dialog_con",
12935#endif
12936#ifdef FEAT_GUI_DIALOG
12937 "dialog_gui",
12938#endif
12939#ifdef FEAT_DIFF
12940 "diff",
12941#endif
12942#ifdef FEAT_DIGRAPHS
12943 "digraphs",
12944#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012945#ifdef FEAT_DIRECTX
12946 "directx",
12947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948#ifdef FEAT_DND
12949 "dnd",
12950#endif
12951#ifdef FEAT_EMACS_TAGS
12952 "emacs_tags",
12953#endif
12954 "eval", /* always present, of course! */
12955#ifdef FEAT_EX_EXTRA
12956 "ex_extra",
12957#endif
12958#ifdef FEAT_SEARCH_EXTRA
12959 "extra_search",
12960#endif
12961#ifdef FEAT_FKMAP
12962 "farsi",
12963#endif
12964#ifdef FEAT_SEARCHPATH
12965 "file_in_path",
12966#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012967#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012968 "filterpipe",
12969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970#ifdef FEAT_FIND_ID
12971 "find_in_path",
12972#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012973#ifdef FEAT_FLOAT
12974 "float",
12975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976#ifdef FEAT_FOLDING
12977 "folding",
12978#endif
12979#ifdef FEAT_FOOTER
12980 "footer",
12981#endif
12982#if !defined(USE_SYSTEM) && defined(UNIX)
12983 "fork",
12984#endif
12985#ifdef FEAT_GETTEXT
12986 "gettext",
12987#endif
12988#ifdef FEAT_GUI
12989 "gui",
12990#endif
12991#ifdef FEAT_GUI_ATHENA
12992# ifdef FEAT_GUI_NEXTAW
12993 "gui_neXtaw",
12994# else
12995 "gui_athena",
12996# endif
12997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998#ifdef FEAT_GUI_GTK
12999 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013002#ifdef FEAT_GUI_GNOME
13003 "gui_gnome",
13004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005#ifdef FEAT_GUI_MAC
13006 "gui_mac",
13007#endif
13008#ifdef FEAT_GUI_MOTIF
13009 "gui_motif",
13010#endif
13011#ifdef FEAT_GUI_PHOTON
13012 "gui_photon",
13013#endif
13014#ifdef FEAT_GUI_W16
13015 "gui_win16",
13016#endif
13017#ifdef FEAT_GUI_W32
13018 "gui_win32",
13019#endif
13020#ifdef FEAT_HANGULIN
13021 "hangul_input",
13022#endif
13023#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13024 "iconv",
13025#endif
13026#ifdef FEAT_INS_EXPAND
13027 "insert_expand",
13028#endif
13029#ifdef FEAT_JUMPLIST
13030 "jumplist",
13031#endif
13032#ifdef FEAT_KEYMAP
13033 "keymap",
13034#endif
13035#ifdef FEAT_LANGMAP
13036 "langmap",
13037#endif
13038#ifdef FEAT_LIBCALL
13039 "libcall",
13040#endif
13041#ifdef FEAT_LINEBREAK
13042 "linebreak",
13043#endif
13044#ifdef FEAT_LISP
13045 "lispindent",
13046#endif
13047#ifdef FEAT_LISTCMDS
13048 "listcmds",
13049#endif
13050#ifdef FEAT_LOCALMAP
13051 "localmap",
13052#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013053#ifdef FEAT_LUA
13054# ifndef DYNAMIC_LUA
13055 "lua",
13056# endif
13057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058#ifdef FEAT_MENU
13059 "menu",
13060#endif
13061#ifdef FEAT_SESSION
13062 "mksession",
13063#endif
13064#ifdef FEAT_MODIFY_FNAME
13065 "modify_fname",
13066#endif
13067#ifdef FEAT_MOUSE
13068 "mouse",
13069#endif
13070#ifdef FEAT_MOUSESHAPE
13071 "mouseshape",
13072#endif
13073#if defined(UNIX) || defined(VMS)
13074# ifdef FEAT_MOUSE_DEC
13075 "mouse_dec",
13076# endif
13077# ifdef FEAT_MOUSE_GPM
13078 "mouse_gpm",
13079# endif
13080# ifdef FEAT_MOUSE_JSB
13081 "mouse_jsbterm",
13082# endif
13083# ifdef FEAT_MOUSE_NET
13084 "mouse_netterm",
13085# endif
13086# ifdef FEAT_MOUSE_PTERM
13087 "mouse_pterm",
13088# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013089# ifdef FEAT_MOUSE_SGR
13090 "mouse_sgr",
13091# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013092# ifdef FEAT_SYSMOUSE
13093 "mouse_sysmouse",
13094# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013095# ifdef FEAT_MOUSE_URXVT
13096 "mouse_urxvt",
13097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098# ifdef FEAT_MOUSE_XTERM
13099 "mouse_xterm",
13100# endif
13101#endif
13102#ifdef FEAT_MBYTE
13103 "multi_byte",
13104#endif
13105#ifdef FEAT_MBYTE_IME
13106 "multi_byte_ime",
13107#endif
13108#ifdef FEAT_MULTI_LANG
13109 "multi_lang",
13110#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013111#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013112#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013113 "mzscheme",
13114#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116#ifdef FEAT_OLE
13117 "ole",
13118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119#ifdef FEAT_PATH_EXTRA
13120 "path_extra",
13121#endif
13122#ifdef FEAT_PERL
13123#ifndef DYNAMIC_PERL
13124 "perl",
13125#endif
13126#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013127#ifdef FEAT_PERSISTENT_UNDO
13128 "persistent_undo",
13129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130#ifdef FEAT_PYTHON
13131#ifndef DYNAMIC_PYTHON
13132 "python",
13133#endif
13134#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013135#ifdef FEAT_PYTHON3
13136#ifndef DYNAMIC_PYTHON3
13137 "python3",
13138#endif
13139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140#ifdef FEAT_POSTSCRIPT
13141 "postscript",
13142#endif
13143#ifdef FEAT_PRINTER
13144 "printer",
13145#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013146#ifdef FEAT_PROFILE
13147 "profile",
13148#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013149#ifdef FEAT_RELTIME
13150 "reltime",
13151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152#ifdef FEAT_QUICKFIX
13153 "quickfix",
13154#endif
13155#ifdef FEAT_RIGHTLEFT
13156 "rightleft",
13157#endif
13158#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13159 "ruby",
13160#endif
13161#ifdef FEAT_SCROLLBIND
13162 "scrollbind",
13163#endif
13164#ifdef FEAT_CMDL_INFO
13165 "showcmd",
13166 "cmdline_info",
13167#endif
13168#ifdef FEAT_SIGNS
13169 "signs",
13170#endif
13171#ifdef FEAT_SMARTINDENT
13172 "smartindent",
13173#endif
13174#ifdef FEAT_SNIFF
13175 "sniff",
13176#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013177#ifdef STARTUPTIME
13178 "startuptime",
13179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180#ifdef FEAT_STL_OPT
13181 "statusline",
13182#endif
13183#ifdef FEAT_SUN_WORKSHOP
13184 "sun_workshop",
13185#endif
13186#ifdef FEAT_NETBEANS_INTG
13187 "netbeans_intg",
13188#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013189#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013190 "spell",
13191#endif
13192#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 "syntax",
13194#endif
13195#if defined(USE_SYSTEM) || !defined(UNIX)
13196 "system",
13197#endif
13198#ifdef FEAT_TAG_BINS
13199 "tag_binary",
13200#endif
13201#ifdef FEAT_TAG_OLDSTATIC
13202 "tag_old_static",
13203#endif
13204#ifdef FEAT_TAG_ANYWHITE
13205 "tag_any_white",
13206#endif
13207#ifdef FEAT_TCL
13208# ifndef DYNAMIC_TCL
13209 "tcl",
13210# endif
13211#endif
13212#ifdef TERMINFO
13213 "terminfo",
13214#endif
13215#ifdef FEAT_TERMRESPONSE
13216 "termresponse",
13217#endif
13218#ifdef FEAT_TEXTOBJ
13219 "textobjects",
13220#endif
13221#ifdef HAVE_TGETENT
13222 "tgetent",
13223#endif
13224#ifdef FEAT_TITLE
13225 "title",
13226#endif
13227#ifdef FEAT_TOOLBAR
13228 "toolbar",
13229#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013230#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13231 "unnamedplus",
13232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233#ifdef FEAT_USR_CMDS
13234 "user-commands", /* was accidentally included in 5.4 */
13235 "user_commands",
13236#endif
13237#ifdef FEAT_VIMINFO
13238 "viminfo",
13239#endif
13240#ifdef FEAT_VERTSPLIT
13241 "vertsplit",
13242#endif
13243#ifdef FEAT_VIRTUALEDIT
13244 "virtualedit",
13245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247#ifdef FEAT_VISUALEXTRA
13248 "visualextra",
13249#endif
13250#ifdef FEAT_VREPLACE
13251 "vreplace",
13252#endif
13253#ifdef FEAT_WILDIGN
13254 "wildignore",
13255#endif
13256#ifdef FEAT_WILDMENU
13257 "wildmenu",
13258#endif
13259#ifdef FEAT_WINDOWS
13260 "windows",
13261#endif
13262#ifdef FEAT_WAK
13263 "winaltkeys",
13264#endif
13265#ifdef FEAT_WRITEBACKUP
13266 "writebackup",
13267#endif
13268#ifdef FEAT_XIM
13269 "xim",
13270#endif
13271#ifdef FEAT_XFONTSET
13272 "xfontset",
13273#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013274#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013275 "xpm",
13276 "xpm_w32", /* for backward compatibility */
13277#else
13278# if defined(HAVE_XPM)
13279 "xpm",
13280# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282#ifdef USE_XSMP
13283 "xsmp",
13284#endif
13285#ifdef USE_XSMP_INTERACT
13286 "xsmp_interact",
13287#endif
13288#ifdef FEAT_XCLIPBOARD
13289 "xterm_clipboard",
13290#endif
13291#ifdef FEAT_XTERM_SAVE
13292 "xterm_save",
13293#endif
13294#if defined(UNIX) && defined(FEAT_X11)
13295 "X11",
13296#endif
13297 NULL
13298 };
13299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 for (i = 0; has_list[i] != NULL; ++i)
13302 if (STRICMP(name, has_list[i]) == 0)
13303 {
13304 n = TRUE;
13305 break;
13306 }
13307
13308 if (n == FALSE)
13309 {
13310 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013311 {
13312 if (name[5] == '-'
13313 && STRLEN(name) > 11
13314 && vim_isdigit(name[6])
13315 && vim_isdigit(name[8])
13316 && vim_isdigit(name[10]))
13317 {
13318 int major = atoi((char *)name + 6);
13319 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013320
13321 /* Expect "patch-9.9.01234". */
13322 n = (major < VIM_VERSION_MAJOR
13323 || (major == VIM_VERSION_MAJOR
13324 && (minor < VIM_VERSION_MINOR
13325 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013326 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013327 }
13328 else
13329 n = has_patch(atoi((char *)name + 5));
13330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 else if (STRICMP(name, "vim_starting") == 0)
13332 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013333#ifdef FEAT_MBYTE
13334 else if (STRICMP(name, "multi_byte_encoding") == 0)
13335 n = has_mbyte;
13336#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013337#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13338 else if (STRICMP(name, "balloon_multiline") == 0)
13339 n = multiline_balloon_available();
13340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341#ifdef DYNAMIC_TCL
13342 else if (STRICMP(name, "tcl") == 0)
13343 n = tcl_enabled(FALSE);
13344#endif
13345#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13346 else if (STRICMP(name, "iconv") == 0)
13347 n = iconv_enabled(FALSE);
13348#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013349#ifdef DYNAMIC_LUA
13350 else if (STRICMP(name, "lua") == 0)
13351 n = lua_enabled(FALSE);
13352#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013353#ifdef DYNAMIC_MZSCHEME
13354 else if (STRICMP(name, "mzscheme") == 0)
13355 n = mzscheme_enabled(FALSE);
13356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357#ifdef DYNAMIC_RUBY
13358 else if (STRICMP(name, "ruby") == 0)
13359 n = ruby_enabled(FALSE);
13360#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013361#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362#ifdef DYNAMIC_PYTHON
13363 else if (STRICMP(name, "python") == 0)
13364 n = python_enabled(FALSE);
13365#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013366#endif
13367#ifdef FEAT_PYTHON3
13368#ifdef DYNAMIC_PYTHON3
13369 else if (STRICMP(name, "python3") == 0)
13370 n = python3_enabled(FALSE);
13371#endif
13372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373#ifdef DYNAMIC_PERL
13374 else if (STRICMP(name, "perl") == 0)
13375 n = perl_enabled(FALSE);
13376#endif
13377#ifdef FEAT_GUI
13378 else if (STRICMP(name, "gui_running") == 0)
13379 n = (gui.in_use || gui.starting);
13380# ifdef FEAT_GUI_W32
13381 else if (STRICMP(name, "gui_win32s") == 0)
13382 n = gui_is_win32s();
13383# endif
13384# ifdef FEAT_BROWSE
13385 else if (STRICMP(name, "browse") == 0)
13386 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13387# endif
13388#endif
13389#ifdef FEAT_SYN_HL
13390 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013391 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392#endif
13393#if defined(WIN3264)
13394 else if (STRICMP(name, "win95") == 0)
13395 n = mch_windows95();
13396#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013397#ifdef FEAT_NETBEANS_INTG
13398 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013399 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 }
13402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013403 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404}
13405
13406/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013407 * "has_key()" function
13408 */
13409 static void
13410f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013411 typval_T *argvars;
13412 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013413{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013414 if (argvars[0].v_type != VAR_DICT)
13415 {
13416 EMSG(_(e_dictreq));
13417 return;
13418 }
13419 if (argvars[0].vval.v_dict == NULL)
13420 return;
13421
13422 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013423 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013424}
13425
13426/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013427 * "haslocaldir()" function
13428 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013429 static void
13430f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013431 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013432 typval_T *rettv;
13433{
13434 rettv->vval.v_number = (curwin->w_localdir != NULL);
13435}
13436
13437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 * "hasmapto()" function
13439 */
13440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 typval_T *argvars;
13443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444{
13445 char_u *name;
13446 char_u *mode;
13447 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013448 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013450 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013451 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 mode = (char_u *)"nvo";
13453 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013456 if (argvars[2].v_type != VAR_UNKNOWN)
13457 abbr = get_tv_number(&argvars[2]);
13458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459
Bram Moolenaar2c932302006-03-18 21:42:09 +000013460 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464}
13465
13466/*
13467 * "histadd()" function
13468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013470f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013471 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473{
13474#ifdef FEAT_CMDHIST
13475 int histype;
13476 char_u *str;
13477 char_u buf[NUMBUFLEN];
13478#endif
13479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 if (check_restricted() || check_secure())
13482 return;
13483#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13485 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 if (histype >= 0)
13487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 if (*str != NUL)
13490 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013491 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 return;
13495 }
13496 }
13497#endif
13498}
13499
13500/*
13501 * "histdel()" function
13502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013505 typval_T *argvars UNUSED;
13506 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507{
13508#ifdef FEAT_CMDHIST
13509 int n;
13510 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013511 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13514 if (str == NULL)
13515 n = 0;
13516 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013518 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013519 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 else
13524 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013526 get_tv_string_buf(&argvars[1], buf));
13527 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528#endif
13529}
13530
13531/*
13532 * "histget()" function
13533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013536 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538{
13539#ifdef FEAT_CMDHIST
13540 int type;
13541 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013542 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013544 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13545 if (str == NULL)
13546 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 {
13549 type = get_histtype(str);
13550 if (argvars[1].v_type == VAR_UNKNOWN)
13551 idx = get_history_idx(type);
13552 else
13553 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13554 /* -1 on type error */
13555 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013560 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561}
13562
13563/*
13564 * "histnr()" function
13565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013568 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570{
13571 int i;
13572
13573#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013574 char_u *history = get_tv_string_chk(&argvars[0]);
13575
13576 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 if (i >= HIST_CMD && i < HIST_COUNT)
13578 i = get_history_idx(i);
13579 else
13580#endif
13581 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583}
13584
13585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 * "highlightID(name)" function
13587 */
13588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013590 typval_T *argvars;
13591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013593 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594}
13595
13596/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013597 * "highlight_exists()" function
13598 */
13599 static void
13600f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *argvars;
13602 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013603{
13604 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13605}
13606
13607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 * "hostname()" function
13609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013611f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013612 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614{
13615 char_u hostname[256];
13616
13617 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->v_type = VAR_STRING;
13619 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620}
13621
13622/*
13623 * iconv() function
13624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013627 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629{
13630#ifdef FEAT_MBYTE
13631 char_u buf1[NUMBUFLEN];
13632 char_u buf2[NUMBUFLEN];
13633 char_u *from, *to, *str;
13634 vimconv_T vimconv;
13635#endif
13636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637 rettv->v_type = VAR_STRING;
13638 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639
13640#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641 str = get_tv_string(&argvars[0]);
13642 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13643 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 vimconv.vc_type = CONV_NONE;
13645 convert_setup(&vimconv, from, to);
13646
13647 /* If the encodings are equal, no conversion needed. */
13648 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652
13653 convert_setup(&vimconv, NULL, NULL);
13654 vim_free(from);
13655 vim_free(to);
13656#endif
13657}
13658
13659/*
13660 * "indent()" function
13661 */
13662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *argvars;
13665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666{
13667 linenr_T lnum;
13668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013669 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674}
13675
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013676/*
13677 * "index()" function
13678 */
13679 static void
13680f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 typval_T *argvars;
13682 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013683{
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 list_T *l;
13685 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013686 long idx = 0;
13687 int ic = FALSE;
13688
13689 rettv->vval.v_number = -1;
13690 if (argvars[0].v_type != VAR_LIST)
13691 {
13692 EMSG(_(e_listreq));
13693 return;
13694 }
13695 l = argvars[0].vval.v_list;
13696 if (l != NULL)
13697 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013698 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013699 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 int error = FALSE;
13702
Bram Moolenaar758711c2005-02-02 23:11:38 +000013703 /* Start at specified item. Use the cached index that list_find()
13704 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013705 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013706 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013707 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 ic = get_tv_number_chk(&argvars[3], &error);
13709 if (error)
13710 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013711 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013712
Bram Moolenaar758711c2005-02-02 23:11:38 +000013713 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013714 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013715 {
13716 rettv->vval.v_number = idx;
13717 break;
13718 }
13719 }
13720}
13721
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722static int inputsecret_flag = 0;
13723
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013724static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13725
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013727 * This function is used by f_input() and f_inputdialog() functions. The third
13728 * argument to f_input() specifies the type of completion to use at the
13729 * prompt. The third argument to f_inputdialog() specifies the value to return
13730 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 */
13732 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013733get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 typval_T *argvars;
13735 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013736 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 char_u *p = NULL;
13740 int c;
13741 char_u buf[NUMBUFLEN];
13742 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013743 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013744 int xp_type = EXPAND_NOTHING;
13745 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749
13750#ifdef NO_CONSOLE_INPUT
13751 /* While starting up, there is no place to enter text. */
13752 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754#endif
13755
13756 cmd_silent = FALSE; /* Want to see the prompt. */
13757 if (prompt != NULL)
13758 {
13759 /* Only the part of the message after the last NL is considered as
13760 * prompt for the command line */
13761 p = vim_strrchr(prompt, '\n');
13762 if (p == NULL)
13763 p = prompt;
13764 else
13765 {
13766 ++p;
13767 c = *p;
13768 *p = NUL;
13769 msg_start();
13770 msg_clr_eos();
13771 msg_puts_attr(prompt, echo_attr);
13772 msg_didout = FALSE;
13773 msg_starthere();
13774 *p = c;
13775 }
13776 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778 if (argvars[1].v_type != VAR_UNKNOWN)
13779 {
13780 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13781 if (defstr != NULL)
13782 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013784 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013785 {
13786 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013787 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013788 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013789
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013790 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013791 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013792
Bram Moolenaar4463f292005-09-25 22:20:24 +000013793 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13794 if (xp_name == NULL)
13795 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013796
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013797 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013798
Bram Moolenaar4463f292005-09-25 22:20:24 +000013799 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13800 &xp_arg) == FAIL)
13801 return;
13802 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013803 }
13804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013805 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013806 {
13807# ifdef FEAT_EX_EXTRA
13808 int save_ex_normal_busy = ex_normal_busy;
13809 ex_normal_busy = 0;
13810# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013811 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013812 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13813 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013814# ifdef FEAT_EX_EXTRA
13815 ex_normal_busy = save_ex_normal_busy;
13816# endif
13817 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013818 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013819 && argvars[1].v_type != VAR_UNKNOWN
13820 && argvars[2].v_type != VAR_UNKNOWN)
13821 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13822 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013823
13824 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826 /* since the user typed this, no need to wait for return */
13827 need_wait_return = FALSE;
13828 msg_didout = FALSE;
13829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 cmd_silent = cmd_silent_save;
13831}
13832
13833/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013834 * "input()" function
13835 * Also handles inputsecret() when inputsecret is set.
13836 */
13837 static void
13838f_input(argvars, rettv)
13839 typval_T *argvars;
13840 typval_T *rettv;
13841{
13842 get_user_input(argvars, rettv, FALSE);
13843}
13844
13845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 * "inputdialog()" function
13847 */
13848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013850 typval_T *argvars;
13851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852{
13853#if defined(FEAT_GUI_TEXTDIALOG)
13854 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13855 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13856 {
13857 char_u *message;
13858 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 message = get_tv_string_chk(&argvars[0]);
13862 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013863 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013864 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 else
13866 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013867 if (message != NULL && defstr != NULL
13868 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013869 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013870 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 else
13872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 if (message != NULL && defstr != NULL
13874 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013875 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013876 rettv->vval.v_string = vim_strsave(
13877 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013879 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013881 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 }
13883 else
13884#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013885 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886}
13887
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013888/*
13889 * "inputlist()" function
13890 */
13891 static void
13892f_inputlist(argvars, rettv)
13893 typval_T *argvars;
13894 typval_T *rettv;
13895{
13896 listitem_T *li;
13897 int selected;
13898 int mouse_used;
13899
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013900#ifdef NO_CONSOLE_INPUT
13901 /* While starting up, there is no place to enter text. */
13902 if (no_console_input())
13903 return;
13904#endif
13905 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13906 {
13907 EMSG2(_(e_listarg), "inputlist()");
13908 return;
13909 }
13910
13911 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013912 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013913 lines_left = Rows; /* avoid more prompt */
13914 msg_scroll = TRUE;
13915 msg_clr_eos();
13916
13917 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13918 {
13919 msg_puts(get_tv_string(&li->li_tv));
13920 msg_putchar('\n');
13921 }
13922
13923 /* Ask for choice. */
13924 selected = prompt_for_number(&mouse_used);
13925 if (mouse_used)
13926 selected -= lines_left;
13927
13928 rettv->vval.v_number = selected;
13929}
13930
13931
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13933
13934/*
13935 * "inputrestore()" function
13936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013939 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941{
13942 if (ga_userinput.ga_len > 0)
13943 {
13944 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13946 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013947 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 }
13949 else if (p_verbose > 1)
13950 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013951 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013952 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 }
13954}
13955
13956/*
13957 * "inputsave()" function
13958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013961 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013964 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 if (ga_grow(&ga_userinput, 1) == OK)
13966 {
13967 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13968 + ga_userinput.ga_len);
13969 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013970 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 }
13972 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974}
13975
13976/*
13977 * "inputsecret()" function
13978 */
13979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013980f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013981 typval_T *argvars;
13982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983{
13984 ++cmdline_star;
13985 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013986 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 --cmdline_star;
13988 --inputsecret_flag;
13989}
13990
13991/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013992 * "insert()" function
13993 */
13994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013996 typval_T *argvars;
13997 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013998{
13999 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014000 listitem_T *item;
14001 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014003
14004 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014005 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014006 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014007 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014008 {
14009 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 before = get_tv_number_chk(&argvars[2], &error);
14011 if (error)
14012 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014013
Bram Moolenaar758711c2005-02-02 23:11:38 +000014014 if (before == l->lv_len)
14015 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014016 else
14017 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014018 item = list_find(l, before);
14019 if (item == NULL)
14020 {
14021 EMSGN(_(e_listidx), before);
14022 l = NULL;
14023 }
14024 }
14025 if (l != NULL)
14026 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014027 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014028 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014029 }
14030 }
14031}
14032
14033/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014034 * "invert(expr)" function
14035 */
14036 static void
14037f_invert(argvars, rettv)
14038 typval_T *argvars;
14039 typval_T *rettv;
14040{
14041 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14042}
14043
14044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 * "isdirectory()" function
14046 */
14047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014048f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014049 typval_T *argvars;
14050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014052 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053}
14054
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014055/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014056 * "islocked()" function
14057 */
14058 static void
14059f_islocked(argvars, rettv)
14060 typval_T *argvars;
14061 typval_T *rettv;
14062{
14063 lval_T lv;
14064 char_u *end;
14065 dictitem_T *di;
14066
14067 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014068 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14069 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014070 if (end != NULL && lv.ll_name != NULL)
14071 {
14072 if (*end != NUL)
14073 EMSG(_(e_trailing));
14074 else
14075 {
14076 if (lv.ll_tv == NULL)
14077 {
14078 if (check_changedtick(lv.ll_name))
14079 rettv->vval.v_number = 1; /* always locked */
14080 else
14081 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014082 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014083 if (di != NULL)
14084 {
14085 /* Consider a variable locked when:
14086 * 1. the variable itself is locked
14087 * 2. the value of the variable is locked.
14088 * 3. the List or Dict value is locked.
14089 */
14090 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14091 || tv_islocked(&di->di_tv));
14092 }
14093 }
14094 }
14095 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014096 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014097 else if (lv.ll_newkey != NULL)
14098 EMSG2(_(e_dictkey), lv.ll_newkey);
14099 else if (lv.ll_list != NULL)
14100 /* List item. */
14101 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14102 else
14103 /* Dictionary item. */
14104 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14105 }
14106 }
14107
14108 clear_lval(&lv);
14109}
14110
Bram Moolenaar33570922005-01-25 22:26:29 +000014111static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014112
14113/*
14114 * Turn a dict into a list:
14115 * "what" == 0: list of keys
14116 * "what" == 1: list of values
14117 * "what" == 2: list of items
14118 */
14119 static void
14120dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014121 typval_T *argvars;
14122 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014123 int what;
14124{
Bram Moolenaar33570922005-01-25 22:26:29 +000014125 list_T *l2;
14126 dictitem_T *di;
14127 hashitem_T *hi;
14128 listitem_T *li;
14129 listitem_T *li2;
14130 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014131 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014132
Bram Moolenaar8c711452005-01-14 21:53:12 +000014133 if (argvars[0].v_type != VAR_DICT)
14134 {
14135 EMSG(_(e_dictreq));
14136 return;
14137 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014138 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014139 return;
14140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014141 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014142 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014144 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014145 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014146 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014147 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014149 --todo;
14150 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014151
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014152 li = listitem_alloc();
14153 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014154 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014155 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014156
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014157 if (what == 0)
14158 {
14159 /* keys() */
14160 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014161 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014162 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14163 }
14164 else if (what == 1)
14165 {
14166 /* values() */
14167 copy_tv(&di->di_tv, &li->li_tv);
14168 }
14169 else
14170 {
14171 /* items() */
14172 l2 = list_alloc();
14173 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014174 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014175 li->li_tv.vval.v_list = l2;
14176 if (l2 == NULL)
14177 break;
14178 ++l2->lv_refcount;
14179
14180 li2 = listitem_alloc();
14181 if (li2 == NULL)
14182 break;
14183 list_append(l2, li2);
14184 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014185 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014186 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14187
14188 li2 = listitem_alloc();
14189 if (li2 == NULL)
14190 break;
14191 list_append(l2, li2);
14192 copy_tv(&di->di_tv, &li2->li_tv);
14193 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014194 }
14195 }
14196}
14197
14198/*
14199 * "items(dict)" function
14200 */
14201 static void
14202f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014203 typval_T *argvars;
14204 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014205{
14206 dict_list(argvars, rettv, 2);
14207}
14208
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014210 * "join()" function
14211 */
14212 static void
14213f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014214 typval_T *argvars;
14215 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014216{
14217 garray_T ga;
14218 char_u *sep;
14219
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014220 if (argvars[0].v_type != VAR_LIST)
14221 {
14222 EMSG(_(e_listreq));
14223 return;
14224 }
14225 if (argvars[0].vval.v_list == NULL)
14226 return;
14227 if (argvars[1].v_type == VAR_UNKNOWN)
14228 sep = (char_u *)" ";
14229 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014231
14232 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233
14234 if (sep != NULL)
14235 {
14236 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014237 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 ga_append(&ga, NUL);
14239 rettv->vval.v_string = (char_u *)ga.ga_data;
14240 }
14241 else
14242 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014243}
14244
14245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014246 * "keys()" function
14247 */
14248 static void
14249f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014250 typval_T *argvars;
14251 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014252{
14253 dict_list(argvars, rettv, 0);
14254}
14255
14256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 * "last_buffer_nr()" function.
14258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014261 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263{
14264 int n = 0;
14265 buf_T *buf;
14266
14267 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14268 if (n < buf->b_fnum)
14269 n = buf->b_fnum;
14270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014272}
14273
14274/*
14275 * "len()" function
14276 */
14277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014278f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014279 typval_T *argvars;
14280 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014281{
14282 switch (argvars[0].v_type)
14283 {
14284 case VAR_STRING:
14285 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->vval.v_number = (varnumber_T)STRLEN(
14287 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014288 break;
14289 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014291 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014292 case VAR_DICT:
14293 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14294 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014295 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014296 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014297 break;
14298 }
14299}
14300
Bram Moolenaar33570922005-01-25 22:26:29 +000014301static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014302
14303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014304libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014305 typval_T *argvars;
14306 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014307 int type;
14308{
14309#ifdef FEAT_LIBCALL
14310 char_u *string_in;
14311 char_u **string_result;
14312 int nr_result;
14313#endif
14314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014315 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014316 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014318
14319 if (check_restricted() || check_secure())
14320 return;
14321
14322#ifdef FEAT_LIBCALL
14323 /* The first two args must be strings, otherwise its meaningless */
14324 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14325 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014326 string_in = NULL;
14327 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014328 string_in = argvars[2].vval.v_string;
14329 if (type == VAR_NUMBER)
14330 string_result = NULL;
14331 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014332 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014333 if (mch_libcall(argvars[0].vval.v_string,
14334 argvars[1].vval.v_string,
14335 string_in,
14336 argvars[2].vval.v_number,
14337 string_result,
14338 &nr_result) == OK
14339 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014341 }
14342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343}
14344
14345/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346 * "libcall()" function
14347 */
14348 static void
14349f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014350 typval_T *argvars;
14351 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014352{
14353 libcall_common(argvars, rettv, VAR_STRING);
14354}
14355
14356/*
14357 * "libcallnr()" function
14358 */
14359 static void
14360f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 typval_T *argvars;
14362 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014363{
14364 libcall_common(argvars, rettv, VAR_NUMBER);
14365}
14366
14367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 * "line(string)" function
14369 */
14370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014372 typval_T *argvars;
14373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374{
14375 linenr_T lnum = 0;
14376 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014377 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014379 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 if (fp != NULL)
14381 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383}
14384
14385/*
14386 * "line2byte(lnum)" function
14387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014390 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392{
14393#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395#else
14396 linenr_T lnum;
14397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14403 if (rettv->vval.v_number >= 0)
14404 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405#endif
14406}
14407
14408/*
14409 * "lispindent(lnum)" function
14410 */
14411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014412f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415{
14416#ifdef FEAT_LISP
14417 pos_T pos;
14418 linenr_T lnum;
14419
14420 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14423 {
14424 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 curwin->w_cursor = pos;
14427 }
14428 else
14429#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431}
14432
14433/*
14434 * "localtime()" function
14435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014438 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014441 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442}
14443
Bram Moolenaar33570922005-01-25 22:26:29 +000014444static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445
14446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014448 typval_T *argvars;
14449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 int exact;
14451{
14452 char_u *keys;
14453 char_u *which;
14454 char_u buf[NUMBUFLEN];
14455 char_u *keys_buf = NULL;
14456 char_u *rhs;
14457 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014458 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014459 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014460 mapblock_T *mp;
14461 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462
14463 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->v_type = VAR_STRING;
14465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 if (*keys == NUL)
14469 return;
14470
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014471 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014473 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014474 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014475 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014476 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014477 if (argvars[3].v_type != VAR_UNKNOWN)
14478 get_dict = get_tv_number(&argvars[3]);
14479 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 else
14482 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014483 if (which == NULL)
14484 return;
14485
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 mode = get_map_mode(&which, 0);
14487
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014488 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014489 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014491
14492 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014494 /* Return a string. */
14495 if (rhs != NULL)
14496 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497
Bram Moolenaarbd743252010-10-20 21:23:33 +020014498 }
14499 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14500 {
14501 /* Return a dictionary. */
14502 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14503 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14504 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505
Bram Moolenaarbd743252010-10-20 21:23:33 +020014506 dict_add_nr_str(dict, "lhs", 0L, lhs);
14507 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14508 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14509 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14510 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14511 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14512 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014513 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014514 dict_add_nr_str(dict, "mode", 0L, mapmode);
14515
14516 vim_free(lhs);
14517 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 }
14519}
14520
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014521#ifdef FEAT_FLOAT
14522/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014523 * "log()" function
14524 */
14525 static void
14526f_log(argvars, rettv)
14527 typval_T *argvars;
14528 typval_T *rettv;
14529{
14530 float_T f;
14531
14532 rettv->v_type = VAR_FLOAT;
14533 if (get_float_arg(argvars, &f) == OK)
14534 rettv->vval.v_float = log(f);
14535 else
14536 rettv->vval.v_float = 0.0;
14537}
14538
14539/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014540 * "log10()" function
14541 */
14542 static void
14543f_log10(argvars, rettv)
14544 typval_T *argvars;
14545 typval_T *rettv;
14546{
14547 float_T f;
14548
14549 rettv->v_type = VAR_FLOAT;
14550 if (get_float_arg(argvars, &f) == OK)
14551 rettv->vval.v_float = log10(f);
14552 else
14553 rettv->vval.v_float = 0.0;
14554}
14555#endif
14556
Bram Moolenaar1dced572012-04-05 16:54:08 +020014557#ifdef FEAT_LUA
14558/*
14559 * "luaeval()" function
14560 */
14561 static void
14562f_luaeval(argvars, rettv)
14563 typval_T *argvars;
14564 typval_T *rettv;
14565{
14566 char_u *str;
14567 char_u buf[NUMBUFLEN];
14568
14569 str = get_tv_string_buf(&argvars[0], buf);
14570 do_luaeval(str, argvars + 1, rettv);
14571}
14572#endif
14573
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014575 * "map()" function
14576 */
14577 static void
14578f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 typval_T *argvars;
14580 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014581{
14582 filter_map(argvars, rettv, TRUE);
14583}
14584
14585/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014586 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 */
14588 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014589f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014590 typval_T *argvars;
14591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594}
14595
14596/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 */
14599 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014601 typval_T *argvars;
14602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605}
14606
Bram Moolenaar33570922005-01-25 22:26:29 +000014607static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608
14609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014610find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014611 typval_T *argvars;
14612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 int type;
14614{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014615 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014616 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014617 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 char_u *pat;
14619 regmatch_T regmatch;
14620 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014621 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 char_u *save_cpo;
14623 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014624 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014625 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014626 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014627 list_T *l = NULL;
14628 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014629 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014630 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631
14632 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14633 save_cpo = p_cpo;
14634 p_cpo = (char_u *)"";
14635
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014636 rettv->vval.v_number = -1;
14637 if (type == 3)
14638 {
14639 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014640 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014641 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014642 }
14643 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014645 rettv->v_type = VAR_STRING;
14646 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014649 if (argvars[0].v_type == VAR_LIST)
14650 {
14651 if ((l = argvars[0].vval.v_list) == NULL)
14652 goto theend;
14653 li = l->lv_first;
14654 }
14655 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014656 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014657 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014658 len = (long)STRLEN(str);
14659 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14662 if (pat == NULL)
14663 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014664
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014665 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 int error = FALSE;
14668
14669 start = get_tv_number_chk(&argvars[2], &error);
14670 if (error)
14671 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014672 if (l != NULL)
14673 {
14674 li = list_find(l, start);
14675 if (li == NULL)
14676 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014677 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014678 }
14679 else
14680 {
14681 if (start < 0)
14682 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014683 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014684 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014685 /* When "count" argument is there ignore matches before "start",
14686 * otherwise skip part of the string. Differs when pattern is "^"
14687 * or "\<". */
14688 if (argvars[3].v_type != VAR_UNKNOWN)
14689 startcol = start;
14690 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014691 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014692 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014693 len -= start;
14694 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014695 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014696
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014697 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 nth = get_tv_number_chk(&argvars[3], &error);
14699 if (error)
14700 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701 }
14702
14703 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14704 if (regmatch.regprog != NULL)
14705 {
14706 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014707
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014708 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014709 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014710 if (l != NULL)
14711 {
14712 if (li == NULL)
14713 {
14714 match = FALSE;
14715 break;
14716 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014717 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014718 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014719 if (str == NULL)
14720 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014721 }
14722
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014723 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014724
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014725 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014726 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014727 if (l == NULL && !match)
14728 break;
14729
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014730 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014731 if (l != NULL)
14732 {
14733 li = li->li_next;
14734 ++idx;
14735 }
14736 else
14737 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014738#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014739 startcol = (colnr_T)(regmatch.startp[0]
14740 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014741#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014742 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014743#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014744 if (startcol > (colnr_T)len
14745 || str + startcol <= regmatch.startp[0])
14746 {
14747 match = FALSE;
14748 break;
14749 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014750 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014751 }
14752
14753 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014755 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014756 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014757 int i;
14758
14759 /* return list with matched string and submatches */
14760 for (i = 0; i < NSUBEXP; ++i)
14761 {
14762 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014763 {
14764 if (list_append_string(rettv->vval.v_list,
14765 (char_u *)"", 0) == FAIL)
14766 break;
14767 }
14768 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014769 regmatch.startp[i],
14770 (int)(regmatch.endp[i] - regmatch.startp[i]))
14771 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014772 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014773 }
14774 }
14775 else if (type == 2)
14776 {
14777 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014778 if (l != NULL)
14779 copy_tv(&li->li_tv, rettv);
14780 else
14781 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014783 }
14784 else if (l != NULL)
14785 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786 else
14787 {
14788 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014789 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 (varnumber_T)(regmatch.startp[0] - str);
14791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014794 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 }
14796 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014797 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 }
14799
14800theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014801 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 p_cpo = save_cpo;
14803}
14804
14805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014806 * "match()" function
14807 */
14808 static void
14809f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014810 typval_T *argvars;
14811 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014812{
14813 find_some_match(argvars, rettv, 1);
14814}
14815
14816/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014817 * "matchadd()" function
14818 */
14819 static void
14820f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014821 typval_T *argvars UNUSED;
14822 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014823{
14824#ifdef FEAT_SEARCH_EXTRA
14825 char_u buf[NUMBUFLEN];
14826 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14827 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14828 int prio = 10; /* default priority */
14829 int id = -1;
14830 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014831 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014832
14833 rettv->vval.v_number = -1;
14834
14835 if (grp == NULL || pat == NULL)
14836 return;
14837 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014838 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014839 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014840 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014841 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014842 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014843 if (argvars[4].v_type != VAR_UNKNOWN)
14844 {
14845 if (argvars[4].v_type != VAR_DICT)
14846 {
14847 EMSG(_(e_dictreq));
14848 return;
14849 }
14850 if (dict_find(argvars[4].vval.v_dict,
14851 (char_u *)"conceal", -1) != NULL)
14852 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14853 (char_u *)"conceal", FALSE);
14854 }
14855 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014856 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014857 if (error == TRUE)
14858 return;
14859 if (id >= 1 && id <= 3)
14860 {
14861 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14862 return;
14863 }
14864
Bram Moolenaar6561d522015-07-21 15:48:27 +020014865 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14866 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014867#endif
14868}
14869
14870/*
14871 * "matchaddpos()" function
14872 */
14873 static void
14874f_matchaddpos(argvars, rettv)
14875 typval_T *argvars UNUSED;
14876 typval_T *rettv UNUSED;
14877{
14878#ifdef FEAT_SEARCH_EXTRA
14879 char_u buf[NUMBUFLEN];
14880 char_u *group;
14881 int prio = 10;
14882 int id = -1;
14883 int error = FALSE;
14884 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014885 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014886
14887 rettv->vval.v_number = -1;
14888
14889 group = get_tv_string_buf_chk(&argvars[0], buf);
14890 if (group == NULL)
14891 return;
14892
14893 if (argvars[1].v_type != VAR_LIST)
14894 {
14895 EMSG2(_(e_listarg), "matchaddpos()");
14896 return;
14897 }
14898 l = argvars[1].vval.v_list;
14899 if (l == NULL)
14900 return;
14901
14902 if (argvars[2].v_type != VAR_UNKNOWN)
14903 {
14904 prio = get_tv_number_chk(&argvars[2], &error);
14905 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014906 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014907 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014908 if (argvars[4].v_type != VAR_UNKNOWN)
14909 {
14910 if (argvars[4].v_type != VAR_DICT)
14911 {
14912 EMSG(_(e_dictreq));
14913 return;
14914 }
14915 if (dict_find(argvars[4].vval.v_dict,
14916 (char_u *)"conceal", -1) != NULL)
14917 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14918 (char_u *)"conceal", FALSE);
14919 }
14920 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014921 }
14922 if (error == TRUE)
14923 return;
14924
14925 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14926 if (id == 1 || id == 2)
14927 {
14928 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14929 return;
14930 }
14931
Bram Moolenaar6561d522015-07-21 15:48:27 +020014932 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14933 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014934#endif
14935}
14936
14937/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014938 * "matcharg()" function
14939 */
14940 static void
14941f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014942 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014943 typval_T *rettv;
14944{
14945 if (rettv_list_alloc(rettv) == OK)
14946 {
14947#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014948 int id = get_tv_number(&argvars[0]);
14949 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014950
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014951 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014952 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014953 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14954 {
14955 list_append_string(rettv->vval.v_list,
14956 syn_id2name(m->hlg_id), -1);
14957 list_append_string(rettv->vval.v_list, m->pattern, -1);
14958 }
14959 else
14960 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014961 list_append_string(rettv->vval.v_list, NULL, -1);
14962 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014963 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014964 }
14965#endif
14966 }
14967}
14968
14969/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014970 * "matchdelete()" function
14971 */
14972 static void
14973f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014974 typval_T *argvars UNUSED;
14975 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014976{
14977#ifdef FEAT_SEARCH_EXTRA
14978 rettv->vval.v_number = match_delete(curwin,
14979 (int)get_tv_number(&argvars[0]), TRUE);
14980#endif
14981}
14982
14983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 * "matchend()" function
14985 */
14986 static void
14987f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014988 typval_T *argvars;
14989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014990{
14991 find_some_match(argvars, rettv, 0);
14992}
14993
14994/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014995 * "matchlist()" function
14996 */
14997 static void
14998f_matchlist(argvars, rettv)
14999 typval_T *argvars;
15000 typval_T *rettv;
15001{
15002 find_some_match(argvars, rettv, 3);
15003}
15004
15005/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006 * "matchstr()" function
15007 */
15008 static void
15009f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015010 typval_T *argvars;
15011 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015012{
15013 find_some_match(argvars, rettv, 2);
15014}
15015
Bram Moolenaar33570922005-01-25 22:26:29 +000015016static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015017
15018 static void
15019max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015020 typval_T *argvars;
15021 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015022 int domax;
15023{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015024 long n = 0;
15025 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015026 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015027
15028 if (argvars[0].v_type == VAR_LIST)
15029 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 list_T *l;
15031 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015032
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015033 l = argvars[0].vval.v_list;
15034 if (l != NULL)
15035 {
15036 li = l->lv_first;
15037 if (li != NULL)
15038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015039 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015040 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015041 {
15042 li = li->li_next;
15043 if (li == NULL)
15044 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015046 if (domax ? i > n : i < n)
15047 n = i;
15048 }
15049 }
15050 }
15051 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015052 else if (argvars[0].v_type == VAR_DICT)
15053 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015054 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015055 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015056 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015057 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015058
15059 d = argvars[0].vval.v_dict;
15060 if (d != NULL)
15061 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015062 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015063 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015064 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015065 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015066 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015067 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015069 if (first)
15070 {
15071 n = i;
15072 first = FALSE;
15073 }
15074 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015075 n = i;
15076 }
15077 }
15078 }
15079 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015080 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015081 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015082 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015083}
15084
15085/*
15086 * "max()" function
15087 */
15088 static void
15089f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015090 typval_T *argvars;
15091 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015092{
15093 max_min(argvars, rettv, TRUE);
15094}
15095
15096/*
15097 * "min()" function
15098 */
15099 static void
15100f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 typval_T *argvars;
15102 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015103{
15104 max_min(argvars, rettv, FALSE);
15105}
15106
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015107static int mkdir_recurse __ARGS((char_u *dir, int prot));
15108
15109/*
15110 * Create the directory in which "dir" is located, and higher levels when
15111 * needed.
15112 */
15113 static int
15114mkdir_recurse(dir, prot)
15115 char_u *dir;
15116 int prot;
15117{
15118 char_u *p;
15119 char_u *updir;
15120 int r = FAIL;
15121
15122 /* Get end of directory name in "dir".
15123 * We're done when it's "/" or "c:/". */
15124 p = gettail_sep(dir);
15125 if (p <= get_past_head(dir))
15126 return OK;
15127
15128 /* If the directory exists we're done. Otherwise: create it.*/
15129 updir = vim_strnsave(dir, (int)(p - dir));
15130 if (updir == NULL)
15131 return FAIL;
15132 if (mch_isdir(updir))
15133 r = OK;
15134 else if (mkdir_recurse(updir, prot) == OK)
15135 r = vim_mkdir_emsg(updir, prot);
15136 vim_free(updir);
15137 return r;
15138}
15139
15140#ifdef vim_mkdir
15141/*
15142 * "mkdir()" function
15143 */
15144 static void
15145f_mkdir(argvars, rettv)
15146 typval_T *argvars;
15147 typval_T *rettv;
15148{
15149 char_u *dir;
15150 char_u buf[NUMBUFLEN];
15151 int prot = 0755;
15152
15153 rettv->vval.v_number = FAIL;
15154 if (check_restricted() || check_secure())
15155 return;
15156
15157 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015158 if (*dir == NUL)
15159 rettv->vval.v_number = FAIL;
15160 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015161 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015162 if (*gettail(dir) == NUL)
15163 /* remove trailing slashes */
15164 *gettail_sep(dir) = NUL;
15165
15166 if (argvars[1].v_type != VAR_UNKNOWN)
15167 {
15168 if (argvars[2].v_type != VAR_UNKNOWN)
15169 prot = get_tv_number_chk(&argvars[2], NULL);
15170 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15171 mkdir_recurse(dir, prot);
15172 }
15173 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015174 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015175}
15176#endif
15177
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 * "mode()" function
15180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015182f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015183 typval_T *argvars;
15184 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015186 char_u buf[3];
15187
15188 buf[1] = NUL;
15189 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 if (VIsual_active)
15192 {
15193 if (VIsual_select)
15194 buf[0] = VIsual_mode + 's' - 'v';
15195 else
15196 buf[0] = VIsual_mode;
15197 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015198 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015199 || State == CONFIRM)
15200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015202 if (State == ASKMORE)
15203 buf[1] = 'm';
15204 else if (State == CONFIRM)
15205 buf[1] = '?';
15206 }
15207 else if (State == EXTERNCMD)
15208 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 else if (State & INSERT)
15210 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015211#ifdef FEAT_VREPLACE
15212 if (State & VREPLACE_FLAG)
15213 {
15214 buf[0] = 'R';
15215 buf[1] = 'v';
15216 }
15217 else
15218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 if (State & REPLACE_FLAG)
15220 buf[0] = 'R';
15221 else
15222 buf[0] = 'i';
15223 }
15224 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015227 if (exmode_active)
15228 buf[1] = 'v';
15229 }
15230 else if (exmode_active)
15231 {
15232 buf[0] = 'c';
15233 buf[1] = 'e';
15234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015238 if (finish_op)
15239 buf[1] = 'o';
15240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015242 /* Clear out the minor mode when the argument is not a non-zero number or
15243 * non-empty string. */
15244 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015245 buf[1] = NUL;
15246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247 rettv->vval.v_string = vim_strsave(buf);
15248 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249}
15250
Bram Moolenaar429fa852013-04-15 12:27:36 +020015251#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015252/*
15253 * "mzeval()" function
15254 */
15255 static void
15256f_mzeval(argvars, rettv)
15257 typval_T *argvars;
15258 typval_T *rettv;
15259{
15260 char_u *str;
15261 char_u buf[NUMBUFLEN];
15262
15263 str = get_tv_string_buf(&argvars[0], buf);
15264 do_mzeval(str, rettv);
15265}
Bram Moolenaar75676462013-01-30 14:55:42 +010015266
15267 void
15268mzscheme_call_vim(name, args, rettv)
15269 char_u *name;
15270 typval_T *args;
15271 typval_T *rettv;
15272{
15273 typval_T argvars[3];
15274
15275 argvars[0].v_type = VAR_STRING;
15276 argvars[0].vval.v_string = name;
15277 copy_tv(args, &argvars[1]);
15278 argvars[2].v_type = VAR_UNKNOWN;
15279 f_call(argvars, rettv);
15280 clear_tv(&argvars[1]);
15281}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015282#endif
15283
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285 * "nextnonblank()" function
15286 */
15287 static void
15288f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015289 typval_T *argvars;
15290 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015291{
15292 linenr_T lnum;
15293
15294 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015296 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015297 {
15298 lnum = 0;
15299 break;
15300 }
15301 if (*skipwhite(ml_get(lnum)) != NUL)
15302 break;
15303 }
15304 rettv->vval.v_number = lnum;
15305}
15306
15307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 * "nr2char()" function
15309 */
15310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015312 typval_T *argvars;
15313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314{
15315 char_u buf[NUMBUFLEN];
15316
15317#ifdef FEAT_MBYTE
15318 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015319 {
15320 int utf8 = 0;
15321
15322 if (argvars[1].v_type != VAR_UNKNOWN)
15323 utf8 = get_tv_number_chk(&argvars[1], NULL);
15324 if (utf8)
15325 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15326 else
15327 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329 else
15330#endif
15331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015332 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 buf[1] = NUL;
15334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015335 rettv->v_type = VAR_STRING;
15336 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015337}
15338
15339/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015340 * "or(expr, expr)" function
15341 */
15342 static void
15343f_or(argvars, rettv)
15344 typval_T *argvars;
15345 typval_T *rettv;
15346{
15347 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15348 | get_tv_number_chk(&argvars[1], NULL);
15349}
15350
15351/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015352 * "pathshorten()" function
15353 */
15354 static void
15355f_pathshorten(argvars, rettv)
15356 typval_T *argvars;
15357 typval_T *rettv;
15358{
15359 char_u *p;
15360
15361 rettv->v_type = VAR_STRING;
15362 p = get_tv_string_chk(&argvars[0]);
15363 if (p == NULL)
15364 rettv->vval.v_string = NULL;
15365 else
15366 {
15367 p = vim_strsave(p);
15368 rettv->vval.v_string = p;
15369 if (p != NULL)
15370 shorten_dir(p);
15371 }
15372}
15373
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015374#ifdef FEAT_FLOAT
15375/*
15376 * "pow()" function
15377 */
15378 static void
15379f_pow(argvars, rettv)
15380 typval_T *argvars;
15381 typval_T *rettv;
15382{
15383 float_T fx, fy;
15384
15385 rettv->v_type = VAR_FLOAT;
15386 if (get_float_arg(argvars, &fx) == OK
15387 && get_float_arg(&argvars[1], &fy) == OK)
15388 rettv->vval.v_float = pow(fx, fy);
15389 else
15390 rettv->vval.v_float = 0.0;
15391}
15392#endif
15393
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015395 * "prevnonblank()" function
15396 */
15397 static void
15398f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015399 typval_T *argvars;
15400 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015401{
15402 linenr_T lnum;
15403
15404 lnum = get_tv_lnum(argvars);
15405 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15406 lnum = 0;
15407 else
15408 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15409 --lnum;
15410 rettv->vval.v_number = lnum;
15411}
15412
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015413#ifdef HAVE_STDARG_H
15414/* This dummy va_list is here because:
15415 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15416 * - locally in the function results in a "used before set" warning
15417 * - using va_start() to initialize it gives "function with fixed args" error */
15418static va_list ap;
15419#endif
15420
Bram Moolenaar8c711452005-01-14 21:53:12 +000015421/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015422 * "printf()" function
15423 */
15424 static void
15425f_printf(argvars, rettv)
15426 typval_T *argvars;
15427 typval_T *rettv;
15428{
15429 rettv->v_type = VAR_STRING;
15430 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015431#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015432 {
15433 char_u buf[NUMBUFLEN];
15434 int len;
15435 char_u *s;
15436 int saved_did_emsg = did_emsg;
15437 char *fmt;
15438
15439 /* Get the required length, allocate the buffer and do it for real. */
15440 did_emsg = FALSE;
15441 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015442 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015443 if (!did_emsg)
15444 {
15445 s = alloc(len + 1);
15446 if (s != NULL)
15447 {
15448 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015449 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015450 }
15451 }
15452 did_emsg |= saved_did_emsg;
15453 }
15454#endif
15455}
15456
15457/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015458 * "pumvisible()" function
15459 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015460 static void
15461f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015462 typval_T *argvars UNUSED;
15463 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015464{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015465#ifdef FEAT_INS_EXPAND
15466 if (pum_visible())
15467 rettv->vval.v_number = 1;
15468#endif
15469}
15470
Bram Moolenaardb913952012-06-29 12:54:53 +020015471#ifdef FEAT_PYTHON3
15472/*
15473 * "py3eval()" function
15474 */
15475 static void
15476f_py3eval(argvars, rettv)
15477 typval_T *argvars;
15478 typval_T *rettv;
15479{
15480 char_u *str;
15481 char_u buf[NUMBUFLEN];
15482
15483 str = get_tv_string_buf(&argvars[0], buf);
15484 do_py3eval(str, rettv);
15485}
15486#endif
15487
15488#ifdef FEAT_PYTHON
15489/*
15490 * "pyeval()" function
15491 */
15492 static void
15493f_pyeval(argvars, rettv)
15494 typval_T *argvars;
15495 typval_T *rettv;
15496{
15497 char_u *str;
15498 char_u buf[NUMBUFLEN];
15499
15500 str = get_tv_string_buf(&argvars[0], buf);
15501 do_pyeval(str, rettv);
15502}
15503#endif
15504
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015506 * "range()" function
15507 */
15508 static void
15509f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015510 typval_T *argvars;
15511 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015512{
15513 long start;
15514 long end;
15515 long stride = 1;
15516 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015517 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015519 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015520 if (argvars[1].v_type == VAR_UNKNOWN)
15521 {
15522 end = start - 1;
15523 start = 0;
15524 }
15525 else
15526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015527 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015529 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015530 }
15531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015532 if (error)
15533 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015534 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015535 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015536 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015537 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015538 else
15539 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015540 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015541 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015542 if (list_append_number(rettv->vval.v_list,
15543 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015544 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015545 }
15546}
15547
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548/*
15549 * "readfile()" function
15550 */
15551 static void
15552f_readfile(argvars, rettv)
15553 typval_T *argvars;
15554 typval_T *rettv;
15555{
15556 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015557 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015558 char_u *fname;
15559 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015560 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15561 int io_size = sizeof(buf);
15562 int readlen; /* size of last fread() */
15563 char_u *prev = NULL; /* previously read bytes, if any */
15564 long prevlen = 0; /* length of data in prev */
15565 long prevsize = 0; /* size of prev buffer */
15566 long maxline = MAXLNUM;
15567 long cnt = 0;
15568 char_u *p; /* position in buf */
15569 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015570
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015571 if (argvars[1].v_type != VAR_UNKNOWN)
15572 {
15573 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15574 binary = TRUE;
15575 if (argvars[2].v_type != VAR_UNKNOWN)
15576 maxline = get_tv_number(&argvars[2]);
15577 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015578
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015579 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015580 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015581
15582 /* Always open the file in binary mode, library functions have a mind of
15583 * their own about CR-LF conversion. */
15584 fname = get_tv_string(&argvars[0]);
15585 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15586 {
15587 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15588 return;
15589 }
15590
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015591 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015592 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015593 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015594
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015595 /* This for loop processes what was read, but is also entered at end
15596 * of file so that either:
15597 * - an incomplete line gets written
15598 * - a "binary" file gets an empty line at the end if it ends in a
15599 * newline. */
15600 for (p = buf, start = buf;
15601 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15602 ++p)
15603 {
15604 if (*p == '\n' || readlen <= 0)
15605 {
15606 listitem_T *li;
15607 char_u *s = NULL;
15608 long_u len = p - start;
15609
15610 /* Finished a line. Remove CRs before NL. */
15611 if (readlen > 0 && !binary)
15612 {
15613 while (len > 0 && start[len - 1] == '\r')
15614 --len;
15615 /* removal may cross back to the "prev" string */
15616 if (len == 0)
15617 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15618 --prevlen;
15619 }
15620 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015621 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015622 else
15623 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015624 /* Change "prev" buffer to be the right size. This way
15625 * the bytes are only copied once, and very long lines are
15626 * allocated only once. */
15627 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015628 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015629 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015630 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015631 prev = NULL; /* the list will own the string */
15632 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015633 }
15634 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015635 if (s == NULL)
15636 {
15637 do_outofmem_msg((long_u) prevlen + len + 1);
15638 failed = TRUE;
15639 break;
15640 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015641
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015642 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015643 {
15644 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015645 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015646 break;
15647 }
15648 li->li_tv.v_type = VAR_STRING;
15649 li->li_tv.v_lock = 0;
15650 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015651 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015652
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015653 start = p + 1; /* step over newline */
15654 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015655 break;
15656 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015657 else if (*p == NUL)
15658 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015659#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015660 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15661 * when finding the BF and check the previous two bytes. */
15662 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015663 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015664 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15665 * + 1, these may be in the "prev" string. */
15666 char_u back1 = p >= buf + 1 ? p[-1]
15667 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15668 char_u back2 = p >= buf + 2 ? p[-2]
15669 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15670 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015671
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015672 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015673 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015674 char_u *dest = p - 2;
15675
15676 /* Usually a BOM is at the beginning of a file, and so at
15677 * the beginning of a line; then we can just step over it.
15678 */
15679 if (start == dest)
15680 start = p + 1;
15681 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015682 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015683 /* have to shuffle buf to close gap */
15684 int adjust_prevlen = 0;
15685
15686 if (dest < buf)
15687 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015688 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015689 dest = buf;
15690 }
15691 if (readlen > p - buf + 1)
15692 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15693 readlen -= 3 - adjust_prevlen;
15694 prevlen -= adjust_prevlen;
15695 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015696 }
15697 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015698 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015699#endif
15700 } /* for */
15701
15702 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15703 break;
15704 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015705 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015706 /* There's part of a line in buf, store it in "prev". */
15707 if (p - start + prevlen >= prevsize)
15708 {
15709 /* need bigger "prev" buffer */
15710 char_u *newprev;
15711
15712 /* A common use case is ordinary text files and "prev" gets a
15713 * fragment of a line, so the first allocation is made
15714 * small, to avoid repeatedly 'allocing' large and
15715 * 'reallocing' small. */
15716 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015717 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015718 else
15719 {
15720 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015721 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015722 prevsize = grow50pc > growmin ? grow50pc : growmin;
15723 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015724 newprev = prev == NULL ? alloc(prevsize)
15725 : vim_realloc(prev, prevsize);
15726 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015727 {
15728 do_outofmem_msg((long_u)prevsize);
15729 failed = TRUE;
15730 break;
15731 }
15732 prev = newprev;
15733 }
15734 /* Add the line part to end of "prev". */
15735 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015736 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015737 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015738 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015739
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015740 /*
15741 * For a negative line count use only the lines at the end of the file,
15742 * free the rest.
15743 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015744 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015745 while (cnt > -maxline)
15746 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015747 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015748 --cnt;
15749 }
15750
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015751 if (failed)
15752 {
15753 list_free(rettv->vval.v_list, TRUE);
15754 /* readfile doc says an empty list is returned on error */
15755 rettv->vval.v_list = list_alloc();
15756 }
15757
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015758 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015759 fclose(fd);
15760}
15761
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015762#if defined(FEAT_RELTIME)
15763static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15764
15765/*
15766 * Convert a List to proftime_T.
15767 * Return FAIL when there is something wrong.
15768 */
15769 static int
15770list2proftime(arg, tm)
15771 typval_T *arg;
15772 proftime_T *tm;
15773{
15774 long n1, n2;
15775 int error = FALSE;
15776
15777 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15778 || arg->vval.v_list->lv_len != 2)
15779 return FAIL;
15780 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15781 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15782# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015783 tm->HighPart = n1;
15784 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015785# else
15786 tm->tv_sec = n1;
15787 tm->tv_usec = n2;
15788# endif
15789 return error ? FAIL : OK;
15790}
15791#endif /* FEAT_RELTIME */
15792
15793/*
15794 * "reltime()" function
15795 */
15796 static void
15797f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015798 typval_T *argvars UNUSED;
15799 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015800{
15801#ifdef FEAT_RELTIME
15802 proftime_T res;
15803 proftime_T start;
15804
15805 if (argvars[0].v_type == VAR_UNKNOWN)
15806 {
15807 /* No arguments: get current time. */
15808 profile_start(&res);
15809 }
15810 else if (argvars[1].v_type == VAR_UNKNOWN)
15811 {
15812 if (list2proftime(&argvars[0], &res) == FAIL)
15813 return;
15814 profile_end(&res);
15815 }
15816 else
15817 {
15818 /* Two arguments: compute the difference. */
15819 if (list2proftime(&argvars[0], &start) == FAIL
15820 || list2proftime(&argvars[1], &res) == FAIL)
15821 return;
15822 profile_sub(&res, &start);
15823 }
15824
15825 if (rettv_list_alloc(rettv) == OK)
15826 {
15827 long n1, n2;
15828
15829# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015830 n1 = res.HighPart;
15831 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015832# else
15833 n1 = res.tv_sec;
15834 n2 = res.tv_usec;
15835# endif
15836 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15837 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15838 }
15839#endif
15840}
15841
15842/*
15843 * "reltimestr()" function
15844 */
15845 static void
15846f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015847 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015848 typval_T *rettv;
15849{
15850#ifdef FEAT_RELTIME
15851 proftime_T tm;
15852#endif
15853
15854 rettv->v_type = VAR_STRING;
15855 rettv->vval.v_string = NULL;
15856#ifdef FEAT_RELTIME
15857 if (list2proftime(&argvars[0], &tm) == OK)
15858 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15859#endif
15860}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015861
Bram Moolenaar0d660222005-01-07 21:51:51 +000015862#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15863static void make_connection __ARGS((void));
15864static int check_connection __ARGS((void));
15865
15866 static void
15867make_connection()
15868{
15869 if (X_DISPLAY == NULL
15870# ifdef FEAT_GUI
15871 && !gui.in_use
15872# endif
15873 )
15874 {
15875 x_force_connect = TRUE;
15876 setup_term_clip();
15877 x_force_connect = FALSE;
15878 }
15879}
15880
15881 static int
15882check_connection()
15883{
15884 make_connection();
15885 if (X_DISPLAY == NULL)
15886 {
15887 EMSG(_("E240: No connection to Vim server"));
15888 return FAIL;
15889 }
15890 return OK;
15891}
15892#endif
15893
15894#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015895static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015896
15897 static void
15898remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015899 typval_T *argvars;
15900 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015901 int expr;
15902{
15903 char_u *server_name;
15904 char_u *keys;
15905 char_u *r = NULL;
15906 char_u buf[NUMBUFLEN];
15907# ifdef WIN32
15908 HWND w;
15909# else
15910 Window w;
15911# endif
15912
15913 if (check_restricted() || check_secure())
15914 return;
15915
15916# ifdef FEAT_X11
15917 if (check_connection() == FAIL)
15918 return;
15919# endif
15920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015921 server_name = get_tv_string_chk(&argvars[0]);
15922 if (server_name == NULL)
15923 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015924 keys = get_tv_string_buf(&argvars[1], buf);
15925# ifdef WIN32
15926 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15927# else
15928 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15929 < 0)
15930# endif
15931 {
15932 if (r != NULL)
15933 EMSG(r); /* sending worked but evaluation failed */
15934 else
15935 EMSG2(_("E241: Unable to send to %s"), server_name);
15936 return;
15937 }
15938
15939 rettv->vval.v_string = r;
15940
15941 if (argvars[2].v_type != VAR_UNKNOWN)
15942 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015943 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015944 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015945 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015947 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015948 v.di_tv.v_type = VAR_STRING;
15949 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015950 idvar = get_tv_string_chk(&argvars[2]);
15951 if (idvar != NULL)
15952 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015953 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015954 }
15955}
15956#endif
15957
15958/*
15959 * "remote_expr()" function
15960 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015961 static void
15962f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015963 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015964 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015965{
15966 rettv->v_type = VAR_STRING;
15967 rettv->vval.v_string = NULL;
15968#ifdef FEAT_CLIENTSERVER
15969 remote_common(argvars, rettv, TRUE);
15970#endif
15971}
15972
15973/*
15974 * "remote_foreground()" function
15975 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015976 static void
15977f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015978 typval_T *argvars UNUSED;
15979 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015980{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015981#ifdef FEAT_CLIENTSERVER
15982# ifdef WIN32
15983 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015984 {
15985 char_u *server_name = get_tv_string_chk(&argvars[0]);
15986
15987 if (server_name != NULL)
15988 serverForeground(server_name);
15989 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015990# else
15991 /* Send a foreground() expression to the server. */
15992 argvars[1].v_type = VAR_STRING;
15993 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15994 argvars[2].v_type = VAR_UNKNOWN;
15995 remote_common(argvars, rettv, TRUE);
15996 vim_free(argvars[1].vval.v_string);
15997# endif
15998#endif
15999}
16000
Bram Moolenaar0d660222005-01-07 21:51:51 +000016001 static void
16002f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016003 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016005{
16006#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016007 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016008 char_u *s = NULL;
16009# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016010 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016011# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016012 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016013
16014 if (check_restricted() || check_secure())
16015 {
16016 rettv->vval.v_number = -1;
16017 return;
16018 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 serverid = get_tv_string_chk(&argvars[0]);
16020 if (serverid == NULL)
16021 {
16022 rettv->vval.v_number = -1;
16023 return; /* type error; errmsg already given */
16024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016025# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016026 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016027 if (n == 0)
16028 rettv->vval.v_number = -1;
16029 else
16030 {
16031 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16032 rettv->vval.v_number = (s != NULL);
16033 }
16034# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016035 if (check_connection() == FAIL)
16036 return;
16037
16038 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016039 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016040# endif
16041
16042 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016044 char_u *retvar;
16045
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 v.di_tv.v_type = VAR_STRING;
16047 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016048 retvar = get_tv_string_chk(&argvars[1]);
16049 if (retvar != NULL)
16050 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016051 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016052 }
16053#else
16054 rettv->vval.v_number = -1;
16055#endif
16056}
16057
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058 static void
16059f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016060 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062{
16063 char_u *r = NULL;
16064
16065#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016066 char_u *serverid = get_tv_string_chk(&argvars[0]);
16067
16068 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016069 {
16070# ifdef WIN32
16071 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016072 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016073
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016074 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016075 if (n != 0)
16076 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16077 if (r == NULL)
16078# else
16079 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016081# endif
16082 EMSG(_("E277: Unable to read a server reply"));
16083 }
16084#endif
16085 rettv->v_type = VAR_STRING;
16086 rettv->vval.v_string = r;
16087}
16088
16089/*
16090 * "remote_send()" function
16091 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016092 static void
16093f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016094 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016095 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096{
16097 rettv->v_type = VAR_STRING;
16098 rettv->vval.v_string = NULL;
16099#ifdef FEAT_CLIENTSERVER
16100 remote_common(argvars, rettv, FALSE);
16101#endif
16102}
16103
16104/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016105 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016106 */
16107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016108f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016109 typval_T *argvars;
16110 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016111{
Bram Moolenaar33570922005-01-25 22:26:29 +000016112 list_T *l;
16113 listitem_T *item, *item2;
16114 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016115 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016116 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016117 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016118 dict_T *d;
16119 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016120 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016121
Bram Moolenaar8c711452005-01-14 21:53:12 +000016122 if (argvars[0].v_type == VAR_DICT)
16123 {
16124 if (argvars[2].v_type != VAR_UNKNOWN)
16125 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016126 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016127 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016129 key = get_tv_string_chk(&argvars[1]);
16130 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016132 di = dict_find(d, key, -1);
16133 if (di == NULL)
16134 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016135 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16136 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016137 {
16138 *rettv = di->di_tv;
16139 init_tv(&di->di_tv);
16140 dictitem_remove(d, di);
16141 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016143 }
16144 }
16145 else if (argvars[0].v_type != VAR_LIST)
16146 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016147 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016148 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016150 int error = FALSE;
16151
16152 idx = get_tv_number_chk(&argvars[1], &error);
16153 if (error)
16154 ; /* type error: do nothing, errmsg already given */
16155 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016156 EMSGN(_(e_listidx), idx);
16157 else
16158 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016159 if (argvars[2].v_type == VAR_UNKNOWN)
16160 {
16161 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016162 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016163 *rettv = item->li_tv;
16164 vim_free(item);
16165 }
16166 else
16167 {
16168 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016169 end = get_tv_number_chk(&argvars[2], &error);
16170 if (error)
16171 ; /* type error: do nothing */
16172 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016173 EMSGN(_(e_listidx), end);
16174 else
16175 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016176 int cnt = 0;
16177
16178 for (li = item; li != NULL; li = li->li_next)
16179 {
16180 ++cnt;
16181 if (li == item2)
16182 break;
16183 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016184 if (li == NULL) /* didn't find "item2" after "item" */
16185 EMSG(_(e_invrange));
16186 else
16187 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016188 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016189 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016190 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016191 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016192 l->lv_first = item;
16193 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016194 item->li_prev = NULL;
16195 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016196 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016197 }
16198 }
16199 }
16200 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016201 }
16202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203}
16204
16205/*
16206 * "rename({from}, {to})" function
16207 */
16208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016209f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016210 typval_T *argvars;
16211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212{
16213 char_u buf[NUMBUFLEN];
16214
16215 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016218 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16219 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220}
16221
16222/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016223 * "repeat()" function
16224 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016225 static void
16226f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016227 typval_T *argvars;
16228 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016229{
16230 char_u *p;
16231 int n;
16232 int slen;
16233 int len;
16234 char_u *r;
16235 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016236
16237 n = get_tv_number(&argvars[1]);
16238 if (argvars[0].v_type == VAR_LIST)
16239 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016240 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016241 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016242 if (list_extend(rettv->vval.v_list,
16243 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016244 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016245 }
16246 else
16247 {
16248 p = get_tv_string(&argvars[0]);
16249 rettv->v_type = VAR_STRING;
16250 rettv->vval.v_string = NULL;
16251
16252 slen = (int)STRLEN(p);
16253 len = slen * n;
16254 if (len <= 0)
16255 return;
16256
16257 r = alloc(len + 1);
16258 if (r != NULL)
16259 {
16260 for (i = 0; i < n; i++)
16261 mch_memmove(r + i * slen, p, (size_t)slen);
16262 r[len] = NUL;
16263 }
16264
16265 rettv->vval.v_string = r;
16266 }
16267}
16268
16269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 * "resolve()" function
16271 */
16272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016274 typval_T *argvars;
16275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276{
16277 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016278#ifdef HAVE_READLINK
16279 char_u *buf = NULL;
16280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016282 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283#ifdef FEAT_SHORTCUT
16284 {
16285 char_u *v = NULL;
16286
16287 v = mch_resolve_shortcut(p);
16288 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016289 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016291 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 }
16293#else
16294# ifdef HAVE_READLINK
16295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 char_u *cpy;
16297 int len;
16298 char_u *remain = NULL;
16299 char_u *q;
16300 int is_relative_to_current = FALSE;
16301 int has_trailing_pathsep = FALSE;
16302 int limit = 100;
16303
16304 p = vim_strsave(p);
16305
16306 if (p[0] == '.' && (vim_ispathsep(p[1])
16307 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16308 is_relative_to_current = TRUE;
16309
16310 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016311 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016314 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316
16317 q = getnextcomp(p);
16318 if (*q != NUL)
16319 {
16320 /* Separate the first path component in "p", and keep the
16321 * remainder (beginning with the path separator). */
16322 remain = vim_strsave(q - 1);
16323 q[-1] = NUL;
16324 }
16325
Bram Moolenaard9462e32011-04-11 21:35:11 +020016326 buf = alloc(MAXPATHL + 1);
16327 if (buf == NULL)
16328 goto fail;
16329
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 for (;;)
16331 {
16332 for (;;)
16333 {
16334 len = readlink((char *)p, (char *)buf, MAXPATHL);
16335 if (len <= 0)
16336 break;
16337 buf[len] = NUL;
16338
16339 if (limit-- == 0)
16340 {
16341 vim_free(p);
16342 vim_free(remain);
16343 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016344 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345 goto fail;
16346 }
16347
16348 /* Ensure that the result will have a trailing path separator
16349 * if the argument has one. */
16350 if (remain == NULL && has_trailing_pathsep)
16351 add_pathsep(buf);
16352
16353 /* Separate the first path component in the link value and
16354 * concatenate the remainders. */
16355 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16356 if (*q != NUL)
16357 {
16358 if (remain == NULL)
16359 remain = vim_strsave(q - 1);
16360 else
16361 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016362 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363 if (cpy != NULL)
16364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365 vim_free(remain);
16366 remain = cpy;
16367 }
16368 }
16369 q[-1] = NUL;
16370 }
16371
16372 q = gettail(p);
16373 if (q > p && *q == NUL)
16374 {
16375 /* Ignore trailing path separator. */
16376 q[-1] = NUL;
16377 q = gettail(p);
16378 }
16379 if (q > p && !mch_isFullName(buf))
16380 {
16381 /* symlink is relative to directory of argument */
16382 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16383 if (cpy != NULL)
16384 {
16385 STRCPY(cpy, p);
16386 STRCPY(gettail(cpy), buf);
16387 vim_free(p);
16388 p = cpy;
16389 }
16390 }
16391 else
16392 {
16393 vim_free(p);
16394 p = vim_strsave(buf);
16395 }
16396 }
16397
16398 if (remain == NULL)
16399 break;
16400
16401 /* Append the first path component of "remain" to "p". */
16402 q = getnextcomp(remain + 1);
16403 len = q - remain - (*q != NUL);
16404 cpy = vim_strnsave(p, STRLEN(p) + len);
16405 if (cpy != NULL)
16406 {
16407 STRNCAT(cpy, remain, len);
16408 vim_free(p);
16409 p = cpy;
16410 }
16411 /* Shorten "remain". */
16412 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016413 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 else
16415 {
16416 vim_free(remain);
16417 remain = NULL;
16418 }
16419 }
16420
16421 /* If the result is a relative path name, make it explicitly relative to
16422 * the current directory if and only if the argument had this form. */
16423 if (!vim_ispathsep(*p))
16424 {
16425 if (is_relative_to_current
16426 && *p != NUL
16427 && !(p[0] == '.'
16428 && (p[1] == NUL
16429 || vim_ispathsep(p[1])
16430 || (p[1] == '.'
16431 && (p[2] == NUL
16432 || vim_ispathsep(p[2]))))))
16433 {
16434 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016435 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 if (cpy != NULL)
16437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 vim_free(p);
16439 p = cpy;
16440 }
16441 }
16442 else if (!is_relative_to_current)
16443 {
16444 /* Strip leading "./". */
16445 q = p;
16446 while (q[0] == '.' && vim_ispathsep(q[1]))
16447 q += 2;
16448 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016449 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 }
16451 }
16452
16453 /* Ensure that the result will have no trailing path separator
16454 * if the argument had none. But keep "/" or "//". */
16455 if (!has_trailing_pathsep)
16456 {
16457 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016458 if (after_pathsep(p, q))
16459 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 }
16461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016462 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 }
16464# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016465 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466# endif
16467#endif
16468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470
16471#ifdef HAVE_READLINK
16472fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016473 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016475 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476}
16477
16478/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016479 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 */
16481 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 typval_T *argvars;
16484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485{
Bram Moolenaar33570922005-01-25 22:26:29 +000016486 list_T *l;
16487 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489 if (argvars[0].v_type != VAR_LIST)
16490 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016491 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016492 && !tv_check_lock(l->lv_lock,
16493 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016494 {
16495 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016496 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016497 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498 while (li != NULL)
16499 {
16500 ni = li->li_prev;
16501 list_append(l, li);
16502 li = ni;
16503 }
16504 rettv->vval.v_list = l;
16505 rettv->v_type = VAR_LIST;
16506 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016507 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509}
16510
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016511#define SP_NOMOVE 0x01 /* don't move cursor */
16512#define SP_REPEAT 0x02 /* repeat to find outer pair */
16513#define SP_RETCOUNT 0x04 /* return matchcount */
16514#define SP_SETPCMARK 0x08 /* set previous context mark */
16515#define SP_START 0x10 /* accept match at start position */
16516#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16517#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016518#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016519
Bram Moolenaar33570922005-01-25 22:26:29 +000016520static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016521
16522/*
16523 * Get flags for a search function.
16524 * Possibly sets "p_ws".
16525 * Returns BACKWARD, FORWARD or zero (for an error).
16526 */
16527 static int
16528get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016529 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016530 int *flagsp;
16531{
16532 int dir = FORWARD;
16533 char_u *flags;
16534 char_u nbuf[NUMBUFLEN];
16535 int mask;
16536
16537 if (varp->v_type != VAR_UNKNOWN)
16538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 flags = get_tv_string_buf_chk(varp, nbuf);
16540 if (flags == NULL)
16541 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016542 while (*flags != NUL)
16543 {
16544 switch (*flags)
16545 {
16546 case 'b': dir = BACKWARD; break;
16547 case 'w': p_ws = TRUE; break;
16548 case 'W': p_ws = FALSE; break;
16549 default: mask = 0;
16550 if (flagsp != NULL)
16551 switch (*flags)
16552 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016553 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016554 case 'e': mask = SP_END; break;
16555 case 'm': mask = SP_RETCOUNT; break;
16556 case 'n': mask = SP_NOMOVE; break;
16557 case 'p': mask = SP_SUBPAT; break;
16558 case 'r': mask = SP_REPEAT; break;
16559 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016560 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561 }
16562 if (mask == 0)
16563 {
16564 EMSG2(_(e_invarg2), flags);
16565 dir = 0;
16566 }
16567 else
16568 *flagsp |= mask;
16569 }
16570 if (dir == 0)
16571 break;
16572 ++flags;
16573 }
16574 }
16575 return dir;
16576}
16577
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016579 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016581 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016582search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016583 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016584 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016585 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016587 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 char_u *pat;
16589 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016590 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 int save_p_ws = p_ws;
16592 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016593 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016594 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016595 proftime_T tm;
16596#ifdef FEAT_RELTIME
16597 long time_limit = 0;
16598#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016599 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016600 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016602 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016603 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016604 if (dir == 0)
16605 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016606 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016607 if (flags & SP_START)
16608 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016609 if (flags & SP_END)
16610 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016611 if (flags & SP_COLUMN)
16612 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016613
Bram Moolenaar76929292008-01-06 19:07:36 +000016614 /* Optional arguments: line number to stop searching and timeout. */
16615 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016616 {
16617 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16618 if (lnum_stop < 0)
16619 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016620#ifdef FEAT_RELTIME
16621 if (argvars[3].v_type != VAR_UNKNOWN)
16622 {
16623 time_limit = get_tv_number_chk(&argvars[3], NULL);
16624 if (time_limit < 0)
16625 goto theend;
16626 }
16627#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016628 }
16629
Bram Moolenaar76929292008-01-06 19:07:36 +000016630#ifdef FEAT_RELTIME
16631 /* Set the time limit, if there is one. */
16632 profile_setlimit(time_limit, &tm);
16633#endif
16634
Bram Moolenaar231334e2005-07-25 20:46:57 +000016635 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016636 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016637 * Check to make sure only those flags are set.
16638 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16639 * flags cannot be set. Check for that condition also.
16640 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016641 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016642 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016644 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016645 goto theend;
16646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016648 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016649 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016650 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016651 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016653 if (flags & SP_SUBPAT)
16654 retval = subpatnum;
16655 else
16656 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016657 if (flags & SP_SETPCMARK)
16658 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016660 if (match_pos != NULL)
16661 {
16662 /* Store the match cursor position */
16663 match_pos->lnum = pos.lnum;
16664 match_pos->col = pos.col + 1;
16665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 /* "/$" will put the cursor after the end of the line, may need to
16667 * correct that here */
16668 check_cursor();
16669 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016670
16671 /* If 'n' flag is used: restore cursor position. */
16672 if (flags & SP_NOMOVE)
16673 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016674 else
16675 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016676theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016678
16679 return retval;
16680}
16681
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016682#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016683
16684/*
16685 * round() is not in C90, use ceil() or floor() instead.
16686 */
16687 float_T
16688vim_round(f)
16689 float_T f;
16690{
16691 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16692}
16693
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016694/*
16695 * "round({float})" function
16696 */
16697 static void
16698f_round(argvars, rettv)
16699 typval_T *argvars;
16700 typval_T *rettv;
16701{
16702 float_T f;
16703
16704 rettv->v_type = VAR_FLOAT;
16705 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016706 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016707 else
16708 rettv->vval.v_float = 0.0;
16709}
16710#endif
16711
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016712/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016713 * "screenattr()" function
16714 */
16715 static void
16716f_screenattr(argvars, rettv)
16717 typval_T *argvars UNUSED;
16718 typval_T *rettv;
16719{
16720 int row;
16721 int col;
16722 int c;
16723
16724 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16725 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16726 if (row < 0 || row >= screen_Rows
16727 || col < 0 || col >= screen_Columns)
16728 c = -1;
16729 else
16730 c = ScreenAttrs[LineOffset[row] + col];
16731 rettv->vval.v_number = c;
16732}
16733
16734/*
16735 * "screenchar()" function
16736 */
16737 static void
16738f_screenchar(argvars, rettv)
16739 typval_T *argvars UNUSED;
16740 typval_T *rettv;
16741{
16742 int row;
16743 int col;
16744 int off;
16745 int c;
16746
16747 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16748 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16749 if (row < 0 || row >= screen_Rows
16750 || col < 0 || col >= screen_Columns)
16751 c = -1;
16752 else
16753 {
16754 off = LineOffset[row] + col;
16755#ifdef FEAT_MBYTE
16756 if (enc_utf8 && ScreenLinesUC[off] != 0)
16757 c = ScreenLinesUC[off];
16758 else
16759#endif
16760 c = ScreenLines[off];
16761 }
16762 rettv->vval.v_number = c;
16763}
16764
16765/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016766 * "screencol()" function
16767 *
16768 * First column is 1 to be consistent with virtcol().
16769 */
16770 static void
16771f_screencol(argvars, rettv)
16772 typval_T *argvars UNUSED;
16773 typval_T *rettv;
16774{
16775 rettv->vval.v_number = screen_screencol() + 1;
16776}
16777
16778/*
16779 * "screenrow()" function
16780 */
16781 static void
16782f_screenrow(argvars, rettv)
16783 typval_T *argvars UNUSED;
16784 typval_T *rettv;
16785{
16786 rettv->vval.v_number = screen_screenrow() + 1;
16787}
16788
16789/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016790 * "search()" function
16791 */
16792 static void
16793f_search(argvars, rettv)
16794 typval_T *argvars;
16795 typval_T *rettv;
16796{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016797 int flags = 0;
16798
16799 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800}
16801
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016803 * "searchdecl()" function
16804 */
16805 static void
16806f_searchdecl(argvars, rettv)
16807 typval_T *argvars;
16808 typval_T *rettv;
16809{
16810 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016811 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016812 int error = FALSE;
16813 char_u *name;
16814
16815 rettv->vval.v_number = 1; /* default: FAIL */
16816
16817 name = get_tv_string_chk(&argvars[0]);
16818 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016819 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016820 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016821 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16822 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16823 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016824 if (!error && name != NULL)
16825 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016826 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016827}
16828
16829/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016830 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016832 static int
16833searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016834 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016835 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836{
16837 char_u *spat, *mpat, *epat;
16838 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 int dir;
16841 int flags = 0;
16842 char_u nbuf1[NUMBUFLEN];
16843 char_u nbuf2[NUMBUFLEN];
16844 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016845 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016846 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016847 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016850 spat = get_tv_string_chk(&argvars[0]);
16851 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16852 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16853 if (spat == NULL || mpat == NULL || epat == NULL)
16854 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 /* Handle the optional fourth argument: flags */
16857 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016858 if (dir == 0)
16859 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016860
16861 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016862 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16863 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016864 if ((flags & (SP_END | SP_SUBPAT)) != 0
16865 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016866 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016867 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016868 goto theend;
16869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016871 /* Using 'r' implies 'W', otherwise it doesn't work. */
16872 if (flags & SP_REPEAT)
16873 p_ws = FALSE;
16874
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016875 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016876 if (argvars[3].v_type == VAR_UNKNOWN
16877 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 skip = (char_u *)"";
16879 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016882 if (argvars[5].v_type != VAR_UNKNOWN)
16883 {
16884 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16885 if (lnum_stop < 0)
16886 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016887#ifdef FEAT_RELTIME
16888 if (argvars[6].v_type != VAR_UNKNOWN)
16889 {
16890 time_limit = get_tv_number_chk(&argvars[6], NULL);
16891 if (time_limit < 0)
16892 goto theend;
16893 }
16894#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016895 }
16896 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016897 if (skip == NULL)
16898 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016900 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016901 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016902
16903theend:
16904 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016905
16906 return retval;
16907}
16908
16909/*
16910 * "searchpair()" function
16911 */
16912 static void
16913f_searchpair(argvars, rettv)
16914 typval_T *argvars;
16915 typval_T *rettv;
16916{
16917 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16918}
16919
16920/*
16921 * "searchpairpos()" function
16922 */
16923 static void
16924f_searchpairpos(argvars, rettv)
16925 typval_T *argvars;
16926 typval_T *rettv;
16927{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016928 pos_T match_pos;
16929 int lnum = 0;
16930 int col = 0;
16931
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016932 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016933 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016934
16935 if (searchpair_cmn(argvars, &match_pos) > 0)
16936 {
16937 lnum = match_pos.lnum;
16938 col = match_pos.col;
16939 }
16940
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016941 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16942 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016943}
16944
16945/*
16946 * Search for a start/middle/end thing.
16947 * Used by searchpair(), see its documentation for the details.
16948 * Returns 0 or -1 for no match,
16949 */
16950 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016951do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16952 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016953 char_u *spat; /* start pattern */
16954 char_u *mpat; /* middle pattern */
16955 char_u *epat; /* end pattern */
16956 int dir; /* BACKWARD or FORWARD */
16957 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016958 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016959 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016960 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016961 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016962{
16963 char_u *save_cpo;
16964 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16965 long retval = 0;
16966 pos_T pos;
16967 pos_T firstpos;
16968 pos_T foundpos;
16969 pos_T save_cursor;
16970 pos_T save_pos;
16971 int n;
16972 int r;
16973 int nest = 1;
16974 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016975 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016976 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016977
16978 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16979 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016980 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016981
Bram Moolenaar76929292008-01-06 19:07:36 +000016982#ifdef FEAT_RELTIME
16983 /* Set the time limit, if there is one. */
16984 profile_setlimit(time_limit, &tm);
16985#endif
16986
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016987 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16988 * start/middle/end (pat3, for the top pair). */
16989 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16990 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16991 if (pat2 == NULL || pat3 == NULL)
16992 goto theend;
16993 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16994 if (*mpat == NUL)
16995 STRCPY(pat3, pat2);
16996 else
16997 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16998 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016999 if (flags & SP_START)
17000 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017001
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 save_cursor = curwin->w_cursor;
17003 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017004 clearpos(&firstpos);
17005 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 pat = pat3;
17007 for (;;)
17008 {
17009 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017010 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17012 /* didn't find it or found the first match again: FAIL */
17013 break;
17014
17015 if (firstpos.lnum == 0)
17016 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017017 if (equalpos(pos, foundpos))
17018 {
17019 /* Found the same position again. Can happen with a pattern that
17020 * has "\zs" at the end and searching backwards. Advance one
17021 * character and try again. */
17022 if (dir == BACKWARD)
17023 decl(&pos);
17024 else
17025 incl(&pos);
17026 }
17027 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017029 /* clear the start flag to avoid getting stuck here */
17030 options &= ~SEARCH_START;
17031
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 /* If the skip pattern matches, ignore this match. */
17033 if (*skip != NUL)
17034 {
17035 save_pos = curwin->w_cursor;
17036 curwin->w_cursor = pos;
17037 r = eval_to_bool(skip, &err, NULL, FALSE);
17038 curwin->w_cursor = save_pos;
17039 if (err)
17040 {
17041 /* Evaluating {skip} caused an error, break here. */
17042 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017043 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 break;
17045 }
17046 if (r)
17047 continue;
17048 }
17049
17050 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17051 {
17052 /* Found end when searching backwards or start when searching
17053 * forward: nested pair. */
17054 ++nest;
17055 pat = pat2; /* nested, don't search for middle */
17056 }
17057 else
17058 {
17059 /* Found end when searching forward or start when searching
17060 * backward: end of (nested) pair; or found middle in outer pair. */
17061 if (--nest == 1)
17062 pat = pat3; /* outer level, search for middle */
17063 }
17064
17065 if (nest == 0)
17066 {
17067 /* Found the match: return matchcount or line number. */
17068 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017069 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017071 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017072 if (flags & SP_SETPCMARK)
17073 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 curwin->w_cursor = pos;
17075 if (!(flags & SP_REPEAT))
17076 break;
17077 nest = 1; /* search for next unmatched */
17078 }
17079 }
17080
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017081 if (match_pos != NULL)
17082 {
17083 /* Store the match cursor position */
17084 match_pos->lnum = curwin->w_cursor.lnum;
17085 match_pos->col = curwin->w_cursor.col + 1;
17086 }
17087
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017089 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 curwin->w_cursor = save_cursor;
17091
17092theend:
17093 vim_free(pat2);
17094 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017095 if (p_cpo == empty_option)
17096 p_cpo = save_cpo;
17097 else
17098 /* Darn, evaluating the {skip} expression changed the value. */
17099 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017100
17101 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102}
17103
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017104/*
17105 * "searchpos()" function
17106 */
17107 static void
17108f_searchpos(argvars, rettv)
17109 typval_T *argvars;
17110 typval_T *rettv;
17111{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017112 pos_T match_pos;
17113 int lnum = 0;
17114 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017115 int n;
17116 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017117
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017118 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017119 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017120
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017121 n = search_cmn(argvars, &match_pos, &flags);
17122 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017123 {
17124 lnum = match_pos.lnum;
17125 col = match_pos.col;
17126 }
17127
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017128 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17129 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017130 if (flags & SP_SUBPAT)
17131 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017132}
17133
17134
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 static void
17136f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017137 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140#ifdef FEAT_CLIENTSERVER
17141 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017142 char_u *server = get_tv_string_chk(&argvars[0]);
17143 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017146 if (server == NULL || reply == NULL)
17147 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017148 if (check_restricted() || check_secure())
17149 return;
17150# ifdef FEAT_X11
17151 if (check_connection() == FAIL)
17152 return;
17153# endif
17154
17155 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 EMSG(_("E258: Unable to send to client"));
17158 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160 rettv->vval.v_number = 0;
17161#else
17162 rettv->vval.v_number = -1;
17163#endif
17164}
17165
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 static void
17167f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170{
17171 char_u *r = NULL;
17172
17173#ifdef FEAT_CLIENTSERVER
17174# ifdef WIN32
17175 r = serverGetVimNames();
17176# else
17177 make_connection();
17178 if (X_DISPLAY != NULL)
17179 r = serverGetVimNames(X_DISPLAY);
17180# endif
17181#endif
17182 rettv->v_type = VAR_STRING;
17183 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184}
17185
17186/*
17187 * "setbufvar()" function
17188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017190f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017191 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017192 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193{
17194 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017197 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 char_u nbuf[NUMBUFLEN];
17199
17200 if (check_restricted() || check_secure())
17201 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017202 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17203 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017204 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 varp = &argvars[2];
17206
17207 if (buf != NULL && varname != NULL && varp != NULL)
17208 {
17209 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211
17212 if (*varname == '&')
17213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 long numval;
17215 char_u *strval;
17216 int error = FALSE;
17217
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017219 numval = get_tv_number_chk(varp, &error);
17220 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017221 if (!error && strval != NULL)
17222 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 }
17224 else
17225 {
17226 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17227 if (bufvarname != NULL)
17228 {
17229 STRCPY(bufvarname, "b:");
17230 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017231 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 vim_free(bufvarname);
17233 }
17234 }
17235
17236 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017237 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239}
17240
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017241 static void
17242f_setcharsearch(argvars, rettv)
17243 typval_T *argvars;
17244 typval_T *rettv UNUSED;
17245{
17246 dict_T *d;
17247 dictitem_T *di;
17248 char_u *csearch;
17249
17250 if (argvars[0].v_type != VAR_DICT)
17251 {
17252 EMSG(_(e_dictreq));
17253 return;
17254 }
17255
17256 if ((d = argvars[0].vval.v_dict) != NULL)
17257 {
17258 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17259 if (csearch != NULL)
17260 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017261#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017262 if (enc_utf8)
17263 {
17264 int pcc[MAX_MCO];
17265 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017266
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017267 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17268 }
17269 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017270#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017271 set_last_csearch(PTR2CHAR(csearch),
17272 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017273 }
17274
17275 di = dict_find(d, (char_u *)"forward", -1);
17276 if (di != NULL)
17277 set_csearch_direction(get_tv_number(&di->di_tv)
17278 ? FORWARD : BACKWARD);
17279
17280 di = dict_find(d, (char_u *)"until", -1);
17281 if (di != NULL)
17282 set_csearch_until(!!get_tv_number(&di->di_tv));
17283 }
17284}
17285
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286/*
17287 * "setcmdpos()" function
17288 */
17289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017290f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017291 typval_T *argvars;
17292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017294 int pos = (int)get_tv_number(&argvars[0]) - 1;
17295
17296 if (pos >= 0)
17297 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298}
17299
17300/*
17301 * "setline()" function
17302 */
17303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017304f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 typval_T *argvars;
17306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307{
17308 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017309 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017310 list_T *l = NULL;
17311 listitem_T *li = NULL;
17312 long added = 0;
17313 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017315 lnum = get_tv_lnum(&argvars[0]);
17316 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017318 l = argvars[1].vval.v_list;
17319 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017321 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017322 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017323
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017324 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017325 for (;;)
17326 {
17327 if (l != NULL)
17328 {
17329 /* list argument, get next string */
17330 if (li == NULL)
17331 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017332 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017333 li = li->li_next;
17334 }
17335
17336 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017337 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017338 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017339
17340 /* When coming here from Insert mode, sync undo, so that this can be
17341 * undone separately from what was previously inserted. */
17342 if (u_sync_once == 2)
17343 {
17344 u_sync_once = 1; /* notify that u_sync() was called */
17345 u_sync(TRUE);
17346 }
17347
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017348 if (lnum <= curbuf->b_ml.ml_line_count)
17349 {
17350 /* existing line, replace it */
17351 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17352 {
17353 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017354 if (lnum == curwin->w_cursor.lnum)
17355 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017356 rettv->vval.v_number = 0; /* OK */
17357 }
17358 }
17359 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17360 {
17361 /* lnum is one past the last line, append the line */
17362 ++added;
17363 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17364 rettv->vval.v_number = 0; /* OK */
17365 }
17366
17367 if (l == NULL) /* only one string argument */
17368 break;
17369 ++lnum;
17370 }
17371
17372 if (added > 0)
17373 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374}
17375
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017376static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17377
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017379 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017380 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017381 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017382set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017383 win_T *wp UNUSED;
17384 typval_T *list_arg UNUSED;
17385 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017386 typval_T *rettv;
17387{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017388#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017389 char_u *act;
17390 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017391#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017392
Bram Moolenaar2641f772005-03-25 21:58:17 +000017393 rettv->vval.v_number = -1;
17394
17395#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017396 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017397 EMSG(_(e_listreq));
17398 else
17399 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017400 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017401
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017402 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017403 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017404 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017405 if (act == NULL)
17406 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017407 if (*act == 'a' || *act == 'r')
17408 action = *act;
17409 }
17410
Bram Moolenaar81484f42012-12-05 15:16:47 +010017411 if (l != NULL && set_errorlist(wp, l, action,
17412 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017413 rettv->vval.v_number = 0;
17414 }
17415#endif
17416}
17417
17418/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017419 * "setloclist()" function
17420 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017421 static void
17422f_setloclist(argvars, rettv)
17423 typval_T *argvars;
17424 typval_T *rettv;
17425{
17426 win_T *win;
17427
17428 rettv->vval.v_number = -1;
17429
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017430 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017431 if (win != NULL)
17432 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17433}
17434
17435/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017436 * "setmatches()" function
17437 */
17438 static void
17439f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017440 typval_T *argvars UNUSED;
17441 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017442{
17443#ifdef FEAT_SEARCH_EXTRA
17444 list_T *l;
17445 listitem_T *li;
17446 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017447 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017448
17449 rettv->vval.v_number = -1;
17450 if (argvars[0].v_type != VAR_LIST)
17451 {
17452 EMSG(_(e_listreq));
17453 return;
17454 }
17455 if ((l = argvars[0].vval.v_list) != NULL)
17456 {
17457
17458 /* To some extent make sure that we are dealing with a list from
17459 * "getmatches()". */
17460 li = l->lv_first;
17461 while (li != NULL)
17462 {
17463 if (li->li_tv.v_type != VAR_DICT
17464 || (d = li->li_tv.vval.v_dict) == NULL)
17465 {
17466 EMSG(_(e_invarg));
17467 return;
17468 }
17469 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017470 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17471 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017472 && dict_find(d, (char_u *)"priority", -1) != NULL
17473 && dict_find(d, (char_u *)"id", -1) != NULL))
17474 {
17475 EMSG(_(e_invarg));
17476 return;
17477 }
17478 li = li->li_next;
17479 }
17480
17481 clear_matches(curwin);
17482 li = l->lv_first;
17483 while (li != NULL)
17484 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017485 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017486 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017487 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017488 char_u *group;
17489 int priority;
17490 int id;
17491 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017492
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017493 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017494 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17495 {
17496 if (s == NULL)
17497 {
17498 s = list_alloc();
17499 if (s == NULL)
17500 return;
17501 }
17502
17503 /* match from matchaddpos() */
17504 for (i = 1; i < 9; i++)
17505 {
17506 sprintf((char *)buf, (char *)"pos%d", i);
17507 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17508 {
17509 if (di->di_tv.v_type != VAR_LIST)
17510 return;
17511
17512 list_append_tv(s, &di->di_tv);
17513 s->lv_refcount++;
17514 }
17515 else
17516 break;
17517 }
17518 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017519
17520 group = get_dict_string(d, (char_u *)"group", FALSE);
17521 priority = (int)get_dict_number(d, (char_u *)"priority");
17522 id = (int)get_dict_number(d, (char_u *)"id");
17523 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17524 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17525 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017526 if (i == 0)
17527 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017528 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017529 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017530 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017531 }
17532 else
17533 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017534 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017535 list_unref(s);
17536 s = NULL;
17537 }
17538
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017539 li = li->li_next;
17540 }
17541 rettv->vval.v_number = 0;
17542 }
17543#endif
17544}
17545
17546/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017547 * "setpos()" function
17548 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017549 static void
17550f_setpos(argvars, rettv)
17551 typval_T *argvars;
17552 typval_T *rettv;
17553{
17554 pos_T pos;
17555 int fnum;
17556 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017557 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017558
Bram Moolenaar08250432008-02-13 11:42:46 +000017559 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017560 name = get_tv_string_chk(argvars);
17561 if (name != NULL)
17562 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017563 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017564 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017565 if (--pos.col < 0)
17566 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017567 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017568 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017569 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017570 if (fnum == curbuf->b_fnum)
17571 {
17572 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017573 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017574 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017575 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017576 curwin->w_set_curswant = FALSE;
17577 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017578 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017579 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017580 }
17581 else
17582 EMSG(_(e_invarg));
17583 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017584 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17585 {
17586 /* set mark */
17587 if (setmark_pos(name[1], &pos, fnum) == OK)
17588 rettv->vval.v_number = 0;
17589 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017590 else
17591 EMSG(_(e_invarg));
17592 }
17593 }
17594}
17595
17596/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017597 * "setqflist()" function
17598 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017599 static void
17600f_setqflist(argvars, rettv)
17601 typval_T *argvars;
17602 typval_T *rettv;
17603{
17604 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17605}
17606
17607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 * "setreg()" function
17609 */
17610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017611f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 typval_T *argvars;
17613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614{
17615 int regname;
17616 char_u *strregname;
17617 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017618 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 int append;
17620 char_u yank_type;
17621 long block_len;
17622
17623 block_len = -1;
17624 yank_type = MAUTO;
17625 append = FALSE;
17626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017627 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017628 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017630 if (strregname == NULL)
17631 return; /* type error; errmsg already given */
17632 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 if (regname == 0 || regname == '@')
17634 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017636 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017638 stropt = get_tv_string_chk(&argvars[2]);
17639 if (stropt == NULL)
17640 return; /* type error */
17641 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 switch (*stropt)
17643 {
17644 case 'a': case 'A': /* append */
17645 append = TRUE;
17646 break;
17647 case 'v': case 'c': /* character-wise selection */
17648 yank_type = MCHAR;
17649 break;
17650 case 'V': case 'l': /* line-wise selection */
17651 yank_type = MLINE;
17652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 case 'b': case Ctrl_V: /* block-wise selection */
17654 yank_type = MBLOCK;
17655 if (VIM_ISDIGIT(stropt[1]))
17656 {
17657 ++stropt;
17658 block_len = getdigits(&stropt) - 1;
17659 --stropt;
17660 }
17661 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 }
17663 }
17664
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017665 if (argvars[1].v_type == VAR_LIST)
17666 {
17667 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017668 char_u **allocval;
17669 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017670 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017671 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017672 int len = argvars[1].vval.v_list->lv_len;
17673 listitem_T *li;
17674
Bram Moolenaar7d647822014-04-05 21:28:56 +020017675 /* First half: use for pointers to result lines; second half: use for
17676 * pointers to allocated copies. */
17677 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017678 if (lstval == NULL)
17679 return;
17680 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017681 allocval = lstval + len + 2;
17682 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017683
17684 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17685 li = li->li_next)
17686 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017687 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017688 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017689 goto free_lstval;
17690 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017691 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017692 /* Need to make a copy, next get_tv_string_buf_chk() will
17693 * overwrite the string. */
17694 strval = vim_strsave(buf);
17695 if (strval == NULL)
17696 goto free_lstval;
17697 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017698 }
17699 *curval++ = strval;
17700 }
17701 *curval++ = NULL;
17702
17703 write_reg_contents_lst(regname, lstval, -1,
17704 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017705free_lstval:
17706 while (curallocval > allocval)
17707 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017708 vim_free(lstval);
17709 }
17710 else
17711 {
17712 strval = get_tv_string_chk(&argvars[1]);
17713 if (strval == NULL)
17714 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017715 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017718 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719}
17720
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017721/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017722 * "settabvar()" function
17723 */
17724 static void
17725f_settabvar(argvars, rettv)
17726 typval_T *argvars;
17727 typval_T *rettv;
17728{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017729#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017730 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017731 tabpage_T *tp;
17732#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017733 char_u *varname, *tabvarname;
17734 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017735
17736 rettv->vval.v_number = 0;
17737
17738 if (check_restricted() || check_secure())
17739 return;
17740
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017741#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017742 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017743#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017744 varname = get_tv_string_chk(&argvars[1]);
17745 varp = &argvars[2];
17746
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017747 if (varname != NULL && varp != NULL
17748#ifdef FEAT_WINDOWS
17749 && tp != NULL
17750#endif
17751 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017752 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017753#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017754 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017755 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017756#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017757
17758 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17759 if (tabvarname != NULL)
17760 {
17761 STRCPY(tabvarname, "t:");
17762 STRCPY(tabvarname + 2, varname);
17763 set_var(tabvarname, varp, TRUE);
17764 vim_free(tabvarname);
17765 }
17766
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017767#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017768 /* Restore current tabpage */
17769 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017770 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017771#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017772 }
17773}
17774
17775/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017776 * "settabwinvar()" function
17777 */
17778 static void
17779f_settabwinvar(argvars, rettv)
17780 typval_T *argvars;
17781 typval_T *rettv;
17782{
17783 setwinvar(argvars, rettv, 1);
17784}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785
17786/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017787 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017790f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017791 typval_T *argvars;
17792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017794 setwinvar(argvars, rettv, 0);
17795}
17796
17797/*
17798 * "setwinvar()" and "settabwinvar()" functions
17799 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017800
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017801 static void
17802setwinvar(argvars, rettv, off)
17803 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017804 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017805 int off;
17806{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 win_T *win;
17808#ifdef FEAT_WINDOWS
17809 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017810 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017811 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812#endif
17813 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017814 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017816 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817
17818 if (check_restricted() || check_secure())
17819 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017820
17821#ifdef FEAT_WINDOWS
17822 if (off == 1)
17823 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17824 else
17825 tp = curtab;
17826#endif
17827 win = find_win_by_nr(&argvars[off], tp);
17828 varname = get_tv_string_chk(&argvars[off + 1]);
17829 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830
17831 if (win != NULL && varname != NULL && varp != NULL)
17832 {
17833#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017834 need_switch_win = !(tp == curtab && win == curwin);
17835 if (!need_switch_win
17836 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017839 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017841 long numval;
17842 char_u *strval;
17843 int error = FALSE;
17844
17845 ++varname;
17846 numval = get_tv_number_chk(varp, &error);
17847 strval = get_tv_string_buf_chk(varp, nbuf);
17848 if (!error && strval != NULL)
17849 set_option_value(varname, numval, strval, OPT_LOCAL);
17850 }
17851 else
17852 {
17853 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17854 if (winvarname != NULL)
17855 {
17856 STRCPY(winvarname, "w:");
17857 STRCPY(winvarname + 2, varname);
17858 set_var(winvarname, varp, TRUE);
17859 vim_free(winvarname);
17860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 }
17862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017864 if (need_switch_win)
17865 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866#endif
17867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868}
17869
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017870#ifdef FEAT_CRYPT
17871/*
17872 * "sha256({string})" function
17873 */
17874 static void
17875f_sha256(argvars, rettv)
17876 typval_T *argvars;
17877 typval_T *rettv;
17878{
17879 char_u *p;
17880
17881 p = get_tv_string(&argvars[0]);
17882 rettv->vval.v_string = vim_strsave(
17883 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17884 rettv->v_type = VAR_STRING;
17885}
17886#endif /* FEAT_CRYPT */
17887
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017889 * "shellescape({string})" function
17890 */
17891 static void
17892f_shellescape(argvars, rettv)
17893 typval_T *argvars;
17894 typval_T *rettv;
17895{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017896 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017897 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017898 rettv->v_type = VAR_STRING;
17899}
17900
17901/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017902 * shiftwidth() function
17903 */
17904 static void
17905f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017906 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017907 typval_T *rettv;
17908{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017909 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017910}
17911
17912/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017913 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 */
17915 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017916f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017917 typval_T *argvars;
17918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017920 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921
Bram Moolenaar0d660222005-01-07 21:51:51 +000017922 p = get_tv_string(&argvars[0]);
17923 rettv->vval.v_string = vim_strsave(p);
17924 simplify_filename(rettv->vval.v_string); /* simplify in place */
17925 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926}
17927
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017928#ifdef FEAT_FLOAT
17929/*
17930 * "sin()" function
17931 */
17932 static void
17933f_sin(argvars, rettv)
17934 typval_T *argvars;
17935 typval_T *rettv;
17936{
17937 float_T f;
17938
17939 rettv->v_type = VAR_FLOAT;
17940 if (get_float_arg(argvars, &f) == OK)
17941 rettv->vval.v_float = sin(f);
17942 else
17943 rettv->vval.v_float = 0.0;
17944}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017945
17946/*
17947 * "sinh()" function
17948 */
17949 static void
17950f_sinh(argvars, rettv)
17951 typval_T *argvars;
17952 typval_T *rettv;
17953{
17954 float_T f;
17955
17956 rettv->v_type = VAR_FLOAT;
17957 if (get_float_arg(argvars, &f) == OK)
17958 rettv->vval.v_float = sinh(f);
17959 else
17960 rettv->vval.v_float = 0.0;
17961}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017962#endif
17963
Bram Moolenaar0d660222005-01-07 21:51:51 +000017964static int
17965#ifdef __BORLANDC__
17966 _RTLENTRYF
17967#endif
17968 item_compare __ARGS((const void *s1, const void *s2));
17969static int
17970#ifdef __BORLANDC__
17971 _RTLENTRYF
17972#endif
17973 item_compare2 __ARGS((const void *s1, const void *s2));
17974
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017975/* struct used in the array that's given to qsort() */
17976typedef struct
17977{
17978 listitem_T *item;
17979 int idx;
17980} sortItem_T;
17981
Bram Moolenaar0d660222005-01-07 21:51:51 +000017982static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017983static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010017984static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017985static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017986static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017987static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017988static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017989static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017990#define ITEM_COMPARE_FAIL 999
17991
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017993 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995 static int
17996#ifdef __BORLANDC__
17997_RTLENTRYF
17998#endif
17999item_compare(s1, s2)
18000 const void *s1;
18001 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018003 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018004 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018005 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018006 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018007 int res;
18008 char_u numbuf1[NUMBUFLEN];
18009 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018011 si1 = (sortItem_T *)s1;
18012 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018013 tv1 = &si1->item->li_tv;
18014 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018015
18016 if (item_compare_numbers)
18017 {
18018 long v1 = get_tv_number(tv1);
18019 long v2 = get_tv_number(tv2);
18020
18021 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18022 }
18023
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018024 /* tv2string() puts quotes around a string and allocates memory. Don't do
18025 * that for string variables. Use a single quote when comparing with a
18026 * non-string to do what the docs promise. */
18027 if (tv1->v_type == VAR_STRING)
18028 {
18029 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18030 p1 = (char_u *)"'";
18031 else
18032 p1 = tv1->vval.v_string;
18033 }
18034 else
18035 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18036 if (tv2->v_type == VAR_STRING)
18037 {
18038 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18039 p2 = (char_u *)"'";
18040 else
18041 p2 = tv2->vval.v_string;
18042 }
18043 else
18044 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018045 if (p1 == NULL)
18046 p1 = (char_u *)"";
18047 if (p2 == NULL)
18048 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018049 if (!item_compare_numeric)
18050 {
18051 if (item_compare_ic)
18052 res = STRICMP(p1, p2);
18053 else
18054 res = STRCMP(p1, p2);
18055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018057 {
18058 double n1, n2;
18059 n1 = strtod((char *)p1, (char **)&p1);
18060 n2 = strtod((char *)p2, (char **)&p2);
18061 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18062 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018063
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018064 /* When the result would be zero, compare the item indexes. Makes the
18065 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018066 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018067 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018068
Bram Moolenaar0d660222005-01-07 21:51:51 +000018069 vim_free(tofree1);
18070 vim_free(tofree2);
18071 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072}
18073
18074 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018075#ifdef __BORLANDC__
18076_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018078item_compare2(s1, s2)
18079 const void *s1;
18080 const void *s2;
18081{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018082 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018084 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018085 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018086 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018088 /* shortcut after failure in previous call; compare all items equal */
18089 if (item_compare_func_err)
18090 return 0;
18091
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018092 si1 = (sortItem_T *)s1;
18093 si2 = (sortItem_T *)s2;
18094
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018095 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018096 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018097 copy_tv(&si1->item->li_tv, &argv[0]);
18098 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099
18100 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018101 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018102 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18103 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018104 clear_tv(&argv[0]);
18105 clear_tv(&argv[1]);
18106
18107 if (res == FAIL)
18108 res = ITEM_COMPARE_FAIL;
18109 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018110 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18111 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018112 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018113 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018114
18115 /* When the result would be zero, compare the pointers themselves. Makes
18116 * the sort stable. */
18117 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018118 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018119
Bram Moolenaar0d660222005-01-07 21:51:51 +000018120 return res;
18121}
18122
18123/*
18124 * "sort({list})" function
18125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018127do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018128 typval_T *argvars;
18129 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018130 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131{
Bram Moolenaar33570922005-01-25 22:26:29 +000018132 list_T *l;
18133 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018134 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018135 long len;
18136 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137
Bram Moolenaar0d660222005-01-07 21:51:51 +000018138 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018139 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 else
18141 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018142 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018143 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018144 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18145 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018146 return;
18147 rettv->vval.v_list = l;
18148 rettv->v_type = VAR_LIST;
18149 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151 len = list_len(l);
18152 if (len <= 1)
18153 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154
Bram Moolenaar0d660222005-01-07 21:51:51 +000018155 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018156 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018157 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018158 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018159 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160 if (argvars[1].v_type != VAR_UNKNOWN)
18161 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018162 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018163 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018164 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018165 else
18166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018167 int error = FALSE;
18168
18169 i = get_tv_number_chk(&argvars[1], &error);
18170 if (error)
18171 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018172 if (i == 1)
18173 item_compare_ic = TRUE;
18174 else
18175 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018176 if (item_compare_func != NULL)
18177 {
18178 if (STRCMP(item_compare_func, "n") == 0)
18179 {
18180 item_compare_func = NULL;
18181 item_compare_numeric = TRUE;
18182 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018183 else if (STRCMP(item_compare_func, "N") == 0)
18184 {
18185 item_compare_func = NULL;
18186 item_compare_numbers = TRUE;
18187 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018188 else if (STRCMP(item_compare_func, "i") == 0)
18189 {
18190 item_compare_func = NULL;
18191 item_compare_ic = TRUE;
18192 }
18193 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018194 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018195
18196 if (argvars[2].v_type != VAR_UNKNOWN)
18197 {
18198 /* optional third argument: {dict} */
18199 if (argvars[2].v_type != VAR_DICT)
18200 {
18201 EMSG(_(e_dictreq));
18202 return;
18203 }
18204 item_compare_selfdict = argvars[2].vval.v_dict;
18205 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207
Bram Moolenaar0d660222005-01-07 21:51:51 +000018208 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018209 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018210 if (ptrs == NULL)
18211 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212
Bram Moolenaar327aa022014-03-25 18:24:23 +010018213 i = 0;
18214 if (sort)
18215 {
18216 /* sort(): ptrs will be the list to sort */
18217 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018218 {
18219 ptrs[i].item = li;
18220 ptrs[i].idx = i;
18221 ++i;
18222 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018223
18224 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018225 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018226 /* test the compare function */
18227 if (item_compare_func != NULL
18228 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018229 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018230 EMSG(_("E702: Sort compare function failed"));
18231 else
18232 {
18233 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018234 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018235 item_compare_func == NULL ? item_compare : item_compare2);
18236
18237 if (!item_compare_func_err)
18238 {
18239 /* Clear the List and append the items in sorted order. */
18240 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18241 l->lv_len = 0;
18242 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018243 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018244 }
18245 }
18246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018249 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18250
18251 /* f_uniq(): ptrs will be a stack of items to remove */
18252 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018253 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018254 item_compare_func_ptr = item_compare_func
18255 ? item_compare2 : item_compare;
18256
18257 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18258 li = li->li_next)
18259 {
18260 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18261 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018262 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018263 if (item_compare_func_err)
18264 {
18265 EMSG(_("E882: Uniq compare function failed"));
18266 break;
18267 }
18268 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 if (!item_compare_func_err)
18271 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018272 while (--i >= 0)
18273 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018274 li = ptrs[i].item->li_next;
18275 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018276 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018277 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018278 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018279 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018280 list_fix_watch(l, li);
18281 listitem_free(li);
18282 l->lv_len--;
18283 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285 }
18286
18287 vim_free(ptrs);
18288 }
18289}
18290
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018291/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018292 * "sort({list})" function
18293 */
18294 static void
18295f_sort(argvars, rettv)
18296 typval_T *argvars;
18297 typval_T *rettv;
18298{
18299 do_sort_uniq(argvars, rettv, TRUE);
18300}
18301
18302/*
18303 * "uniq({list})" function
18304 */
18305 static void
18306f_uniq(argvars, rettv)
18307 typval_T *argvars;
18308 typval_T *rettv;
18309{
18310 do_sort_uniq(argvars, rettv, FALSE);
18311}
18312
18313/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018314 * "soundfold({word})" function
18315 */
18316 static void
18317f_soundfold(argvars, rettv)
18318 typval_T *argvars;
18319 typval_T *rettv;
18320{
18321 char_u *s;
18322
18323 rettv->v_type = VAR_STRING;
18324 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018325#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018326 rettv->vval.v_string = eval_soundfold(s);
18327#else
18328 rettv->vval.v_string = vim_strsave(s);
18329#endif
18330}
18331
18332/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018333 * "spellbadword()" function
18334 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018335 static void
18336f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018337 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018338 typval_T *rettv;
18339{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018340 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018341 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018342 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018343
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018344 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018345 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018346
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018347#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018348 if (argvars[0].v_type == VAR_UNKNOWN)
18349 {
18350 /* Find the start and length of the badly spelled word. */
18351 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18352 if (len != 0)
18353 word = ml_get_cursor();
18354 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018355 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018356 {
18357 char_u *str = get_tv_string_chk(&argvars[0]);
18358 int capcol = -1;
18359
18360 if (str != NULL)
18361 {
18362 /* Check the argument for spelling. */
18363 while (*str != NUL)
18364 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018365 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018366 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018367 {
18368 word = str;
18369 break;
18370 }
18371 str += len;
18372 }
18373 }
18374 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018375#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018376
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018377 list_append_string(rettv->vval.v_list, word, len);
18378 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018379 attr == HLF_SPB ? "bad" :
18380 attr == HLF_SPR ? "rare" :
18381 attr == HLF_SPL ? "local" :
18382 attr == HLF_SPC ? "caps" :
18383 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018384}
18385
18386/*
18387 * "spellsuggest()" function
18388 */
18389 static void
18390f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018391 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018392 typval_T *rettv;
18393{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018394#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018395 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018396 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018397 int maxcount;
18398 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018399 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018400 listitem_T *li;
18401 int need_capital = FALSE;
18402#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018403
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018404 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018405 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018406
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018407#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018408 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018409 {
18410 str = get_tv_string(&argvars[0]);
18411 if (argvars[1].v_type != VAR_UNKNOWN)
18412 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018413 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018414 if (maxcount <= 0)
18415 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018416 if (argvars[2].v_type != VAR_UNKNOWN)
18417 {
18418 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18419 if (typeerr)
18420 return;
18421 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018422 }
18423 else
18424 maxcount = 25;
18425
Bram Moolenaar4770d092006-01-12 23:22:24 +000018426 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018427
18428 for (i = 0; i < ga.ga_len; ++i)
18429 {
18430 str = ((char_u **)ga.ga_data)[i];
18431
18432 li = listitem_alloc();
18433 if (li == NULL)
18434 vim_free(str);
18435 else
18436 {
18437 li->li_tv.v_type = VAR_STRING;
18438 li->li_tv.v_lock = 0;
18439 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018440 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018441 }
18442 }
18443 ga_clear(&ga);
18444 }
18445#endif
18446}
18447
Bram Moolenaar0d660222005-01-07 21:51:51 +000018448 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018449f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018450 typval_T *argvars;
18451 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018452{
18453 char_u *str;
18454 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018455 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456 regmatch_T regmatch;
18457 char_u patbuf[NUMBUFLEN];
18458 char_u *save_cpo;
18459 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018460 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018461 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018462 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018463
18464 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18465 save_cpo = p_cpo;
18466 p_cpo = (char_u *)"";
18467
18468 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018469 if (argvars[1].v_type != VAR_UNKNOWN)
18470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018471 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18472 if (pat == NULL)
18473 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018474 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018475 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018476 }
18477 if (pat == NULL || *pat == NUL)
18478 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018480 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018482 if (typeerr)
18483 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484
Bram Moolenaar0d660222005-01-07 21:51:51 +000018485 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18486 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018488 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018489 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018490 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018491 if (*str == NUL)
18492 match = FALSE; /* empty item at the end */
18493 else
18494 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018495 if (match)
18496 end = regmatch.startp[0];
18497 else
18498 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018499 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18500 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018501 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018502 if (list_append_string(rettv->vval.v_list, str,
18503 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018504 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018505 }
18506 if (!match)
18507 break;
18508 /* Advance to just after the match. */
18509 if (regmatch.endp[0] > str)
18510 col = 0;
18511 else
18512 {
18513 /* Don't get stuck at the same match. */
18514#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018515 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018516#else
18517 col = 1;
18518#endif
18519 }
18520 str = regmatch.endp[0];
18521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522
Bram Moolenaar473de612013-06-08 18:19:48 +020018523 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525
Bram Moolenaar0d660222005-01-07 21:51:51 +000018526 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527}
18528
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018529#ifdef FEAT_FLOAT
18530/*
18531 * "sqrt()" function
18532 */
18533 static void
18534f_sqrt(argvars, rettv)
18535 typval_T *argvars;
18536 typval_T *rettv;
18537{
18538 float_T f;
18539
18540 rettv->v_type = VAR_FLOAT;
18541 if (get_float_arg(argvars, &f) == OK)
18542 rettv->vval.v_float = sqrt(f);
18543 else
18544 rettv->vval.v_float = 0.0;
18545}
18546
18547/*
18548 * "str2float()" function
18549 */
18550 static void
18551f_str2float(argvars, rettv)
18552 typval_T *argvars;
18553 typval_T *rettv;
18554{
18555 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18556
18557 if (*p == '+')
18558 p = skipwhite(p + 1);
18559 (void)string2float(p, &rettv->vval.v_float);
18560 rettv->v_type = VAR_FLOAT;
18561}
18562#endif
18563
Bram Moolenaar2c932302006-03-18 21:42:09 +000018564/*
18565 * "str2nr()" function
18566 */
18567 static void
18568f_str2nr(argvars, rettv)
18569 typval_T *argvars;
18570 typval_T *rettv;
18571{
18572 int base = 10;
18573 char_u *p;
18574 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018575 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018576
18577 if (argvars[1].v_type != VAR_UNKNOWN)
18578 {
18579 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018580 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018581 {
18582 EMSG(_(e_invarg));
18583 return;
18584 }
18585 }
18586
18587 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018588 if (*p == '+')
18589 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018590 switch (base)
18591 {
18592 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18593 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18594 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18595 default: what = 0;
18596 }
18597 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018598 rettv->vval.v_number = n;
18599}
18600
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601#ifdef HAVE_STRFTIME
18602/*
18603 * "strftime({format}[, {time}])" function
18604 */
18605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018606f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018607 typval_T *argvars;
18608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609{
18610 char_u result_buf[256];
18611 struct tm *curtime;
18612 time_t seconds;
18613 char_u *p;
18614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018618 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 seconds = time(NULL);
18620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 curtime = localtime(&seconds);
18623 /* MSVC returns NULL for an invalid value of seconds. */
18624 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 else
18627 {
18628# ifdef FEAT_MBYTE
18629 vimconv_T conv;
18630 char_u *enc;
18631
18632 conv.vc_type = CONV_NONE;
18633 enc = enc_locale();
18634 convert_setup(&conv, p_enc, enc);
18635 if (conv.vc_type != CONV_NONE)
18636 p = string_convert(&conv, p, NULL);
18637# endif
18638 if (p != NULL)
18639 (void)strftime((char *)result_buf, sizeof(result_buf),
18640 (char *)p, curtime);
18641 else
18642 result_buf[0] = NUL;
18643
18644# ifdef FEAT_MBYTE
18645 if (conv.vc_type != CONV_NONE)
18646 vim_free(p);
18647 convert_setup(&conv, enc, p_enc);
18648 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018649 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 else
18651# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653
18654# ifdef FEAT_MBYTE
18655 /* Release conversion descriptors */
18656 convert_setup(&conv, NULL, NULL);
18657 vim_free(enc);
18658# endif
18659 }
18660}
18661#endif
18662
18663/*
18664 * "stridx()" function
18665 */
18666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018667f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018668 typval_T *argvars;
18669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670{
18671 char_u buf[NUMBUFLEN];
18672 char_u *needle;
18673 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018674 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018676 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018678 needle = get_tv_string_chk(&argvars[1]);
18679 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018680 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018681 if (needle == NULL || haystack == NULL)
18682 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683
Bram Moolenaar33570922005-01-25 22:26:29 +000018684 if (argvars[2].v_type != VAR_UNKNOWN)
18685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018686 int error = FALSE;
18687
18688 start_idx = get_tv_number_chk(&argvars[2], &error);
18689 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018690 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018691 if (start_idx >= 0)
18692 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018693 }
18694
18695 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18696 if (pos != NULL)
18697 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698}
18699
18700/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018701 * "string()" function
18702 */
18703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018705 typval_T *argvars;
18706 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018707{
18708 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018709 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018711 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018712 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018713 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018714 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018715 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716}
18717
18718/*
18719 * "strlen()" function
18720 */
18721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018723 typval_T *argvars;
18724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018726 rettv->vval.v_number = (varnumber_T)(STRLEN(
18727 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728}
18729
18730/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018731 * "strchars()" function
18732 */
18733 static void
18734f_strchars(argvars, rettv)
18735 typval_T *argvars;
18736 typval_T *rettv;
18737{
18738 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018739 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018740#ifdef FEAT_MBYTE
18741 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018742 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018743#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018744
18745 if (argvars[1].v_type != VAR_UNKNOWN)
18746 skipcc = get_tv_number_chk(&argvars[1], NULL);
18747 if (skipcc < 0 || skipcc > 1)
18748 EMSG(_(e_invarg));
18749 else
18750 {
18751#ifdef FEAT_MBYTE
18752 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18753 while (*s != NUL)
18754 {
18755 func_mb_ptr2char_adv(&s);
18756 ++len;
18757 }
18758 rettv->vval.v_number = len;
18759#else
18760 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18761#endif
18762 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018763}
18764
18765/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018766 * "strdisplaywidth()" function
18767 */
18768 static void
18769f_strdisplaywidth(argvars, rettv)
18770 typval_T *argvars;
18771 typval_T *rettv;
18772{
18773 char_u *s = get_tv_string(&argvars[0]);
18774 int col = 0;
18775
18776 if (argvars[1].v_type != VAR_UNKNOWN)
18777 col = get_tv_number(&argvars[1]);
18778
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018779 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018780}
18781
18782/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018783 * "strwidth()" function
18784 */
18785 static void
18786f_strwidth(argvars, rettv)
18787 typval_T *argvars;
18788 typval_T *rettv;
18789{
18790 char_u *s = get_tv_string(&argvars[0]);
18791
18792 rettv->vval.v_number = (varnumber_T)(
18793#ifdef FEAT_MBYTE
18794 mb_string2cells(s, -1)
18795#else
18796 STRLEN(s)
18797#endif
18798 );
18799}
18800
18801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 * "strpart()" function
18803 */
18804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018806 typval_T *argvars;
18807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808{
18809 char_u *p;
18810 int n;
18811 int len;
18812 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018813 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018815 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 slen = (int)STRLEN(p);
18817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018818 n = get_tv_number_chk(&argvars[1], &error);
18819 if (error)
18820 len = 0;
18821 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018822 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 else
18824 len = slen - n; /* default len: all bytes that are available. */
18825
18826 /*
18827 * Only return the overlap between the specified part and the actual
18828 * string.
18829 */
18830 if (n < 0)
18831 {
18832 len += n;
18833 n = 0;
18834 }
18835 else if (n > slen)
18836 n = slen;
18837 if (len < 0)
18838 len = 0;
18839 else if (n + len > slen)
18840 len = slen - n;
18841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018842 rettv->v_type = VAR_STRING;
18843 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844}
18845
18846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018847 * "strridx()" function
18848 */
18849 static void
18850f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018851 typval_T *argvars;
18852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018853{
18854 char_u buf[NUMBUFLEN];
18855 char_u *needle;
18856 char_u *haystack;
18857 char_u *rest;
18858 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018859 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018861 needle = get_tv_string_chk(&argvars[1]);
18862 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018863
18864 rettv->vval.v_number = -1;
18865 if (needle == NULL || haystack == NULL)
18866 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018867
18868 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018869 if (argvars[2].v_type != VAR_UNKNOWN)
18870 {
18871 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018872 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018873 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018874 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018875 }
18876 else
18877 end_idx = haystack_len;
18878
Bram Moolenaar0d660222005-01-07 21:51:51 +000018879 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018880 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018881 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018882 lastmatch = haystack + end_idx;
18883 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018884 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018885 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018886 for (rest = haystack; *rest != '\0'; ++rest)
18887 {
18888 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018889 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018890 break;
18891 lastmatch = rest;
18892 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018893 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018894
18895 if (lastmatch == NULL)
18896 rettv->vval.v_number = -1;
18897 else
18898 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18899}
18900
18901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 * "strtrans()" function
18903 */
18904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018905f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018906 typval_T *argvars;
18907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018909 rettv->v_type = VAR_STRING;
18910 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911}
18912
18913/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018914 * "submatch()" function
18915 */
18916 static void
18917f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018918 typval_T *argvars;
18919 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018920{
Bram Moolenaar41571762014-04-02 19:00:58 +020018921 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018922 int no;
18923 int retList = 0;
18924
18925 no = (int)get_tv_number_chk(&argvars[0], &error);
18926 if (error)
18927 return;
18928 error = FALSE;
18929 if (argvars[1].v_type != VAR_UNKNOWN)
18930 retList = get_tv_number_chk(&argvars[1], &error);
18931 if (error)
18932 return;
18933
18934 if (retList == 0)
18935 {
18936 rettv->v_type = VAR_STRING;
18937 rettv->vval.v_string = reg_submatch(no);
18938 }
18939 else
18940 {
18941 rettv->v_type = VAR_LIST;
18942 rettv->vval.v_list = reg_submatch_list(no);
18943 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018944}
18945
18946/*
18947 * "substitute()" function
18948 */
18949 static void
18950f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018951 typval_T *argvars;
18952 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018953{
18954 char_u patbuf[NUMBUFLEN];
18955 char_u subbuf[NUMBUFLEN];
18956 char_u flagsbuf[NUMBUFLEN];
18957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018958 char_u *str = get_tv_string_chk(&argvars[0]);
18959 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18960 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18961 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18962
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018964 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18965 rettv->vval.v_string = NULL;
18966 else
18967 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018968}
18969
18970/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018971 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018974f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977{
18978 int id = 0;
18979#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018980 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 long col;
18982 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018983 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018985 lnum = get_tv_lnum(argvars); /* -1 on type error */
18986 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18987 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018989 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018990 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018991 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992#endif
18993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018994 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995}
18996
18997/*
18998 * "synIDattr(id, what [, mode])" function
18999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019001f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019002 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004{
19005 char_u *p = NULL;
19006#ifdef FEAT_SYN_HL
19007 int id;
19008 char_u *what;
19009 char_u *mode;
19010 char_u modebuf[NUMBUFLEN];
19011 int modec;
19012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019013 id = get_tv_number(&argvars[0]);
19014 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019017 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019019 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 modec = 0; /* replace invalid with current */
19021 }
19022 else
19023 {
19024#ifdef FEAT_GUI
19025 if (gui.in_use)
19026 modec = 'g';
19027 else
19028#endif
19029 if (t_colors > 1)
19030 modec = 'c';
19031 else
19032 modec = 't';
19033 }
19034
19035
19036 switch (TOLOWER_ASC(what[0]))
19037 {
19038 case 'b':
19039 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19040 p = highlight_color(id, what, modec);
19041 else /* bold */
19042 p = highlight_has_attr(id, HL_BOLD, modec);
19043 break;
19044
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019045 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 p = highlight_color(id, what, modec);
19047 break;
19048
19049 case 'i':
19050 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19051 p = highlight_has_attr(id, HL_INVERSE, modec);
19052 else /* italic */
19053 p = highlight_has_attr(id, HL_ITALIC, modec);
19054 break;
19055
19056 case 'n': /* name */
19057 p = get_highlight_name(NULL, id - 1);
19058 break;
19059
19060 case 'r': /* reverse */
19061 p = highlight_has_attr(id, HL_INVERSE, modec);
19062 break;
19063
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019064 case 's':
19065 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19066 p = highlight_color(id, what, modec);
19067 else /* standout */
19068 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 break;
19070
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019071 case 'u':
19072 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19073 /* underline */
19074 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19075 else
19076 /* undercurl */
19077 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 break;
19079 }
19080
19081 if (p != NULL)
19082 p = vim_strsave(p);
19083#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019084 rettv->v_type = VAR_STRING;
19085 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086}
19087
19088/*
19089 * "synIDtrans(id)" function
19090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019093 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095{
19096 int id;
19097
19098#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019099 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100
19101 if (id > 0)
19102 id = syn_get_final_id(id);
19103 else
19104#endif
19105 id = 0;
19106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108}
19109
19110/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019111 * "synconcealed(lnum, col)" function
19112 */
19113 static void
19114f_synconcealed(argvars, rettv)
19115 typval_T *argvars UNUSED;
19116 typval_T *rettv;
19117{
19118#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19119 long lnum;
19120 long col;
19121 int syntax_flags = 0;
19122 int cchar;
19123 int matchid = 0;
19124 char_u str[NUMBUFLEN];
19125#endif
19126
19127 rettv->v_type = VAR_LIST;
19128 rettv->vval.v_list = NULL;
19129
19130#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19131 lnum = get_tv_lnum(argvars); /* -1 on type error */
19132 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19133
19134 vim_memset(str, NUL, sizeof(str));
19135
19136 if (rettv_list_alloc(rettv) != FAIL)
19137 {
19138 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19139 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19140 && curwin->w_p_cole > 0)
19141 {
19142 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19143 syntax_flags = get_syntax_info(&matchid);
19144
19145 /* get the conceal character */
19146 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19147 {
19148 cchar = syn_get_sub_char();
19149 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19150 cchar = lcs_conceal;
19151 if (cchar != NUL)
19152 {
19153# ifdef FEAT_MBYTE
19154 if (has_mbyte)
19155 (*mb_char2bytes)(cchar, str);
19156 else
19157# endif
19158 str[0] = cchar;
19159 }
19160 }
19161 }
19162
19163 list_append_number(rettv->vval.v_list,
19164 (syntax_flags & HL_CONCEAL) != 0);
19165 /* -1 to auto-determine strlen */
19166 list_append_string(rettv->vval.v_list, str, -1);
19167 list_append_number(rettv->vval.v_list, matchid);
19168 }
19169#endif
19170}
19171
19172/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019173 * "synstack(lnum, col)" function
19174 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019175 static void
19176f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019177 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019178 typval_T *rettv;
19179{
19180#ifdef FEAT_SYN_HL
19181 long lnum;
19182 long col;
19183 int i;
19184 int id;
19185#endif
19186
19187 rettv->v_type = VAR_LIST;
19188 rettv->vval.v_list = NULL;
19189
19190#ifdef FEAT_SYN_HL
19191 lnum = get_tv_lnum(argvars); /* -1 on type error */
19192 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19193
19194 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019195 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019196 && rettv_list_alloc(rettv) != FAIL)
19197 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019198 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019199 for (i = 0; ; ++i)
19200 {
19201 id = syn_get_stack_item(i);
19202 if (id < 0)
19203 break;
19204 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19205 break;
19206 }
19207 }
19208#endif
19209}
19210
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019212get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 typval_T *argvars;
19214 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019215 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019217 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019219 char_u *infile = NULL;
19220 char_u buf[NUMBUFLEN];
19221 int err = FALSE;
19222 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019223 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019224 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019226 rettv->v_type = VAR_STRING;
19227 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019228 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019229 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019230
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019231 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019232 {
19233 /*
19234 * Write the string to a temp file, to be used for input of the shell
19235 * command.
19236 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019237 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019238 {
19239 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019240 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019241 }
19242
19243 fd = mch_fopen((char *)infile, WRITEBIN);
19244 if (fd == NULL)
19245 {
19246 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019247 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019248 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019249 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019250 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019251 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19252 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019253 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019254 else
19255 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019256 size_t len;
19257
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019258 p = get_tv_string_buf_chk(&argvars[1], buf);
19259 if (p == NULL)
19260 {
19261 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019262 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019263 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019264 len = STRLEN(p);
19265 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019266 err = TRUE;
19267 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019268 if (fclose(fd) != 0)
19269 err = TRUE;
19270 if (err)
19271 {
19272 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019273 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019274 }
19275 }
19276
Bram Moolenaar52a72462014-08-29 15:53:52 +020019277 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19278 * echoes typeahead, that messes up the display. */
19279 if (!msg_silent)
19280 flags += SHELL_COOKED;
19281
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019282 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019284 int len;
19285 listitem_T *li;
19286 char_u *s = NULL;
19287 char_u *start;
19288 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019289 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290
Bram Moolenaar52a72462014-08-29 15:53:52 +020019291 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019292 if (res == NULL)
19293 goto errret;
19294
19295 list = list_alloc();
19296 if (list == NULL)
19297 goto errret;
19298
19299 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019301 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019302 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019303 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019304 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019305
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019306 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019307 if (s == NULL)
19308 goto errret;
19309
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019310 for (p = s; start < end; ++p, ++start)
19311 *p = *start == NUL ? NL : *start;
19312 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019313
19314 li = listitem_alloc();
19315 if (li == NULL)
19316 {
19317 vim_free(s);
19318 goto errret;
19319 }
19320 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019321 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019322 li->li_tv.vval.v_string = s;
19323 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019325
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019326 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019327 rettv->v_type = VAR_LIST;
19328 rettv->vval.v_list = list;
19329 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019331 else
19332 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019333 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019334#ifdef USE_CR
19335 /* translate <CR> into <NL> */
19336 if (res != NULL)
19337 {
19338 char_u *s;
19339
19340 for (s = res; *s; ++s)
19341 {
19342 if (*s == CAR)
19343 *s = NL;
19344 }
19345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346#else
19347# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019348 /* translate <CR><NL> into <NL> */
19349 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019351 char_u *s, *d;
19352
19353 d = res;
19354 for (s = res; *s; ++s)
19355 {
19356 if (s[0] == CAR && s[1] == NL)
19357 ++s;
19358 *d++ = *s;
19359 }
19360 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362# endif
19363#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019364 rettv->vval.v_string = res;
19365 res = NULL;
19366 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019367
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019368errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019369 if (infile != NULL)
19370 {
19371 mch_remove(infile);
19372 vim_free(infile);
19373 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019374 if (res != NULL)
19375 vim_free(res);
19376 if (list != NULL)
19377 list_free(list, TRUE);
19378}
19379
19380/*
19381 * "system()" function
19382 */
19383 static void
19384f_system(argvars, rettv)
19385 typval_T *argvars;
19386 typval_T *rettv;
19387{
19388 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19389}
19390
19391/*
19392 * "systemlist()" function
19393 */
19394 static void
19395f_systemlist(argvars, rettv)
19396 typval_T *argvars;
19397 typval_T *rettv;
19398{
19399 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400}
19401
19402/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019403 * "tabpagebuflist()" function
19404 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019405 static void
19406f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019407 typval_T *argvars UNUSED;
19408 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019409{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019410#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019411 tabpage_T *tp;
19412 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019413
19414 if (argvars[0].v_type == VAR_UNKNOWN)
19415 wp = firstwin;
19416 else
19417 {
19418 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19419 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019420 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019421 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019422 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019423 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019424 for (; wp != NULL; wp = wp->w_next)
19425 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019426 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019427 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019428 }
19429#endif
19430}
19431
19432
19433/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019434 * "tabpagenr()" function
19435 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019436 static void
19437f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019438 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019439 typval_T *rettv;
19440{
19441 int nr = 1;
19442#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019443 char_u *arg;
19444
19445 if (argvars[0].v_type != VAR_UNKNOWN)
19446 {
19447 arg = get_tv_string_chk(&argvars[0]);
19448 nr = 0;
19449 if (arg != NULL)
19450 {
19451 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019452 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019453 else
19454 EMSG2(_(e_invexpr2), arg);
19455 }
19456 }
19457 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019458 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019459#endif
19460 rettv->vval.v_number = nr;
19461}
19462
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019463
19464#ifdef FEAT_WINDOWS
19465static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19466
19467/*
19468 * Common code for tabpagewinnr() and winnr().
19469 */
19470 static int
19471get_winnr(tp, argvar)
19472 tabpage_T *tp;
19473 typval_T *argvar;
19474{
19475 win_T *twin;
19476 int nr = 1;
19477 win_T *wp;
19478 char_u *arg;
19479
19480 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19481 if (argvar->v_type != VAR_UNKNOWN)
19482 {
19483 arg = get_tv_string_chk(argvar);
19484 if (arg == NULL)
19485 nr = 0; /* type error; errmsg already given */
19486 else if (STRCMP(arg, "$") == 0)
19487 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19488 else if (STRCMP(arg, "#") == 0)
19489 {
19490 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19491 if (twin == NULL)
19492 nr = 0;
19493 }
19494 else
19495 {
19496 EMSG2(_(e_invexpr2), arg);
19497 nr = 0;
19498 }
19499 }
19500
19501 if (nr > 0)
19502 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19503 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019504 {
19505 if (wp == NULL)
19506 {
19507 /* didn't find it in this tabpage */
19508 nr = 0;
19509 break;
19510 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019511 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019512 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019513 return nr;
19514}
19515#endif
19516
19517/*
19518 * "tabpagewinnr()" function
19519 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019520 static void
19521f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019522 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019523 typval_T *rettv;
19524{
19525 int nr = 1;
19526#ifdef FEAT_WINDOWS
19527 tabpage_T *tp;
19528
19529 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19530 if (tp == NULL)
19531 nr = 0;
19532 else
19533 nr = get_winnr(tp, &argvars[1]);
19534#endif
19535 rettv->vval.v_number = nr;
19536}
19537
19538
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019539/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019540 * "tagfiles()" function
19541 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019542 static void
19543f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019544 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019545 typval_T *rettv;
19546{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019547 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019548 tagname_T tn;
19549 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019550
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019551 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019552 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019553 fname = alloc(MAXPATHL);
19554 if (fname == NULL)
19555 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019556
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019557 for (first = TRUE; ; first = FALSE)
19558 if (get_tagfname(&tn, first, fname) == FAIL
19559 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019560 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019561 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019562 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019563}
19564
19565/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019566 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019567 */
19568 static void
19569f_taglist(argvars, rettv)
19570 typval_T *argvars;
19571 typval_T *rettv;
19572{
19573 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019574
19575 tag_pattern = get_tv_string(&argvars[0]);
19576
19577 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019578 if (*tag_pattern == NUL)
19579 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019580
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019581 if (rettv_list_alloc(rettv) == OK)
19582 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019583}
19584
19585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 * "tempname()" function
19587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019589f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019590 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592{
19593 static int x = 'A';
19594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019595 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019596 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597
19598 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19599 * names. Skip 'I' and 'O', they are used for shell redirection. */
19600 do
19601 {
19602 if (x == 'Z')
19603 x = '0';
19604 else if (x == '9')
19605 x = 'A';
19606 else
19607 {
19608#ifdef EBCDIC
19609 if (x == 'I')
19610 x = 'J';
19611 else if (x == 'R')
19612 x = 'S';
19613 else
19614#endif
19615 ++x;
19616 }
19617 } while (x == 'I' || x == 'O');
19618}
19619
19620/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019621 * "test(list)" function: Just checking the walls...
19622 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019623 static void
19624f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019625 typval_T *argvars UNUSED;
19626 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019627{
19628 /* Used for unit testing. Change the code below to your liking. */
19629#if 0
19630 listitem_T *li;
19631 list_T *l;
19632 char_u *bad, *good;
19633
19634 if (argvars[0].v_type != VAR_LIST)
19635 return;
19636 l = argvars[0].vval.v_list;
19637 if (l == NULL)
19638 return;
19639 li = l->lv_first;
19640 if (li == NULL)
19641 return;
19642 bad = get_tv_string(&li->li_tv);
19643 li = li->li_next;
19644 if (li == NULL)
19645 return;
19646 good = get_tv_string(&li->li_tv);
19647 rettv->vval.v_number = test_edit_score(bad, good);
19648#endif
19649}
19650
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019651#ifdef FEAT_FLOAT
19652/*
19653 * "tan()" function
19654 */
19655 static void
19656f_tan(argvars, rettv)
19657 typval_T *argvars;
19658 typval_T *rettv;
19659{
19660 float_T f;
19661
19662 rettv->v_type = VAR_FLOAT;
19663 if (get_float_arg(argvars, &f) == OK)
19664 rettv->vval.v_float = tan(f);
19665 else
19666 rettv->vval.v_float = 0.0;
19667}
19668
19669/*
19670 * "tanh()" function
19671 */
19672 static void
19673f_tanh(argvars, rettv)
19674 typval_T *argvars;
19675 typval_T *rettv;
19676{
19677 float_T f;
19678
19679 rettv->v_type = VAR_FLOAT;
19680 if (get_float_arg(argvars, &f) == OK)
19681 rettv->vval.v_float = tanh(f);
19682 else
19683 rettv->vval.v_float = 0.0;
19684}
19685#endif
19686
Bram Moolenaard52d9742005-08-21 22:20:28 +000019687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 * "tolower(string)" function
19689 */
19690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019692 typval_T *argvars;
19693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694{
19695 char_u *p;
19696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019697 p = vim_strsave(get_tv_string(&argvars[0]));
19698 rettv->v_type = VAR_STRING;
19699 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700
19701 if (p != NULL)
19702 while (*p != NUL)
19703 {
19704#ifdef FEAT_MBYTE
19705 int l;
19706
19707 if (enc_utf8)
19708 {
19709 int c, lc;
19710
19711 c = utf_ptr2char(p);
19712 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019713 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 /* TODO: reallocate string when byte count changes. */
19715 if (utf_char2len(lc) == l)
19716 utf_char2bytes(lc, p);
19717 p += l;
19718 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019719 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 p += l; /* skip multi-byte character */
19721 else
19722#endif
19723 {
19724 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19725 ++p;
19726 }
19727 }
19728}
19729
19730/*
19731 * "toupper(string)" function
19732 */
19733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 typval_T *argvars;
19736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019738 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019739 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740}
19741
19742/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019743 * "tr(string, fromstr, tostr)" function
19744 */
19745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 typval_T *argvars;
19748 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019749{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019750 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019751 char_u *fromstr;
19752 char_u *tostr;
19753 char_u *p;
19754#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019755 int inlen;
19756 int fromlen;
19757 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019758 int idx;
19759 char_u *cpstr;
19760 int cplen;
19761 int first = TRUE;
19762#endif
19763 char_u buf[NUMBUFLEN];
19764 char_u buf2[NUMBUFLEN];
19765 garray_T ga;
19766
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019767 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019768 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19769 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019770
19771 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019772 rettv->v_type = VAR_STRING;
19773 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019774 if (fromstr == NULL || tostr == NULL)
19775 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019776 ga_init2(&ga, (int)sizeof(char), 80);
19777
19778#ifdef FEAT_MBYTE
19779 if (!has_mbyte)
19780#endif
19781 /* not multi-byte: fromstr and tostr must be the same length */
19782 if (STRLEN(fromstr) != STRLEN(tostr))
19783 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019784#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019785error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019786#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019787 EMSG2(_(e_invarg2), fromstr);
19788 ga_clear(&ga);
19789 return;
19790 }
19791
19792 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019793 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019794 {
19795#ifdef FEAT_MBYTE
19796 if (has_mbyte)
19797 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019798 inlen = (*mb_ptr2len)(in_str);
19799 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019800 cplen = inlen;
19801 idx = 0;
19802 for (p = fromstr; *p != NUL; p += fromlen)
19803 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019804 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019805 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019806 {
19807 for (p = tostr; *p != NUL; p += tolen)
19808 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019809 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019810 if (idx-- == 0)
19811 {
19812 cplen = tolen;
19813 cpstr = p;
19814 break;
19815 }
19816 }
19817 if (*p == NUL) /* tostr is shorter than fromstr */
19818 goto error;
19819 break;
19820 }
19821 ++idx;
19822 }
19823
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019824 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019825 {
19826 /* Check that fromstr and tostr have the same number of
19827 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019828 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019829 first = FALSE;
19830 for (p = tostr; *p != NUL; p += tolen)
19831 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019832 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019833 --idx;
19834 }
19835 if (idx != 0)
19836 goto error;
19837 }
19838
Bram Moolenaarcde88542015-08-11 19:14:00 +020019839 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019840 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019841 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019842
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019843 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019844 }
19845 else
19846#endif
19847 {
19848 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019849 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019850 if (p != NULL)
19851 ga_append(&ga, tostr[p - fromstr]);
19852 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019853 ga_append(&ga, *in_str);
19854 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019855 }
19856 }
19857
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019858 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019859 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019860 ga_append(&ga, NUL);
19861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019862 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019863}
19864
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019865#ifdef FEAT_FLOAT
19866/*
19867 * "trunc({float})" function
19868 */
19869 static void
19870f_trunc(argvars, rettv)
19871 typval_T *argvars;
19872 typval_T *rettv;
19873{
19874 float_T f;
19875
19876 rettv->v_type = VAR_FLOAT;
19877 if (get_float_arg(argvars, &f) == OK)
19878 /* trunc() is not in C90, use floor() or ceil() instead. */
19879 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19880 else
19881 rettv->vval.v_float = 0.0;
19882}
19883#endif
19884
Bram Moolenaar8299df92004-07-10 09:47:34 +000019885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 * "type(expr)" function
19887 */
19888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019889f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019890 typval_T *argvars;
19891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019893 int n;
19894
19895 switch (argvars[0].v_type)
19896 {
19897 case VAR_NUMBER: n = 0; break;
19898 case VAR_STRING: n = 1; break;
19899 case VAR_FUNC: n = 2; break;
19900 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019901 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019902#ifdef FEAT_FLOAT
19903 case VAR_FLOAT: n = 5; break;
19904#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019905 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19906 }
19907 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908}
19909
19910/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019911 * "undofile(name)" function
19912 */
19913 static void
19914f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019915 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019916 typval_T *rettv;
19917{
19918 rettv->v_type = VAR_STRING;
19919#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019920 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019921 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019922
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019923 if (*fname == NUL)
19924 {
19925 /* If there is no file name there will be no undo file. */
19926 rettv->vval.v_string = NULL;
19927 }
19928 else
19929 {
19930 char_u *ffname = FullName_save(fname, FALSE);
19931
19932 if (ffname != NULL)
19933 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19934 vim_free(ffname);
19935 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019936 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019937#else
19938 rettv->vval.v_string = NULL;
19939#endif
19940}
19941
19942/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019943 * "undotree()" function
19944 */
19945 static void
19946f_undotree(argvars, rettv)
19947 typval_T *argvars UNUSED;
19948 typval_T *rettv;
19949{
19950 if (rettv_dict_alloc(rettv) == OK)
19951 {
19952 dict_T *dict = rettv->vval.v_dict;
19953 list_T *list;
19954
Bram Moolenaar730cde92010-06-27 05:18:54 +020019955 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019956 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019957 dict_add_nr_str(dict, "save_last",
19958 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019959 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19960 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019961 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019962
19963 list = list_alloc();
19964 if (list != NULL)
19965 {
19966 u_eval_tree(curbuf->b_u_oldhead, list);
19967 dict_add_list(dict, "entries", list);
19968 }
19969 }
19970}
19971
19972/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019973 * "values(dict)" function
19974 */
19975 static void
19976f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019977 typval_T *argvars;
19978 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019979{
19980 dict_list(argvars, rettv, 1);
19981}
19982
19983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 * "virtcol(string)" function
19985 */
19986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019988 typval_T *argvars;
19989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990{
19991 colnr_T vcol = 0;
19992 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019993 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019995 fp = var2fpos(&argvars[0], FALSE, &fnum);
19996 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19997 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 {
19999 getvvcol(curwin, fp, NULL, NULL, &vcol);
20000 ++vcol;
20001 }
20002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020003 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004}
20005
20006/*
20007 * "visualmode()" function
20008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020011 typval_T *argvars UNUSED;
20012 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 char_u str[2];
20015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 str[0] = curbuf->b_visual_mode_eval;
20018 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020019 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020
20021 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020022 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024}
20025
20026/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020027 * "wildmenumode()" function
20028 */
20029 static void
20030f_wildmenumode(argvars, rettv)
20031 typval_T *argvars UNUSED;
20032 typval_T *rettv UNUSED;
20033{
20034#ifdef FEAT_WILDMENU
20035 if (wild_menu_showing)
20036 rettv->vval.v_number = 1;
20037#endif
20038}
20039
20040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 * "winbufnr(nr)" function
20042 */
20043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020044f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020045 typval_T *argvars;
20046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047{
20048 win_T *wp;
20049
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020050 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020052 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055}
20056
20057/*
20058 * "wincol()" function
20059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020062 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064{
20065 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067}
20068
20069/*
20070 * "winheight(nr)" function
20071 */
20072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020073f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 typval_T *argvars;
20075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076{
20077 win_T *wp;
20078
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020079 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020083 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084}
20085
20086/*
20087 * "winline()" function
20088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020090f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020091 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093{
20094 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096}
20097
20098/*
20099 * "winnr()" function
20100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020102f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020103 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105{
20106 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020107
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020109 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020111 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112}
20113
20114/*
20115 * "winrestcmd()" function
20116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020119 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121{
20122#ifdef FEAT_WINDOWS
20123 win_T *wp;
20124 int winnr = 1;
20125 garray_T ga;
20126 char_u buf[50];
20127
20128 ga_init2(&ga, (int)sizeof(char), 70);
20129 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20130 {
20131 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20132 ga_concat(&ga, buf);
20133# ifdef FEAT_VERTSPLIT
20134 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20135 ga_concat(&ga, buf);
20136# endif
20137 ++winnr;
20138 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020139 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020145 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146}
20147
20148/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020149 * "winrestview()" function
20150 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020151 static void
20152f_winrestview(argvars, rettv)
20153 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020154 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020155{
20156 dict_T *dict;
20157
20158 if (argvars[0].v_type != VAR_DICT
20159 || (dict = argvars[0].vval.v_dict) == NULL)
20160 EMSG(_(e_invarg));
20161 else
20162 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020163 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20164 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20165 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20166 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020167#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020168 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20169 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020170#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020171 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20172 {
20173 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20174 curwin->w_set_curswant = FALSE;
20175 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020176
Bram Moolenaar82c25852014-05-28 16:47:16 +020020177 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20178 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020179#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020180 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20181 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020182#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020183 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20184 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20185 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20186 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020187
20188 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020189 win_new_height(curwin, curwin->w_height);
20190# ifdef FEAT_VERTSPLIT
20191 win_new_width(curwin, W_WIDTH(curwin));
20192# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020193 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020194
Bram Moolenaarb851a962014-10-31 15:45:52 +010020195 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020196 curwin->w_topline = 1;
20197 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20198 curwin->w_topline = curbuf->b_ml.ml_line_count;
20199#ifdef FEAT_DIFF
20200 check_topfill(curwin, TRUE);
20201#endif
20202 }
20203}
20204
20205/*
20206 * "winsaveview()" function
20207 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020208 static void
20209f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020210 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020211 typval_T *rettv;
20212{
20213 dict_T *dict;
20214
Bram Moolenaara800b422010-06-27 01:15:55 +020020215 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020216 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020217 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020218
20219 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20220 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20221#ifdef FEAT_VIRTUALEDIT
20222 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20223#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020224 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020225 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20226
20227 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20228#ifdef FEAT_DIFF
20229 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20230#endif
20231 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20232 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20233}
20234
20235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 * "winwidth(nr)" function
20237 */
20238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020239f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 typval_T *argvars;
20241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242{
20243 win_T *wp;
20244
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020245 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 else
20249#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020252 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253#endif
20254}
20255
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020257 * "wordcount()" function
20258 */
20259 static void
20260f_wordcount(argvars, rettv)
20261 typval_T *argvars UNUSED;
20262 typval_T *rettv;
20263{
20264 if (rettv_dict_alloc(rettv) == FAIL)
20265 return;
20266 cursor_pos_info(rettv->vval.v_dict);
20267}
20268
20269/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020270 * Write list of strings to file
20271 */
20272 static int
20273write_list(fd, list, binary)
20274 FILE *fd;
20275 list_T *list;
20276 int binary;
20277{
20278 listitem_T *li;
20279 int c;
20280 int ret = OK;
20281 char_u *s;
20282
20283 for (li = list->lv_first; li != NULL; li = li->li_next)
20284 {
20285 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20286 {
20287 if (*s == '\n')
20288 c = putc(NUL, fd);
20289 else
20290 c = putc(*s, fd);
20291 if (c == EOF)
20292 {
20293 ret = FAIL;
20294 break;
20295 }
20296 }
20297 if (!binary || li->li_next != NULL)
20298 if (putc('\n', fd) == EOF)
20299 {
20300 ret = FAIL;
20301 break;
20302 }
20303 if (ret == FAIL)
20304 {
20305 EMSG(_(e_write));
20306 break;
20307 }
20308 }
20309 return ret;
20310}
20311
20312/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020313 * "writefile()" function
20314 */
20315 static void
20316f_writefile(argvars, rettv)
20317 typval_T *argvars;
20318 typval_T *rettv;
20319{
20320 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020321 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020322 char_u *fname;
20323 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020324 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020325
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020326 if (check_restricted() || check_secure())
20327 return;
20328
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020329 if (argvars[0].v_type != VAR_LIST)
20330 {
20331 EMSG2(_(e_listarg), "writefile()");
20332 return;
20333 }
20334 if (argvars[0].vval.v_list == NULL)
20335 return;
20336
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020337 if (argvars[2].v_type != VAR_UNKNOWN)
20338 {
20339 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20340 binary = TRUE;
20341 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20342 append = TRUE;
20343 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020344
20345 /* Always open the file in binary mode, library functions have a mind of
20346 * their own about CR-LF conversion. */
20347 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020348 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20349 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020350 {
20351 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20352 ret = -1;
20353 }
20354 else
20355 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020356 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20357 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020358 fclose(fd);
20359 }
20360
20361 rettv->vval.v_number = ret;
20362}
20363
20364/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020365 * "xor(expr, expr)" function
20366 */
20367 static void
20368f_xor(argvars, rettv)
20369 typval_T *argvars;
20370 typval_T *rettv;
20371{
20372 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20373 ^ get_tv_number_chk(&argvars[1], NULL);
20374}
20375
20376
20377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020379 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 */
20381 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020382var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020384 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020385 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020387 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020389 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390
Bram Moolenaara5525202006-03-02 22:52:09 +000020391 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020392 if (varp->v_type == VAR_LIST)
20393 {
20394 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020395 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020396 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020397 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020398
20399 l = varp->vval.v_list;
20400 if (l == NULL)
20401 return NULL;
20402
20403 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020404 pos.lnum = list_find_nr(l, 0L, &error);
20405 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020406 return NULL; /* invalid line number */
20407
20408 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020409 pos.col = list_find_nr(l, 1L, &error);
20410 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020411 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020412 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020413
20414 /* We accept "$" for the column number: last column. */
20415 li = list_find(l, 1L);
20416 if (li != NULL && li->li_tv.v_type == VAR_STRING
20417 && li->li_tv.vval.v_string != NULL
20418 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20419 pos.col = len + 1;
20420
Bram Moolenaara5525202006-03-02 22:52:09 +000020421 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020422 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020423 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020424 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020425
Bram Moolenaara5525202006-03-02 22:52:09 +000020426#ifdef FEAT_VIRTUALEDIT
20427 /* Get the virtual offset. Defaults to zero. */
20428 pos.coladd = list_find_nr(l, 2L, &error);
20429 if (error)
20430 pos.coladd = 0;
20431#endif
20432
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020433 return &pos;
20434 }
20435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020436 name = get_tv_string_chk(varp);
20437 if (name == NULL)
20438 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020439 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020441 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20442 {
20443 if (VIsual_active)
20444 return &VIsual;
20445 return &curwin->w_cursor;
20446 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020447 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020449 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20451 return NULL;
20452 return pp;
20453 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020454
20455#ifdef FEAT_VIRTUALEDIT
20456 pos.coladd = 0;
20457#endif
20458
Bram Moolenaar477933c2007-07-17 14:32:23 +000020459 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020460 {
20461 pos.col = 0;
20462 if (name[1] == '0') /* "w0": first visible line */
20463 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020464 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020465 pos.lnum = curwin->w_topline;
20466 return &pos;
20467 }
20468 else if (name[1] == '$') /* "w$": last visible line */
20469 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020470 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020471 pos.lnum = curwin->w_botline - 1;
20472 return &pos;
20473 }
20474 }
20475 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020477 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 {
20479 pos.lnum = curbuf->b_ml.ml_line_count;
20480 pos.col = 0;
20481 }
20482 else
20483 {
20484 pos.lnum = curwin->w_cursor.lnum;
20485 pos.col = (colnr_T)STRLEN(ml_get_curline());
20486 }
20487 return &pos;
20488 }
20489 return NULL;
20490}
20491
20492/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020493 * Convert list in "arg" into a position and optional file number.
20494 * When "fnump" is NULL there is no file number, only 3 items.
20495 * Note that the column is passed on as-is, the caller may want to decrement
20496 * it to use 1 for the first column.
20497 * Return FAIL when conversion is not possible, doesn't check the position for
20498 * validity.
20499 */
20500 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020501list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020502 typval_T *arg;
20503 pos_T *posp;
20504 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020505 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020506{
20507 list_T *l = arg->vval.v_list;
20508 long i = 0;
20509 long n;
20510
Bram Moolenaar493c1782014-05-28 14:34:46 +020020511 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20512 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020513 if (arg->v_type != VAR_LIST
20514 || l == NULL
20515 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020516 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020517 return FAIL;
20518
20519 if (fnump != NULL)
20520 {
20521 n = list_find_nr(l, i++, NULL); /* fnum */
20522 if (n < 0)
20523 return FAIL;
20524 if (n == 0)
20525 n = curbuf->b_fnum; /* current buffer */
20526 *fnump = n;
20527 }
20528
20529 n = list_find_nr(l, i++, NULL); /* lnum */
20530 if (n < 0)
20531 return FAIL;
20532 posp->lnum = n;
20533
20534 n = list_find_nr(l, i++, NULL); /* col */
20535 if (n < 0)
20536 return FAIL;
20537 posp->col = n;
20538
20539#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020540 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020541 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020542 posp->coladd = 0;
20543 else
20544 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020545#endif
20546
Bram Moolenaar493c1782014-05-28 14:34:46 +020020547 if (curswantp != NULL)
20548 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20549
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020550 return OK;
20551}
20552
20553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 * Get the length of an environment variable name.
20555 * Advance "arg" to the first character after the name.
20556 * Return 0 for error.
20557 */
20558 static int
20559get_env_len(arg)
20560 char_u **arg;
20561{
20562 char_u *p;
20563 int len;
20564
20565 for (p = *arg; vim_isIDc(*p); ++p)
20566 ;
20567 if (p == *arg) /* no name found */
20568 return 0;
20569
20570 len = (int)(p - *arg);
20571 *arg = p;
20572 return len;
20573}
20574
20575/*
20576 * Get the length of the name of a function or internal variable.
20577 * "arg" is advanced to the first non-white character after the name.
20578 * Return 0 if something is wrong.
20579 */
20580 static int
20581get_id_len(arg)
20582 char_u **arg;
20583{
20584 char_u *p;
20585 int len;
20586
20587 /* Find the end of the name. */
20588 for (p = *arg; eval_isnamec(*p); ++p)
20589 ;
20590 if (p == *arg) /* no name found */
20591 return 0;
20592
20593 len = (int)(p - *arg);
20594 *arg = skipwhite(p);
20595
20596 return len;
20597}
20598
20599/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020600 * Get the length of the name of a variable or function.
20601 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020603 * Return -1 if curly braces expansion failed.
20604 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 * If the name contains 'magic' {}'s, expand them and return the
20606 * expanded name in an allocated string via 'alias' - caller must free.
20607 */
20608 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020609get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 char_u **arg;
20611 char_u **alias;
20612 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020613 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614{
20615 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 char_u *p;
20617 char_u *expr_start;
20618 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619
20620 *alias = NULL; /* default to no alias */
20621
20622 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20623 && (*arg)[2] == (int)KE_SNR)
20624 {
20625 /* hard coded <SNR>, already translated */
20626 *arg += 3;
20627 return get_id_len(arg) + 3;
20628 }
20629 len = eval_fname_script(*arg);
20630 if (len > 0)
20631 {
20632 /* literal "<SID>", "s:" or "<SNR>" */
20633 *arg += len;
20634 }
20635
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020637 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020639 p = find_name_end(*arg, &expr_start, &expr_end,
20640 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 if (expr_start != NULL)
20642 {
20643 char_u *temp_string;
20644
20645 if (!evaluate)
20646 {
20647 len += (int)(p - *arg);
20648 *arg = skipwhite(p);
20649 return len;
20650 }
20651
20652 /*
20653 * Include any <SID> etc in the expanded string:
20654 * Thus the -len here.
20655 */
20656 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20657 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020658 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659 *alias = temp_string;
20660 *arg = skipwhite(p);
20661 return (int)STRLEN(temp_string);
20662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663
20664 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020665 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 EMSG2(_(e_invexpr2), *arg);
20667
20668 return len;
20669}
20670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020671/*
20672 * Find the end of a variable or function name, taking care of magic braces.
20673 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20674 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020675 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020676 * Return a pointer to just after the name. Equal to "arg" if there is no
20677 * valid name.
20678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020680find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 char_u *arg;
20682 char_u **expr_start;
20683 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020684 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020686 int mb_nest = 0;
20687 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 char_u *p;
20689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020690 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020692 *expr_start = NULL;
20693 *expr_end = NULL;
20694 }
20695
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020696 /* Quick check for valid starting character. */
20697 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20698 return arg;
20699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020700 for (p = arg; *p != NUL
20701 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020702 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020703 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020704 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020705 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020706 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020707 if (*p == '\'')
20708 {
20709 /* skip over 'string' to avoid counting [ and ] inside it. */
20710 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20711 ;
20712 if (*p == NUL)
20713 break;
20714 }
20715 else if (*p == '"')
20716 {
20717 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20718 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20719 if (*p == '\\' && p[1] != NUL)
20720 ++p;
20721 if (*p == NUL)
20722 break;
20723 }
20724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020725 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020727 if (*p == '[')
20728 ++br_nest;
20729 else if (*p == ']')
20730 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020733 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020735 if (*p == '{')
20736 {
20737 mb_nest++;
20738 if (expr_start != NULL && *expr_start == NULL)
20739 *expr_start = p;
20740 }
20741 else if (*p == '}')
20742 {
20743 mb_nest--;
20744 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20745 *expr_end = p;
20746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 }
20749
20750 return p;
20751}
20752
20753/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020754 * Expands out the 'magic' {}'s in a variable/function name.
20755 * Note that this can call itself recursively, to deal with
20756 * constructs like foo{bar}{baz}{bam}
20757 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20758 * "in_start" ^
20759 * "expr_start" ^
20760 * "expr_end" ^
20761 * "in_end" ^
20762 *
20763 * Returns a new allocated string, which the caller must free.
20764 * Returns NULL for failure.
20765 */
20766 static char_u *
20767make_expanded_name(in_start, expr_start, expr_end, in_end)
20768 char_u *in_start;
20769 char_u *expr_start;
20770 char_u *expr_end;
20771 char_u *in_end;
20772{
20773 char_u c1;
20774 char_u *retval = NULL;
20775 char_u *temp_result;
20776 char_u *nextcmd = NULL;
20777
20778 if (expr_end == NULL || in_end == NULL)
20779 return NULL;
20780 *expr_start = NUL;
20781 *expr_end = NUL;
20782 c1 = *in_end;
20783 *in_end = NUL;
20784
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020785 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020786 if (temp_result != NULL && nextcmd == NULL)
20787 {
20788 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20789 + (in_end - expr_end) + 1));
20790 if (retval != NULL)
20791 {
20792 STRCPY(retval, in_start);
20793 STRCAT(retval, temp_result);
20794 STRCAT(retval, expr_end + 1);
20795 }
20796 }
20797 vim_free(temp_result);
20798
20799 *in_end = c1; /* put char back for error messages */
20800 *expr_start = '{';
20801 *expr_end = '}';
20802
20803 if (retval != NULL)
20804 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020805 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020806 if (expr_start != NULL)
20807 {
20808 /* Further expansion! */
20809 temp_result = make_expanded_name(retval, expr_start,
20810 expr_end, temp_result);
20811 vim_free(retval);
20812 retval = temp_result;
20813 }
20814 }
20815
20816 return retval;
20817}
20818
20819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020821 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 */
20823 static int
20824eval_isnamec(c)
20825 int c;
20826{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020827 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20828}
20829
20830/*
20831 * Return TRUE if character "c" can be used as the first character in a
20832 * variable or function name (excluding '{' and '}').
20833 */
20834 static int
20835eval_isnamec1(c)
20836 int c;
20837{
20838 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839}
20840
20841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842 * Set number v: variable to "val".
20843 */
20844 void
20845set_vim_var_nr(idx, val)
20846 int idx;
20847 long val;
20848{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020849 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850}
20851
20852/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020853 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 */
20855 long
20856get_vim_var_nr(idx)
20857 int idx;
20858{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020859 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860}
20861
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020862/*
20863 * Get string v: variable value. Uses a static buffer, can only be used once.
20864 */
20865 char_u *
20866get_vim_var_str(idx)
20867 int idx;
20868{
20869 return get_tv_string(&vimvars[idx].vv_tv);
20870}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020871
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020873 * Get List v: variable value. Caller must take care of reference count when
20874 * needed.
20875 */
20876 list_T *
20877get_vim_var_list(idx)
20878 int idx;
20879{
20880 return vimvars[idx].vv_list;
20881}
20882
20883/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020884 * Set v:char to character "c".
20885 */
20886 void
20887set_vim_var_char(c)
20888 int c;
20889{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020890 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020891
20892#ifdef FEAT_MBYTE
20893 if (has_mbyte)
20894 buf[(*mb_char2bytes)(c, buf)] = NUL;
20895 else
20896#endif
20897 {
20898 buf[0] = c;
20899 buf[1] = NUL;
20900 }
20901 set_vim_var_string(VV_CHAR, buf, -1);
20902}
20903
20904/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020905 * Set v:count to "count" and v:count1 to "count1".
20906 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 */
20908 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020909set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 long count;
20911 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020912 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020914 if (set_prevcount)
20915 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020916 vimvars[VV_COUNT].vv_nr = count;
20917 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918}
20919
20920/*
20921 * Set string v: variable to a copy of "val".
20922 */
20923 void
20924set_vim_var_string(idx, val, len)
20925 int idx;
20926 char_u *val;
20927 int len; /* length of "val" to use or -1 (whole string) */
20928{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020929 /* Need to do this (at least) once, since we can't initialize a union.
20930 * Will always be invoked when "v:progname" is set. */
20931 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20932
Bram Moolenaare9a41262005-01-15 22:18:47 +000020933 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020935 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020937 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020939 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940}
20941
20942/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020943 * Set List v: variable to "val".
20944 */
20945 void
20946set_vim_var_list(idx, val)
20947 int idx;
20948 list_T *val;
20949{
20950 list_unref(vimvars[idx].vv_list);
20951 vimvars[idx].vv_list = val;
20952 if (val != NULL)
20953 ++val->lv_refcount;
20954}
20955
20956/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020957 * Set Dictionary v: variable to "val".
20958 */
20959 void
20960set_vim_var_dict(idx, val)
20961 int idx;
20962 dict_T *val;
20963{
20964 int todo;
20965 hashitem_T *hi;
20966
20967 dict_unref(vimvars[idx].vv_dict);
20968 vimvars[idx].vv_dict = val;
20969 if (val != NULL)
20970 {
20971 ++val->dv_refcount;
20972
20973 /* Set readonly */
20974 todo = (int)val->dv_hashtab.ht_used;
20975 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
20976 {
20977 if (HASHITEM_EMPTY(hi))
20978 continue;
20979 --todo;
20980 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20981 }
20982 }
20983}
20984
20985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 * Set v:register if needed.
20987 */
20988 void
20989set_reg_var(c)
20990 int c;
20991{
20992 char_u regname;
20993
20994 if (c == 0 || c == ' ')
20995 regname = '"';
20996 else
20997 regname = c;
20998 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020999 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 set_vim_var_string(VV_REG, &regname, 1);
21001}
21002
21003/*
21004 * Get or set v:exception. If "oldval" == NULL, return the current value.
21005 * Otherwise, restore the value to "oldval" and return NULL.
21006 * Must always be called in pairs to save and restore v:exception! Does not
21007 * take care of memory allocations.
21008 */
21009 char_u *
21010v_exception(oldval)
21011 char_u *oldval;
21012{
21013 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021014 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015
Bram Moolenaare9a41262005-01-15 22:18:47 +000021016 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 return NULL;
21018}
21019
21020/*
21021 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21022 * Otherwise, restore the value to "oldval" and return NULL.
21023 * Must always be called in pairs to save and restore v:throwpoint! Does not
21024 * take care of memory allocations.
21025 */
21026 char_u *
21027v_throwpoint(oldval)
21028 char_u *oldval;
21029{
21030 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021031 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032
Bram Moolenaare9a41262005-01-15 22:18:47 +000021033 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 return NULL;
21035}
21036
21037#if defined(FEAT_AUTOCMD) || defined(PROTO)
21038/*
21039 * Set v:cmdarg.
21040 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21041 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21042 * Must always be called in pairs!
21043 */
21044 char_u *
21045set_cmdarg(eap, oldarg)
21046 exarg_T *eap;
21047 char_u *oldarg;
21048{
21049 char_u *oldval;
21050 char_u *newval;
21051 unsigned len;
21052
Bram Moolenaare9a41262005-01-15 22:18:47 +000021053 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021054 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021056 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021057 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021058 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 }
21060
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021061 if (eap->force_bin == FORCE_BIN)
21062 len = 6;
21063 else if (eap->force_bin == FORCE_NOBIN)
21064 len = 8;
21065 else
21066 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021067
21068 if (eap->read_edit)
21069 len += 7;
21070
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021071 if (eap->force_ff != 0)
21072 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21073# ifdef FEAT_MBYTE
21074 if (eap->force_enc != 0)
21075 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021076 if (eap->bad_char != 0)
21077 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021078# endif
21079
21080 newval = alloc(len + 1);
21081 if (newval == NULL)
21082 return NULL;
21083
21084 if (eap->force_bin == FORCE_BIN)
21085 sprintf((char *)newval, " ++bin");
21086 else if (eap->force_bin == FORCE_NOBIN)
21087 sprintf((char *)newval, " ++nobin");
21088 else
21089 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021090
21091 if (eap->read_edit)
21092 STRCAT(newval, " ++edit");
21093
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021094 if (eap->force_ff != 0)
21095 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21096 eap->cmd + eap->force_ff);
21097# ifdef FEAT_MBYTE
21098 if (eap->force_enc != 0)
21099 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21100 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021101 if (eap->bad_char == BAD_KEEP)
21102 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21103 else if (eap->bad_char == BAD_DROP)
21104 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21105 else if (eap->bad_char != 0)
21106 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021107# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021108 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021109 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110}
21111#endif
21112
21113/*
21114 * Get the value of internal variable "name".
21115 * Return OK or FAIL.
21116 */
21117 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021118get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 char_u *name;
21120 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021121 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021122 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021123 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021124 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125{
21126 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021127 typval_T *tv = NULL;
21128 typval_T atv;
21129 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131
21132 /* truncate the name, so that we can use strcmp() */
21133 cc = name[len];
21134 name[len] = NUL;
21135
21136 /*
21137 * Check for "b:changedtick".
21138 */
21139 if (STRCMP(name, "b:changedtick") == 0)
21140 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021141 atv.v_type = VAR_NUMBER;
21142 atv.vval.v_number = curbuf->b_changedtick;
21143 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 }
21145
21146 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 * Check for user-defined variables.
21148 */
21149 else
21150 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021151 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021153 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021154 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021155 if (dip != NULL)
21156 *dip = v;
21157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 }
21159
Bram Moolenaare9a41262005-01-15 22:18:47 +000021160 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021162 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164 ret = FAIL;
21165 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021166 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021167 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168
21169 name[len] = cc;
21170
21171 return ret;
21172}
21173
21174/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021175 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21176 * Also handle function call with Funcref variable: func(expr)
21177 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21178 */
21179 static int
21180handle_subscript(arg, rettv, evaluate, verbose)
21181 char_u **arg;
21182 typval_T *rettv;
21183 int evaluate; /* do more than finding the end */
21184 int verbose; /* give error messages */
21185{
21186 int ret = OK;
21187 dict_T *selfdict = NULL;
21188 char_u *s;
21189 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021190 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021191
21192 while (ret == OK
21193 && (**arg == '['
21194 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021195 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021196 && !vim_iswhite(*(*arg - 1)))
21197 {
21198 if (**arg == '(')
21199 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021200 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021201 if (evaluate)
21202 {
21203 functv = *rettv;
21204 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021205
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021206 /* Invoke the function. Recursive! */
21207 s = functv.vval.v_string;
21208 }
21209 else
21210 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021211 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021212 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21213 &len, evaluate, selfdict);
21214
21215 /* Clear the funcref afterwards, so that deleting it while
21216 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021217 if (evaluate)
21218 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021219
21220 /* Stop the expression evaluation when immediately aborting on
21221 * error, or when an interrupt occurred or an exception was thrown
21222 * but not caught. */
21223 if (aborting())
21224 {
21225 if (ret == OK)
21226 clear_tv(rettv);
21227 ret = FAIL;
21228 }
21229 dict_unref(selfdict);
21230 selfdict = NULL;
21231 }
21232 else /* **arg == '[' || **arg == '.' */
21233 {
21234 dict_unref(selfdict);
21235 if (rettv->v_type == VAR_DICT)
21236 {
21237 selfdict = rettv->vval.v_dict;
21238 if (selfdict != NULL)
21239 ++selfdict->dv_refcount;
21240 }
21241 else
21242 selfdict = NULL;
21243 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21244 {
21245 clear_tv(rettv);
21246 ret = FAIL;
21247 }
21248 }
21249 }
21250 dict_unref(selfdict);
21251 return ret;
21252}
21253
21254/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021255 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021256 * value).
21257 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021258 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021259alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021260{
Bram Moolenaar33570922005-01-25 22:26:29 +000021261 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021262}
21263
21264/*
21265 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 * The string "s" must have been allocated, it is consumed.
21267 * Return NULL for out of memory, the variable otherwise.
21268 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021269 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021270alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 char_u *s;
21272{
Bram Moolenaar33570922005-01-25 22:26:29 +000021273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021275 rettv = alloc_tv();
21276 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021278 rettv->v_type = VAR_STRING;
21279 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 }
21281 else
21282 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284}
21285
21286/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021287 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021289 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021290free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021291 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292{
21293 if (varp != NULL)
21294 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021295 switch (varp->v_type)
21296 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021297 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021298 func_unref(varp->vval.v_string);
21299 /*FALLTHROUGH*/
21300 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021301 vim_free(varp->vval.v_string);
21302 break;
21303 case VAR_LIST:
21304 list_unref(varp->vval.v_list);
21305 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 case VAR_DICT:
21307 dict_unref(varp->vval.v_dict);
21308 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021309 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021310#ifdef FEAT_FLOAT
21311 case VAR_FLOAT:
21312#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021313 case VAR_UNKNOWN:
21314 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021315 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021316 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021317 break;
21318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 vim_free(varp);
21320 }
21321}
21322
21323/*
21324 * Free the memory for a variable value and set the value to NULL or 0.
21325 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021326 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021327clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021328 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329{
21330 if (varp != NULL)
21331 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021332 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021334 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021335 func_unref(varp->vval.v_string);
21336 /*FALLTHROUGH*/
21337 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021338 vim_free(varp->vval.v_string);
21339 varp->vval.v_string = NULL;
21340 break;
21341 case VAR_LIST:
21342 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021343 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021344 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021345 case VAR_DICT:
21346 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021347 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021348 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021349 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021350 varp->vval.v_number = 0;
21351 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021352#ifdef FEAT_FLOAT
21353 case VAR_FLOAT:
21354 varp->vval.v_float = 0.0;
21355 break;
21356#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021357 case VAR_UNKNOWN:
21358 break;
21359 default:
21360 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021362 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 }
21364}
21365
21366/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367 * Set the value of a variable to NULL without freeing items.
21368 */
21369 static void
21370init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021371 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021372{
21373 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021374 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021375}
21376
21377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 * Get the number value of a variable.
21379 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021380 * For incompatible types, return 0.
21381 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21382 * caller of incompatible types: it sets *denote to TRUE if "denote"
21383 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 */
21385 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021386get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021387 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021389 int error = FALSE;
21390
21391 return get_tv_number_chk(varp, &error); /* return 0L on error */
21392}
21393
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021394 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021395get_tv_number_chk(varp, denote)
21396 typval_T *varp;
21397 int *denote;
21398{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021399 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021401 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021403 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021404 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021405#ifdef FEAT_FLOAT
21406 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021407 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021408 break;
21409#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021410 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021411 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021412 break;
21413 case VAR_STRING:
21414 if (varp->vval.v_string != NULL)
21415 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021416 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021417 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021418 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021419 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021420 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021421 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021422 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021423 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021424 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021425 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021428 if (denote == NULL) /* useful for values that must be unsigned */
21429 n = -1;
21430 else
21431 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021432 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433}
21434
21435/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021436 * Get the lnum from the first argument.
21437 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021438 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 */
21440 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021441get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021442 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443{
Bram Moolenaar33570922005-01-25 22:26:29 +000021444 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 linenr_T lnum;
21446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021447 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 if (lnum == 0) /* no valid number, try using line() */
21449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021450 rettv.v_type = VAR_NUMBER;
21451 f_line(argvars, &rettv);
21452 lnum = rettv.vval.v_number;
21453 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 }
21455 return lnum;
21456}
21457
21458/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021459 * Get the lnum from the first argument.
21460 * Also accepts "$", then "buf" is used.
21461 * Returns 0 on error.
21462 */
21463 static linenr_T
21464get_tv_lnum_buf(argvars, buf)
21465 typval_T *argvars;
21466 buf_T *buf;
21467{
21468 if (argvars[0].v_type == VAR_STRING
21469 && argvars[0].vval.v_string != NULL
21470 && argvars[0].vval.v_string[0] == '$'
21471 && buf != NULL)
21472 return buf->b_ml.ml_line_count;
21473 return get_tv_number_chk(&argvars[0], NULL);
21474}
21475
21476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 * Get the string value of a variable.
21478 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021479 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21480 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 * If the String variable has never been set, return an empty string.
21482 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021483 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21484 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 */
21486 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021487get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021488 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489{
21490 static char_u mybuf[NUMBUFLEN];
21491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021492 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493}
21494
21495 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021496get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021497 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021498 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021500 char_u *res = get_tv_string_buf_chk(varp, buf);
21501
21502 return res != NULL ? res : (char_u *)"";
21503}
21504
Bram Moolenaar7d647822014-04-05 21:28:56 +020021505/*
21506 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21507 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021508 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021509get_tv_string_chk(varp)
21510 typval_T *varp;
21511{
21512 static char_u mybuf[NUMBUFLEN];
21513
21514 return get_tv_string_buf_chk(varp, mybuf);
21515}
21516
21517 static char_u *
21518get_tv_string_buf_chk(varp, buf)
21519 typval_T *varp;
21520 char_u *buf;
21521{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021522 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021524 case VAR_NUMBER:
21525 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21526 return buf;
21527 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021528 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021529 break;
21530 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021531 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021532 break;
21533 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021534 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021535 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021536#ifdef FEAT_FLOAT
21537 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021538 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021539 break;
21540#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021541 case VAR_STRING:
21542 if (varp->vval.v_string != NULL)
21543 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021544 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021545 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021546 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021549 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550}
21551
21552/*
21553 * Find variable "name" in the list of variables.
21554 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021555 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021556 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021557 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021559 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021560find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021562 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021563 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021566 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567
Bram Moolenaara7043832005-01-21 11:56:39 +000021568 ht = find_var_ht(name, &varname);
21569 if (htp != NULL)
21570 *htp = ht;
21571 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021573 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574}
21575
21576/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021577 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021578 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021580 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021581find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021582 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021583 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021584 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021585 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021586{
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 hashitem_T *hi;
21588
21589 if (*varname == NUL)
21590 {
21591 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021592 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021593 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021594 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021595 case 'g': return &globvars_var;
21596 case 'v': return &vimvars_var;
21597 case 'b': return &curbuf->b_bufvar;
21598 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021599#ifdef FEAT_WINDOWS
21600 case 't': return &curtab->tp_winvar;
21601#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021602 case 'l': return current_funccal == NULL
21603 ? NULL : &current_funccal->l_vars_var;
21604 case 'a': return current_funccal == NULL
21605 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021606 }
21607 return NULL;
21608 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021609
21610 hi = hash_find(ht, varname);
21611 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021612 {
21613 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021614 * worked find the variable again. Don't auto-load a script if it was
21615 * loaded already, otherwise it would be loaded every time when
21616 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021617 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021618 {
21619 /* Note: script_autoload() may make "hi" invalid. It must either
21620 * be obtained again or not used. */
21621 if (!script_autoload(varname, FALSE) || aborting())
21622 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021623 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021624 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021625 if (HASHITEM_EMPTY(hi))
21626 return NULL;
21627 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021628 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021629}
21630
21631/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021632 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021633 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021634 * Set "varname" to the start of name without ':'.
21635 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021637find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 char_u *name;
21639 char_u **varname;
21640{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021641 hashitem_T *hi;
21642
Bram Moolenaar73627d02015-08-11 15:46:09 +020021643 if (name[0] == NUL)
21644 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 if (name[1] != ':')
21646 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021647 /* The name must not start with a colon or #. */
21648 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 return NULL;
21650 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021651
21652 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021653 hi = hash_find(&compat_hashtab, name);
21654 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021655 return &compat_hashtab;
21656
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021658 return &globvarht; /* global variable */
21659 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 }
21661 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021662 if (*name == 'g') /* global variable */
21663 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021664 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21665 */
21666 if (vim_strchr(name + 2, ':') != NULL
21667 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021668 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021670 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021672 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021673#ifdef FEAT_WINDOWS
21674 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021675 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021676#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021677 if (*name == 'v') /* v: variable */
21678 return &vimvarht;
21679 if (*name == 'a' && current_funccal != NULL) /* function argument */
21680 return &current_funccal->l_avars.dv_hashtab;
21681 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21682 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 if (*name == 's' /* script variable */
21684 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21685 return &SCRIPT_VARS(current_SID);
21686 return NULL;
21687}
21688
21689/*
21690 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021691 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021692 * Returns NULL when it doesn't exist.
21693 */
21694 char_u *
21695get_var_value(name)
21696 char_u *name;
21697{
Bram Moolenaar33570922005-01-25 22:26:29 +000021698 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021700 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 if (v == NULL)
21702 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021703 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704}
21705
21706/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021707 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 * sourcing this script and when executing functions defined in the script.
21709 */
21710 void
21711new_script_vars(id)
21712 scid_T id;
21713{
Bram Moolenaara7043832005-01-21 11:56:39 +000021714 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 hashtab_T *ht;
21716 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021717
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21719 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021720 /* Re-allocating ga_data means that an ht_array pointing to
21721 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021722 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021723 for (i = 1; i <= ga_scripts.ga_len; ++i)
21724 {
21725 ht = &SCRIPT_VARS(i);
21726 if (ht->ht_mask == HT_INIT_SIZE - 1)
21727 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021728 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021729 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021730 }
21731
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 while (ga_scripts.ga_len < id)
21733 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021734 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021735 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021736 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 }
21739 }
21740}
21741
21742/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21744 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 */
21746 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021747init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021748 dict_T *dict;
21749 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021750 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751{
Bram Moolenaar33570922005-01-25 22:26:29 +000021752 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021753 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021754 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021755 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021756 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021757 dict_var->di_tv.vval.v_dict = dict;
21758 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021759 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021760 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21761 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762}
21763
21764/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021765 * Unreference a dictionary initialized by init_var_dict().
21766 */
21767 void
21768unref_var_dict(dict)
21769 dict_T *dict;
21770{
21771 /* Now the dict needs to be freed if no one else is using it, go back to
21772 * normal reference counting. */
21773 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21774 dict_unref(dict);
21775}
21776
21777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021779 * Frees all allocated variables and the value they contain.
21780 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 */
21782 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021783vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021784 hashtab_T *ht;
21785{
21786 vars_clear_ext(ht, TRUE);
21787}
21788
21789/*
21790 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21791 */
21792 static void
21793vars_clear_ext(ht, free_val)
21794 hashtab_T *ht;
21795 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796{
Bram Moolenaara7043832005-01-21 11:56:39 +000021797 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021798 hashitem_T *hi;
21799 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800
Bram Moolenaar33570922005-01-25 22:26:29 +000021801 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021802 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021803 for (hi = ht->ht_array; todo > 0; ++hi)
21804 {
21805 if (!HASHITEM_EMPTY(hi))
21806 {
21807 --todo;
21808
Bram Moolenaar33570922005-01-25 22:26:29 +000021809 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021810 * ht_array might change then. hash_clear() takes care of it
21811 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021812 v = HI2DI(hi);
21813 if (free_val)
21814 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021815 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021816 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021817 }
21818 }
21819 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021820 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821}
21822
Bram Moolenaara7043832005-01-21 11:56:39 +000021823/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 * Delete a variable from hashtab "ht" at item "hi".
21825 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021828delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021829 hashtab_T *ht;
21830 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831{
Bram Moolenaar33570922005-01-25 22:26:29 +000021832 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021833
21834 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021835 clear_tv(&di->di_tv);
21836 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837}
21838
21839/*
21840 * List the value of one internal variable.
21841 */
21842 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021843list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021844 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021846 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021848 char_u *tofree;
21849 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021850 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021851
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021852 current_copyID += COPYID_INC;
21853 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021854 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021855 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021856 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857}
21858
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021860list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 char_u *prefix;
21862 char_u *name;
21863 int type;
21864 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021865 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866{
Bram Moolenaar31859182007-08-14 20:41:13 +000021867 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21868 msg_start();
21869 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 if (name != NULL) /* "a:" vars don't have a name stored */
21871 msg_puts(name);
21872 msg_putchar(' ');
21873 msg_advance(22);
21874 if (type == VAR_NUMBER)
21875 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021876 else if (type == VAR_FUNC)
21877 msg_putchar('*');
21878 else if (type == VAR_LIST)
21879 {
21880 msg_putchar('[');
21881 if (*string == '[')
21882 ++string;
21883 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021884 else if (type == VAR_DICT)
21885 {
21886 msg_putchar('{');
21887 if (*string == '{')
21888 ++string;
21889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 else
21891 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021892
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021894
21895 if (type == VAR_FUNC)
21896 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021897 if (*first)
21898 {
21899 msg_clr_eos();
21900 *first = FALSE;
21901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902}
21903
21904/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021905 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 * If the variable already exists, the value is updated.
21907 * Otherwise the variable is created.
21908 */
21909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021910set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021913 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914{
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021917 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021919 ht = find_var_ht(name, &varname);
21920 if (ht == NULL || *varname == NUL)
21921 {
21922 EMSG2(_(e_illvar), name);
21923 return;
21924 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021925 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021926
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021927 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21928 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021929
Bram Moolenaar33570922005-01-25 22:26:29 +000021930 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021933 if (var_check_ro(v->di_flags, name, FALSE)
21934 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021935 return;
21936 if (v->di_tv.v_type != tv->v_type
21937 && !((v->di_tv.v_type == VAR_STRING
21938 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021939 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021940 || tv->v_type == VAR_NUMBER))
21941#ifdef FEAT_FLOAT
21942 && !((v->di_tv.v_type == VAR_NUMBER
21943 || v->di_tv.v_type == VAR_FLOAT)
21944 && (tv->v_type == VAR_NUMBER
21945 || tv->v_type == VAR_FLOAT))
21946#endif
21947 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021948 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021949 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950 return;
21951 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021952
21953 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021954 * Handle setting internal v: variables separately where needed to
21955 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021956 */
21957 if (ht == &vimvarht)
21958 {
21959 if (v->di_tv.v_type == VAR_STRING)
21960 {
21961 vim_free(v->di_tv.vval.v_string);
21962 if (copy || tv->v_type != VAR_STRING)
21963 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21964 else
21965 {
21966 /* Take over the string to avoid an extra alloc/free. */
21967 v->di_tv.vval.v_string = tv->vval.v_string;
21968 tv->vval.v_string = NULL;
21969 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021970 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021971 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021972 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021974 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021975 if (STRCMP(varname, "searchforward") == 0)
21976 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021977#ifdef FEAT_SEARCH_EXTRA
21978 else if (STRCMP(varname, "hlsearch") == 0)
21979 {
21980 no_hlsearch = !v->di_tv.vval.v_number;
21981 redraw_all_later(SOME_VALID);
21982 }
21983#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021984 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021985 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021986 else if (v->di_tv.v_type != tv->v_type)
21987 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000021988 }
21989
21990 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 }
21992 else /* add a new variable */
21993 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021994 /* Can't add "v:" variable. */
21995 if (ht == &vimvarht)
21996 {
21997 EMSG2(_(e_illvar), name);
21998 return;
21999 }
22000
Bram Moolenaar92124a32005-06-17 22:03:40 +000022001 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022002 if (!valid_varname(varname))
22003 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022004
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022005 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22006 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022007 if (v == NULL)
22008 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022012 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 return;
22014 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022015 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022017
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022018 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022020 else
22021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022022 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022023 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022024 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026}
22027
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022028/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022029 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022030 * Also give an error message.
22031 */
22032 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022033var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022034 int flags;
22035 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022036 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022037{
22038 if (flags & DI_FLAGS_RO)
22039 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022040 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 return TRUE;
22042 }
22043 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22044 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022045 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 return TRUE;
22047 }
22048 return FALSE;
22049}
22050
22051/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022052 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22053 * Also give an error message.
22054 */
22055 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022056var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022057 int flags;
22058 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022059 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022060{
22061 if (flags & DI_FLAGS_FIX)
22062 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022063 EMSG2(_("E795: Cannot delete variable %s"),
22064 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022065 return TRUE;
22066 }
22067 return FALSE;
22068}
22069
22070/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022071 * Check if a funcref is assigned to a valid variable name.
22072 * Return TRUE and give an error if not.
22073 */
22074 static int
22075var_check_func_name(name, new_var)
22076 char_u *name; /* points to start of variable name */
22077 int new_var; /* TRUE when creating the variable */
22078{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022079 /* Allow for w: b: s: and t:. */
22080 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022081 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22082 ? name[2] : name[0]))
22083 {
22084 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22085 name);
22086 return TRUE;
22087 }
22088 /* Don't allow hiding a function. When "v" is not NULL we might be
22089 * assigning another function to the same var, the type is checked
22090 * below. */
22091 if (new_var && function_exists(name))
22092 {
22093 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22094 name);
22095 return TRUE;
22096 }
22097 return FALSE;
22098}
22099
22100/*
22101 * Check if a variable name is valid.
22102 * Return FALSE and give an error if not.
22103 */
22104 static int
22105valid_varname(varname)
22106 char_u *varname;
22107{
22108 char_u *p;
22109
22110 for (p = varname; *p != NUL; ++p)
22111 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22112 && *p != AUTOLOAD_CHAR)
22113 {
22114 EMSG2(_(e_illvar), varname);
22115 return FALSE;
22116 }
22117 return TRUE;
22118}
22119
22120/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022121 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022122 * Also give an error message, using "name" or _("name") when use_gettext is
22123 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022124 */
22125 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022126tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022127 int lock;
22128 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022129 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022130{
22131 if (lock & VAR_LOCKED)
22132 {
22133 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022134 name == NULL ? (char_u *)_("Unknown")
22135 : use_gettext ? (char_u *)_(name)
22136 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022137 return TRUE;
22138 }
22139 if (lock & VAR_FIXED)
22140 {
22141 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022142 name == NULL ? (char_u *)_("Unknown")
22143 : use_gettext ? (char_u *)_(name)
22144 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022145 return TRUE;
22146 }
22147 return FALSE;
22148}
22149
22150/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022151 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022152 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022153 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022154 * It is OK for "from" and "to" to point to the same item. This is used to
22155 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022156 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022157 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022158copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022159 typval_T *from;
22160 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022162 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022163 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022164 switch (from->v_type)
22165 {
22166 case VAR_NUMBER:
22167 to->vval.v_number = from->vval.v_number;
22168 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022169#ifdef FEAT_FLOAT
22170 case VAR_FLOAT:
22171 to->vval.v_float = from->vval.v_float;
22172 break;
22173#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 case VAR_STRING:
22175 case VAR_FUNC:
22176 if (from->vval.v_string == NULL)
22177 to->vval.v_string = NULL;
22178 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022179 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022180 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022181 if (from->v_type == VAR_FUNC)
22182 func_ref(to->vval.v_string);
22183 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022184 break;
22185 case VAR_LIST:
22186 if (from->vval.v_list == NULL)
22187 to->vval.v_list = NULL;
22188 else
22189 {
22190 to->vval.v_list = from->vval.v_list;
22191 ++to->vval.v_list->lv_refcount;
22192 }
22193 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022194 case VAR_DICT:
22195 if (from->vval.v_dict == NULL)
22196 to->vval.v_dict = NULL;
22197 else
22198 {
22199 to->vval.v_dict = from->vval.v_dict;
22200 ++to->vval.v_dict->dv_refcount;
22201 }
22202 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022203 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022204 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205 break;
22206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207}
22208
22209/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022210 * Make a copy of an item.
22211 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022212 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22213 * reference to an already copied list/dict can be used.
22214 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022215 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022216 static int
22217item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022218 typval_T *from;
22219 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022220 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022221 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022222{
22223 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022224 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022225
Bram Moolenaar33570922005-01-25 22:26:29 +000022226 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022227 {
22228 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022229 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022230 }
22231 ++recurse;
22232
22233 switch (from->v_type)
22234 {
22235 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022236#ifdef FEAT_FLOAT
22237 case VAR_FLOAT:
22238#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022239 case VAR_STRING:
22240 case VAR_FUNC:
22241 copy_tv(from, to);
22242 break;
22243 case VAR_LIST:
22244 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022245 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022246 if (from->vval.v_list == NULL)
22247 to->vval.v_list = NULL;
22248 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22249 {
22250 /* use the copy made earlier */
22251 to->vval.v_list = from->vval.v_list->lv_copylist;
22252 ++to->vval.v_list->lv_refcount;
22253 }
22254 else
22255 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22256 if (to->vval.v_list == NULL)
22257 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022258 break;
22259 case VAR_DICT:
22260 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022261 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022262 if (from->vval.v_dict == NULL)
22263 to->vval.v_dict = NULL;
22264 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22265 {
22266 /* use the copy made earlier */
22267 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22268 ++to->vval.v_dict->dv_refcount;
22269 }
22270 else
22271 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22272 if (to->vval.v_dict == NULL)
22273 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022274 break;
22275 default:
22276 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022277 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022278 }
22279 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022280 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022281}
22282
22283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 * ":echo expr1 ..." print each argument separated with a space, add a
22285 * newline at the end.
22286 * ":echon expr1 ..." print each argument plain.
22287 */
22288 void
22289ex_echo(eap)
22290 exarg_T *eap;
22291{
22292 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022293 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022294 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 char_u *p;
22296 int needclr = TRUE;
22297 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022298 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299
22300 if (eap->skip)
22301 ++emsg_skip;
22302 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22303 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022304 /* If eval1() causes an error message the text from the command may
22305 * still need to be cleared. E.g., "echo 22,44". */
22306 need_clr_eos = needclr;
22307
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022309 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 {
22311 /*
22312 * Report the invalid expression unless the expression evaluation
22313 * has been cancelled due to an aborting error, an interrupt, or an
22314 * exception.
22315 */
22316 if (!aborting())
22317 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022318 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 break;
22320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022321 need_clr_eos = FALSE;
22322
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 if (!eap->skip)
22324 {
22325 if (atstart)
22326 {
22327 atstart = FALSE;
22328 /* Call msg_start() after eval1(), evaluating the expression
22329 * may cause a message to appear. */
22330 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022331 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022332 /* Mark the saved text as finishing the line, so that what
22333 * follows is displayed on a new line when scrolling back
22334 * at the more prompt. */
22335 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 }
22339 else if (eap->cmdidx == CMD_echo)
22340 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022341 current_copyID += COPYID_INC;
22342 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022343 if (p != NULL)
22344 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022346 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022348 if (*p != TAB && needclr)
22349 {
22350 /* remove any text still there from the command */
22351 msg_clr_eos();
22352 needclr = FALSE;
22353 }
22354 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 }
22356 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022357 {
22358#ifdef FEAT_MBYTE
22359 if (has_mbyte)
22360 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022361 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022362
22363 (void)msg_outtrans_len_attr(p, i, echo_attr);
22364 p += i - 1;
22365 }
22366 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022368 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022371 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022373 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 arg = skipwhite(arg);
22375 }
22376 eap->nextcmd = check_nextcmd(arg);
22377
22378 if (eap->skip)
22379 --emsg_skip;
22380 else
22381 {
22382 /* remove text that may still be there from the command */
22383 if (needclr)
22384 msg_clr_eos();
22385 if (eap->cmdidx == CMD_echo)
22386 msg_end();
22387 }
22388}
22389
22390/*
22391 * ":echohl {name}".
22392 */
22393 void
22394ex_echohl(eap)
22395 exarg_T *eap;
22396{
22397 int id;
22398
22399 id = syn_name2id(eap->arg);
22400 if (id == 0)
22401 echo_attr = 0;
22402 else
22403 echo_attr = syn_id2attr(id);
22404}
22405
22406/*
22407 * ":execute expr1 ..." execute the result of an expression.
22408 * ":echomsg expr1 ..." Print a message
22409 * ":echoerr expr1 ..." Print an error
22410 * Each gets spaces around each argument and a newline at the end for
22411 * echo commands
22412 */
22413 void
22414ex_execute(eap)
22415 exarg_T *eap;
22416{
22417 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 int ret = OK;
22420 char_u *p;
22421 garray_T ga;
22422 int len;
22423 int save_did_emsg;
22424
22425 ga_init2(&ga, 1, 80);
22426
22427 if (eap->skip)
22428 ++emsg_skip;
22429 while (*arg != NUL && *arg != '|' && *arg != '\n')
22430 {
22431 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022432 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433 {
22434 /*
22435 * Report the invalid expression unless the expression evaluation
22436 * has been cancelled due to an aborting error, an interrupt, or an
22437 * exception.
22438 */
22439 if (!aborting())
22440 EMSG2(_(e_invexpr2), p);
22441 ret = FAIL;
22442 break;
22443 }
22444
22445 if (!eap->skip)
22446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022447 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 len = (int)STRLEN(p);
22449 if (ga_grow(&ga, len + 2) == FAIL)
22450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022451 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 ret = FAIL;
22453 break;
22454 }
22455 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 ga.ga_len += len;
22459 }
22460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022461 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 arg = skipwhite(arg);
22463 }
22464
22465 if (ret != FAIL && ga.ga_data != NULL)
22466 {
22467 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022470 out_flush();
22471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 else if (eap->cmdidx == CMD_echoerr)
22473 {
22474 /* We don't want to abort following commands, restore did_emsg. */
22475 save_did_emsg = did_emsg;
22476 EMSG((char_u *)ga.ga_data);
22477 if (!force_abort)
22478 did_emsg = save_did_emsg;
22479 }
22480 else if (eap->cmdidx == CMD_execute)
22481 do_cmdline((char_u *)ga.ga_data,
22482 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22483 }
22484
22485 ga_clear(&ga);
22486
22487 if (eap->skip)
22488 --emsg_skip;
22489
22490 eap->nextcmd = check_nextcmd(arg);
22491}
22492
22493/*
22494 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22495 * "arg" points to the "&" or '+' when called, to "option" when returning.
22496 * Returns NULL when no option name found. Otherwise pointer to the char
22497 * after the option name.
22498 */
22499 static char_u *
22500find_option_end(arg, opt_flags)
22501 char_u **arg;
22502 int *opt_flags;
22503{
22504 char_u *p = *arg;
22505
22506 ++p;
22507 if (*p == 'g' && p[1] == ':')
22508 {
22509 *opt_flags = OPT_GLOBAL;
22510 p += 2;
22511 }
22512 else if (*p == 'l' && p[1] == ':')
22513 {
22514 *opt_flags = OPT_LOCAL;
22515 p += 2;
22516 }
22517 else
22518 *opt_flags = 0;
22519
22520 if (!ASCII_ISALPHA(*p))
22521 return NULL;
22522 *arg = p;
22523
22524 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22525 p += 4; /* termcap option */
22526 else
22527 while (ASCII_ISALPHA(*p))
22528 ++p;
22529 return p;
22530}
22531
22532/*
22533 * ":function"
22534 */
22535 void
22536ex_function(eap)
22537 exarg_T *eap;
22538{
22539 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022540 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 int j;
22542 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022544 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 char_u *name = NULL;
22546 char_u *p;
22547 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022548 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 garray_T newargs;
22550 garray_T newlines;
22551 int varargs = FALSE;
22552 int mustend = FALSE;
22553 int flags = 0;
22554 ufunc_T *fp;
22555 int indent;
22556 int nesting;
22557 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022558 dictitem_T *v;
22559 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022560 static int func_nr = 0; /* number for nameless function */
22561 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022562 hashtab_T *ht;
22563 int todo;
22564 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022565 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566
22567 /*
22568 * ":function" without argument: list functions.
22569 */
22570 if (ends_excmd(*eap->arg))
22571 {
22572 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022573 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022574 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022575 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022576 {
22577 if (!HASHITEM_EMPTY(hi))
22578 {
22579 --todo;
22580 fp = HI2UF(hi);
22581 if (!isdigit(*fp->uf_name))
22582 list_func_head(fp, FALSE);
22583 }
22584 }
22585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 eap->nextcmd = check_nextcmd(eap->arg);
22587 return;
22588 }
22589
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022590 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022591 * ":function /pat": list functions matching pattern.
22592 */
22593 if (*eap->arg == '/')
22594 {
22595 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22596 if (!eap->skip)
22597 {
22598 regmatch_T regmatch;
22599
22600 c = *p;
22601 *p = NUL;
22602 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22603 *p = c;
22604 if (regmatch.regprog != NULL)
22605 {
22606 regmatch.rm_ic = p_ic;
22607
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022608 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022609 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22610 {
22611 if (!HASHITEM_EMPTY(hi))
22612 {
22613 --todo;
22614 fp = HI2UF(hi);
22615 if (!isdigit(*fp->uf_name)
22616 && vim_regexec(&regmatch, fp->uf_name, 0))
22617 list_func_head(fp, FALSE);
22618 }
22619 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022620 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022621 }
22622 }
22623 if (*p == '/')
22624 ++p;
22625 eap->nextcmd = check_nextcmd(p);
22626 return;
22627 }
22628
22629 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022630 * Get the function name. There are these situations:
22631 * func normal function name
22632 * "name" == func, "fudi.fd_dict" == NULL
22633 * dict.func new dictionary entry
22634 * "name" == NULL, "fudi.fd_dict" set,
22635 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22636 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022637 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022638 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22639 * dict.func existing dict entry that's not a Funcref
22640 * "name" == NULL, "fudi.fd_dict" set,
22641 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022642 * s:func script-local function name
22643 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022646 name = trans_function_name(&p, eap->skip, 0, &fudi);
22647 paren = (vim_strchr(p, '(') != NULL);
22648 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 {
22650 /*
22651 * Return on an invalid expression in braces, unless the expression
22652 * evaluation has been cancelled due to an aborting error, an
22653 * interrupt, or an exception.
22654 */
22655 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022657 if (!eap->skip && fudi.fd_newkey != NULL)
22658 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022659 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 else
22663 eap->skip = TRUE;
22664 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022665
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 /* An error in a function call during evaluation of an expression in magic
22667 * braces should not cause the function not to be defined. */
22668 saved_did_emsg = did_emsg;
22669 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670
22671 /*
22672 * ":function func" with only function name: list function.
22673 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 {
22676 if (!ends_excmd(*skipwhite(p)))
22677 {
22678 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022679 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 }
22681 eap->nextcmd = check_nextcmd(p);
22682 if (eap->nextcmd != NULL)
22683 *p = NUL;
22684 if (!eap->skip && !got_int)
22685 {
22686 fp = find_func(name);
22687 if (fp != NULL)
22688 {
22689 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022690 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022692 if (FUNCLINE(fp, j) == NULL)
22693 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 msg_putchar('\n');
22695 msg_outnum((long)(j + 1));
22696 if (j < 9)
22697 msg_putchar(' ');
22698 if (j < 99)
22699 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022700 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 out_flush(); /* show a line at a time */
22702 ui_breakcheck();
22703 }
22704 if (!got_int)
22705 {
22706 msg_putchar('\n');
22707 msg_puts((char_u *)" endfunction");
22708 }
22709 }
22710 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022711 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022713 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 }
22715
22716 /*
22717 * ":function name(arg1, arg2)" Define function.
22718 */
22719 p = skipwhite(p);
22720 if (*p != '(')
22721 {
22722 if (!eap->skip)
22723 {
22724 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022725 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 }
22727 /* attempt to continue by skipping some text */
22728 if (vim_strchr(p, '(') != NULL)
22729 p = vim_strchr(p, '(');
22730 }
22731 p = skipwhite(p + 1);
22732
22733 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22734 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22735
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022736 if (!eap->skip)
22737 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022738 /* Check the name of the function. Unless it's a dictionary function
22739 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022740 if (name != NULL)
22741 arg = name;
22742 else
22743 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022744 if (arg != NULL && (fudi.fd_di == NULL
22745 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022746 {
22747 if (*arg == K_SPECIAL)
22748 j = 3;
22749 else
22750 j = 0;
22751 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22752 : eval_isnamec(arg[j])))
22753 ++j;
22754 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022755 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022756 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022757 /* Disallow using the g: dict. */
22758 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22759 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022760 }
22761
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 /*
22763 * Isolate the arguments: "arg1, arg2, ...)"
22764 */
22765 while (*p != ')')
22766 {
22767 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22768 {
22769 varargs = TRUE;
22770 p += 3;
22771 mustend = TRUE;
22772 }
22773 else
22774 {
22775 arg = p;
22776 while (ASCII_ISALNUM(*p) || *p == '_')
22777 ++p;
22778 if (arg == p || isdigit(*arg)
22779 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22780 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22781 {
22782 if (!eap->skip)
22783 EMSG2(_("E125: Illegal argument: %s"), arg);
22784 break;
22785 }
22786 if (ga_grow(&newargs, 1) == FAIL)
22787 goto erret;
22788 c = *p;
22789 *p = NUL;
22790 arg = vim_strsave(arg);
22791 if (arg == NULL)
22792 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022793
22794 /* Check for duplicate argument name. */
22795 for (i = 0; i < newargs.ga_len; ++i)
22796 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22797 {
22798 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022799 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022800 goto erret;
22801 }
22802
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22804 *p = c;
22805 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 if (*p == ',')
22807 ++p;
22808 else
22809 mustend = TRUE;
22810 }
22811 p = skipwhite(p);
22812 if (mustend && *p != ')')
22813 {
22814 if (!eap->skip)
22815 EMSG2(_(e_invarg2), eap->arg);
22816 break;
22817 }
22818 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022819 if (*p != ')')
22820 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821 ++p; /* skip the ')' */
22822
Bram Moolenaare9a41262005-01-15 22:18:47 +000022823 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 for (;;)
22825 {
22826 p = skipwhite(p);
22827 if (STRNCMP(p, "range", 5) == 0)
22828 {
22829 flags |= FC_RANGE;
22830 p += 5;
22831 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022832 else if (STRNCMP(p, "dict", 4) == 0)
22833 {
22834 flags |= FC_DICT;
22835 p += 4;
22836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 else if (STRNCMP(p, "abort", 5) == 0)
22838 {
22839 flags |= FC_ABORT;
22840 p += 5;
22841 }
22842 else
22843 break;
22844 }
22845
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022846 /* When there is a line break use what follows for the function body.
22847 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22848 if (*p == '\n')
22849 line_arg = p + 1;
22850 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 EMSG(_(e_trailing));
22852
22853 /*
22854 * Read the body of the function, until ":endfunction" is found.
22855 */
22856 if (KeyTyped)
22857 {
22858 /* Check if the function already exists, don't let the user type the
22859 * whole function before telling him it doesn't work! For a script we
22860 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022861 if (!eap->skip && !eap->forceit)
22862 {
22863 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22864 EMSG(_(e_funcdict));
22865 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022866 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022869 if (!eap->skip && did_emsg)
22870 goto erret;
22871
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 msg_putchar('\n'); /* don't overwrite the function name */
22873 cmdline_row = msg_row;
22874 }
22875
22876 indent = 2;
22877 nesting = 0;
22878 for (;;)
22879 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022880 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022881 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022882 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022883 saved_wait_return = FALSE;
22884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022886 sourcing_lnum_off = sourcing_lnum;
22887
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022888 if (line_arg != NULL)
22889 {
22890 /* Use eap->arg, split up in parts by line breaks. */
22891 theline = line_arg;
22892 p = vim_strchr(theline, '\n');
22893 if (p == NULL)
22894 line_arg += STRLEN(line_arg);
22895 else
22896 {
22897 *p = NUL;
22898 line_arg = p + 1;
22899 }
22900 }
22901 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 theline = getcmdline(':', 0L, indent);
22903 else
22904 theline = eap->getline(':', eap->cookie, indent);
22905 if (KeyTyped)
22906 lines_left = Rows - 1;
22907 if (theline == NULL)
22908 {
22909 EMSG(_("E126: Missing :endfunction"));
22910 goto erret;
22911 }
22912
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022913 /* Detect line continuation: sourcing_lnum increased more than one. */
22914 if (sourcing_lnum > sourcing_lnum_off + 1)
22915 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22916 else
22917 sourcing_lnum_off = 0;
22918
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 if (skip_until != NULL)
22920 {
22921 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22922 * don't check for ":endfunc". */
22923 if (STRCMP(theline, skip_until) == 0)
22924 {
22925 vim_free(skip_until);
22926 skip_until = NULL;
22927 }
22928 }
22929 else
22930 {
22931 /* skip ':' and blanks*/
22932 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22933 ;
22934
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022935 /* Check for "endfunction". */
22936 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022938 if (line_arg == NULL)
22939 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 break;
22941 }
22942
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022943 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 * at "end". */
22945 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22946 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022947 else if (STRNCMP(p, "if", 2) == 0
22948 || STRNCMP(p, "wh", 2) == 0
22949 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 || STRNCMP(p, "try", 3) == 0)
22951 indent += 2;
22952
22953 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022954 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022956 if (*p == '!')
22957 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022959 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22960 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022962 ++nesting;
22963 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 }
22965 }
22966
22967 /* Check for ":append" or ":insert". */
22968 p = skip_range(p, NULL);
22969 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22970 || (p[0] == 'i'
22971 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22972 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22973 skip_until = vim_strsave((char_u *)".");
22974
22975 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22976 arg = skipwhite(skiptowhite(p));
22977 if (arg[0] == '<' && arg[1] =='<'
22978 && ((p[0] == 'p' && p[1] == 'y'
22979 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22980 || (p[0] == 'p' && p[1] == 'e'
22981 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22982 || (p[0] == 't' && p[1] == 'c'
22983 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022984 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22985 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22987 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022988 || (p[0] == 'm' && p[1] == 'z'
22989 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 ))
22991 {
22992 /* ":python <<" continues until a dot, like ":append" */
22993 p = skipwhite(arg + 2);
22994 if (*p == NUL)
22995 skip_until = vim_strsave((char_u *)".");
22996 else
22997 skip_until = vim_strsave(p);
22998 }
22999 }
23000
23001 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023002 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023003 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023004 if (line_arg == NULL)
23005 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023007 }
23008
23009 /* Copy the line to newly allocated memory. get_one_sourceline()
23010 * allocates 250 bytes per line, this saves 80% on average. The cost
23011 * is an extra alloc/free. */
23012 p = vim_strsave(theline);
23013 if (p != NULL)
23014 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023015 if (line_arg == NULL)
23016 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023017 theline = p;
23018 }
23019
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023020 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23021
23022 /* Add NULL lines for continuation lines, so that the line count is
23023 * equal to the index in the growarray. */
23024 while (sourcing_lnum_off-- > 0)
23025 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023026
23027 /* Check for end of eap->arg. */
23028 if (line_arg != NULL && *line_arg == NUL)
23029 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 }
23031
23032 /* Don't define the function when skipping commands or when an error was
23033 * detected. */
23034 if (eap->skip || did_emsg)
23035 goto erret;
23036
23037 /*
23038 * If there are no errors, add the function
23039 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023040 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023041 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023042 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023043 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023044 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023045 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023046 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023047 goto erret;
23048 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023050 fp = find_func(name);
23051 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023053 if (!eap->forceit)
23054 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023055 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023056 goto erret;
23057 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023058 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023059 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023060 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023061 name);
23062 goto erret;
23063 }
23064 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023065 ga_clear_strings(&(fp->uf_args));
23066 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023067 vim_free(name);
23068 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 }
23071 else
23072 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023073 char numbuf[20];
23074
23075 fp = NULL;
23076 if (fudi.fd_newkey == NULL && !eap->forceit)
23077 {
23078 EMSG(_(e_funcdict));
23079 goto erret;
23080 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023081 if (fudi.fd_di == NULL)
23082 {
23083 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023084 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023085 goto erret;
23086 }
23087 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023088 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023089 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023090
23091 /* Give the function a sequential number. Can only be used with a
23092 * Funcref! */
23093 vim_free(name);
23094 sprintf(numbuf, "%d", ++func_nr);
23095 name = vim_strsave((char_u *)numbuf);
23096 if (name == NULL)
23097 goto erret;
23098 }
23099
23100 if (fp == NULL)
23101 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023102 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023103 {
23104 int slen, plen;
23105 char_u *scriptname;
23106
23107 /* Check that the autoload name matches the script name. */
23108 j = FAIL;
23109 if (sourcing_name != NULL)
23110 {
23111 scriptname = autoload_name(name);
23112 if (scriptname != NULL)
23113 {
23114 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023115 plen = (int)STRLEN(p);
23116 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023117 if (slen > plen && fnamecmp(p,
23118 sourcing_name + slen - plen) == 0)
23119 j = OK;
23120 vim_free(scriptname);
23121 }
23122 }
23123 if (j == FAIL)
23124 {
23125 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23126 goto erret;
23127 }
23128 }
23129
23130 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 if (fp == NULL)
23132 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023133
23134 if (fudi.fd_dict != NULL)
23135 {
23136 if (fudi.fd_di == NULL)
23137 {
23138 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023139 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023140 if (fudi.fd_di == NULL)
23141 {
23142 vim_free(fp);
23143 goto erret;
23144 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023145 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23146 {
23147 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023148 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023149 goto erret;
23150 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023151 }
23152 else
23153 /* overwrite existing dict entry */
23154 clear_tv(&fudi.fd_di->di_tv);
23155 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023156 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023157 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023158 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023159
23160 /* behave like "dict" was used */
23161 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023162 }
23163
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023165 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023166 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23167 {
23168 vim_free(fp);
23169 goto erret;
23170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023172 fp->uf_args = newargs;
23173 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023174#ifdef FEAT_PROFILE
23175 fp->uf_tml_count = NULL;
23176 fp->uf_tml_total = NULL;
23177 fp->uf_tml_self = NULL;
23178 fp->uf_profiling = FALSE;
23179 if (prof_def_func())
23180 func_do_profile(fp);
23181#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023182 fp->uf_varargs = varargs;
23183 fp->uf_flags = flags;
23184 fp->uf_calls = 0;
23185 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023186 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187
23188erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 ga_clear_strings(&newargs);
23190 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023191ret_free:
23192 vim_free(skip_until);
23193 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023196 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197}
23198
23199/*
23200 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023201 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023203 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023204 * TFN_INT: internal function name OK
23205 * TFN_QUIET: be quiet
23206 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 * Advances "pp" to just after the function name (if no error).
23208 */
23209 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023210trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 char_u **pp;
23212 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023213 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023214 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023216 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 char_u *start;
23218 char_u *end;
23219 int lead;
23220 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023222 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023223
23224 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023225 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023227
23228 /* Check for hard coded <SNR>: already translated function ID (from a user
23229 * command). */
23230 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23231 && (*pp)[2] == (int)KE_SNR)
23232 {
23233 *pp += 3;
23234 len = get_id_len(pp) + 3;
23235 return vim_strnsave(start, len);
23236 }
23237
23238 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23239 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023241 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023243
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023244 /* Note that TFN_ flags use the same values as GLV_ flags. */
23245 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023246 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023247 if (end == start)
23248 {
23249 if (!skip)
23250 EMSG(_("E129: Function name required"));
23251 goto theend;
23252 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023253 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023254 {
23255 /*
23256 * Report an invalid expression in braces, unless the expression
23257 * evaluation has been cancelled due to an aborting error, an
23258 * interrupt, or an exception.
23259 */
23260 if (!aborting())
23261 {
23262 if (end != NULL)
23263 EMSG2(_(e_invarg2), start);
23264 }
23265 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023266 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023267 goto theend;
23268 }
23269
23270 if (lv.ll_tv != NULL)
23271 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023272 if (fdp != NULL)
23273 {
23274 fdp->fd_dict = lv.ll_dict;
23275 fdp->fd_newkey = lv.ll_newkey;
23276 lv.ll_newkey = NULL;
23277 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023278 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023279 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23280 {
23281 name = vim_strsave(lv.ll_tv->vval.v_string);
23282 *pp = end;
23283 }
23284 else
23285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023286 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23287 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023288 EMSG(_(e_funcref));
23289 else
23290 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023291 name = NULL;
23292 }
23293 goto theend;
23294 }
23295
23296 if (lv.ll_name == NULL)
23297 {
23298 /* Error found, but continue after the function name. */
23299 *pp = end;
23300 goto theend;
23301 }
23302
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023303 /* Check if the name is a Funcref. If so, use the value. */
23304 if (lv.ll_exp_name != NULL)
23305 {
23306 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023307 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023308 if (name == lv.ll_exp_name)
23309 name = NULL;
23310 }
23311 else
23312 {
23313 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023314 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023315 if (name == *pp)
23316 name = NULL;
23317 }
23318 if (name != NULL)
23319 {
23320 name = vim_strsave(name);
23321 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023322 if (STRNCMP(name, "<SNR>", 5) == 0)
23323 {
23324 /* Change "<SNR>" to the byte sequence. */
23325 name[0] = K_SPECIAL;
23326 name[1] = KS_EXTRA;
23327 name[2] = (int)KE_SNR;
23328 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23329 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023330 goto theend;
23331 }
23332
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023333 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023334 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023335 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023336 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23337 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23338 {
23339 /* When there was "s:" already or the name expanded to get a
23340 * leading "s:" then remove it. */
23341 lv.ll_name += 2;
23342 len -= 2;
23343 lead = 2;
23344 }
23345 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023346 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023347 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023348 /* skip over "s:" and "g:" */
23349 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023350 lv.ll_name += 2;
23351 len = (int)(end - lv.ll_name);
23352 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023353
23354 /*
23355 * Copy the function name to allocated memory.
23356 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23357 * Accept <SNR>123_name() outside a script.
23358 */
23359 if (skip)
23360 lead = 0; /* do nothing */
23361 else if (lead > 0)
23362 {
23363 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023364 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23365 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023366 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023367 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023368 if (current_SID <= 0)
23369 {
23370 EMSG(_(e_usingsid));
23371 goto theend;
23372 }
23373 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23374 lead += (int)STRLEN(sid_buf);
23375 }
23376 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023377 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023378 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023379 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023380 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023381 goto theend;
23382 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023383 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023384 {
23385 char_u *cp = vim_strchr(lv.ll_name, ':');
23386
23387 if (cp != NULL && cp < end)
23388 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023389 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023390 goto theend;
23391 }
23392 }
23393
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023394 name = alloc((unsigned)(len + lead + 1));
23395 if (name != NULL)
23396 {
23397 if (lead > 0)
23398 {
23399 name[0] = K_SPECIAL;
23400 name[1] = KS_EXTRA;
23401 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023402 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023403 STRCPY(name + 3, sid_buf);
23404 }
23405 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023406 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023407 }
23408 *pp = end;
23409
23410theend:
23411 clear_lval(&lv);
23412 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413}
23414
23415/*
23416 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23417 * Return 2 if "p" starts with "s:".
23418 * Return 0 otherwise.
23419 */
23420 static int
23421eval_fname_script(p)
23422 char_u *p;
23423{
23424 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23425 || STRNICMP(p + 1, "SNR>", 4) == 0))
23426 return 5;
23427 if (p[0] == 's' && p[1] == ':')
23428 return 2;
23429 return 0;
23430}
23431
23432/*
23433 * Return TRUE if "p" starts with "<SID>" or "s:".
23434 * Only works if eval_fname_script() returned non-zero for "p"!
23435 */
23436 static int
23437eval_fname_sid(p)
23438 char_u *p;
23439{
23440 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23441}
23442
23443/*
23444 * List the head of the function: "name(arg1, arg2)".
23445 */
23446 static void
23447list_func_head(fp, indent)
23448 ufunc_T *fp;
23449 int indent;
23450{
23451 int j;
23452
23453 msg_start();
23454 if (indent)
23455 MSG_PUTS(" ");
23456 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023457 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 {
23459 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023460 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 }
23462 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023463 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023465 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 {
23467 if (j)
23468 MSG_PUTS(", ");
23469 msg_puts(FUNCARG(fp, j));
23470 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023471 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 {
23473 if (j)
23474 MSG_PUTS(", ");
23475 MSG_PUTS("...");
23476 }
23477 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023478 if (fp->uf_flags & FC_ABORT)
23479 MSG_PUTS(" abort");
23480 if (fp->uf_flags & FC_RANGE)
23481 MSG_PUTS(" range");
23482 if (fp->uf_flags & FC_DICT)
23483 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023484 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023485 if (p_verbose > 0)
23486 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487}
23488
23489/*
23490 * Find a function by name, return pointer to it in ufuncs.
23491 * Return NULL for unknown function.
23492 */
23493 static ufunc_T *
23494find_func(name)
23495 char_u *name;
23496{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023497 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023499 hi = hash_find(&func_hashtab, name);
23500 if (!HASHITEM_EMPTY(hi))
23501 return HI2UF(hi);
23502 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503}
23504
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023505#if defined(EXITFREE) || defined(PROTO)
23506 void
23507free_all_functions()
23508{
23509 hashitem_T *hi;
23510
23511 /* Need to start all over every time, because func_free() may change the
23512 * hash table. */
23513 while (func_hashtab.ht_used > 0)
23514 for (hi = func_hashtab.ht_array; ; ++hi)
23515 if (!HASHITEM_EMPTY(hi))
23516 {
23517 func_free(HI2UF(hi));
23518 break;
23519 }
23520}
23521#endif
23522
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023523 int
23524translated_function_exists(name)
23525 char_u *name;
23526{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023527 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023528 return find_internal_func(name) >= 0;
23529 return find_func(name) != NULL;
23530}
23531
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023532/*
23533 * Return TRUE if a function "name" exists.
23534 */
23535 static int
23536function_exists(name)
23537 char_u *name;
23538{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023539 char_u *nm = name;
23540 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023541 int n = FALSE;
23542
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023543 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23544 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023545 nm = skipwhite(nm);
23546
23547 /* Only accept "funcname", "funcname ", "funcname (..." and
23548 * "funcname(...", not "funcname!...". */
23549 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023550 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023551 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023552 return n;
23553}
23554
Bram Moolenaara1544c02013-05-30 12:35:52 +020023555 char_u *
23556get_expanded_name(name, check)
23557 char_u *name;
23558 int check;
23559{
23560 char_u *nm = name;
23561 char_u *p;
23562
23563 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23564
23565 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023566 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023567 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023568
Bram Moolenaara1544c02013-05-30 12:35:52 +020023569 vim_free(p);
23570 return NULL;
23571}
23572
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023573/*
23574 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023575 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23576 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023577 */
23578 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023579builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023580 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023581 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023582{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023583 char_u *p;
23584
23585 if (!ASCII_ISLOWER(name[0]))
23586 return FALSE;
23587 p = vim_strchr(name, AUTOLOAD_CHAR);
23588 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023589}
23590
Bram Moolenaar05159a02005-02-26 23:04:13 +000023591#if defined(FEAT_PROFILE) || defined(PROTO)
23592/*
23593 * Start profiling function "fp".
23594 */
23595 static void
23596func_do_profile(fp)
23597 ufunc_T *fp;
23598{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023599 int len = fp->uf_lines.ga_len;
23600
23601 if (len == 0)
23602 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023603 fp->uf_tm_count = 0;
23604 profile_zero(&fp->uf_tm_self);
23605 profile_zero(&fp->uf_tm_total);
23606 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023607 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023608 if (fp->uf_tml_total == NULL)
23609 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023610 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023611 if (fp->uf_tml_self == NULL)
23612 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023613 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023614 fp->uf_tml_idx = -1;
23615 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23616 || fp->uf_tml_self == NULL)
23617 return; /* out of memory */
23618
23619 fp->uf_profiling = TRUE;
23620}
23621
23622/*
23623 * Dump the profiling results for all functions in file "fd".
23624 */
23625 void
23626func_dump_profile(fd)
23627 FILE *fd;
23628{
23629 hashitem_T *hi;
23630 int todo;
23631 ufunc_T *fp;
23632 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023633 ufunc_T **sorttab;
23634 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023635
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023636 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023637 if (todo == 0)
23638 return; /* nothing to dump */
23639
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023640 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023641
Bram Moolenaar05159a02005-02-26 23:04:13 +000023642 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23643 {
23644 if (!HASHITEM_EMPTY(hi))
23645 {
23646 --todo;
23647 fp = HI2UF(hi);
23648 if (fp->uf_profiling)
23649 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023650 if (sorttab != NULL)
23651 sorttab[st_len++] = fp;
23652
Bram Moolenaar05159a02005-02-26 23:04:13 +000023653 if (fp->uf_name[0] == K_SPECIAL)
23654 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23655 else
23656 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23657 if (fp->uf_tm_count == 1)
23658 fprintf(fd, "Called 1 time\n");
23659 else
23660 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23661 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23662 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23663 fprintf(fd, "\n");
23664 fprintf(fd, "count total (s) self (s)\n");
23665
23666 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23667 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023668 if (FUNCLINE(fp, i) == NULL)
23669 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023670 prof_func_line(fd, fp->uf_tml_count[i],
23671 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023672 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23673 }
23674 fprintf(fd, "\n");
23675 }
23676 }
23677 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023678
23679 if (sorttab != NULL && st_len > 0)
23680 {
23681 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23682 prof_total_cmp);
23683 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23684 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23685 prof_self_cmp);
23686 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23687 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023688
23689 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023690}
Bram Moolenaar73830342005-02-28 22:48:19 +000023691
23692 static void
23693prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23694 FILE *fd;
23695 ufunc_T **sorttab;
23696 int st_len;
23697 char *title;
23698 int prefer_self; /* when equal print only self time */
23699{
23700 int i;
23701 ufunc_T *fp;
23702
23703 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23704 fprintf(fd, "count total (s) self (s) function\n");
23705 for (i = 0; i < 20 && i < st_len; ++i)
23706 {
23707 fp = sorttab[i];
23708 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23709 prefer_self);
23710 if (fp->uf_name[0] == K_SPECIAL)
23711 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23712 else
23713 fprintf(fd, " %s()\n", fp->uf_name);
23714 }
23715 fprintf(fd, "\n");
23716}
23717
23718/*
23719 * Print the count and times for one function or function line.
23720 */
23721 static void
23722prof_func_line(fd, count, total, self, prefer_self)
23723 FILE *fd;
23724 int count;
23725 proftime_T *total;
23726 proftime_T *self;
23727 int prefer_self; /* when equal print only self time */
23728{
23729 if (count > 0)
23730 {
23731 fprintf(fd, "%5d ", count);
23732 if (prefer_self && profile_equal(total, self))
23733 fprintf(fd, " ");
23734 else
23735 fprintf(fd, "%s ", profile_msg(total));
23736 if (!prefer_self && profile_equal(total, self))
23737 fprintf(fd, " ");
23738 else
23739 fprintf(fd, "%s ", profile_msg(self));
23740 }
23741 else
23742 fprintf(fd, " ");
23743}
23744
23745/*
23746 * Compare function for total time sorting.
23747 */
23748 static int
23749#ifdef __BORLANDC__
23750_RTLENTRYF
23751#endif
23752prof_total_cmp(s1, s2)
23753 const void *s1;
23754 const void *s2;
23755{
23756 ufunc_T *p1, *p2;
23757
23758 p1 = *(ufunc_T **)s1;
23759 p2 = *(ufunc_T **)s2;
23760 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23761}
23762
23763/*
23764 * Compare function for self time sorting.
23765 */
23766 static int
23767#ifdef __BORLANDC__
23768_RTLENTRYF
23769#endif
23770prof_self_cmp(s1, s2)
23771 const void *s1;
23772 const void *s2;
23773{
23774 ufunc_T *p1, *p2;
23775
23776 p1 = *(ufunc_T **)s1;
23777 p2 = *(ufunc_T **)s2;
23778 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23779}
23780
Bram Moolenaar05159a02005-02-26 23:04:13 +000023781#endif
23782
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023783/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023784 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023785 * Return TRUE if a package was loaded.
23786 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023787 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023788script_autoload(name, reload)
23789 char_u *name;
23790 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023791{
23792 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023793 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023794 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023795 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023796
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023797 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023798 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023799 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023800 return FALSE;
23801
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023802 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023803
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023804 /* Find the name in the list of previously loaded package names. Skip
23805 * "autoload/", it's always the same. */
23806 for (i = 0; i < ga_loaded.ga_len; ++i)
23807 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23808 break;
23809 if (!reload && i < ga_loaded.ga_len)
23810 ret = FALSE; /* was loaded already */
23811 else
23812 {
23813 /* Remember the name if it wasn't loaded already. */
23814 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23815 {
23816 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23817 tofree = NULL;
23818 }
23819
23820 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023821 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023822 ret = TRUE;
23823 }
23824
23825 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023826 return ret;
23827}
23828
23829/*
23830 * Return the autoload script name for a function or variable name.
23831 * Returns NULL when out of memory.
23832 */
23833 static char_u *
23834autoload_name(name)
23835 char_u *name;
23836{
23837 char_u *p;
23838 char_u *scriptname;
23839
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023840 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023841 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23842 if (scriptname == NULL)
23843 return FALSE;
23844 STRCPY(scriptname, "autoload/");
23845 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023846 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023847 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023848 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023849 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023850 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023851}
23852
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23854
23855/*
23856 * Function given to ExpandGeneric() to obtain the list of user defined
23857 * function names.
23858 */
23859 char_u *
23860get_user_func_name(xp, idx)
23861 expand_T *xp;
23862 int idx;
23863{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023864 static long_u done;
23865 static hashitem_T *hi;
23866 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867
23868 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023870 done = 0;
23871 hi = func_hashtab.ht_array;
23872 }
23873 if (done < func_hashtab.ht_used)
23874 {
23875 if (done++ > 0)
23876 ++hi;
23877 while (HASHITEM_EMPTY(hi))
23878 ++hi;
23879 fp = HI2UF(hi);
23880
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023881 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023882 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023883
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023884 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23885 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886
23887 cat_func_name(IObuff, fp);
23888 if (xp->xp_context != EXPAND_USER_FUNC)
23889 {
23890 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023891 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 STRCAT(IObuff, ")");
23893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 return IObuff;
23895 }
23896 return NULL;
23897}
23898
23899#endif /* FEAT_CMDL_COMPL */
23900
23901/*
23902 * Copy the function name of "fp" to buffer "buf".
23903 * "buf" must be able to hold the function name plus three bytes.
23904 * Takes care of script-local function names.
23905 */
23906 static void
23907cat_func_name(buf, fp)
23908 char_u *buf;
23909 ufunc_T *fp;
23910{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023911 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 {
23913 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023914 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915 }
23916 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023917 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918}
23919
23920/*
23921 * ":delfunction {name}"
23922 */
23923 void
23924ex_delfunction(eap)
23925 exarg_T *eap;
23926{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023927 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928 char_u *p;
23929 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023930 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931
23932 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023933 name = trans_function_name(&p, eap->skip, 0, &fudi);
23934 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023936 {
23937 if (fudi.fd_dict != NULL && !eap->skip)
23938 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 if (!ends_excmd(*skipwhite(p)))
23942 {
23943 vim_free(name);
23944 EMSG(_(e_trailing));
23945 return;
23946 }
23947 eap->nextcmd = check_nextcmd(p);
23948 if (eap->nextcmd != NULL)
23949 *p = NUL;
23950
23951 if (!eap->skip)
23952 fp = find_func(name);
23953 vim_free(name);
23954
23955 if (!eap->skip)
23956 {
23957 if (fp == NULL)
23958 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023959 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 return;
23961 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023962 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 {
23964 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23965 return;
23966 }
23967
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023968 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023970 /* Delete the dict item that refers to the function, it will
23971 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023972 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023974 else
23975 func_free(fp);
23976 }
23977}
23978
23979/*
23980 * Free a function and remove it from the list of functions.
23981 */
23982 static void
23983func_free(fp)
23984 ufunc_T *fp;
23985{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023986 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023987
23988 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023989 ga_clear_strings(&(fp->uf_args));
23990 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023991#ifdef FEAT_PROFILE
23992 vim_free(fp->uf_tml_count);
23993 vim_free(fp->uf_tml_total);
23994 vim_free(fp->uf_tml_self);
23995#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023996
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023997 /* remove the function from the function hashtable */
23998 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23999 if (HASHITEM_EMPTY(hi))
24000 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024001 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024002 hash_remove(&func_hashtab, hi);
24003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024004 vim_free(fp);
24005}
24006
24007/*
24008 * Unreference a Function: decrement the reference count and free it when it
24009 * becomes zero. Only for numbered functions.
24010 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024011 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012func_unref(name)
24013 char_u *name;
24014{
24015 ufunc_T *fp;
24016
24017 if (name != NULL && isdigit(*name))
24018 {
24019 fp = find_func(name);
24020 if (fp == NULL)
24021 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024022 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024023 {
24024 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024025 * when "uf_calls" becomes zero. */
24026 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024027 func_free(fp);
24028 }
24029 }
24030}
24031
24032/*
24033 * Count a reference to a Function.
24034 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024035 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024036func_ref(name)
24037 char_u *name;
24038{
24039 ufunc_T *fp;
24040
24041 if (name != NULL && isdigit(*name))
24042 {
24043 fp = find_func(name);
24044 if (fp == NULL)
24045 EMSG2(_(e_intern2), "func_ref()");
24046 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024047 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 }
24049}
24050
24051/*
24052 * Call a user function.
24053 */
24054 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024055call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056 ufunc_T *fp; /* pointer to function */
24057 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024058 typval_T *argvars; /* arguments */
24059 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060 linenr_T firstline; /* first line of range */
24061 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024062 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063{
Bram Moolenaar33570922005-01-25 22:26:29 +000024064 char_u *save_sourcing_name;
24065 linenr_T save_sourcing_lnum;
24066 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024067 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024068 int save_did_emsg;
24069 static int depth = 0;
24070 dictitem_T *v;
24071 int fixvar_idx = 0; /* index in fixvar[] */
24072 int i;
24073 int ai;
24074 char_u numbuf[NUMBUFLEN];
24075 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024076 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024077#ifdef FEAT_PROFILE
24078 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024079 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081
24082 /* If depth of calling is getting too high, don't execute the function */
24083 if (depth >= p_mfd)
24084 {
24085 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024086 rettv->v_type = VAR_NUMBER;
24087 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 return;
24089 }
24090 ++depth;
24091
24092 line_breakcheck(); /* check for CTRL-C hit */
24093
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024094 fc = (funccall_T *)alloc(sizeof(funccall_T));
24095 fc->caller = current_funccal;
24096 current_funccal = fc;
24097 fc->func = fp;
24098 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024099 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024100 fc->linenr = 0;
24101 fc->returned = FALSE;
24102 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024104 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24105 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106
Bram Moolenaar33570922005-01-25 22:26:29 +000024107 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024108 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024109 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24110 * each argument variable and saves a lot of time.
24111 */
24112 /*
24113 * Init l: variables.
24114 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024115 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024116 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024117 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024118 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24119 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024120 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024121 name = v->di_key;
24122 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024123 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024124 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024125 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024126 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024127 v->di_tv.vval.v_dict = selfdict;
24128 ++selfdict->dv_refcount;
24129 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024130
Bram Moolenaar33570922005-01-25 22:26:29 +000024131 /*
24132 * Init a: variables.
24133 * Set a:0 to "argcount".
24134 * Set a:000 to a list with room for the "..." arguments.
24135 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024136 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024137 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024138 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024139 /* Use "name" to avoid a warning from some compiler that checks the
24140 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024141 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024142 name = v->di_key;
24143 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024144 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024145 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024146 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024147 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024148 v->di_tv.vval.v_list = &fc->l_varlist;
24149 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24150 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24151 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024152
24153 /*
24154 * Set a:firstline to "firstline" and a:lastline to "lastline".
24155 * Set a:name to named arguments.
24156 * Set a:N to the "..." arguments.
24157 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024158 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024159 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024160 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024161 (varnumber_T)lastline);
24162 for (i = 0; i < argcount; ++i)
24163 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024164 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024165 if (ai < 0)
24166 /* named argument a:name */
24167 name = FUNCARG(fp, i);
24168 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024169 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024170 /* "..." argument a:1, a:2, etc. */
24171 sprintf((char *)numbuf, "%d", ai + 1);
24172 name = numbuf;
24173 }
24174 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24175 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024176 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024177 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24178 }
24179 else
24180 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024181 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24182 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024183 if (v == NULL)
24184 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024185 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024186 }
24187 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024188 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024189
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024190 /* Note: the values are copied directly to avoid alloc/free.
24191 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024193 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024194
24195 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24196 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024197 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24198 fc->l_listitems[ai].li_tv = argvars[i];
24199 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024200 }
24201 }
24202
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 /* Don't redraw while executing the function. */
24204 ++RedrawingDisabled;
24205 save_sourcing_name = sourcing_name;
24206 save_sourcing_lnum = sourcing_lnum;
24207 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024208 /* need space for function name + ("function " + 3) or "[number]" */
24209 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24210 + STRLEN(fp->uf_name) + 20;
24211 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 if (sourcing_name != NULL)
24213 {
24214 if (save_sourcing_name != NULL
24215 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024216 sprintf((char *)sourcing_name, "%s[%d]..",
24217 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 else
24219 STRCPY(sourcing_name, "function ");
24220 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24221
24222 if (p_verbose >= 12)
24223 {
24224 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024225 verbose_enter_scroll();
24226
Bram Moolenaar555b2802005-05-19 21:08:39 +000024227 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 if (p_verbose >= 14)
24229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024231 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024232 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024233 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234
24235 msg_puts((char_u *)"(");
24236 for (i = 0; i < argcount; ++i)
24237 {
24238 if (i > 0)
24239 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024240 if (argvars[i].v_type == VAR_NUMBER)
24241 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 else
24243 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024244 /* Do not want errors such as E724 here. */
24245 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024246 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024247 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024248 if (s != NULL)
24249 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024250 if (vim_strsize(s) > MSG_BUF_CLEN)
24251 {
24252 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24253 s = buf;
24254 }
24255 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024256 vim_free(tofree);
24257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 }
24259 }
24260 msg_puts((char_u *)")");
24261 }
24262 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024263
24264 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 --no_wait_return;
24266 }
24267 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024268#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024269 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024270 {
24271 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24272 func_do_profile(fp);
24273 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024274 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024275 {
24276 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024277 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024278 profile_zero(&fp->uf_tm_children);
24279 }
24280 script_prof_save(&wait_start);
24281 }
24282#endif
24283
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024285 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 save_did_emsg = did_emsg;
24287 did_emsg = FALSE;
24288
24289 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024290 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24292
24293 --RedrawingDisabled;
24294
24295 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024298 clear_tv(rettv);
24299 rettv->v_type = VAR_NUMBER;
24300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301 }
24302
Bram Moolenaar05159a02005-02-26 23:04:13 +000024303#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024304 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024305 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024306 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024307 profile_end(&call_start);
24308 profile_sub_wait(&wait_start, &call_start);
24309 profile_add(&fp->uf_tm_total, &call_start);
24310 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024311 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024312 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024313 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24314 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024315 }
24316 }
24317#endif
24318
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319 /* when being verbose, mention the return value */
24320 if (p_verbose >= 12)
24321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024323 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024326 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024327 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024328 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024329 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024332 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024333 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024334 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024335 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024336
Bram Moolenaar555b2802005-05-19 21:08:39 +000024337 /* The value may be very long. Skip the middle part, so that we
24338 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024339 * truncate it at the end. Don't want errors such as E724 here. */
24340 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024341 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024342 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024343 if (s != NULL)
24344 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024345 if (vim_strsize(s) > MSG_BUF_CLEN)
24346 {
24347 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24348 s = buf;
24349 }
24350 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024351 vim_free(tofree);
24352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353 }
24354 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024355
24356 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 --no_wait_return;
24358 }
24359
24360 vim_free(sourcing_name);
24361 sourcing_name = save_sourcing_name;
24362 sourcing_lnum = save_sourcing_lnum;
24363 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024364#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024365 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024366 script_prof_restore(&wait_start);
24367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368
24369 if (p_verbose >= 12 && sourcing_name != NULL)
24370 {
24371 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024372 verbose_enter_scroll();
24373
Bram Moolenaar555b2802005-05-19 21:08:39 +000024374 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024376
24377 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 --no_wait_return;
24379 }
24380
24381 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024382 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024384
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024385 /* If the a:000 list and the l: and a: dicts are not referenced we can
24386 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024387 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24388 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24389 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24390 {
24391 free_funccal(fc, FALSE);
24392 }
24393 else
24394 {
24395 hashitem_T *hi;
24396 listitem_T *li;
24397 int todo;
24398
24399 /* "fc" is still in use. This can happen when returning "a:000" or
24400 * assigning "l:" to a global variable.
24401 * Link "fc" in the list for garbage collection later. */
24402 fc->caller = previous_funccal;
24403 previous_funccal = fc;
24404
24405 /* Make a copy of the a: variables, since we didn't do that above. */
24406 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24407 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24408 {
24409 if (!HASHITEM_EMPTY(hi))
24410 {
24411 --todo;
24412 v = HI2DI(hi);
24413 copy_tv(&v->di_tv, &v->di_tv);
24414 }
24415 }
24416
24417 /* Make a copy of the a:000 items, since we didn't do that above. */
24418 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24419 copy_tv(&li->li_tv, &li->li_tv);
24420 }
24421}
24422
24423/*
24424 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024425 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024426 */
24427 static int
24428can_free_funccal(fc, copyID)
24429 funccall_T *fc;
24430 int copyID;
24431{
24432 return (fc->l_varlist.lv_copyID != copyID
24433 && fc->l_vars.dv_copyID != copyID
24434 && fc->l_avars.dv_copyID != copyID);
24435}
24436
24437/*
24438 * Free "fc" and what it contains.
24439 */
24440 static void
24441free_funccal(fc, free_val)
24442 funccall_T *fc;
24443 int free_val; /* a: vars were allocated */
24444{
24445 listitem_T *li;
24446
24447 /* The a: variables typevals may not have been allocated, only free the
24448 * allocated variables. */
24449 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24450
24451 /* free all l: variables */
24452 vars_clear(&fc->l_vars.dv_hashtab);
24453
24454 /* Free the a:000 variables if they were allocated. */
24455 if (free_val)
24456 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24457 clear_tv(&li->li_tv);
24458
24459 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460}
24461
24462/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024463 * Add a number variable "name" to dict "dp" with value "nr".
24464 */
24465 static void
24466add_nr_var(dp, v, name, nr)
24467 dict_T *dp;
24468 dictitem_T *v;
24469 char *name;
24470 varnumber_T nr;
24471{
24472 STRCPY(v->di_key, name);
24473 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24474 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24475 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024476 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024477 v->di_tv.vval.v_number = nr;
24478}
24479
24480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 * ":return [expr]"
24482 */
24483 void
24484ex_return(eap)
24485 exarg_T *eap;
24486{
24487 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024488 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489 int returning = FALSE;
24490
24491 if (current_funccal == NULL)
24492 {
24493 EMSG(_("E133: :return not inside a function"));
24494 return;
24495 }
24496
24497 if (eap->skip)
24498 ++emsg_skip;
24499
24500 eap->nextcmd = NULL;
24501 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024502 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 {
24504 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024505 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024507 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508 }
24509 /* It's safer to return also on error. */
24510 else if (!eap->skip)
24511 {
24512 /*
24513 * Return unless the expression evaluation has been cancelled due to an
24514 * aborting error, an interrupt, or an exception.
24515 */
24516 if (!aborting())
24517 returning = do_return(eap, FALSE, TRUE, NULL);
24518 }
24519
24520 /* When skipping or the return gets pending, advance to the next command
24521 * in this line (!returning). Otherwise, ignore the rest of the line.
24522 * Following lines will be ignored by get_func_line(). */
24523 if (returning)
24524 eap->nextcmd = NULL;
24525 else if (eap->nextcmd == NULL) /* no argument */
24526 eap->nextcmd = check_nextcmd(arg);
24527
24528 if (eap->skip)
24529 --emsg_skip;
24530}
24531
24532/*
24533 * Return from a function. Possibly makes the return pending. Also called
24534 * for a pending return at the ":endtry" or after returning from an extra
24535 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024536 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024537 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 * FALSE when the return gets pending.
24539 */
24540 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024541do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542 exarg_T *eap;
24543 int reanimate;
24544 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024545 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546{
24547 int idx;
24548 struct condstack *cstack = eap->cstack;
24549
24550 if (reanimate)
24551 /* Undo the return. */
24552 current_funccal->returned = FALSE;
24553
24554 /*
24555 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24556 * not in its finally clause (which then is to be executed next) is found.
24557 * In this case, make the ":return" pending for execution at the ":endtry".
24558 * Otherwise, return normally.
24559 */
24560 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24561 if (idx >= 0)
24562 {
24563 cstack->cs_pending[idx] = CSTP_RETURN;
24564
24565 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024566 /* A pending return again gets pending. "rettv" points to an
24567 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024568 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024569 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 else
24571 {
24572 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024573 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024575 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024577 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 {
24579 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024580 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024581 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582 else
24583 EMSG(_(e_outofmem));
24584 }
24585 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024586 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587
24588 if (reanimate)
24589 {
24590 /* The pending return value could be overwritten by a ":return"
24591 * without argument in a finally clause; reset the default
24592 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024593 current_funccal->rettv->v_type = VAR_NUMBER;
24594 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595 }
24596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024597 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 }
24599 else
24600 {
24601 current_funccal->returned = TRUE;
24602
24603 /* If the return is carried out now, store the return value. For
24604 * a return immediately after reanimation, the value is already
24605 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024606 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024608 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024609 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024611 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 }
24613 }
24614
24615 return idx < 0;
24616}
24617
24618/*
24619 * Free the variable with a pending return value.
24620 */
24621 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024622discard_pending_return(rettv)
24623 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624{
Bram Moolenaar33570922005-01-25 22:26:29 +000024625 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626}
24627
24628/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024629 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 * is an allocated string. Used by report_pending() for verbose messages.
24631 */
24632 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024633get_return_cmd(rettv)
24634 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024636 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024637 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024638 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024640 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024641 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024642 if (s == NULL)
24643 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024644
24645 STRCPY(IObuff, ":return ");
24646 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24647 if (STRLEN(s) + 8 >= IOSIZE)
24648 STRCPY(IObuff + IOSIZE - 4, "...");
24649 vim_free(tofree);
24650 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651}
24652
24653/*
24654 * Get next function line.
24655 * Called by do_cmdline() to get the next line.
24656 * Returns allocated string, or NULL for end of function.
24657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 char_u *
24659get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024660 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024662 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663{
Bram Moolenaar33570922005-01-25 22:26:29 +000024664 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024665 ufunc_T *fp = fcp->func;
24666 char_u *retval;
24667 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668
24669 /* If breakpoints have been added/deleted need to check for it. */
24670 if (fcp->dbg_tick != debug_tick)
24671 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024672 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 sourcing_lnum);
24674 fcp->dbg_tick = debug_tick;
24675 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024676#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024677 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024678 func_line_end(cookie);
24679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680
Bram Moolenaar05159a02005-02-26 23:04:13 +000024681 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024682 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24683 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684 retval = NULL;
24685 else
24686 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024687 /* Skip NULL lines (continuation lines). */
24688 while (fcp->linenr < gap->ga_len
24689 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24690 ++fcp->linenr;
24691 if (fcp->linenr >= gap->ga_len)
24692 retval = NULL;
24693 else
24694 {
24695 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24696 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024697#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024698 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024699 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024700#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702 }
24703
24704 /* Did we encounter a breakpoint? */
24705 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24706 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024707 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024709 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710 sourcing_lnum);
24711 fcp->dbg_tick = debug_tick;
24712 }
24713
24714 return retval;
24715}
24716
Bram Moolenaar05159a02005-02-26 23:04:13 +000024717#if defined(FEAT_PROFILE) || defined(PROTO)
24718/*
24719 * Called when starting to read a function line.
24720 * "sourcing_lnum" must be correct!
24721 * When skipping lines it may not actually be executed, but we won't find out
24722 * until later and we need to store the time now.
24723 */
24724 void
24725func_line_start(cookie)
24726 void *cookie;
24727{
24728 funccall_T *fcp = (funccall_T *)cookie;
24729 ufunc_T *fp = fcp->func;
24730
24731 if (fp->uf_profiling && sourcing_lnum >= 1
24732 && sourcing_lnum <= fp->uf_lines.ga_len)
24733 {
24734 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024735 /* Skip continuation lines. */
24736 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24737 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024738 fp->uf_tml_execed = FALSE;
24739 profile_start(&fp->uf_tml_start);
24740 profile_zero(&fp->uf_tml_children);
24741 profile_get_wait(&fp->uf_tml_wait);
24742 }
24743}
24744
24745/*
24746 * Called when actually executing a function line.
24747 */
24748 void
24749func_line_exec(cookie)
24750 void *cookie;
24751{
24752 funccall_T *fcp = (funccall_T *)cookie;
24753 ufunc_T *fp = fcp->func;
24754
24755 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24756 fp->uf_tml_execed = TRUE;
24757}
24758
24759/*
24760 * Called when done with a function line.
24761 */
24762 void
24763func_line_end(cookie)
24764 void *cookie;
24765{
24766 funccall_T *fcp = (funccall_T *)cookie;
24767 ufunc_T *fp = fcp->func;
24768
24769 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24770 {
24771 if (fp->uf_tml_execed)
24772 {
24773 ++fp->uf_tml_count[fp->uf_tml_idx];
24774 profile_end(&fp->uf_tml_start);
24775 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024776 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024777 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24778 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024779 }
24780 fp->uf_tml_idx = -1;
24781 }
24782}
24783#endif
24784
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785/*
24786 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024787 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788 */
24789 int
24790func_has_ended(cookie)
24791 void *cookie;
24792{
Bram Moolenaar33570922005-01-25 22:26:29 +000024793 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794
24795 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24796 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024797 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024798 || fcp->returned);
24799}
24800
24801/*
24802 * return TRUE if cookie indicates a function which "abort"s on errors.
24803 */
24804 int
24805func_has_abort(cookie)
24806 void *cookie;
24807{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024808 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809}
24810
24811#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24812typedef enum
24813{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024814 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24815 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24816 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817} var_flavour_T;
24818
24819static var_flavour_T var_flavour __ARGS((char_u *varname));
24820
24821 static var_flavour_T
24822var_flavour(varname)
24823 char_u *varname;
24824{
24825 char_u *p = varname;
24826
24827 if (ASCII_ISUPPER(*p))
24828 {
24829 while (*(++p))
24830 if (ASCII_ISLOWER(*p))
24831 return VAR_FLAVOUR_SESSION;
24832 return VAR_FLAVOUR_VIMINFO;
24833 }
24834 else
24835 return VAR_FLAVOUR_DEFAULT;
24836}
24837#endif
24838
24839#if defined(FEAT_VIMINFO) || defined(PROTO)
24840/*
24841 * Restore global vars that start with a capital from the viminfo file
24842 */
24843 int
24844read_viminfo_varlist(virp, writing)
24845 vir_T *virp;
24846 int writing;
24847{
24848 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024849 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024850 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851
24852 if (!writing && (find_viminfo_parameter('!') != NULL))
24853 {
24854 tab = vim_strchr(virp->vir_line + 1, '\t');
24855 if (tab != NULL)
24856 {
24857 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024858 switch (*tab)
24859 {
24860 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024861#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024862 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024863#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024864 case 'D': type = VAR_DICT; break;
24865 case 'L': type = VAR_LIST; break;
24866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867
24868 tab = vim_strchr(tab, '\t');
24869 if (tab != NULL)
24870 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024871 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024872 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024873 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024875#ifdef FEAT_FLOAT
24876 else if (type == VAR_FLOAT)
24877 (void)string2float(tab + 1, &tv.vval.v_float);
24878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024880 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024881 if (type == VAR_DICT || type == VAR_LIST)
24882 {
24883 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24884
24885 if (etv == NULL)
24886 /* Failed to parse back the dict or list, use it as a
24887 * string. */
24888 tv.v_type = VAR_STRING;
24889 else
24890 {
24891 vim_free(tv.vval.v_string);
24892 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024893 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024894 }
24895 }
24896
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024897 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024898
24899 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024900 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024901 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24902 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 }
24904 }
24905 }
24906
24907 return viminfo_readline(virp);
24908}
24909
24910/*
24911 * Write global vars that start with a capital to the viminfo file
24912 */
24913 void
24914write_viminfo_varlist(fp)
24915 FILE *fp;
24916{
Bram Moolenaar33570922005-01-25 22:26:29 +000024917 hashitem_T *hi;
24918 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024919 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024920 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024921 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024922 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024923 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924
24925 if (find_viminfo_parameter('!') == NULL)
24926 return;
24927
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024928 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024929
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024930 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024931 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024933 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024935 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024936 this_var = HI2DI(hi);
24937 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024938 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024939 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024940 {
24941 case VAR_STRING: s = "STR"; break;
24942 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024943#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024944 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024945#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024946 case VAR_DICT: s = "DIC"; break;
24947 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024948 default: continue;
24949 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024950 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024951 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024952 if (p != NULL)
24953 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024954 vim_free(tofree);
24955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956 }
24957 }
24958}
24959#endif
24960
24961#if defined(FEAT_SESSION) || defined(PROTO)
24962 int
24963store_session_globals(fd)
24964 FILE *fd;
24965{
Bram Moolenaar33570922005-01-25 22:26:29 +000024966 hashitem_T *hi;
24967 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024968 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969 char_u *p, *t;
24970
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024971 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024972 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024974 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024976 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024977 this_var = HI2DI(hi);
24978 if ((this_var->di_tv.v_type == VAR_NUMBER
24979 || this_var->di_tv.v_type == VAR_STRING)
24980 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024981 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024982 /* Escape special characters with a backslash. Turn a LF and
24983 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024984 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024985 (char_u *)"\\\"\n\r");
24986 if (p == NULL) /* out of memory */
24987 break;
24988 for (t = p; *t != NUL; ++t)
24989 if (*t == '\n')
24990 *t = 'n';
24991 else if (*t == '\r')
24992 *t = 'r';
24993 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024994 this_var->di_key,
24995 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24996 : ' ',
24997 p,
24998 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24999 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025000 || put_eol(fd) == FAIL)
25001 {
25002 vim_free(p);
25003 return FAIL;
25004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025005 vim_free(p);
25006 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025007#ifdef FEAT_FLOAT
25008 else if (this_var->di_tv.v_type == VAR_FLOAT
25009 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25010 {
25011 float_T f = this_var->di_tv.vval.v_float;
25012 int sign = ' ';
25013
25014 if (f < 0)
25015 {
25016 f = -f;
25017 sign = '-';
25018 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025019 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025020 this_var->di_key, sign, f) < 0)
25021 || put_eol(fd) == FAIL)
25022 return FAIL;
25023 }
25024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025 }
25026 }
25027 return OK;
25028}
25029#endif
25030
Bram Moolenaar661b1822005-07-28 22:36:45 +000025031/*
25032 * Display script name where an item was last set.
25033 * Should only be invoked when 'verbose' is non-zero.
25034 */
25035 void
25036last_set_msg(scriptID)
25037 scid_T scriptID;
25038{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025039 char_u *p;
25040
Bram Moolenaar661b1822005-07-28 22:36:45 +000025041 if (scriptID != 0)
25042 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025043 p = home_replace_save(NULL, get_scriptname(scriptID));
25044 if (p != NULL)
25045 {
25046 verbose_enter();
25047 MSG_PUTS(_("\n\tLast set from "));
25048 MSG_PUTS(p);
25049 vim_free(p);
25050 verbose_leave();
25051 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025052 }
25053}
25054
Bram Moolenaard812df62008-11-09 12:46:09 +000025055/*
25056 * List v:oldfiles in a nice way.
25057 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025058 void
25059ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025060 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025061{
25062 list_T *l = vimvars[VV_OLDFILES].vv_list;
25063 listitem_T *li;
25064 int nr = 0;
25065
25066 if (l == NULL)
25067 msg((char_u *)_("No old files"));
25068 else
25069 {
25070 msg_start();
25071 msg_scroll = TRUE;
25072 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25073 {
25074 msg_outnum((long)++nr);
25075 MSG_PUTS(": ");
25076 msg_outtrans(get_tv_string(&li->li_tv));
25077 msg_putchar('\n');
25078 out_flush(); /* output one line at a time */
25079 ui_breakcheck();
25080 }
25081 /* Assume "got_int" was set to truncate the listing. */
25082 got_int = FALSE;
25083
25084#ifdef FEAT_BROWSE_CMD
25085 if (cmdmod.browse)
25086 {
25087 quit_more = FALSE;
25088 nr = prompt_for_number(FALSE);
25089 msg_starthere();
25090 if (nr > 0)
25091 {
25092 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25093 (long)nr);
25094
25095 if (p != NULL)
25096 {
25097 p = expand_env_save(p);
25098 eap->arg = p;
25099 eap->cmdidx = CMD_edit;
25100 cmdmod.browse = FALSE;
25101 do_exedit(eap, NULL);
25102 vim_free(p);
25103 }
25104 }
25105 }
25106#endif
25107 }
25108}
25109
Bram Moolenaar53744302015-07-17 17:38:22 +020025110/* reset v:option_new, v:option_old and v:option_type */
25111 void
25112reset_v_option_vars()
25113{
25114 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25115 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25116 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25117}
25118
25119
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120#endif /* FEAT_EVAL */
25121
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025123#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124
25125#ifdef WIN3264
25126/*
25127 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25128 */
25129static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25130static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25131static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25132
25133/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025134 * Get the short path (8.3) for the filename in "fnamep".
25135 * Only works for a valid file name.
25136 * When the path gets longer "fnamep" is changed and the allocated buffer
25137 * is put in "bufp".
25138 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25139 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025140 */
25141 static int
25142get_short_pathname(fnamep, bufp, fnamelen)
25143 char_u **fnamep;
25144 char_u **bufp;
25145 int *fnamelen;
25146{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025147 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148 char_u *newbuf;
25149
25150 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151 l = GetShortPathName(*fnamep, *fnamep, len);
25152 if (l > len - 1)
25153 {
25154 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025155 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156 newbuf = vim_strnsave(*fnamep, l);
25157 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025158 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159
25160 vim_free(*bufp);
25161 *fnamep = *bufp = newbuf;
25162
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025163 /* Really should always succeed, as the buffer is big enough. */
25164 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165 }
25166
25167 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025168 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169}
25170
25171/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025172 * Get the short path (8.3) for the filename in "fname". The converted
25173 * path is returned in "bufp".
25174 *
25175 * Some of the directories specified in "fname" may not exist. This function
25176 * will shorten the existing directories at the beginning of the path and then
25177 * append the remaining non-existing path.
25178 *
25179 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025180 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025181 * bufp - Pointer to an allocated buffer for the filename.
25182 * fnamelen - Length of the filename pointed to by fname
25183 *
25184 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 */
25186 static int
25187shortpath_for_invalid_fname(fname, bufp, fnamelen)
25188 char_u **fname;
25189 char_u **bufp;
25190 int *fnamelen;
25191{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025192 char_u *short_fname, *save_fname, *pbuf_unused;
25193 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025195 int old_len, len;
25196 int new_len, sfx_len;
25197 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198
25199 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025200 old_len = *fnamelen;
25201 save_fname = vim_strnsave(*fname, old_len);
25202 pbuf_unused = NULL;
25203 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025205 endp = save_fname + old_len - 1; /* Find the end of the copy */
25206 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025208 /*
25209 * Try shortening the supplied path till it succeeds by removing one
25210 * directory at a time from the tail of the path.
25211 */
25212 len = 0;
25213 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025215 /* go back one path-separator */
25216 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25217 --endp;
25218 if (endp <= save_fname)
25219 break; /* processed the complete path */
25220
25221 /*
25222 * Replace the path separator with a NUL and try to shorten the
25223 * resulting path.
25224 */
25225 ch = *endp;
25226 *endp = 0;
25227 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025228 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025229 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25230 {
25231 retval = FAIL;
25232 goto theend;
25233 }
25234 *endp = ch; /* preserve the string */
25235
25236 if (len > 0)
25237 break; /* successfully shortened the path */
25238
25239 /* failed to shorten the path. Skip the path separator */
25240 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241 }
25242
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025243 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025245 /*
25246 * Succeeded in shortening the path. Now concatenate the shortened
25247 * path with the remaining path at the tail.
25248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025250 /* Compute the length of the new path. */
25251 sfx_len = (int)(save_endp - endp) + 1;
25252 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025254 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025256 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025258 /* There is not enough space in the currently allocated string,
25259 * copy it to a buffer big enough. */
25260 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025262 {
25263 retval = FAIL;
25264 goto theend;
25265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266 }
25267 else
25268 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025269 /* Transfer short_fname to the main buffer (it's big enough),
25270 * unless get_short_pathname() did its work in-place. */
25271 *fname = *bufp = save_fname;
25272 if (short_fname != save_fname)
25273 vim_strncpy(save_fname, short_fname, len);
25274 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025276
25277 /* concat the not-shortened part of the path */
25278 vim_strncpy(*fname + len, endp, sfx_len);
25279 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025281
25282theend:
25283 vim_free(pbuf_unused);
25284 vim_free(save_fname);
25285
25286 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287}
25288
25289/*
25290 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025291 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 */
25293 static int
25294shortpath_for_partial(fnamep, bufp, fnamelen)
25295 char_u **fnamep;
25296 char_u **bufp;
25297 int *fnamelen;
25298{
25299 int sepcount, len, tflen;
25300 char_u *p;
25301 char_u *pbuf, *tfname;
25302 int hasTilde;
25303
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025304 /* Count up the path separators from the RHS.. so we know which part
25305 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025307 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 if (vim_ispathsep(*p))
25309 ++sepcount;
25310
25311 /* Need full path first (use expand_env() to remove a "~/") */
25312 hasTilde = (**fnamep == '~');
25313 if (hasTilde)
25314 pbuf = tfname = expand_env_save(*fnamep);
25315 else
25316 pbuf = tfname = FullName_save(*fnamep, FALSE);
25317
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025318 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025320 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322
25323 if (len == 0)
25324 {
25325 /* Don't have a valid filename, so shorten the rest of the
25326 * path if we can. This CAN give us invalid 8.3 filenames, but
25327 * there's not a lot of point in guessing what it might be.
25328 */
25329 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025330 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 }
25333
25334 /* Count the paths backward to find the beginning of the desired string. */
25335 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025336 {
25337#ifdef FEAT_MBYTE
25338 if (has_mbyte)
25339 p -= mb_head_off(tfname, p);
25340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341 if (vim_ispathsep(*p))
25342 {
25343 if (sepcount == 0 || (hasTilde && sepcount == 1))
25344 break;
25345 else
25346 sepcount --;
25347 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025349 if (hasTilde)
25350 {
25351 --p;
25352 if (p >= tfname)
25353 *p = '~';
25354 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356 }
25357 else
25358 ++p;
25359
25360 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25361 vim_free(*bufp);
25362 *fnamelen = (int)STRLEN(p);
25363 *bufp = pbuf;
25364 *fnamep = p;
25365
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025366 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367}
25368#endif /* WIN3264 */
25369
25370/*
25371 * Adjust a filename, according to a string of modifiers.
25372 * *fnamep must be NUL terminated when called. When returning, the length is
25373 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025374 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375 * When there is an error, *fnamep is set to NULL.
25376 */
25377 int
25378modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25379 char_u *src; /* string with modifiers */
25380 int *usedlen; /* characters after src that are used */
25381 char_u **fnamep; /* file name so far */
25382 char_u **bufp; /* buffer for allocated file name or NULL */
25383 int *fnamelen; /* length of fnamep */
25384{
25385 int valid = 0;
25386 char_u *tail;
25387 char_u *s, *p, *pbuf;
25388 char_u dirname[MAXPATHL];
25389 int c;
25390 int has_fullname = 0;
25391#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025392 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025393 int has_shortname = 0;
25394#endif
25395
25396repeat:
25397 /* ":p" - full path/file_name */
25398 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25399 {
25400 has_fullname = 1;
25401
25402 valid |= VALID_PATH;
25403 *usedlen += 2;
25404
25405 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25406 if ((*fnamep)[0] == '~'
25407#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25408 && ((*fnamep)[1] == '/'
25409# ifdef BACKSLASH_IN_FILENAME
25410 || (*fnamep)[1] == '\\'
25411# endif
25412 || (*fnamep)[1] == NUL)
25413
25414#endif
25415 )
25416 {
25417 *fnamep = expand_env_save(*fnamep);
25418 vim_free(*bufp); /* free any allocated file name */
25419 *bufp = *fnamep;
25420 if (*fnamep == NULL)
25421 return -1;
25422 }
25423
25424 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025425 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426 {
25427 if (vim_ispathsep(*p)
25428 && p[1] == '.'
25429 && (p[2] == NUL
25430 || vim_ispathsep(p[2])
25431 || (p[2] == '.'
25432 && (p[3] == NUL || vim_ispathsep(p[3])))))
25433 break;
25434 }
25435
25436 /* FullName_save() is slow, don't use it when not needed. */
25437 if (*p != NUL || !vim_isAbsName(*fnamep))
25438 {
25439 *fnamep = FullName_save(*fnamep, *p != NUL);
25440 vim_free(*bufp); /* free any allocated file name */
25441 *bufp = *fnamep;
25442 if (*fnamep == NULL)
25443 return -1;
25444 }
25445
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025446#ifdef WIN3264
25447# if _WIN32_WINNT >= 0x0500
25448 if (vim_strchr(*fnamep, '~') != NULL)
25449 {
25450 /* Expand 8.3 filename to full path. Needed to make sure the same
25451 * file does not have two different names.
25452 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25453 p = alloc(_MAX_PATH + 1);
25454 if (p != NULL)
25455 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025456 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025457 {
25458 vim_free(*bufp);
25459 *bufp = *fnamep = p;
25460 }
25461 else
25462 vim_free(p);
25463 }
25464 }
25465# endif
25466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 /* Append a path separator to a directory. */
25468 if (mch_isdir(*fnamep))
25469 {
25470 /* Make room for one or two extra characters. */
25471 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25472 vim_free(*bufp); /* free any allocated file name */
25473 *bufp = *fnamep;
25474 if (*fnamep == NULL)
25475 return -1;
25476 add_pathsep(*fnamep);
25477 }
25478 }
25479
25480 /* ":." - path relative to the current directory */
25481 /* ":~" - path relative to the home directory */
25482 /* ":8" - shortname path - postponed till after */
25483 while (src[*usedlen] == ':'
25484 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25485 {
25486 *usedlen += 2;
25487 if (c == '8')
25488 {
25489#ifdef WIN3264
25490 has_shortname = 1; /* Postpone this. */
25491#endif
25492 continue;
25493 }
25494 pbuf = NULL;
25495 /* Need full path first (use expand_env() to remove a "~/") */
25496 if (!has_fullname)
25497 {
25498 if (c == '.' && **fnamep == '~')
25499 p = pbuf = expand_env_save(*fnamep);
25500 else
25501 p = pbuf = FullName_save(*fnamep, FALSE);
25502 }
25503 else
25504 p = *fnamep;
25505
25506 has_fullname = 0;
25507
25508 if (p != NULL)
25509 {
25510 if (c == '.')
25511 {
25512 mch_dirname(dirname, MAXPATHL);
25513 s = shorten_fname(p, dirname);
25514 if (s != NULL)
25515 {
25516 *fnamep = s;
25517 if (pbuf != NULL)
25518 {
25519 vim_free(*bufp); /* free any allocated file name */
25520 *bufp = pbuf;
25521 pbuf = NULL;
25522 }
25523 }
25524 }
25525 else
25526 {
25527 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25528 /* Only replace it when it starts with '~' */
25529 if (*dirname == '~')
25530 {
25531 s = vim_strsave(dirname);
25532 if (s != NULL)
25533 {
25534 *fnamep = s;
25535 vim_free(*bufp);
25536 *bufp = s;
25537 }
25538 }
25539 }
25540 vim_free(pbuf);
25541 }
25542 }
25543
25544 tail = gettail(*fnamep);
25545 *fnamelen = (int)STRLEN(*fnamep);
25546
25547 /* ":h" - head, remove "/file_name", can be repeated */
25548 /* Don't remove the first "/" or "c:\" */
25549 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25550 {
25551 valid |= VALID_HEAD;
25552 *usedlen += 2;
25553 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025554 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025555 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556 *fnamelen = (int)(tail - *fnamep);
25557#ifdef VMS
25558 if (*fnamelen > 0)
25559 *fnamelen += 1; /* the path separator is part of the path */
25560#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025561 if (*fnamelen == 0)
25562 {
25563 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25564 p = vim_strsave((char_u *)".");
25565 if (p == NULL)
25566 return -1;
25567 vim_free(*bufp);
25568 *bufp = *fnamep = tail = p;
25569 *fnamelen = 1;
25570 }
25571 else
25572 {
25573 while (tail > s && !after_pathsep(s, tail))
25574 mb_ptr_back(*fnamep, tail);
25575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 }
25577
25578 /* ":8" - shortname */
25579 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25580 {
25581 *usedlen += 2;
25582#ifdef WIN3264
25583 has_shortname = 1;
25584#endif
25585 }
25586
25587#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025588 /*
25589 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 */
25591 if (has_shortname)
25592 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025593 /* Copy the string if it is shortened by :h and when it wasn't copied
25594 * yet, because we are going to change it in place. Avoids changing
25595 * the buffer name for "%:8". */
25596 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025597 {
25598 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025599 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025600 return -1;
25601 vim_free(*bufp);
25602 *bufp = *fnamep = p;
25603 }
25604
25605 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025606 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607 if (!has_fullname && !vim_isAbsName(*fnamep))
25608 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025609 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610 return -1;
25611 }
25612 else
25613 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025614 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615
Bram Moolenaardc935552011-08-17 15:23:23 +020025616 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025618 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619 return -1;
25620
25621 if (l == 0)
25622 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025623 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025625 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 return -1;
25627 }
25628 *fnamelen = l;
25629 }
25630 }
25631#endif /* WIN3264 */
25632
25633 /* ":t" - tail, just the basename */
25634 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25635 {
25636 *usedlen += 2;
25637 *fnamelen -= (int)(tail - *fnamep);
25638 *fnamep = tail;
25639 }
25640
25641 /* ":e" - extension, can be repeated */
25642 /* ":r" - root, without extension, can be repeated */
25643 while (src[*usedlen] == ':'
25644 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25645 {
25646 /* find a '.' in the tail:
25647 * - for second :e: before the current fname
25648 * - otherwise: The last '.'
25649 */
25650 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25651 s = *fnamep - 2;
25652 else
25653 s = *fnamep + *fnamelen - 1;
25654 for ( ; s > tail; --s)
25655 if (s[0] == '.')
25656 break;
25657 if (src[*usedlen + 1] == 'e') /* :e */
25658 {
25659 if (s > tail)
25660 {
25661 *fnamelen += (int)(*fnamep - (s + 1));
25662 *fnamep = s + 1;
25663#ifdef VMS
25664 /* cut version from the extension */
25665 s = *fnamep + *fnamelen - 1;
25666 for ( ; s > *fnamep; --s)
25667 if (s[0] == ';')
25668 break;
25669 if (s > *fnamep)
25670 *fnamelen = s - *fnamep;
25671#endif
25672 }
25673 else if (*fnamep <= tail)
25674 *fnamelen = 0;
25675 }
25676 else /* :r */
25677 {
25678 if (s > tail) /* remove one extension */
25679 *fnamelen = (int)(s - *fnamep);
25680 }
25681 *usedlen += 2;
25682 }
25683
25684 /* ":s?pat?foo?" - substitute */
25685 /* ":gs?pat?foo?" - global substitute */
25686 if (src[*usedlen] == ':'
25687 && (src[*usedlen + 1] == 's'
25688 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25689 {
25690 char_u *str;
25691 char_u *pat;
25692 char_u *sub;
25693 int sep;
25694 char_u *flags;
25695 int didit = FALSE;
25696
25697 flags = (char_u *)"";
25698 s = src + *usedlen + 2;
25699 if (src[*usedlen + 1] == 'g')
25700 {
25701 flags = (char_u *)"g";
25702 ++s;
25703 }
25704
25705 sep = *s++;
25706 if (sep)
25707 {
25708 /* find end of pattern */
25709 p = vim_strchr(s, sep);
25710 if (p != NULL)
25711 {
25712 pat = vim_strnsave(s, (int)(p - s));
25713 if (pat != NULL)
25714 {
25715 s = p + 1;
25716 /* find end of substitution */
25717 p = vim_strchr(s, sep);
25718 if (p != NULL)
25719 {
25720 sub = vim_strnsave(s, (int)(p - s));
25721 str = vim_strnsave(*fnamep, *fnamelen);
25722 if (sub != NULL && str != NULL)
25723 {
25724 *usedlen = (int)(p + 1 - src);
25725 s = do_string_sub(str, pat, sub, flags);
25726 if (s != NULL)
25727 {
25728 *fnamep = s;
25729 *fnamelen = (int)STRLEN(s);
25730 vim_free(*bufp);
25731 *bufp = s;
25732 didit = TRUE;
25733 }
25734 }
25735 vim_free(sub);
25736 vim_free(str);
25737 }
25738 vim_free(pat);
25739 }
25740 }
25741 /* after using ":s", repeat all the modifiers */
25742 if (didit)
25743 goto repeat;
25744 }
25745 }
25746
Bram Moolenaar26df0922014-02-23 23:39:13 +010025747 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25748 {
25749 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25750 if (p == NULL)
25751 return -1;
25752 vim_free(*bufp);
25753 *bufp = *fnamep = p;
25754 *fnamelen = (int)STRLEN(p);
25755 *usedlen += 2;
25756 }
25757
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 return valid;
25759}
25760
25761/*
25762 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25763 * "flags" can be "g" to do a global substitute.
25764 * Returns an allocated string, NULL for error.
25765 */
25766 char_u *
25767do_string_sub(str, pat, sub, flags)
25768 char_u *str;
25769 char_u *pat;
25770 char_u *sub;
25771 char_u *flags;
25772{
25773 int sublen;
25774 regmatch_T regmatch;
25775 int i;
25776 int do_all;
25777 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025778 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779 garray_T ga;
25780 char_u *ret;
25781 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025782 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783
25784 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25785 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025786 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787
25788 ga_init2(&ga, 1, 200);
25789
25790 do_all = (flags[0] == 'g');
25791
25792 regmatch.rm_ic = p_ic;
25793 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25794 if (regmatch.regprog != NULL)
25795 {
25796 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025797 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025798 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25799 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025800 /* Skip empty match except for first match. */
25801 if (regmatch.startp[0] == regmatch.endp[0])
25802 {
25803 if (zero_width == regmatch.startp[0])
25804 {
25805 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025806 i = MB_PTR2LEN(tail);
25807 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25808 (size_t)i);
25809 ga.ga_len += i;
25810 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025811 continue;
25812 }
25813 zero_width = regmatch.startp[0];
25814 }
25815
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816 /*
25817 * Get some space for a temporary buffer to do the substitution
25818 * into. It will contain:
25819 * - The text up to where the match is.
25820 * - The substituted text.
25821 * - The text after the match.
25822 */
25823 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025824 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25826 {
25827 ga_clear(&ga);
25828 break;
25829 }
25830
25831 /* copy the text up to where the match is */
25832 i = (int)(regmatch.startp[0] - tail);
25833 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25834 /* add the substituted text */
25835 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25836 + ga.ga_len + i, TRUE, TRUE, FALSE);
25837 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025838 tail = regmatch.endp[0];
25839 if (*tail == NUL)
25840 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841 if (!do_all)
25842 break;
25843 }
25844
25845 if (ga.ga_data != NULL)
25846 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25847
Bram Moolenaar473de612013-06-08 18:19:48 +020025848 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849 }
25850
25851 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25852 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025853 if (p_cpo == empty_option)
25854 p_cpo = save_cpo;
25855 else
25856 /* Darn, evaluating {sub} expression changed the value. */
25857 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858
25859 return ret;
25860}
25861
25862#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */